qutip_qip.algorithms

Classes

BitFlipCode()

Implementation of the 3-qubit bit-flip quantum error correction code using projective measurements and classically controlled correction gates.

PhaseFlipCode()

Implementation of the 3-qubit phase-flip quantum error correction code.

ShorCode()

Constructs the 9-qubit Shor code encoding circuit using BitFlipCode and PhaseFlipCode.

Functions

qft([N])

Quantum Fourier Transform operator on N qubits.

qpe(U, num_counting_qubits[, target_qubits, ...])

Quantum Phase Estimation circuit implementation for QuTiP.

grover(oracle, search_qubits, num_solutions)

Construct the Grover search algorithm's circuit.

grover_oracle(search_qubits, marked_states)

Constructs a Phase Oracle circuit for Grover's algorithm.

class qutip_qip.algorithms.BitFlipCode[source]

Bases: object

Implementation of the 3-qubit bit-flip quantum error correction code using projective measurements and classically controlled correction gates.

This code detects and corrects a single bit-flip (X) error on any of the three qubits using two syndrome (ancilla) qubits.

decode_circuit(data_qubits: list[int | integer] | tuple[int | integer, ...] | ndarray[tuple[Any, ...], dtype[integer]]) QubitCircuit[source]

Constructs the decoding circuit which is the inverse of the encoding operation, used to recover the original logical qubit. TOFFOLI gate verifies parity.

Parameters:
data_qubitssequence of int

Indices of 3 data qubits.

Returns:
qcQubitCircuit

The decoding quantum circuit.

Raises:
ValueError: If the number of data qubits is not 3.
encode_circuit(data_qubits: list[int | integer] | tuple[int | integer, ...] | ndarray[tuple[Any, ...], dtype[integer]]) QubitCircuit[source]

Constructs the encoding circuit for the bit-flip code. The first qubit is the control, and CNOT gates are applied from it to the other data qubits to encode logical states \(|0\rangle\) or \(|1\rangle\).

Parameters:
data_qubitssequence of int

Indices of 3 data qubits.

Returns:
qcQubitCircuit

The encoding quantum circuit.

Raises:
ValueError: If the number of data qubits is not 3.
property n_data: int
Returns:

int

Number of data qubits (always 3 for bit-flip code).

property n_syndrome: int
Returns:

int

Number of syndrome qubits used for error detection (2 for this code).

syndrome_and_correction_circuit(data_qubits: list[int | integer] | tuple[int | integer, ...] | ndarray[tuple[Any, ...], dtype[integer]], syndrome_qubits: list[int | integer] | tuple[int | integer, ...] | ndarray[tuple[Any, ...], dtype[integer]]) QubitCircuit[source]

Constructs the circuit for syndrome extraction and classical error correction. The circuit measures parity between qubit pairs and applies X gates conditionally.

Parameters:
data_qubitssequence of int

Indices of 3 data qubits.

syndrome_qubitssequence of int

Indices of 2 syndrome qubits.

Returns:
qcQubitCircuit

Circuit for syndrome measurement and X correction.

Raises:
ValueError: If the number of data or syndrome qubits is incorrect.
class qutip_qip.algorithms.PhaseFlipCode[source]

Bases: object

Implementation of the 3-qubit phase-flip quantum error correction code.

This code protects against a single Z (phase) error by encoding the logical qubit into a 3-qubit entangled state using Hadamard transformations and CNOT gates, then measuring parity between pairs of qubits using ancilla qubits. Conditional Z corrections are applied based on classical measurement outcomes.

decode_circuit(data_qubits: list[int | integer] | tuple[int | integer, ...] | ndarray[tuple[Any, ...], dtype[integer]]) QubitCircuit[source]

Constructs the decoding circuit that reverses the encoding operation.

It first applies the Hadamard (H) gates to convert the qubits back from the X-basis to the Z-basis, then applies the inverse of the CX encoding to decode the qubits. A Toffoli gate is applied in the end to verify parity.

Parameters:
data_qubitssequence of int

Indices of 3 data qubits.

Returns:
qcQubitCircuit

The decoding circuit.

Raises:
ValueError: If the number of data qubits is not 3.
encode_circuit(data_qubits: list[int | integer] | tuple[int | integer, ...] | ndarray[tuple[Any, ...], dtype[integer]]) QubitCircuit[source]

Constructs the encoding circuit for the phase-flip code.

The logical qubit is first encoded by two CX gates and then converted to the X-basis using Hadamard (H). This creates redundancy to detect and correct a single phase error.

Parameters:
data_qubitssequence of int

Indices of 3 data qubits.

Returns:
qcQubitCircuit

The encoding quantum circuit.

Raises:
ValueError: If the number of data qubits is not 3.
property n_data: int
Returns:

int

Number of data qubits (3).

property n_syndrome: int
Returns:

int

Number of syndrome qubits (2).

syndrome_and_correction_circuit(data_qubits: list[int | integer] | tuple[int | integer, ...] | ndarray[tuple[Any, ...], dtype[integer]], syndrome_qubits: list[int | integer] | tuple[int | integer, ...] | ndarray[tuple[Any, ...], dtype[integer]]) QubitCircuit[source]

Builds the circuit for syndrome extraction and correction.

The data qubits are temporarily converted back to the Z-basis so parity can be measured between pairs using ancillas and CNOT gates. Measurements are stored in classical bits, and Z corrections are applied conditionally based on the measured syndrome.

