LogicalQ.Logical

Classes

LogicalCircuit

Core LogicalQ representation of a logical quantum circuit.

LogicalQubit

A LogicalQubit

LogicalRegister

A register containing LogicalQubits

LogicalStatevector

A LogicalStatevector

LogicalDensityMatrix

A LogicalDensityMatrix

Functions

logical_state_fidelity(state1, state2)

Module Contents

class LogicalQ.Logical.LogicalCircuit(n_logical_qubits, label, stabilizer_tableau, name=None)

Bases: qiskit.QuantumCircuit

Core LogicalQ representation of a logical quantum circuit.

Parameters:
  • n_logical_qubits (int)

  • label (Iterable[int])

  • stabilizer_tableau (Iterable[str])

  • name (str | None)

n_logical_qubits
stabilizer_tableau
n_stabilizers
label
n_physical_qubits
logical_qubit_type = 'qec'
n_ancilla_qubits
n_measure_qubits
flagged_stabilizers_1 = []
flagged_stabilizers_2 = []
x_stabilizers = []
z_stabilizers = []
qed_cycle_indices_initial
qed_cycle_indices_final
qec_cycle_indices_initial
qec_cycle_indices_final
logical_qregs = []
ancilla_qregs = []
logical_op_qregs = []
enc_verif_cregs = []
curr_syndrome_cregs = []
prev_syndrome_cregs = []
flagged_syndrome_diff_cregs = []
unflagged_syndrome_diff_cregs = []
pauli_frame_cregs = []
logical_op_meas_cregs = []
final_measurement_cregs = []
qreg_lists
creg_lists
output_creg
cbit_setter_qreg
data_without_qec
data_without_qed
classmethod from_physical_circuit(physical_circuit, label, stabilizer_tableau, name=None, max_iterations=1)

Construct a LogicalCircuit from a physical qiskit circuit.

Parameters:
  • physical_circuit (qiskit.QuantumCircuit) – The QuantumCircuit to construct from.

  • label (Iterable[int]) – The QECC label.

  • stabilizer_tableau (Iterable[str]) – The set of stabilizers for the QECC.

  • name (str) – An optional name for the circuit.

  • max_iterations (int) – Number of times, to attempt to encod qubits.

Returns:

The LogicalCircuit constructed from the physical.

Return type:

LogicalCircuit

add_logical_qubits(logical_qubit_count, logical_qubit_types='qec')

Add logical qubit(s) to the LogicalCircuit.

Parameters:
  • logical_qubit_count (int) – The number of logical qubits to add.

  • logical_qubit_types (str | Iterable[str])

group_stabilizers()

Generate the stabilizers for the code.

generate_code()

Generate the encoding circuit and logical operators for the selected tableau.

encode(*qubits, max_iterations=1, initial_states=None)

Prepare logical qubit(s) in the specified initial state(s).

Parameters:
  • qubits (Iterable[int]) – Indices of logical qubits to be encoded.

  • max_iterations (int) – Maximum number of encoding attempts.

  • initial_states (Iterable[list[int]] | None) – Initial states in which to encode each qubit.

initialize(statevector, logical_qubit_indices=None, normalize=False)

Initialize qubits in a specific state.

Qubit initialization is done by first resetting the qubits to \(|0\rangle\) followed by calling StatePreparation class to prepare the qubits in a specified state. Both these steps are included in the Initialize instruction.

Parameters:
  • params

    The state to initialize to, can be either of the following.

    • Statevector or vector of complex amplitudes to initialize to.

    • Labels of basis states of the Pauli eigenstates Z, X, Y. See Statevector.from_label(). Notice the order of the labels is reversed with respect to the qubit index to be applied to. Example label '01' initializes the qubit zero to \(|1\rangle\) and the qubit one to \(|0\rangle\).

    • An integer that is used as a bitmap indicating which qubits to initialize to \(|1\rangle\). Example: setting params to 5 would initialize qubit 0 and qubit 2 to \(|1\rangle\) and qubit 1 to \(|0\rangle\).

  • qubits – Qubits to initialize. If None the initialization is applied to all qubits in the circuit.

  • normalize (bool) – Whether to normalize an input array to a unit vector.

  • logical_qubit_indices (int | Iterable[int] | None)

Returns:

A handle to the instructions created.

Examples

Prepare a qubit in the state \((|0\rangle - |1\rangle) / \sqrt{2}\).

output:

     ┌──────────────────────────────┐
