ofex.clifford¶
This module provides tools and utilities for working with Clifford operators and Pauli operators in the context of quantum computation.
The module includes:
Conversion utilities between Pauli operators and tableau form (pauli_to_tableau, tableau_to_pauli).
Functions for performing and visualizing tableau arithmetic (dot_tableau, str_tableau, str_tableau_side_by_side).
Algorithms for diagonalizing sets of mutually-commuting Pauli operators (diagonalizing_clifford).
Simulation and unitary matrix application for Clifford operators (clifford_apply, clifford_qiskit, clifford_simulation, clifford_unitary_mat).
- ofex.clifford.pauli_to_tableau(pauli_list: List[QubitOperator] | QubitOperator, num_qubits: int) Tuple[FieldArray, ndarray][source]¶
Converts a Pauli operator or list of Pauli operators into tableau form.
- Parameters:
pauli_list (int) – A QubitOperator with multiple terms or a list of QubitOperators with single term to be converted.
num_qubits (int) – The total number of qubits being represented.
- Returns:
- G: A
integer matrix in tableau form, where
is the number of qubits,
is the number of terms in the Pauli list,The first
rows represent
(X components of Pauli terms),The next
rows represent
(Z components of Pauli terms).
- G: A
coeff: A 1D array of length
corresponding to the coefficients of the Pauli terms.
- Return type:
Tuple[FieldArray, np.ndarray]
- ofex.clifford.tableau_to_pauli(mat: FieldArray, coeffs: ndarray | None = None, ph: FieldArray | None = None) List[QubitOperator][source]¶
Converts a tableau representation of Pauli operators back into QubitOperator objects.
- Parameters:
mat (FieldArray) – A
FieldArray matrix representing the tableau form of the Pauli operators,
where
is the number of qubits and
is the number of operators.
The first
rows represent
(X components of the Pauli terms),
and the next
rows represent
(Z components of the Pauli terms).coeffs (ndarray, optional) – A 1D NumPy array of coefficients of length
for the operators. If not specified,
default coefficients of
will be assigned depending on the phase (ph) array.ph (FieldArray, optional) – A 1D FieldArray of length
encoding the phase of each Pauli operator (with values 0 or 1).
If not provided, all values are assumed to be 0.
- Returns:
A list of QubitOperator objects representing the corresponding Pauli operators described by the tableau, each with its associated coefficient.
- Return type:
List[QubitOperator]
- ofex.clifford.dot_tableau(a: FieldArray, b: FieldArray) FieldArray[source]¶
Compute the relationship between two vectors a and b as described in Section VIII of Arxiv:1701.08213. This function determines whether the vectors commute or anti-commute based on their dot product, following the conventions provided in the referenced document.
- Parameters:
a (FieldArray) – The first input vector or matrix (:math: 2N times r).
b (FieldArray) – The second input vector or matrix (:math: 2N times r).
- Returns:
Returns 0 if the vectors commute, or 1 if the vectors anti-commute.
- Return type:
FieldArray
- ofex.clifford.str_tableau(mat: FieldArray, ph: FieldArray | None = None) str[source]¶
Generates a string representation of the tableau form of Pauli operators.
- Parameters:
mat (FieldArray) – A
FieldArray matrix representing the tableau of Pauli operators,
where
is the number of qubits and
is the number of operators.
The first
rows correspond to the X components (
),
and the next
rows to the Z components (
) of the tableau.ph (FieldArray, optional) – An optional FieldArray of length
representing the phase of each operator.
If provided, it is included in the returned string after the tableau rows;
otherwise, no phase information will be included.
- Returns:
A formatted string representing the tableau row by row, with a separator line after the X component rows and before the phase information (if present). The separation helps visually distinguish between components and phase.
- Return type:
str
- ofex.clifford.str_tableau_side_by_side(mat1: FieldArray, ph1: FieldArray | None, mat2: FieldArray, ph2: FieldArray | None) str[source]¶
Generates a side-by-side string representation of two tableau matrices and their phases.
- Parameters:
mat1 (FieldArray) – The first
tableau matrix where
is the number of qubits
and
is the number of operators.ph1 (Optional[FieldArray]) – The optional phase information for the first tableau matrix.
mat2 (FieldArray) – The second
tableau matrix where
is the number of qubits
and
is the number of operators.ph2 (Optional[FieldArray]) – The optional phase information for the second tableau matrix.
- Returns:
A string representation of the two matrices displayed side-by-side.
- Return type:
str
- ofex.clifford.diagonalizing_clifford_tableau(mat: FieldArray, coeff_list: ndarray | None = None, debug=False) Tuple[FieldArray, ndarray, List[str]][source]¶
Diagonalizes a set of mutually commuting Pauli operators using Clifford gates.
This function computes the Clifford operations needed to transform a set of commuting Pauli operators into a diagonal form, represented by a Pauli tableau. It ensures The operations ensure commutation among the input operators and apply transformations step by step to achieve diagonalization. The resulting tableau contains Z-type operators where the upper portion is set to zero.
- Parameters:
mat (FieldArray) – A binary matrix (Pauli tableau) representing the Pauli operators.
coeff_list (Optional[np.ndarray], optional) – An array of coefficients corresponding to the operators in the Pauli tableau. If None, a default array of ones is used.
debug (bool, optional) – If set to True, additional debug information will be printed for each step, showing intermediate transformations. Defaults to False.
- Returns:
- mat (FieldArray): A Pauli tableau of diagonalized operators.
These are Z-type operators, and the upper half is zero.
coeff (np.ndarray): The coefficients of the transformed Pauli operators.
- clifford_history (List[str]): A log of the Clifford operations (as strings)
applied during the diagonalization process.
- Return type:
Tuple[FieldArray, np.ndarray, List[str]]
- Raises:
ValueError – If the input Pauli operators do not commute.
- ofex.clifford.diagonalizing_clifford(pauli_list: List[QubitOperator] | QubitOperator, num_qubits: int, debug=False) Tuple[FieldArray, ndarray, List[str]][source]¶
Diagonalizes a set of mutually commuting Pauli operators using Clifford gates.
This function computes the Clifford operations needed to transform a set of commuting Pauli operators into a diagonal form, represented by a Pauli tableau. It ensures commutation among the input operators, generates the tableau, and applies gate transformations step by step to achieve diagonalization.
- Parameters:
pauli_list (Union[List[QubitOperator], QubitOperator]) – A single or list of mutually commuting Pauli operators.
num_qubits (int) – The number of qubits involved in the operators.
debug (bool, optional) – If True, outputs debug information for intermediate transformation steps. Defaults to False.
- Returns:
- mat (FieldArray): A Pauli tableau of diagonalized operators.
These are Z-type operators, and the upper half is zero.
coeff (np.ndarray): The coefficients of the transformed Pauli operators.
- clifford_history (List[str]): A log of the Clifford operations (as strings)
applied during the diagonalization process.
- Return type:
Tuple[FieldArray, np.ndarray, List[str]]
- Raises:
ValueError – If the input Pauli operators do not commute.
- ofex.clifford.clifford_apply_pauli(pauli: QubitOperator | List[QubitOperator], num_qubits: int, clifford_hist: List[str], inverse: bool = False) QubitOperator | List[QubitOperator][source]¶
Applies a sequence of Clifford operations to a Pauli operator or a list of Pauli operators.
- Parameters:
pauli (Union[QubitOperator, List[QubitOperator]]) – The Pauli operator(s) to which the Clifford operations are applied. Can be a single QubitOperator or a list of QubitOperators.
num_qubits (int) – The number of qubits in the quantum system.
clifford_hist (List[str]) – A list of Clifford operations to apply, specified as strings in the following formats: - “H_<index>”: Hadamard gate applied to qubit at <index>. - “S_<index>”: S gate applied to qubit at <index>. - “CX_<index1>_<index2>”: CNOT gate with qubit at <index1> as control and <index2> as target. - “CZ_<index1>_<index2>”: CZ gate between qubits at <index1> and <index2>. - “QSW_<index1>_<index2>”: Qubit label swap between qubits at <index1> and <index2>.
inverse (bool) – If True, the operations in the Clifford history are applied in reverse order.
- Returns:
- The resulting Pauli operator(s) after applying the
Clifford operations. Returns a single QubitOperator if the input pauli is a single QubitOperator, or a list of QubitOperators if the input pauli is a list.
- Return type:
Union[QubitOperator, List[QubitOperator]]
- ofex.clifford.clifford_apply(mat: FieldArray, ph: FieldArray | None, clifford_hist: List[str], inverse: bool = False) Tuple[FieldArray, FieldArray][source]¶
Applies a sequence of Clifford operations to a stabilizer matrix and phase vector.
- Parameters:
mat (FieldArray) – The stabilizer matrix representing the quantum state in a symplectic form.
ph (Optional[FieldArray]) – The phase vector associated with the quantum state. If None, a zero vector with appropriate dimensions is used.
clifford_hist (List[str]) – A list of Clifford operations to apply, specified in string format. Supported operations: - “H_<index>” for Hadamard gate applied to qubit at <index>. - “S_<index>” for S gate applied to qubit at <index>. - “CX_<index1>_<index2>” for CNOT gate from control qubit <index1> to target qubit <index2>. - “CZ_<index1>_<index2>” for CZ gate between qubits at <index1> and <index2>. - “QSW_<index1>_<index2>” for qubit label swap between qubits at <index1> and <index2>.
inverse (bool) – If True, the operations in the Clifford history are applied in reverse order.
- Returns:
- A tuple containing the updated stabilizer matrix and phase vector after
applying the Clifford operations.
- Return type:
Tuple[FieldArray, FieldArray]
- ofex.clifford.clifford_qiskit(num_qubits: int, clifford_hist: List[str], init_state: ndarray | Dict[BinaryFockVector, Number] | lil_matrix | None, inverse=False) QuantumCircuit[source]¶
Constructs the Qiskit equivalent of a quantum circuit from a given sequence of Clifford operations.
- Parameters:
num_qubits (int) – The total number of qubits in the circuit.
clifford_hist (List[str]) – A list of Clifford operations (e.g.,
"H_<index>","S_<index>","CX_<index1>_<index2>","CZ_<index1>_<index2>","QSW_<index1>_<index2>").init_state (Optional[State]) – The initial quantum state. If None, the circuit starts in the default
|0...0⟩state.inverse (bool) – If True, the operations in the Clifford history are applied in reverse order. Defaults to False.
- Returns:
A Qiskit quantum circuit representing the applied Clifford operations.
- Return type:
QuantumCircuit
- ofex.clifford.clifford_simulation(init_state: ndarray | Dict[BinaryFockVector, Number] | lil_matrix, clifford_history: List[str], inv=False) ndarray[source]¶
Simulates the action of a sequence of Clifford operations on an initial quantum state.
- Parameters:
init_state (State) – The initial quantum state vector as a NumPy array.
clifford_history (List[str]) – A list of Clifford operations (e.g., “H_<index>”, “S_<index>”, “CX_<index1>_<index2>”, “CZ_<index1>_<index2>”, “QSW_<index1>_<index2>”), applied in order.
inv (bool) – If True, the operations in the Clifford history are applied in reverse order. Defaults to False.
- Returns:
The resulting quantum state vector after applying the Clifford operations.
- Return type:
np.ndarray
- ofex.clifford.clifford_unitary_mat(clifford_history, num_qubits, inv=False) ndarray[source]¶
Constructs and returns the unitary matrix representation of a sequence of Clifford operations.
- Parameters:
clifford_history (List[str]) – A list of Clifford operations to apply, specified in string format, e.g., “H_<index>”, “S_<index>”, “CX_<index1>_<index2>”, “CZ_<index1>_<index2>”, “QSW_<index1>_<index2>”.
num_qubits (int) – The total number of qubits for which the unitary matrix is to be constructed.
inv (bool) – If True, the operations in the Clifford history are applied in reverse order. Defaults to False.
- Returns:
The unitary matrix representation of the specified Clifford operations.
- Return type:
np.ndarray