ket.qulib

Quantum library.

Utilities for preparing quantum states and building quantum algorithms.

Modules ket.qulib

math

Quantum arithmetic operations for quantum states.

prepare

Quantum state preparation.

Functions ket.qulib

draw(gate, qubits[, args, qpu_size, ...])

Draw a quantum gate using Qiskit.

dump_matrix(gate[, num_qubits, args, process])

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}}\).

unitary(matrix)

Create a quantum gate from 2x2 unitary matrix.

draw(gate: Callable, qubits: int | list[int], args: tuple = (), *, qpu_size: int | None = None, u4_gate: Literal['CX', 'CZ'] | None = None, u2_gates: Literal['ZYZ', 'RzSx'] | None = None, coupling_graph: list[tuple[int, int]] | None = None, title: str | None = None, keep_order: bool = True, **kwargs)

Draw a quantum gate using Qiskit.

Note

This method requires additional dependencies from ket-lang[plot].

Install with: pip install ket-lang[plot].

Parameters:
  • gate – Quantum gate function.

  • qubits – Number of qubits.

  • args – Classical arguments to pass to the gate function.

  • qpu_size – Size of the quantum processing unit (QPU). If specified, the number of qubits will be adjusted to fit the QPU size.

  • u4_gate – Type of U4 gate to use, either “CX” or “CZ”.

  • u2_gates – Type of U2 gates to use, either “ZYZ” or “RzSx”.

  • coupling_graph – Coupling graph of the QPU, specified as a list of tuples representing connected qubits.

  • title – Title for the circuit diagram.

  • keep_order – Maintain the gate call order.

  • **kwargs – Keyword arguments to pass to the Qiskit drawer.

Returns:

Qiskit circuit diagram of the quantum gate.

dump_matrix(gate: Callable, num_qubits: int | list[int] = 1, args=(), process: Process | None = None) list[list[complex]]

Get the matrix representation of a quantum gate.

This function calculates the matrix representation of a quantum gate.

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

  • num_qubits – Number of qubits.

  • args – Classical arguments to pass to the gate function.

  • process – Quantum process used to generate the matrix.

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.

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.