q_0: ┤ Initialize(0.70711,-0.70711) ├
     └──────────────────────────────┘

Initialize from a string two qubits in the state \(|10\rangle\). The order of the labels is reversed with respect to qubit index. More information about labels for basis states are in Statevector.from_label().

output:

     ┌──────────────────┐
q_0: ┤0                 ├
     │  Initialize(0,1) │
q_1: ┤1                 ├
     └──────────────────┘

Initialize two qubits from an array of complex amplitudes.

output:

     ┌────────────────────────────────────┐
q_0: ┤0                                   ├
     │  Initialize(0,0.70711,-0.70711j,0) │
q_1: ┤1                                   ├
     └────────────────────────────────────┘
reset(logical_qubit_indices=None, reset_ancillas=False)

Reset all physical qubits associated with specified logical qubits, optionally including ancillas.

Parameters:
  • logical_qubit_indices (int | Iterable[int] | None) – Indices of logical qubits to reset. If None, then reset all.

  • reset_ancillas (bool) – Specifies whether to reset (both general and logical operation) ancilla qubits associated with each logical qubit.

reset_ancillas(logical_qubit_indices=None)

Reset all ancillas associated with specified logical qubits.

Parameters:

logical_qubit_indices (Iterable[int] | None) – Indices of logical qubits to reset. If None, then reset all.

steane_flagged_circuit1(logical_qubit_indices)

Measure first set of flagged syndromes for the Steane code.

Parameters:

logical_qubit_indices (Iterable[int])

steane_flagged_circuit2(logical_qubit_indices)

Measure second set of flagged syndromes for the Steane code.

Parameters:

logical_qubit_indices (Iterable[int])

measure_stabilizers(logical_qubit_indices=None, stabilizer_indices=None)

Measure specified stabilizers to the circuit as controlled Pauli operators.

Parameters:
  • logical_qubit_indices (Iterable[int] | None) – Indices of logical qubits for which to measure stabilizers.

  • stabilizer_indices (Iterable[int] | None) – Indices of stabilizers to measure.

measure_syndrome_diff(logical_qubit_indices=None, stabilizer_indices=None, flagged=False, steane_flag_1=False, steane_flag_2=False)

Measure flagged or unflagged syndrome differences for specified logical qubits and stabilizers.

Parameters:
  • logical_qubit_indices (Iterable[int] | None) – Logical qubits for which to measure indices.

  • stabilizer_indices (Iterable[int] | None) – Stabilizers for which to measure indices.

  • flagged (bool) – Whether to measure flagged or unflagged differences.

  • steane_flag_1 (bool) – Whether to measure the first Steane code syndrome. Takes priority over steane_flag_2.

  • steane_flag_2 (bool) – Whether to measure the second Steane code syndrome. Ignored if steane_flag_1 is True.

optimize_qec_cycle_indices(logical_qubit_indices=None, constraint_model=None, ignore_existing_qec=False, clear_existing_qec=False)

Compute optimal QEC cycle indices.

Parameters:
  • logical_qubit_indices (Iterable[int] | None) – Logical qubits for which to compute optimal QEC cycle indices.

  • constraint_model (dict[str, float] | None) – Constraint model, i.e., dictionary of gadget costs for the circuit.

  • ignore_existing_qec (bool) – Whether to ignore QEC-related parameters in the constraint model.

  • clear_existing_qec (bool) – Whether to remove existing QEC cycles.

Returns:

Dictionary with optimal QEC cycle indices for each requested qubit.

Return type:

dict[int, int]

insert_qec_cycles(logical_qubit_indices=None, qec_cycle_indices=None, clear_existing_qec=False)

Insert QEC cycles at specified indices in the circuit data.

Parameters:
  • logical_qubit_indices (Iterable[int] | None) – Logical qubits for which to insert QEC cycles.

  • qec_cycle_indices (dict[int, list] | list[list] | None) – Indices at which to insert QEC cycles for each logical qubit.

  • clear_existing_qec (bool) – Whether to clear the existing QEC cycles.

Returns:

Original circuit data and data after appending QEC cycles.

Return type:

tuple[qiskit.circuit.quantumcircuitdata.QuantumCircuitData, qiskit.circuit.quantumcircuitdata.QuantumCircuitData]

append_qed_cycle(logical_qubit_indices=None, perform_flagged_syndrome_measurements=True)

Append a QED cycle to the end of the circuit.