Parameters:
data_qubitssequence of int

Indices of 3 data qubits.

syndrome_qubitssequence of int

Indices of 2 syndrome qubits.

Returns:
qcQubitCircuit

Circuit for syndrome measurement and Z correction.

Raises:
ValueError: If there are not exactly 3 data qubits and 2 syndrome qubits.
class qutip_qip.algorithms.ShorCode[source]

Bases: object

Constructs the 9-qubit Shor code encoding circuit using BitFlipCode and PhaseFlipCode.

The Shor code protects against arbitrary single-qubit errors by combining bit-flip and phase-flip redundancy encoding.

encode_circuit() QubitCircuit[source]

Construct the 9-qubit Shor code encoding circuit.

Returns:
qcQubitCircuit

Circuit that encodes one logical qubit into the Shor code.

qutip_qip.algorithms.grover(oracle: QubitCircuit | Gate, search_qubits: int | list[int | integer] | tuple[int | integer, ...] | ndarray[tuple[Any, ...], dtype[integer]], num_solutions: int, num_iterations: int | None = None, num_qubits: int | None = None) QubitCircuit[source]

Construct the Grover search algorithm’s circuit.

Parameters:
oracleQubitCircuit or Gate

The oracle that flips the phase of marked states.

search_qubitsint or sequence of int

The qubits to run the search on.

num_solutionsint

The number of expected solutions M.

num_iterationsint, optional

Number of iterations. Defaults to optimal for M solutions.

num_qubitsint, optional

Total number of qubits in the system.

Returns:
qcQubitCircuit

Quantum Circuit implementing Grover’s search algorithm.

Raises:
ValueError

If the oracle gate targets do not match the algorithm qubits.

Notes

The algorithm performs the following steps:

  1. Apply Hadamard gates to all qubits to create superposition.

  2. For each iteration:
    1. Apply the oracle (phase flip on marked states).

    2. Apply the diffusion operator (inversion about the mean):
      • Hadamard gates

      • X gates

      • Multi-controlled Z gate

      • X gates

      • Hadamard gates

References

[1]

L. K. Grover, “A fast quantum mechanical algorithm for database search,” Proceedings of the 28th Annual ACM Symposium on Theory of Computing (1996).

Examples

Search for state \(|01\rangle\) using 2 qubits:

>>> from qutip_qip.algorithms.grover import grover, grover_oracle
>>> oracle = grover_oracle([0, 1], marked_states=1)
>>> qc = grover(oracle, search_qubits=[0, 1], num_solutions=1)
qutip_qip.algorithms.grover_oracle(search_qubits: int | list[int | integer] | tuple[int | integer, ...] | ndarray[tuple[Any, ...], dtype[integer]], marked_states: int | list[int | integer] | tuple[int | integer, ...] | ndarray[tuple[Any, ...], dtype[integer]]) QubitCircuit[source]

Constructs a Phase Oracle circuit for Grover’s algorithm.

Parameters:
search_qubitsint or sequence of int

The specific qubit indices the oracle acts on.

marked_statesint or sequence of int

The states to mark (integer representation).

Returns:
qcQubitCircuit

The oracle circuit.

qutip_qip.algorithms.qft(N: int = 1) Qobj[source]

Quantum Fourier Transform operator on N qubits.

Parameters:
Nint

Number of qubits. (default = 1)

Returns:
QFTQobj

Quantum Fourier transform operator.

qutip_qip.algorithms.qft_gate_sequence(N: int = 1, swapping: bool = True, to_cnot: bool = False) QubitCircuit[source]

Quantum Fourier Transform operator on N qubits returning the gate sequence.

Parameters:
Nint

Number of qubits.

swappingbool, optional

Flag indicating sequence of swap gates to be applied at the end or not (default: True).

to_cnotbool, optional

Flag to decompose controlled phase gates to CNOT gates (default: False).

Returns:
qcQubitCircuit

Gate sequence of Hadamard and controlled rotation gates implementing QFT.

qutip_qip.algorithms.qft_steps(N: int = 1, swapping: bool = True) list[Qobj][source]

Quantum Fourier Transform operator on N qubits returning the individual steps as unitary matrices operating from left to right.

Parameters:
Nint

Number of qubits.

swappingbool, optional

Flag indicating sequence of swap gates to be applied at the end or not.

Returns:
U_step_listlist of Qobj

List of Hadamard and controlled rotation gates implementing QFT.

qutip_qip.algorithms.qpe(U: Qobj, num_counting_qubits: int, target_qubits: int | list[int | integer] | tuple[int | integer, ...] | ndarray[tuple[Any, ...], dtype[integer]] | None = None, to_cnot: bool = False) QubitCircuit[source]

Quantum Phase Estimation circuit implementation for QuTiP.

Parameters:
UQobj

Unitary operator whose eigenvalue we want to estimate. Should be a unitary quantum operator.

num_counting_qubitsint

Number of counting qubits to use for the phase estimation. More qubits provide higher precision.

target_qubitsint or sequence of int, optional

Index or indices of the target qubit(s) where the eigenstate is prepared. If None, target_qubits is set automatically based on U’s dimension.

to_cnotbool, optional

Flag to decompose controlled phase gates to CNOT gates (default: False)

Returns:
qcQubitCircuit

Gate sequence implementing Quantum Phase Estimation.