Cell Class
- class cell.QuantumCircuitCell
Bases:
object
A class to represent a quantum circuit cell, with options for single-qubit gates, controlled gates, and swap gates, measurments.
The gate can be specified as a single qubit gate (e.g., I, X, Y, Z, H, SWAP,M), a controlled qubit gate, or a swap gate, allowing for versatile operations on qubits in a quantum system.
Example
>>> gate = QuantumCircuitCell() >>> gate.set_controlled_qubit_gate(1, 2, "P", np.pi/2) >>> gate.print_matrix() >>> print(gate.get_control_index()) >>> print(gate.get_target_index()) >>> print(gate.is_control_gate()) Output: [ 1.00 0.00 ] [ 0.00 0.00+1.00j ] 1 2 True
- apply_conditional_gate() None
This method updates the gate matrix depending on the value of the classical bit connected to this gate. If the classical bit is 1 we keep the matrix of the specified unitary gate otherwise the gate will not be applied and will act as if a identity gate.
- conditional_gate_input(classical_bit: int, gate_type: str, phi: float = 0.0) None
This method recieves an input classical bit. If the input bit is one we apply the desired unitary operation otherwise we don’t won’t to change the qubit state applying the identity matrix.
- Parameters:
classical_bit (int) – The input classical bit.
gate_type (str) – The desired unitary opertion gate type.
- get_control_index() int
Return the control qubit index if this gate is a control or swap gate.
- Returns:
The index of the control qubit.
- Return type:
int
- Raises:
ValueError – If the gate is not a control or swap gate.
- get_gate_type() str
This function returns the gate type of this gate (e.g., I, X, Y, Z, H, SWAP) :return: The gate type :rtype: str
- get_matrix() ndarray[Any, dtype[complex128]]
Return the matrix representation of the gate.
- Returns:
matrix – The matrix of the current get.
- Return type:
NDArray[np.complex128]
- get_target_index() int
Return the target qubit index if this gate is a control or swap gate.
- Returns:
The index of the target qubit.
- Return type:
int
- Raises:
ValueError – If the gate is not a control or swap gate.
- is_classical_bit() bool
Check if this cell represents a classical bit.
- Returns:
return_val – True if the cell is a classical bit, False otherwise.
- Return type:
bool
- is_conditional_gate() bool
This methods returns a boolean value if the gate is a conditional gate or not.
A conditional gate is a gate that acts on one qubits with an unitary operation if the classical bit input is one. Other wise the gate is just an identity gate.
- Returns:
return_val – True if the gate is a measure gate and false otherwiss.
- Return type:
bool
- is_control_gate() bool
Returns true or false if the gate is a control gate.
- Returns:
Boolean value if the gate is a control gate or not.
- Return type:
Bool
- is_measure_gate() bool
This methods returns a boolean value if the gate is a measure gate or not.
- Returns:
return_val – True if the gate is a measure gate and false otherwiss.
- Return type:
bool
- is_single_qubit_gate() bool
This method returns if the gate is a single qubit gate or not.
- Returns:
Returns boolean value if the gate is a single qubit gate.
- Return type:
bool
- is_swap_gate() bool
Returns true or false if the gate is a swap gate.
- Returns:
Boolean value if the gate is a swap gate or not.
- Return type:
Bool
- measure(input_state: MultiQubit) MultiQubit
The method performs a measurment on the current qubit and saves the collapsed state in the classical register specified in measure gate initialization
- Parameters:
input_state (MultiQubit) – The input quantum state on which the measurement will be performed.
- Returns:
The multi qubit quantum state of the whole system. We expect the non measured qubits to be intact.
- Return type:
- Raises:
ValueError – If the current gate is not a measurement gate.
- print_matrix() None
Print the gate matrix with formatted complex values.
Values close to zero are rounded to zero for better readability.
- set_classical_bit() None
Set this cell to represent a classical bit.
- set_conditional_gate(gate_type: str, phi: float, c_reg: ClassicalRegister, c_reg_index: int) None
This method sets this gate to be a a conditional gate.
A conditional gate is a gate that acts on one qubit with an unitary operation if the classical bit is one in the specified classical register. Otherwise the gate will act as an ordinary identity gate.
- Parameters:
c_reg (ClassicalRegister) – The classical register object to which the classical bit will be stored.
c_reg_int (int) – The index corresponding to the exact classical bit index in the classical register.
- set_controlled_qubit_gate(control_qubit: int, target_qubit: int, gate_type: str = 'I', phi: float = 0.0) None
Set up a controlled gate with the specified control and target qubits and gate type.
- Parameters:
control_qubit (int) – Index of the control qubit.
target_qubit (int) – Index of the target qubit.
gate_type (str) – Type of the gate, should be one of (‘I’, ‘X’, ‘Y’, ‘Z’, ‘H’, ‘P’).
phi (float) – Rotation angle in radians, used only if gate_type is ‘P’ (phase gate).
- Raises:
ValueError – If indices are negative or if the gate type is invalid.
- set_measure_gate(target_qubit: int, c_reg: ClassicalRegister, c_reg_index: int) None
The method sets this gate to be a measurement gate.
- Parameters:
target_qubit (int) – The qubit index on which the measurement is performed.
c_reg (ClassicalRegister) – The classical register object to which the classical bit will be stored.
c_reg_int (int) – The index corresponding to the exact classical bit index in the classical register.
- Raises:
ValueError – If the provided bit index is invalid. Should be between 0 and number of classical bit in the register
- set_single_qubit_gate(gate_type: str = 'I', phi: float = 0.0) None
Set the gate matrix for a specified single qubit gate type or phase gate.
- Parameters:
gate_type (str) – Type of the gate, should be one of (‘I’, ‘X’, ‘Y’, ‘Z’, ‘H’, ‘P’).
phi (float) – Rotation angle in radians, used only if gate_type is ‘P’ (phase gate).
- Raises:
ValueError – If the gate type is invalid.
- set_swap_gate(first_qubit: int, second_qubit: int) None
Configure a swap gate with the specified first and second qubits.
- Parameters:
first_qubit (int) – Index of the first qubit to swap with.
second_qubit (int) – Index of the second qubit to swap with.
- Raises:
ValueError – If indices are negative.