Parameters:
  • logical_qubit_indices (Iterable[int] | None) – Logical qubits for which to add QED cycles.

  • perform_flagged_syndrome_measurements (bool)

Returns:

Qubits and indices with QED cycles, before and after appending.

Return type:

tuple[dict[int, int], dict[int, int]]

append_qec_cycle(logical_qubit_indices=None, perform_flagged_syndrome_measurements=True)

Append a QEC cycle to the end of the circuit.

Parameters:
  • logical_qubit_indices (Iterable[int] | None) – Logical qubits for which to add QEC cycles.

  • perform_flagged_syndrome_measurements (bool)

Returns:

Qubits and indices with QEC cycles, before and after appending.

Return type:

tuple[dict[int, int], dict[int, int]]

clear_qec_cycles(logical_qubit_indices=None, qec_cycle_indices=None)

Clear QEC cycles (either specified or all) on specified logical qubits.

Parameters:
  • logical_qubit_indices (Iterable[int] | None) – Logical qubits from which to clear QEC cycles. Clears all if None.

  • qec_cycle_indices (Iterable[int] | None) – QEC cycles to clear. Clears all if None

Returns:

This method is not yet implemented.

Return type:

NotImplementedError

apply_decoding(logical_qubit_indices, stabilizer_indices, with_flagged)

Decode the syndrome measurements to determine the correction to apply.

Parameters:
  • logical_qubit_indices (Iterable[int]) – Logical qubits to apply decoding to.

  • stabilizer_indices (Iterable[int]) – Stabilizers for which to decode syndromes.

  • with_flagged (bool) – Whether to decode with flagged or unflagged syndrome differences.

measure(logical_qubit_indices, cbit_indices, with_error_detection=True, with_error_correction=True)

Measure specified qubits (in the Z basis) into classical bits.

Parameters:
  • logical_qubit_indices (Iterable[int]) – Logical qubits to measure.

  • cbit_indices (Iterable[int]) – Classical bits to which to record measurements.

  • with_error_detection (bool) – Whether to measure syndromes after measurement.

  • with_error_correction (bool) – Whether to measure and correct syndromes after measurement.

Raises:

ValueError – if logical_qubit_indices or cbit_indices is not an iterable or if number of qubits and cbits does not match.

measure_all(inplace=True, with_error_correction=True)

Add measurements to all qubits.

Parameters:
  • inplace (bool) – Whether to perform measurements on this circuit or a copy. If False, a new circuit is returned.

  • with_error_correction (bool) – Whether to perform measurements with QEC.

Returns:

Circuit with measurements, if inplace = False.

Return type:

LogicalCircuit

remove_final_measurements(inplace=False)

Remove final measurements from circuit.

Parameters:

inplace (bool) – Whether to remove measurements in place, i.e., for this circuit (not supported).

Raises:

NotImplementedError – if in-place measurement is attempted.

Returns:

The circuit without measurements.

Return type:

LogicalCircuit

get_logical_counts(physical_counts, logical_qubit_indices=None)

Get logical counts from physical counts.

Parameters:
  • physical_counts (Iterable[int]) – Physical counts to convert to logical counts.

  • logical_qubit_indices (Iterable[int]) – Logical qubits to get counts for. If None, then get counts for all.

Returns:

Logical qubit counts.

Return type:

dict[str, int]

h(*targets, method='Coherent_Feedback')

Logical single-qubit Hadamard (\(\hat H\)) gate

x(*targets)

Logical single-qubit Pauli-X (\(\hat X\)) gate

y(*targets)

Logical single-qubit Pauli-Y (\(\hat Y\)) gate

z(*targets)

Logical single-qubit Pauli-Z (\(\hat Z\)) gate

s(*targets, method='Coherent_Feedback')

Logical single-qubit S (\(\hat S\)) gate

Definition: [1 0] [0 i]

sdg(*targets, method='Coherent_Feedback')

Logical S^dagger gate

Definition: [1 0] [0 -i]

t(*targets, method='Coherent_Feedback')

Logical T gate

Definition: [1 0 ] [0 e^(ipi/4)]

tdg(*targets, method='Coherent_Feedback')

Logical T^dagger gate

Definition: [1 0 ] [0 e^(-ipi/4)]

cx(control, *_targets, method='Ancilla_Assisted')

Logical Controlled-PauliX gate

cz(control, *_targets, method='Ancilla_Assisted')

Logical Controlled-PauliZ gate

