Circuit Class
- class circuit.QuantumCircuit(number_of_qubits: int, num_of_layers: int = 1, device=None)
Bases:
object
- Variables:
number_of_compatible_qubits (int) – Number of qubits in this circuit.
A class to represent a quantum circuit using quantum gates.
The QuantumCircuit class allows for building a quantum circuit, by adding and removing a quantum gate on every qubit at each vertical and horizontal axis. Every iteration from left to right is described by layers. Each layer is compromised of a tensor product of single qubit gates or controlled gates.
Steps for circuit building:
Initializes a first empty layer without any gates.
Adds single qubit gates and control gates. Build a two dimensional array. Every row of the array represents a layer and every column represents a gate on that particular qubit. If no gate is chosen the identity gate will be the default gate.
Move to the next layer and repeat step 2.
After finishing designing the whole circuit we compute the final matrix. This is done by computing the Krorecker product in each layer and multiplying all the matrices of all layers.
Example Usage:
>>> q0 = Qubit(1,0) >>> q1 = Qubit(0,1) >>> mt = MultiQubit() >>> mt.add_qubit(q1) >>> mt.add_qubit(q1) >>> mt.add_qubit(q1) >>> mt.add_qubit(q0) >>> mt.add_qubit(q0) >>> mt.print_tensor_form() >>> circuit = Circuit(mt) >>> circuit.add_controlled_qubit_gate(0,0,1,"X") >>> circuit.add_layer() >>> circuit.add_single_qubit_gate(3,1,"X") >>> circuit.add_single_qubit_gate(0,1,"X") >>> circuit.add_swap_gate(2,4,0) >>> circuit.compute_circuit() >>> circuit.draw_circuit() >>> result = circuit.apply_circuit() >>> result.print_tensor_form() Tensor product in basis state form: |11100⟩ Circuit Diagram: q0: ─[CX]──[X]── q1: ──●0──────── q2: ──⨉───────── q3: ───────[X]── q4: ──⨉───────── Tensor product in basis state form: |11011⟩
- add_conditional_gate(target_qubit: int, gate_type: str, phi: float, layer_index: int, c_reg: ClassicalRegister, c_reg_index: int) None
This method adds a conditional gate and applies the specified unitary of the specified classical bit is one. :param qubit_index: The index of the qubit to which the measure gate will be applied to. :type qubit_index: int :param layer_index: The layer index to which the measure gate will be applied to. :type layer_index: int :param c_reg: The classical register object to which the classical bit will be stored. :type c_reg: ClassicalRegister :param c_reg_int: The index corresponding to the exact classical bit index in the classical register. :type c_reg_int: int
- Raises:
ValueError – If qubit or layer index are invalid a ValueError will be raised.
ValueError – If the provided bit index is invalid. Should be between 0 and number of classical bit in the register
- add_controlled_qubit_gate(target_qubit: int, layer_index: int, control_qubit: int, gate_type: str, phi: float = 0.0) None
Add a controlled quantum gate to the circuit.
- Parameters:
target_qubit (int) – Index of the target qubit
layer_index (int) – Index of the layer where the gate should be added
control_qubit (int) – Index of the control qubit
gate_type (str) – Type of quantum gate (‘X’, ‘Y’, ‘Z’, ‘H’, ‘P’)
phi (float, optional) – Phase angle for phase gates (default: 0.0)
- Raises:
ValueError – If qubit indices or layer index is invalid, or if cells are occupied
- add_layer(layer_index: int = None) None
Adds a new empty layer to the circuit.
Adds to the end by default if no index is given. The new layer is initialized with None values (identity gates).
- Parameters:
layer_index (int, optional) – The index to which a layer will be added.
- add_measure_gate(qubit_index: int, layer_index: int, c_reg: ClassicalRegister, c_reg_index: int) None
Add measure gate method adds a measure gate to the circuit in a specified layer,on a specified qubit. After performing a measurement the collapsed state of a qubit is saved as a classical bit using the proivided classical register.
- Parameters:
qubit_index (int) – The index of the qubit to which the measure gate will be applied to.
layer_index (int) – The layer index to which the measure gate will be applied to.
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 qubit or layer index are invalid a ValueError will be raised.
ValueError – If the provided bit index is invalid. Should be between 0 and number of classical bit in the register
- add_single_qubit_gate(target_qubit: int, layer_index: int, gate_type: str, phi: float = 0.0) None
Add a single-qubit gate to the circuit.
- Parameters:
target_qubit (int) – Index of the qubit to which the gate is applied.
layer_index (int) – Index of the layer where the gate should be added.
gate_type (str) – Type of quantum gate to apply. Supported types include ‘X’, ‘Y’, ‘Z’, ‘H’, and ‘P’.
phi (float, optional) – Phase angle for phase gates. Default is 0.0.
- Raises:
ValueError – If the qubit index or layer index is invalid, or if the target cell is already occupied.
- add_swap_gate(first_qubit: int, second_qubit: int, layer_index: int) None
Add a SWAP gate between two qubits in the circuit.
- Parameters:
first_qubit (int) – Index of the first qubit to swap
second_qubit (int) – Index of the second qubit to swap
layer_index (int) – Index of the layer where the gate should be added
- Raises:
ValueError – If qubit indices or layer index is invalid, or if cells are occupied
- device_in_use() None
This function prints the device in use by this quantum circuit.
- draw_circuit(matplotlib: bool = True) None
Print a visualization of the quantum circuit. You can specify to visualize in matplotlib or CLI. The method will visualize using matplotlib by default.
The visualization includes: - Horizontal lines (─) representing qubit wires - Single qubit gates with their type (H, X, Y, Z, P) - Control points (●) for controlled gates - Target points (⊕ for X gates, ◯ for others) - Swap gates (⨉)
- Parameters:
matplotlib (bool) – True for printing using matplotlib and false for CLI.
- get_array() ndarray[Any, dtype[_ScalarType_co]]
This function returns the matrix with all of the gates.
- Returns:
Matrix with all of the gates.
- Return type:
NDArray
- get_circuit_operator_matrix() ndarray[Any, dtype[complex128]]
This function returns the final computed matrix of the entire circuit:
- Returns:
Matrix representing the entire circuit.
- Return type:
NDarray
- get_number_of_compatible_qubits() int
This function returns the number of qubits this circuit is compatible with.
- Returns:
The number of qubit this circuit is compatible with.
- Return type:
int
- get_number_of_layers() int
This function returns the number of layers in this circuit. :returns: The number of layers in the circuit. :rtype: int
- load_qft_preset() None
This method loads a prebuild Quantum Fourier Transform circuit using the number of qubits given. This QFT circuit is the regular circuit using a traditional design opposed to the dynamical one.
- measure_all(input_state: MultiQubit) MultiQubit
This method applies the circuit on the input state and measures the resulting state. The measured state will be the collapsed state of on of the possible states.
- Parameters:
input_state (MultiQubit) – The input state is the state the circuit will be applied on and will be measured.
- Returns:
The collapsed state due to measurement.
- Return type:
- print_operator_matrix() None
Print the final matrix that is applid on the state.
- remove_layer(layer_index: int = None) None
Remove the a specified layer from the circuit. If no layer was specified than the last layer is removed by default.
- Parameters:
layer_index (int) – The index to which a layer will be removed.
- remove_single_qubit_gate(qubit_index: int, layer_index: int) None
Remove a single qubit gate from the circuit.
- Parameters:
qubit_index (int) – Index of the qubit to remove the gate from
layer_index (int) – Index of the layer where the gate should be removed
- Raises:
ValueError – If qubit index or layer index is invalid
- remove_two_qubit_gate(target_qubit: int, control_qubit: int, layer_index: int) None
Remove a two-qubit gate (controlled or SWAP) from the circuit.
- Parameters:
target_qubit (int) – Index of the target qubit
control_qubit (int) – Index of the control qubit
layer_index (int) – Index of the layer where the gate should be removed
- Raises:
ValueError – If qubit indices or layer index is invalid
- reset_circuit() None
Reset the circuit to its initial empty state.
Clears all gates and returns to a single empty layer with identity gates.
- run_circuit(input_state: MultiQubit) MultiQubit
Run the the whole circuit on the input quantum state. The method runs both on dynamical and non dynamical circuits and returns the final state.
- Parameters:
input_state (MultiQubit) – New quantum state to use as input.
- Returns:
Resulting quantum state after applying the circuit.
- Return type:
- Raises:
ValueError – If the input state has a different number of qubits.
ValueError – If the layer index is not valid.