ket.lib

Quantum library.

Utilities for preparing quantum states and building quantum algorithms.

Functions ket.lib

dump_matrix(gate[, size, simulator])

Get the matrix representation of a quantum gate.

flip_to_control(control_state[, qubits])

Flip qubits from \(\ket{\texttt{control_state}}\) to \(\ket{1\dots1}\).

phase_oracle(state[, qubits])

Transform qubits from \(\ket{\texttt{state}}\) to \(-\ket{\texttt{state}}\).

prepare_bell(qubit_a, qubit_b)

Prepare a Bell state = \(\frac{1}{\sqrt{2}}(\ket{0}+\ket{1})\) state.

prepare_ghz(qubits)

Prepare a GHZ = \(\frac{1}{\sqrt{2}}(\ket{0\dots0}+\ket{1\dots1})\) state.

prepare_pauli(pauli, eigenvalue[, qubits])

Prepare a quantum state in the +1 or -1 eigenstate of a Pauli operator.

prepare_w(qubits)

Prepare a W = \(\frac{1}{\sqrt{n}}\sum_{k=0}^{n}\ket{2^k}\) state.

unitary(matrix)

Create a quantum gate from 2x2 unitary matrix.

dump_matrix(gate: Callable, size: int = 1, simulator: Literal['sparse', 'dense'] = 'sparse') list[list[complex]]

Get the matrix representation of a quantum gate.

This function calculates the matrix representation of a quantum gate using the specified simulator.

Parameters:
  • gate – Quantum gate operation to obtain the matrix for.

  • size – Number of qubits.

  • simulator – Simulator type to use.

Returns:

Matrix representation of the quantum gate.

flip_to_control(control_state: int | list[int], qubits: Quant | None = None) Quant | Callable[[Quant], Quant]

Flip qubits from \(\ket{\texttt{control_state}}\) to \(\ket{1\dots1}\).

The primary usage of this gate is to change the state when controlled applications are applied. For instance, all controlled operations are only applied if the control qubits’ state is \(\ket{1}\). This gate is useful for using another state as control.

Example

from ket import *

p = Process()
q = p.alloc(3)

H(q[:2])

with around(flip_to_control(0b01), q[:2]):
    ctrl(q[:2], X)(q[2])
phase_oracle(state: int, qubits: Quant | None = None) Quant | Callable[[Quant], Quant]

Transform qubits from \(\ket{\texttt{state}}\) to \(-\ket{\texttt{state}}\).

This gate is useful for marking states in Grover’s algorithm.

prepare_bell(qubit_a: Quant, qubit_b: Quant) Quant

Prepare a Bell state = \(\frac{1}{\sqrt{2}}(\ket{0}+\ket{1})\) state.

prepare_ghz(qubits: Quant) Quant

Prepare a GHZ = \(\frac{1}{\sqrt{2}}(\ket{0\dots0}+\ket{1\dots1})\) state.

prepare_pauli(pauli: Literal['X', 'Y', 'Z'], eigenvalue: Literal[1, -1], qubits: Quant | None = None) Quant | Callable[[Quant], Quant]

Prepare a quantum state in the +1 or -1 eigenstate of a Pauli operator.

This function prepares a quantum state in the +1 or -1 eigenstate of a specified Pauli operator. The resulting quantum state can be obtained by either directly calling the function with qubits, or by returning a closure that can be applied to qubits later.

Parameters:
  • pauli – Pauli operator to prepare the eigenstate for.

  • eigenvalue – Eigenvalue of the Pauli operator (+1 or -1).

  • qubits – Qubits to prepare the eigenstate on. If None, returns a closure.

Returns:

If qubits is provided, returns the resulting quantum state. If qubits is None, returns a closure that can be applied to qubits later.

prepare_w(qubits: Quant) Quant

Prepare a W = \(\frac{1}{\sqrt{n}}\sum_{k=0}^{n}\ket{2^k}\) state.

unitary(matrix: list[list[complex]]) Callable[[Quant], Quant]

Create a quantum gate from 2x2 unitary matrix.

The provided unitary matrix is decomposed into a sequence of rotation gates, which together implement an equivalent unitary transformation. When the gate is used in a controlled operation, the resulting unitary is equivalent up to a global phase.

Parameters:

matrix – Unitary matrix in the format [[a, b], [c, d]].

Returns:

Returns a new callable that implements the unitary operation.

Raises:

ValueError – If the input matrix is not unitary.