cy(control, *_targets, method='Ancilla_Assisted')

Logical Controlled-PauliY gate

mcmt(gate, controls, targets)

Logical Multi-Control Multi-Target gate

append_sk_decomposition(circuit, targets, label=None, recursion_degree=1, basis_gates=None, depth=10, return_discretized_subcircuit=False)

Append the Solovay-Kitaev decomposition of a circuit in terms of logical basis gates.

u(theta, phi, lam, targets, method='Rotation')

Apply UGate.

For the full matrix form of this gate, see the underlying gate documentation.

Parameters:
  • theta (float) – The \(\theta\) rotation angle of the gate.

  • phi (float) – The \(\phi\) rotation angle of the gate.

  • lam (float) – The \(\lambda\) rotation angle of the gate.

  • qubit – The qubit(s) to apply the gate to.

Returns:

A handle to the instructions created.

r(axis, theta, targets, label='R', method='S-K', recursion_degree=1, depth=10)

Logical single-qubit rotation (\(R\)) gate

Parameters:
  • theta (float) – Rotation angle in radians

  • method (str) – Gate realization method; options: “S-K” (Solovay-Kitaev algorithm) (Default: “S-K”)

  • axis (str)

  • label (str)

  • recursion_degree (int)

  • depth (int)

rx(theta, targets, method='S-K', recursion_degree=1, depth=10)

Logical single-qubit X rotation (\(RX\)) gate

Parameters:
  • theta (float) – Rotation angle in radians

  • method (str) – Gate realization method; options: “S-K” (Solovay-Kitaev algorithm) (Default: “S-K”)

  • recursion_degree (int)

  • depth (int)

ry(theta, targets, method='S-K', recursion_degree=1, depth=10)

Logical single-qubit Y rotation (\(RY\)) gate

Parameters:
  • theta (float) – Rotation angle in radians

  • method – Gate realization method; options: “S-K” (Solovay-Kitaev algorithm) (Default: “S-K”)

rz(phi, targets, method='S-K', recursion_degree=1, depth=10)

Logical single-qubit Z rotation (\(RZ\)) gate

Parameters:
  • theta – Rotation angle in radians

  • method – Gate realization method; options: “S-K” (Solovay-Kitaev algorithm) (Default: “S-K”)

  • phi (float)

  • targets (Iterable)

rxx(theta, targets, method='S-K', recursion_degree=1, depth=10)

Apply RXXGate.

For the full matrix form of this gate, see the underlying gate documentation.

Parameters:
  • theta (float) – The angle of the rotation.

  • qubit1 – The qubit(s) to apply the gate to.

  • qubit2 – The qubit(s) to apply the gate to.

Returns:

A handle to the instructions created.

ryy(theta, targets, method='S-K', recursion_degree=1, depth=10)

Apply RYYGate.

For the full matrix form of this gate, see the underlying gate documentation.

Parameters:
  • theta (float) – The rotation angle of the gate.

  • qubit1 – The qubit(s) to apply the gate to.

  • qubit2 – The qubit(s) to apply the gate to.

Returns:

A handle to the instructions created.

rzz(theta, targets, method='S-K', recursion_degree=1, depth=10)

Apply RZZGate.

For the full matrix form of this gate, see the underlying gate documentation.

Parameters:
  • theta (float) – The rotation angle of the gate.

  • qubit1 – The qubit(s) to apply the gate to.

  • qubit2 – The qubit(s) to apply the gate to.

Returns:

A handle to the instructions created.

sx(target, **kwargs)

Logical single-qubit SQRT(X) (\(\sqrt{X}\)) gate

sxdg(target, **kwargs)

Logical single-qubit SQRT(X)-adjoint (\(\sqrt{X}^\dagger\)) gate

append(instruction, qargs=None, cargs=None, copy=True)

Append instruction to LogicalCircuit

add_error(l_ind, p_ind, error_type)
set_cbit(cbit, value)
cbit_not(cbit)
cbit_and(cbits, values)
cbit_xor(cbits)
draw(output=None, scale=None, filename=None, style=None, interactive=False, plot_barriers=True, reverse_bits=None, justify=None, vertical_compression='medium', idle_wires=None, with_layout=True, fold=None, ax=None, initial_state=False, cregbundle=None, wire_order=None, expr_len=30, fold_qec=True, fold_qed=True, fold_logicalop=True)

LogicalCircuit drawer based on Qiskit circuit drawer

class LogicalQ.Logical.LogicalQubit(regs=None, qregs=None, cregs=None)

Bases: list

A LogicalQubit

_data = []
qregs = []
cregs = []
class LogicalQ.Logical.LogicalRegister(qregs=None, cregs=None)

Bases: list

A register containing LogicalQubits

qregs = None
cregs = None
class LogicalQ.Logical.LogicalStatevector(data, n_logical_qubits=None, label=None, stabilizer_tableau=None, logical_circuit=None, basis=None, dims=None)

Bases: qiskit.quantum_info.Statevector

A LogicalStatevector

Parameters:
  • data (numpy.ndarray | qiskit.QuantumCircuit | LogicalCircuit | qiskit.quantum_info.Statevector)

  • n_logical_qubits (int | None)

  • label (Iterable[int] | None)

  • stabilizer_tableau (Iterable[str] | None)

  • logical_circuit (LogicalCircuit | None)

  • basis (str | None)

  • dims (int | None)

_logical_decomposition = None
classmethod from_counts(counts, n_logical_qubits, label, stabilizer_tableau, basis='physical')

Construct a LogicalStatevector from measurement counts.

Parameters:
  • counts (dict) – The set of counts measured from a circuit execution.

  • n_logical_qubits (int) – The number of logical qubits.

  • label (tuple) – The quantum error correction code [[n,k,d]] (given as a tuple).

  • stabilizer_tableau (Iterable[str]) – The set of stabilizers for the QECC.

  • basis (str) – The basis in which each respective count’s vector is given, physical or logical.

Returns:

The normalized LogicalStatevector constructed from the counts.

Raises:

ValueError – if the counts format could not be parsed or if basis is invalid.

Return type:

LogicalStatevector

classmethod from_basis_str(basis_str, n_logical_qubits, label, stabilizer_tableau, basis='physical')

Construct a LogicalStatevector from a string in the logical computational basis.

Parameters:
  • basis_str – Either a binary bitstring or its hex equivalent to identify the basis.

  • n_logical_qubits – Number of logical qubits.

  • label – The label of the quantum error correction code [[n,k,d]].

  • stabilizer_tableau – The set of stabilizers for the QECC.

  • basis – The basis in which each respective count’s vector is given, physical or logical.

Returns:

The LogicalStatevector of

the given basis state.

Return type:

LogicalStatevector

Raises:

ValueError – if the basis_str could not be parsed due to improper format.

property logical_decomposition

Compute the decomposition of the LogicalStatevector in the logical basis.

Parameters:

atol (float) – Tolerance within which to set probability amplitude to zero.

Returns:

The set of coefficients \(\alpha_i, \delta\), where

\(|\psi\rangle = \sum_{x=0}^{2^n - 1}\alpha_x|x\rangle + \delta|\psi^\perp\rangle\), where \(|\psi^\perp\rangle\) is the component of the state vector not in the codespace. Note that coefficients are returned in ascending order of value, e.g., 000, 001, 010, 011, 100, 101, etc.

Return type:

np.ndarray

__array__(basis='physical', dtype=None, copy=_numpy_compat.COPY_ONLY_IF_NEEDED)

Return an array representation of the object.

Parameters:
  • basis – The basis, physical or logical, in which to represent the vector.

  • dtype – The desired data-type for the array.

  • copy – If True, then the array data is copied

Returns:

The logical_decomposition()

if basis is logical and the statevector data otherwise.

Return type:

np.ndarray

Raises:

ValueError – if the basis is invalid.

__repr__(basis='logical')

Return a string representation of the statevector.

Parameters:

basis – The basis, logical or physical, in which to return the array.

Returns:

String representation of the statevector in the requested basis.

Return type:

np.ndarray

Raises:

ValueError – if the basis is invalid.

draw(output=None)

Return a visual representation of the statevector in the logical basis.

Parameters:

output – The method in which to draw. Valid choices are text, latex, and latex_source.

Returns:

string or LaTeX representation of the statevector.

Return type:

str or IPython.display.Latex

Raises:

ValueError – if the draw method is invalid.

class LogicalQ.Logical.LogicalDensityMatrix(data, n_logical_qubits=None, label=None, stabilizer_tableau=None, dims=None)

Bases: qiskit.quantum_info.DensityMatrix

A LogicalDensityMatrix

_logical_decomposition = None
abstract property logical_decomposition
__repr__(basis='logical')
LogicalQ.Logical.logical_state_fidelity(state1, state2)