Gates#
All supported gates can be accessed from the qibo.gates module.
Read below for a complete list of supported gates.
All gates support the controlled_by method that allows to control
the gate on an arbitrary number of qubits. For example
gates.X(0).controlled_by(1, 2)is equivalent togates.TOFFOLI(1, 2, 0),gates.RY(0, np.pi).controlled_by(1, 2, 3)applies the Y-rotation to qubit 0 when qubits 1, 2 and 3 are in the|111>state.gates.SWAP(0, 1).controlled_by(3, 4)swaps qubits 0 and 1 when qubits 3 and 4 are in the|11>state.
Abstract gate#
- class qibo.gates.abstract.Gate[source]
The base class for gate implementation.
All base gates should inherit this class.
- name
Name of the gate.
- Type:
- draw_label
Optional label for drawing the gate in a circuit with
qibo.models.Circuit.draw().- Type:
- is_controlled_by
Trueif the gate was created using theqibo.gates.abstract.Gate.controlled_by()method, otherwiseFalse.- Type:
- init_args
Arguments used to initialize the gate.
- Type:
- init_kwargs
Arguments used to initialize the gate.
- Type:
- target_qubits
Tuple with ids of target qubits.
- Type:
- control_qubits
Tuple with ids of control qubits sorted in increasing order.
- Type:
- basis_rotation()[source]
Transformation required to rotate the basis for measuring the gate.
- property clifford: bool
Return boolean value representing if a Gate is Clifford or not.
- commutes(gate)[source]
Checks if two gates commute.
- Parameters:
gate – Gate to check if it commutes with the current gate.
- Returns:
Trueif the gates commute,Falseotherwise.- Return type:
- property control_qubits: tuple[int, ...]
Tuple with ids of control qubits sorted in increasing order.
- dagger()[source]
Returns the dagger (conjugate transpose) of the gate.
Note that dagger is not persistent for parametrized gates. For example, applying a dagger to an
qibo.gates.gates.RXgate will change the sign of its parameter at the time of application. However, if the parameter is updated after that, for example usingqibo.models.circuit.Circuit.set_parameters(), then the action of dagger will be lost.- Returns:
object representing the dagger of the original gate.
- Return type:
qibo.gates.Gate
- decompose(*free, method='standard', **kwargs)[source]
Decomposes multi-control gates to gates supported by OpenQASM.
Decompositions are based on Phys. Rev. A 52, 3457 (1995). If the gate is already controlled, it recursively decomposes the base gate and updates the control qubits accordingly. The exception are one-qubit gates controlled by more than one qubit, which are decomposed by
qibo.transpiler.multicontrolled_decompositions.multi_controlled_decomposition()(only ifmethod="standard"). Withoutfreequbits no (dirty) auxiliary qubits are used. The decomposition is applied recursively, so that the returned gates are controlled by at most one qubit.- Parameters:
free (int) – Ids of free qubits that can be used as dirty auxiliary qubits, that is, they can be in any state and are left unchanged.
method (str, optional) – Choice of gate set for the decomposition. If
"standard", decomposes circuit intoqibo.gates.gates.CNOT,qibo.gates.gates.RX,qibo.gates.gates.RY,qibo.gates.gates.RZ,qibo.gates.gates.U1,qibo.gates.gates.U2,qibo.gates.gates.U3, and Clifford gates. If"clifford_plus_t", decomposes the circuit intoqibo.gates.gates.CNOT,qibo.gates.gates.H,qibo.gates.gates.S,qibo.gates.gates.X,qibo.gates.gates.Y,qibo.gates.gates.Z, andqibo.gates.gates.T. Defaults to"standard".kwargs (dict, optional) – Additional arguments. When
method = "clifford_plus_t", one can setepsilon(\(\epsilon\)) precision for the transpilation of each gate into the Clifford +qibo.gates.gates.Tgate set. This precision defaults to \(\epsilon = 10^{-16}\). Another possible keyword argument ismpmath_dps, which defines the number of decimal places used by thempmathpackage.mpmmath_dpsdefaults to \(256\). For gates controlled by more than one qubit andmethod = "standard", the keyword argumentsclean,minimize_toffolisandminimize_depthare passed toqibo.transpiler.multicontrolled_decompositions.multi_controlled_decomposition().
- Returns:
Gates that have the same effect as applying the original gate.
- Return type:
List[
qibo.gates.abstract.Gate]
- static from_dict(raw)[source]
Load from serialization.
Essentially the counter-part of
raw().- Parameters:
raw (dict) –
- abstract generator(backend)[source]
This function returns the gate’s generator.
- Returns:
generator.
- Return type:
ArrayLike
- generator_eigenvalue()[source]
This function returns the eigenvalues of the gate’s generator.
- Returns:
eigenvalue of the generator.
- Return type:
- property hamming_weight: bool
Return boolean value representing if a Gate is Hamming-weight-preserving or not.
- matrix(backend=None)[source]
Returns the matrix representation of the gate.
If gate has controlled qubits inserted by
qibo.gates.Gate.controlled_by(), thenqibo.gates.Gate.matrix()returns the matrix of the original gate.from qibo import gates gate = gates.SWAP(3, 4).controlled_by(0, 1, 2) print(gate.matrix())
To return the full matrix that takes the control qubits into account, one should use
qibo.models.Circuit.unitary(), e.g.from qibo import Circuit, gates nqubits = 5 circuit = Circuit(nqubits) circuit.add(gates.SWAP(3, 4).controlled_by(0, 1, 2)) print(circuit.unitary())
- Parameters:
backend (
qibo.backends.abstract.Backend, optional) – backend to be used in the execution. IfNone, it uses the current backend. Defaults toNone.- Returns:
Matrix representation of gate.
- Return type:
ArrayLike
- on_qubits(qubit_map)[source]
Creates the same gate targeting different qubits.
- Parameters:
qubit_map (int) – Dictionary mapping original qubit indices to new ones.
- Returns:
A
qibo.gates.Gateobject of the original gate type targeting the given qubits.
Example
from qibo import Circuit, gates circuit = Circuit(4) # Add some CNOT gates # equivalent to gates.CNOT(2, 3) circuit.add(gates.CNOT(2, 3).on_qubits({2: 2, 3: 3})) # equivalent to gates.CNOT(3, 0) circuit.add(gates.CNOT(2, 3).on_qubits({2: 3, 3: 0})) # equivalent to gates.CNOT(1, 3) circuit.add(gates.CNOT(2, 3).on_qubits({2: 1, 3: 3})) # equivalent to gates.CNOT(2, 1) circuit.add(gates.CNOT(2, 3).on_qubits({2: 2, 3: 1})) circuit.draw()
0: ───X───── 1: ───|─o─X─ 2: ─o─|─|─o─ 3: ─X─o─X───
- property parameters: tuple[float, ...]
Returns a tuple containing the current value of gate’s parameters.
- property qasm_label: str | tuple[str, str]
String corresponding to OpenQASM operation of the gate. For more exotic gates, both the internal qibo name of the gate and the custom gate QASM definition are returned as a tuple for broader compatibility.
- property qubits: tuple[int, ...]
Tuple with ids of all qubits (control and target) that the gate acts.
- property raw: dict
Serialize the gate to a JSON-compatible dictionary.
The returned dictionary always contains the following keys:
{ "name": str, # gate name (e.g. "h", "cx", "rz") "init_args": list, # positional args of the gate constructor "init_kwargs": dict, # keyword args of the gate constructor "_target_qubits": list, # ids of the target qubits "_control_qubits": list, # ids of the control qubits "_class": str, # gate class name (e.g. "H", "CNOT", "RZ") }Only the constructor keyword arguments listed in
qibo.gates.abstract.REQUIRED_FIELDS_INIT_KWARGS(e.g.theta,phi,lam,basis,collapse, …) are kept ininit_kwargs. The values are restricted to JSON-compatible scalar types. Measurement gates additionally carry ameasurement_resultentry. The dictionary can be loaded back into a gate withqibo.gates.Gate.from_dict().- Returns:
JSON-compatible representation of the gate.
- Return type:
- to_json()[source]
Dump gate to JSON.
Note
Consider using
raw()directly.
Single qubit gates#
Hadamard (H)#
Pauli X (X)#
- class qibo.gates.X(q)[source]#
The Pauli-\(X\) gate.
Corresponds to the following unitary matrix
\[\begin{split}\begin{pmatrix} 0 & 1 \\ 1 & 0 \\ \end{pmatrix}\end{split}\]- Parameters:
q (int) – the qubit id number.
- property qasm_label: str#
String corresponding to OpenQASM operation of the gate. For more exotic gates, both the internal qibo name of the gate and the custom gate QASM definition are returned as a tuple for broader compatibility.
- decompose(*free, method='standard', **kwargs)[source]#
Decomposes multi-control gates to gates supported by OpenQASM.
Decompositions are based on Phys. Rev. A 52, 3457 (1995). If the gate is already controlled, it recursively decomposes the base gate and updates the control qubits accordingly. The exception are one-qubit gates controlled by more than one qubit, which are decomposed by
qibo.transpiler.multicontrolled_decompositions.multi_controlled_decomposition()(only ifmethod="standard"). Withoutfreequbits no (dirty) auxiliary qubits are used. The decomposition is applied recursively, so that the returned gates are controlled by at most one qubit.- Parameters:
free (int) – Ids of free qubits that can be used as dirty auxiliary qubits, that is, they can be in any state and are left unchanged.
method (str, optional) – Choice of gate set for the decomposition. If
"standard", decomposes circuit intoqibo.gates.gates.CNOT,qibo.gates.gates.RX,qibo.gates.gates.RY,qibo.gates.gates.RZ,qibo.gates.gates.U1,qibo.gates.gates.U2,qibo.gates.gates.U3, and Clifford gates. If"clifford_plus_t", decomposes the circuit intoqibo.gates.gates.CNOT,qibo.gates.gates.H,qibo.gates.gates.S,qibo.gates.gates.X,qibo.gates.gates.Y,qibo.gates.gates.Z, andqibo.gates.gates.T. Defaults to"standard".kwargs (dict, optional) – Additional arguments. When
method = "clifford_plus_t", one can setepsilon(\(\epsilon\)) precision for the transpilation of each gate into the Clifford +qibo.gates.gates.Tgate set. This precision defaults to \(\epsilon = 10^{-16}\). Another possible keyword argument ismpmath_dps, which defines the number of decimal places used by thempmathpackage.mpmmath_dpsdefaults to \(256\). For gates controlled by more than one qubit andmethod = "standard", the keyword argumentsclean,minimize_toffolisandminimize_depthare passed toqibo.transpiler.multicontrolled_decompositions.multi_controlled_decomposition().
- Returns:
Gates that have the same effect as applying the original gate.
- Return type:
List[
qibo.gates.abstract.Gate]
Pauli Y (Y)#
- class qibo.gates.Y(q)[source]#
The Pauli-\(Y\) gate.
Corresponds to the following unitary matrix
\[\begin{split}\begin{pmatrix} 0 & -i \\ i & 0 \\ \end{pmatrix}\end{split}\]- Parameters:
q (int) – the qubit id number.
Pauli Z (Z)#
- class qibo.gates.Z(q)[source]#
The Pauli-\(Z\) gate.
Corresponds to the following unitary matrix
\[\begin{split}\begin{pmatrix} 1 & 0 \\ 0 & -1 \\ \end{pmatrix}\end{split}\]- Parameters:
q (int) – the qubit id number.
- property hamming_weight: bool#
Return boolean value representing if a Gate is Hamming-weight-preserving or not.
Square-root of Pauli X (SX)#
- class qibo.gates.SX(q)[source]#
The \(\sqrt{X}\) gate.
Corresponds to the following unitary matrix
\[\begin{split}\frac{1}{2} \, \begin{pmatrix} 1 + i & 1 - i \\ 1 - i & 1 + i \\ \end{pmatrix}\end{split}\]- Parameters:
q (int) – the qubit id number.
- property qasm_label: str#
String corresponding to OpenQASM operation of the gate. For more exotic gates, both the internal qibo name of the gate and the custom gate QASM definition are returned as a tuple for broader compatibility.
- decompose(*free, method='standard', **kwargs)[source]#
Decomposition of \(\sqrt{X}\) up to global phase.
A global phase difference exists between the definitions of \(\sqrt{X}\) and \(\text{RX}(\pi / 2)\), with \(\text{RX}\) being the
qibo.gates.RXgate. More precisely, \(\sqrt{X} = e^{i \pi / 4} \, \text{RX}(\pi / 2)\).
S gate (S)#
- class qibo.gates.S(q)[source]#
The \(S\) gate.
Corresponds to the following unitary matrix
\[\begin{split}\begin{pmatrix} 1 & 0 \\ 0 & i \\ \end{pmatrix}\end{split}\]- Parameters:
q (int) – the qubit id number.
T gate (T)#
- class qibo.gates.T(q)[source]#
The T gate.
Corresponds to the following unitary matrix
\[\begin{split}\begin{pmatrix} 1 & 0 \\ 0 & e^{i \pi / 4} \\ \end{pmatrix}\end{split}\]- Parameters:
q (int) – the qubit id number.
Identity (I)#
Align (A)#
Measurement (M)#
- class qibo.gates.M(*q, register_name=None, collapse=False, basis=<class 'qibo.gates.gates.Z'>, p0=None, p1=None)[source]#
The measure gate.
- Parameters:
*q (int) – id numbers of the qubits to measure. It is possible to measure multiple qubits using
gates.M(0, 1, 2, ...). If the qubits to measure are held in an iterable (eg. list) the*operator can be used, for examplegates.M(*[0, 1, 4])orgates.M(*range(5)).register_name (str, optional) – Optional name of the register to distinguish it from other registers when used in circuits.
collapse (bool) – Collapse the state vector after the measurement is performed. Can be used only for single shot measurements. If
Truethe collapsed state vector is returned. IfFalsethe measurement result is returned.basis (
qibo.gates.Gateor str or list, optional) – Basis to measure. Can be either: - a qibo gate - the string representing the gate - a callable that accepts a qubit, for example:lambda q: gates.RX(q, 0.2)- a list of the above, if a different basis will be used for each measurement qubit. Defaults is toqibo.gates.Z.p0 (dict, optional) – bitflip probability map. Can be: A dictionary that maps each measured qubit to the probability that it is flipped, a list or tuple that has the same length as the tuple of measured qubits or a single float number. If a single float is given the same probability will be used for all qubits.
p1 (dict, optional) – bitflip probability map for asymmetric bitflips. Same as
p0but controls the 1->0 bitflip probability. Ifp1isNonethenp0will be used both for 0->1 and 1->0 bitflips.
- property raw: dict#
Serialize to dictionary.
The values used in the serialization should be compatible with a JSON dump (or any other one supporting a minimal set of scalar types). Though the specific implementation is up to the specific gate.
- add(gate)[source]#
Adds target qubits to a measurement gate.
This method is only used for creating the global measurement gate used by the
qibo.models.Circuit. The user is not supposed to use this method and aValueErroris raised if he does so.- Parameters:
gate – Measurement gate to add its qubits in the current gate.
- matrix(backend=None)[source]#
Returns the matrix representation of the gate.
If gate has controlled qubits inserted by
qibo.gates.Gate.controlled_by(), thenqibo.gates.Gate.matrix()returns the matrix of the original gate.from qibo import gates gate = gates.SWAP(3, 4).controlled_by(0, 1, 2) print(gate.matrix())
To return the full matrix that takes the control qubits into account, one should use
qibo.models.Circuit.unitary(), e.g.from qibo import Circuit, gates nqubits = 5 circuit = Circuit(nqubits) circuit.add(gates.SWAP(3, 4).controlled_by(0, 1, 2)) print(circuit.unitary())
- Parameters:
backend (
qibo.backends.abstract.Backend, optional) – backend to be used in the execution. IfNone, it uses the current backend. Defaults toNone.- Returns:
Matrix representation of gate.
- Return type:
ArrayLike
- classmethod load(payload)[source]#
Constructs a measurement gate starting from a json serialized one.
- on_qubits(qubit_map)[source]#
Creates the same measurement gate targeting different qubits and preserving the measurement result register.
- Parameters:
qubit_map (dict) – dictionary mapping original qubit indices to new ones.
- Returns:
object of the original gate type targeting the given qubits.
- Return type:
qibo.gates.Gate.M
Example
from qibo import Circuit, gates measurement = gates.M(0, 1) circuit = Circuit(3) circuit.add(measurement.on_qubits({0: 0, 1: 2})) assert circuit.queue[0].result is measurement.result circuit.draw()
0: ─M─ 1: ─|─ 2: ─M─
Rotation X-axis (RX)#
- class qibo.gates.RX(q, theta, trainable=True)[source]#
Rotation around the X-axis of the Bloch sphere.
Corresponds to the following unitary matrix
\[\begin{split}\begin{pmatrix} \cos \frac{\theta }{2} & -i\sin \frac{\theta }{2} \\ -i\sin \frac{\theta }{2} & \cos \frac{\theta }{2} \\ \end{pmatrix}\end{split}\]- Parameters:
q (int) – the qubit id number.
theta (float) – the rotation angle.
trainable (bool) – whether gate parameters can be updated using
qibo.models.circuit.Circuit.set_parameters(). Defaults toTrue.
- property hamming_weight: bool#
Return boolean value representing if a Gate is Hamming-weight-preserving or not.
Rotation Y-axis (RY)#
- class qibo.gates.RY(q, theta, trainable=True)[source]#
Rotation around the Y-axis of the Bloch sphere.
Corresponds to the following unitary matrix
\[\begin{split}\begin{pmatrix} \cos \frac{\theta }{2} & -\sin \frac{\theta }{2} \\ \sin \frac{\theta }{2} & \cos \frac{\theta }{2} \\ \end{pmatrix}\end{split}\]- Parameters:
q (int) – the qubit id number.
theta (float) – the rotation angle.
trainable (bool) – whether gate parameters can be updated using
qibo.models.circuit.Circuit.set_parameters(). Defaults toTrue.
- property hamming_weight: bool#
Return boolean value representing if a Gate is Hamming-weight-preserving or not.
Rotation Z-axis (RZ)#
- class qibo.gates.RZ(q, theta, trainable=True)[source]#
Rotation around the Z-axis of the Bloch sphere.
Corresponds to the following unitary matrix
\[\begin{split}\begin{pmatrix} e^{-i \theta / 2} & 0 \\ 0 & e^{i \theta / 2} \\ \end{pmatrix}\end{split}\]- Parameters:
q (int) – the qubit id number.
theta (float) – the rotation angle.
trainable (bool) – whether gate parameters can be updated using
qibo.models.circuit.Circuit.set_parameters(). Defaults toTrue.
- property hamming_weight: bool#
Return boolean value representing if a Gate is Hamming-weight-preserving or not.
First general unitary (U1)#
- class qibo.gates.U1(q, theta, trainable=True)[source]#
First general unitary gate. Also known as Phase gate or P gate.
Corresponds to the following unitary matrix
\[\begin{split}\begin{pmatrix} 1 & 0 \\ 0 & e^{i \theta} \\ \end{pmatrix}\end{split}\]- Parameters:
q (int) – the qubit id number.
theta (float) – the rotation angle.
trainable (bool) – whether gate parameters can be updated using
qibo.models.circuit.Circuit.set_parameters(). Defaults toTrue.
Second general unitary (U2)#
- class qibo.gates.U2(q, phi, lam, trainable=True)[source]#
Second general unitary gate.
Corresponds to the following unitary matrix
\[\begin{split}\frac{1}{\sqrt{2}} \begin{pmatrix} e^{-i(\phi + \lambda )/2} & -e^{-i(\phi - \lambda )/2} \\ e^{i(\phi - \lambda )/2} & e^{i (\phi + \lambda )/2} \\ \end{pmatrix}\end{split}\]- Parameters:
Third general unitary (U3)#
- class qibo.gates.U3(q, theta, phi, lam, trainable=True)[source]#
Third general unitary gate.
Corresponds to the following unitary matrix
\[\begin{split}\begin{pmatrix} e^{-i(\phi + \lambda )/2}\cos\left (\frac{\theta }{2}\right ) & -e^{-i(\phi - \lambda )/2}\sin\left (\frac{\theta }{2}\right ) \\ e^{i(\phi - \lambda )/2}\sin\left (\frac{\theta }{2}\right ) & e^{i (\phi + \lambda )/2}\cos\left (\frac{\theta }{2}\right ) \\ \end{pmatrix}\end{split}\]- Parameters:
- decompose(*free, method='standard', **kwargs)[source]#
Decomposition of \(U_{3}\) up to global phase.
A global phase difference exists between the definitions of \(U3\) and this decomposition. More precisely,
\[U_{3}(\theta, \phi, \lambda) = e^{i \, \frac{3 \pi}{2}} \, \text{RZ}(\phi + \pi) \, \sqrt{X} \, \text{RZ}(\theta + \pi) \, \sqrt{X} \, \text{RZ}(\lambda) \, ,\]where \(\text{RZ}\) and \(\sqrt{X}\) are, respectively,
qibo.gates.RZand :class`qibo.gates.SX`.
Two qubit gates#
Controlled-NOT (CNOT)#
Controlled-Y (CY)#
Controlled-phase (CZ)#
- class qibo.gates.CZ(q0, q1)[source]#
The Controlled-Phase gate.
Corresponds to the following unitary matrix
\[\begin{split}\begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & -1 \\ \end{pmatrix}\end{split}\]
Controlled-Hadamard (CH)#
Controlled-Square Root of X (CSX)#
Controlled-rotation X-axis (CRX)#
- class qibo.gates.CRX(q0, q1, theta, trainable=True)[source]#
Controlled rotation around the X-axis for the Bloch sphere.
Corresponds to the following unitary matrix
\[\begin{split}\begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & \cos \frac{\theta }{2} & -i\sin \frac{\theta }{2} \\ 0 & 0 & -i\sin \frac{\theta }{2} & \cos \frac{\theta }{2} \\ \end{pmatrix}\end{split}\]- Parameters:
q0 (int) – the control qubit id number.
q1 (int) – the target qubit id number.
theta (float) – the rotation angle.
trainable (bool) – whether gate parameters can be updated using
qibo.models.circuit.Circuit.set_parameters(). Defaults toTrue.
Controlled-rotation Y-axis (CRY)#
- class qibo.gates.CRY(q0, q1, theta, trainable=True)[source]#
Controlled rotation around the Y-axis for the Bloch sphere.
Corresponds to the following unitary matrix
\[\begin{split}\begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & \cos \frac{\theta }{2} & -\sin \frac{\theta }{2} \\ 0 & 0 & \sin \frac{\theta }{2} & \cos \frac{\theta }{2} \\ \end{pmatrix}\end{split}\]Note that this differs from the
qibo.gates.RZgate.- Parameters:
q0 (int) – the control qubit id number.
q1 (int) – the target qubit id number.
theta (float) – the rotation angle.
trainable (bool) – whether gate parameters can be updated using
qibo.models.circuit.Circuit.set_parameters(). Defaults toTrue.
Controlled-rotation Z-axis (CRZ)#
- class qibo.gates.CRZ(q0, q1, theta, trainable=True)[source]#
Controlled rotation around the Z-axis for the Bloch sphere.
Corresponds to the following unitary matrix
\[\begin{split}\begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & e^{-i \theta / 2} & 0 \\ 0 & 0 & 0 & e^{i \theta / 2} \\ \end{pmatrix}\end{split}\]- Parameters:
q0 (int) – the control qubit id number.
q1 (int) – the target qubit id number.
theta (float) – the rotation angle.
trainable (bool) – whether gate parameters can be updated using
qibo.models.circuit.Circuit.set_parameters(). Defaults toTrue.
Controlled first general unitary (CU1)#
- class qibo.gates.CU1(q0, q1, theta, trainable=True)[source]#
Controlled first general unitary gate. Also known as CPhase.
Corresponds to the following unitary matrix
\[\begin{split}\begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & e^{i \theta } \\ \end{pmatrix}\end{split}\]Note that this differs from the
qibo.gates.CRZgate.- Parameters:
q0 (int) – the control qubit id number.
q1 (int) – the target qubit id number.
theta (float) – the rotation angle.
trainable (bool) – whether gate parameters can be updated using
qibo.models.circuit.Circuit.set_parameters(). Defaults toTrue.
Controlled second general unitary (CU2)#
- class qibo.gates.CU2(q0, q1, phi, lam, trainable=True)[source]#
Controlled second general unitary gate.
Corresponds to the following unitary matrix
\[\begin{split}\frac{1}{\sqrt{2}} \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & e^{-i(\phi + \lambda )/2} & -e^{-i(\phi - \lambda )/2} \\ 0 & 0 & e^{i(\phi - \lambda )/2} & e^{i (\phi + \lambda )/2} \\ \end{pmatrix}\end{split}\]- Parameters:
Controlled third general unitary (CU3)#
- class qibo.gates.CU3(q0, q1, theta, phi, lam, trainable=True)[source]#
Controlled third general unitary gate.
Corresponds to the following unitary matrix
\[\begin{split}\begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & e^{-i(\phi + \lambda )/2}\cos\left (\frac{\theta }{2}\right ) & -e^{-i(\phi - \lambda )/2}\sin\left (\frac{\theta }{2}\right ) \\ 0 & 0 & e^{i(\phi - \lambda )/2}\sin\left (\frac{\theta }{2}\right ) & e^{i (\phi + \lambda )/2}\cos\left (\frac{\theta }{2}\right ) \\ \end{pmatrix}\end{split}\]- Parameters:
q0 (int) – the control qubit id number.
q1 (int) – the target qubit id number.
theta (float) – first rotation angle.
phi (float) – second rotation angle.
lamb (float) – third rotation angle.
trainable (bool) – whether gate parameters can be updated using
qibo.models.circuit.Circuit.set_parameters(). Defaults toTrue.lam (float) –
Swap (SWAP)#
- class qibo.gates.SWAP(q0, q1)[source]#
The swap gate.
Corresponds to the following unitary matrix
\[\begin{split}\begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ \end{pmatrix}\end{split}\]- Parameters:
iSwap (iSWAP)#
- class qibo.gates.iSWAP(q0, q1)[source]#
The iSWAP gate.
Corresponds to the following unitary matrix
\[\begin{split}\begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & i & 0 \\ 0 & i & 0 & 0 \\ 0 & 0 & 0 & 1 \\ \end{pmatrix}\end{split}\]- Parameters:
Square root of iSwap (SiSWAP)#
f-Swap (FSWAP)#
- class qibo.gates.FSWAP(q0, q1)[source]#
The fermionic swap gate.
Corresponds to the following unitary matrix
\[\begin{split}\begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & -1 \\ \end{pmatrix}\end{split}\]- Parameters:
fSim#
- class qibo.gates.fSim(q0, q1, theta, phi, trainable=True)[source]#
The fSim gate defined in arXiv:2001.08343.
Corresponds to the following unitary matrix
\[\begin{split}\begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & \cos \theta & -i\sin \theta & 0 \\ 0 & -i\sin \theta & \cos \theta & 0 \\ 0 & 0 & 0 & e^{-i \phi } \\ \end{pmatrix}\end{split}\]- Parameters:
q0 (int) – the first qubit to be swapped id number.
q1 (int) – the second qubit to be swapped id number.
theta (float) – Angle for the one-qubit rotation.
phi (float) – Angle for the
|11>phase.trainable (bool) – whether gate parameters can be updated using
qibo.models.circuit.Circuit.set_parameters(). Defaults toTrue.
Sycamore gate#
- class qibo.gates.SYC(q0, q1)[source]#
The Sycamore gate, defined in the Supplementary Information of Quantum supremacy using a programmable superconducting processor.
Corresponding to the following unitary matrix
\[\begin{split}\text{fSim}(\pi / 2, \, \pi / 6) = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & -i & 0 \\ 0 & -i & 0 & 0 \\ 0 & 0 & 0 & e^{-i \pi / 6} \\ \end{pmatrix} \, ,\end{split}\]where \(\text{fSim}\) is the
qibo.gates.fSimgate.- Parameters:
fSim with general rotation#
- class qibo.gates.GeneralizedfSim(q0, q1, unitary, phi, trainable=True)[source]#
The fSim gate with a general rotation.
Corresponds to the following unitary matrix
\[\begin{split}\begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & R_{00} & R_{01} & 0 \\ 0 & R_{10} & R_{11} & 0 \\ 0 & 0 & 0 & e^{-i \phi } \\ \end{pmatrix}\end{split}\]- Parameters:
q0 (int) – the first qubit to be swapped id number.
q1 (int) – the second qubit to be swapped id number.
unitary (np.ndarray) – Unitary that corresponds to the one-qubit rotation.
phi (float) – Angle for the
|11>phase.trainable (bool) – whether gate parameters can be updated using
qibo.models.circuit.Circuit.set_parameters(). Defaults toTrue.
Parametric XX interaction (RXX)#
- class qibo.gates.RXX(q0, q1, theta, trainable=True)[source]#
Parametric 2-qubit XX interaction, or rotation about XX-axis.
Corresponds to the following unitary matrix
\[\begin{split}\begin{pmatrix} \cos \frac{\theta }{2} & 0 & 0 & -i\sin \frac{\theta }{2} \\ 0 & \cos \frac{\theta }{2} & -i\sin \frac{\theta }{2} & 0 \\ 0 & -i\sin \frac{\theta }{2} & \cos \frac{\theta }{2} & 0 \\ -i\sin \frac{\theta }{2} & 0 & 0 & \cos \frac{\theta }{2} \\ \end{pmatrix}\end{split}\]- Parameters:
q0 (int) – the first entangled qubit id number.
q1 (int) – the second entangled qubit id number.
theta (float) – the rotation angle.
trainable (bool) – whether gate parameters can be updated using
qibo.models.circuit.Circuit.set_parameters(). Defaults toTrue.
Parametric YY interaction (RYY)#
- class qibo.gates.RYY(q0, q1, theta, trainable=True)[source]#
Parametric 2-qubit YY interaction, or rotation about YY-axis.
Corresponds to the following unitary matrix
\[\begin{split}\begin{pmatrix} \cos \frac{\theta }{2} & 0 & 0 & i\sin \frac{\theta }{2} \\ 0 & \cos \frac{\theta }{2} & -i\sin \frac{\theta }{2} & 0 \\ 0 & -i\sin \frac{\theta }{2} & \cos \frac{\theta }{2} & 0 \\ i\sin \frac{\theta }{2} & 0 & 0 & \cos \frac{\theta }{2} \\ \end{pmatrix}\end{split}\]- Parameters:
q0 (int) – the first entangled qubit id number.
q1 (int) – the second entangled qubit id number.
trainable (bool) – whether gate parameters can be updated using
qibo.models.circuit.Circuit.set_parameters(). Defaults toTrue.theta (float) –
Parametric ZZ interaction (RZZ)#
- class qibo.gates.RZZ(q0, q1, theta, trainable=True)[source]#
Parametric 2-qubit ZZ interaction, or rotation about ZZ-axis.
Corresponds to the following unitary matrix
\[\begin{split}\begin{pmatrix} e^{-i \theta / 2} & 0 & 0 & 0 \\ 0 & e^{i \theta / 2} & 0 & 0 \\ 0 & 0 & e^{i \theta / 2} & 0 \\ 0 & 0 & 0 & e^{-i \theta / 2} \\ \end{pmatrix}\end{split}\]- Parameters:
q0 (int) – the first entangled qubit id number.
q1 (int) – the second entangled qubit id number.
theta (float) – the rotation angle.
trainable (bool) – whether gate parameters can be updated using
qibo.models.circuit.Circuit.set_parameters(). Defaults toTrue.
Parametric ZX interaction (RZX)#
- class qibo.gates.RZX(q0, q1, theta, trainable=True)[source]#
Parametric 2-qubit ZX interaction, or rotation about ZX-axis.
Corresponds to the following unitary matrix
\[\begin{split}\begin{pmatrix} \text{RX}(\theta) & 0 \\ 0 & \text{RX}(-\theta) \\ \end{pmatrix} = \begin{pmatrix} \cos{\frac{\theta}{2}} & -i \sin{\frac{\theta}{2}} & 0 & 0 \\ -i \sin{\frac{\theta}{2}} & \cos{\frac{\theta}{2}} & 0 & 0 \\ 0 & 0 & \cos{\frac{\theta}{2}} & i \sin{\frac{\theta}{2}} \\ 0 & 0 & i \sin{\frac{\theta}{2}} & \cos{\frac{\theta}{2}} \\ \end{pmatrix} \, ,\end{split}\]where \(\text{RX}\) is the
qibo.gates.RXgate.- Parameters:
q0 (int) – the first entangled qubit id number.
q1 (int) – the second entangled qubit id number.
theta (float) – the rotation angle.
trainable (bool) – whether gate parameters can be updated using
qibo.models.circuit.Circuit.set_parameters(). Defaults toTrue.
Parametric XX-YY interaction (RXXYY)#
- class qibo.gates.RXXYY(q0, q1, theta, trainable=True)[source]#
Parametric 2-qubit \(XX + YY\) interaction, or rotation about \(XX + YY\)-axis.
Corresponds to the following unitary matrix
\[\begin{split}\exp\left(-i \frac{\theta}{4}(XX + YY)\right) = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & \cos{\frac{\theta}{2}} & -i \sin{\frac{\theta}{2}} & 0 \\ 0 & -i \sin{\frac{\theta}{2}} & \cos{\frac{\theta}{2}} & 0 \\ 0 & 0 & 0 & 1 \\ \end{pmatrix} \, ,\end{split}\]- Parameters:
q0 (int) – the first entangled qubit id number.
q1 (int) – the second entangled qubit id number.
theta (float) – the rotation angle.
trainable (bool) – whether gate parameters can be updated using
qibo.models.circuit.Circuit.set_parameters(). Defaults toTrue.
- property hamming_weight: bool#
Return boolean value representing if a Gate is Hamming-weight-preserving or not.
Givens gate#
- class qibo.gates.GIVENS(q0, q1, theta, trainable=True)[source]#
The Givens gate.
Corresponds to the following unitary matrix
\[\begin{split}\exp(i \, \frac{\theta}{2} \, (X \otimes Y - Y \otimes X)) = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & \cos(\theta) & -\sin(\theta) & 0 \\ 0 & \sin(\theta) & \cos(\theta) & 0 \\ 0 & 0 & 0 & 1 \\ \end{pmatrix}\end{split}\]- Parameters:
q0 (int) – the first qubit id number.
q1 (int) – the second qubit id number.
theta (float) – the rotation angle.
trainable (bool) – whether gate parameters can be updated using
qibo.models.circuit.Circuit.set_parameters(). Defaults toTrue.
- property hamming_weight: bool#
Return boolean value representing if a Gate is Hamming-weight-preserving or not.
- decompose(*free, method='standard', **kwargs)[source]#
Decomposition of GIVENS gate according to the decomposition of the RBS gate in Ref. [1].
References
1. R. M. S. Farias, T. O. Maciel, G. Camilo, R. Lin, S. Ramos-Calderer, and L. Aolita, Quantum encoder for fixed-Hamming-weight subspaces, Phys. Rev. Applied 23, 044014 (2025).
Reconfigurable Beam Splitter gate (RBS)#
- class qibo.gates.RBS(q0, q1, theta, trainable=True)[source]#
The Reconfigurable Beam Splitter gate.
Corresponds to the following unitary matrix
\[\begin{split}\exp(-i \, \frac{\theta}{2} \, (X \otimes Y - Y \otimes X)) = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & \cos(\theta) & \sin(\theta) & 0 \\ 0 & -\sin(\theta) & \cos(\theta) & 0 \\ 0 & 0 & 0 & 1 \\ \end{pmatrix}\end{split}\]Note that, in our implementation, \(\text{RBS}(\theta) = \text{Givens}(-\theta)\), where \(\text{Givens}\) is the
qibo.gates.GIVENSgate. However, we point out that this definition is not unique.- Parameters:
q0 (int) – the first qubit id number.
q1 (int) – the second qubit id number.
theta (float) – the rotation angle.
trainable (bool) – whether gate parameters can be updated using
qibo.models.circuit.Circuit.set_parameters(). Defaults toTrue.
Echo Cross-Resonance gate (ECR)#
- class qibo.gates.ECR(q0, q1)[source]#
THe Echo Cross-Resonance gate.
Corresponds to the following matrix
\[\begin{split}\frac{1}{\sqrt{2}} \left( X \, I - Y \, X \right) = \frac{1}{\sqrt{2}} \, \begin{pmatrix} 0 & 0 & 1 & i \\ 0 & 0 & i & 1 \\ 1 & -i & 0 & 0 \\ -i & 1 & 0 & 0 \\ \end{pmatrix}\end{split}\]- decompose(*free, method='standard', **kwargs)[source]#
Decomposition of \(\textup{ECR}\) gate.
The decomposition is exact, including the global phase. More precisely,
\[\textup{ECR} = X(q_{0}) \, \textup{CNOT}(q_{0}, q_{1}) \, R_{X}(q_{1}, \pi / 2) \, S(q_{0}) \, ,\]where \(R_{X}(q_{1}, \pi / 2) = e^{-i \pi / 4} \sqrt{X}(q_{1})\). If
method = "clifford_plus_t", \(R_{X}(q_{1}, \pi / 2)\) is the product \(Z \, Y \, S \, H \, S\) of Clifford gates.
Special gates#
Toffoli#
- class qibo.gates.TOFFOLI(q0, q1, q2)[source]#
The Toffoli gate.
Corresponds to the following unitary matrix
\[\begin{split}\begin{pmatrix} 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \\ \end{pmatrix}\end{split}\]- Parameters:
- property qasm_label: str#
String corresponding to OpenQASM operation of the gate. For more exotic gates, both the internal qibo name of the gate and the custom gate QASM definition are returned as a tuple for broader compatibility.
- congruent()[source]#
Congruent representation of
TOFFOLIgate.The congruent representation is based on Sec. 6.2 of arXiv:9503016. The sequence of the gates produced here has the same effect as
TOFFOLIwith the phase of the|101>state reversed.- Returns:
List with
RYandCNOTgates that have the same effect as applying the originalTOFFOLIgate.- Return type:
list[qibo.gates.abstract.Gate]
CCZ#
- class qibo.gates.CCZ(q0, q1, q2)[source]#
The controlled-CZ gate.
Corresponds to the following unitary matrix
\[\begin{split}\begin{pmatrix} 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & -1 \\ \end{pmatrix}\end{split}\]- Parameters:
Deutsch#
- class qibo.gates.DEUTSCH(q0, q1, q2, theta, trainable=True)[source]#
The Deutsch gate.
Corresponds to the following unitary matrix
\[\begin{split}\begin{pmatrix} 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & i \cos{\theta} & \sin{\theta} \\ 0 & 0 & 0 & 0 & 0 & 0 & \sin{\theta} & i \cos{\theta} \\ \end{pmatrix}\end{split}\]- Parameters:
Fan-out#
- class qibo.gates.FanOut(*q)[source]#
The \(\textrm{FanOut}\) gate.
Given a register containing \(k \geq 2\) qubits, this gate is equivalent to \(k - 1\)
qibo.gates.CNOTgates in which the control qubit for all CNOTs is the first qubit in the register while the each CNOT has each of the remaining qubits in the register as its target qubit.- Parameters:
*q (int) – Ids of the qubits which the gate acts on. The first id is considered to be the control qubit.
Generalized Reconfigurable Beam Splitter (RBS)#
- class qibo.gates.GeneralizedRBS(qubits_in, qubits_out, theta, phi=0.0, trainable=True)[source]#
The generalized (complex) Reconfigurable Beam Splitter gate (\(\text{gRBS}\)).
Given a register called
qubits_incontaining \(m\) qubits and a register namedqubits_outcontaining \(m'\) qubits, the \(\text{gRBS}\) is a \((m + m')\)-qubit gate that has the following matrix representation:\[\begin{split}\begin{pmatrix} I & & & & \\ & e^{-i\phi}\cos\theta & & e^{-i\phi}\sin\theta & \\ & & I' & & \\ & -e^{i\phi}\sin\theta & & e^{i\phi}\cos\theta & \\ & & & & I\\ \end{pmatrix} \,\, ,\end{split}\]where \(I\) and \(I'\) are, respectively, identity matrices of size \(2^{m} - 1\) and \(2^{m}(2^{m'} - 2)\).
This unitary matrix is also known as a Givens rotation.
References
1. R. M. S. Farias, T. O. Maciel, G. Camilo, R. Lin, S. Ramos-Calderer, and L. Aolita, Quantum encoder for fixed-Hamming-weight subspaces, Phys. Rev. Applied 23, 044014 (2025)
- Parameters:
theta (float) – the rotation angle.
phi (float) – the phase angle. Defaults to \(0.0\).
trainable (bool) – whether gate parameter can be updated using
qibo.models.circuit.Circuit.set_parameters(). Defaults toTrue.
- property hamming_weight: bool#
Return boolean value representing if a Gate is Hamming-weight-preserving or not.
- on_qubits(qubit_map)[source]#
Creates the same gate targeting different qubits.
- Parameters:
qubit_map (int) – Dictionary mapping original qubit indices to new ones.
- Returns:
A
qibo.gates.Gateobject of the original gate type targeting the given qubits.- Return type:
Gate
Example
from qibo import Circuit, gates circuit = Circuit(4) # Add some CNOT gates # equivalent to gates.CNOT(2, 3) circuit.add(gates.CNOT(2, 3).on_qubits({2: 2, 3: 3})) # equivalent to gates.CNOT(3, 0) circuit.add(gates.CNOT(2, 3).on_qubits({2: 3, 3: 0})) # equivalent to gates.CNOT(1, 3) circuit.add(gates.CNOT(2, 3).on_qubits({2: 1, 3: 3})) # equivalent to gates.CNOT(2, 1) circuit.add(gates.CNOT(2, 3).on_qubits({2: 2, 3: 1})) circuit.draw()
0: ───X───── 1: ───|─o─X─ 2: ─o─|─|─o─ 3: ─X─o─X───
Multiplexor#
- class qibo.gates.Multiplexor(controls, target, angles, axis='y', trainable=True)[source]#
Multiplexed (uniformly controlled) single-qubit rotation.
Given \(k\) control qubits and one target qubit, this gate rotates the target qubit around the
axisof the Bloch sphere by an angle \(\theta_{j}\) that depends on the computational basis state \(|j\rangle\) of the control qubits, with \(0 \leq j < 2^{k}\). Its matrix representation is block diagonal,\[\sum_{j=0}^{2^{k} - 1} |j\rangle\langle j| \otimes R_{a}(\theta_{j}) \, ,\]where \(R_{a}\) is
qibo.gates.RYorqibo.gates.RZ. The first control qubit is the most significant bit of \(j\).When decomposed, this gate uses \(2^{k}\) rotations and \(2^{k}\)
qibo.gates.CNOTgates arranged in a Gray-code order.References
1. M. Möttönen, J. J. Vartiainen, V. Bergholm, and M. M. Salomaa, Quantum circuits for general multiqubit gates, Phys. Rev. Lett. 93, 130502 (2004).
2. V. V. Shende, S. S. Bullock, and I. L. Markov, Synthesis of quantum-logic circuits, IEEE Trans. Comput.-Aided Des. Integr. Circuits Syst. 25, 1000 (2006).
- Parameters:
target (int) – id of the target qubit.
angles (ArrayLike) – the \(2^{k}\) rotation angles.
axis (str, optional) – rotation axis, either
"y"or"z". Defaults to"y".trainable (bool, optional) – whether gate parameters can be updated using
qibo.models.circuit.Circuit.set_parameters(). Defaults toTrue.
- on_qubits(qubit_map)[source]#
Creates the same gate targeting different qubits.
- Parameters:
qubit_map (int) – Dictionary mapping original qubit indices to new ones.
- Returns:
A
qibo.gates.Gateobject of the original gate type targeting the given qubits.- Return type:
Gate
Example
from qibo import Circuit, gates circuit = Circuit(4) # Add some CNOT gates # equivalent to gates.CNOT(2, 3) circuit.add(gates.CNOT(2, 3).on_qubits({2: 2, 3: 3})) # equivalent to gates.CNOT(3, 0) circuit.add(gates.CNOT(2, 3).on_qubits({2: 3, 3: 0})) # equivalent to gates.CNOT(1, 3) circuit.add(gates.CNOT(2, 3).on_qubits({2: 1, 3: 3})) # equivalent to gates.CNOT(2, 1) circuit.add(gates.CNOT(2, 3).on_qubits({2: 2, 3: 1})) circuit.draw()
0: ───X───── 1: ───|─o─X─ 2: ─o─|─|─o─ 3: ─X─o─X───
Arbitrary unitary#
- class qibo.gates.Unitary(unitary, *q, trainable=True, name=None, draw_label=None, check_unitary=True)[source]#
Arbitrary unitary gate.
- Parameters:
unitary (_Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]) – Unitary matrix as a tensor supported by the backend.
*q (int) – Qubit id numbers that the gate acts on.
trainable (bool, optional) – whether gate parameters can be updated using
qibo.models.circuit.Circuit.set_parameters(). Defaults toTrue.name (str, optional) – name for the gate.
draw_label (str, optional) – label for drawing the gate in a circuit with
qibo.models.Circuit.draw().check_unitary (bool, optional) – if
True, checks ifunitaryis an unitary operator. IfFalse, check is not performed andunitaryattribute defaults toFalse. Note that, even when the check is performed, there is no enforcement. This allows the user to create non-unitary gates. Default isTrue.
- property parameters: tuple[float, ...]#
Returns a tuple containing the current value of gate’s parameters.
- property hamming_weight: bool#
Return boolean value representing if a Gate is Hamming-weight-preserving or not.
- on_qubits(qubit_map)[source]#
Creates the same gate targeting different qubits.
- Parameters:
qubit_map (int) – Dictionary mapping original qubit indices to new ones.
- Returns:
A
qibo.gates.Gateobject of the original gate type targeting the given qubits.- Return type:
Gate
Example
from qibo import Circuit, gates circuit = Circuit(4) # Add some CNOT gates # equivalent to gates.CNOT(2, 3) circuit.add(gates.CNOT(2, 3).on_qubits({2: 2, 3: 3})) # equivalent to gates.CNOT(3, 0) circuit.add(gates.CNOT(2, 3).on_qubits({2: 3, 3: 0})) # equivalent to gates.CNOT(1, 3) circuit.add(gates.CNOT(2, 3).on_qubits({2: 1, 3: 3})) # equivalent to gates.CNOT(2, 1) circuit.add(gates.CNOT(2, 3).on_qubits({2: 2, 3: 1})) circuit.draw()
0: ───X───── 1: ───|─o─X─ 2: ─o─|─|─o─ 3: ─X─o─X───
Barrier#
- class qibo.gates.Barrier(*q)[source]#
Directive that separates the operations before and after it on the given qubits.
It does not change the quantum state (its action is the identity), so it never affects simulation results. Its only purpose is to mark a boundary that circuit-manipulation tools must respect:
qibo.models.circuit.Circuit.fuse()does not fuse gates across it;qibo.gates.abstract.Gate.commutes()always returnsFalse, so commutation-based rewriting never moves gates through it;it is drawn by
qibo.models.circuit.Circuit.draw()and exported to OpenQASM (Open Quantum Assembly Language) asbarrier q[i], q[j];.
Note
Like every
qibo.gates.abstract.SpecialGate, a barrier is treated as acting on all qubits of the circuit byqibo.models.circuit.Circuit.fuse(), even when it lists only a subset of them. This is conservative: it can only prevent fusions, never allow a wrong one.- Parameters:
q (int, ...) – Indices of the qubits the barrier acts on.
Example
from qibo import Circuit, gates from qibo.gates.special import Barrier circuit = Circuit(3) circuit.add(gates.H(0)) circuit.add(gates.H(1)) circuit.add(Barrier(0, 1)) circuit.add(gates.CNOT(0, 1)) circuit.add(Barrier(*range(circuit.nqubits))) circuit.draw()
0: ─H─░─o─░─ 1: ─H─░─X─░─ 2: ───────░─
- apply(backend, state, nqubits)[source]#
Return
stateunchanged, since a barrier does not act on the state.
- apply_clifford(backend, state, nqubits)[source]#
Return
stateunchanged, since a barrier does not act on the state.
Callback gate#
- class qibo.gates.CallbackGate(callback)[source]#
Calculates a
qibo.callbacks.Callbackat a specific point in the circuit.This gate performs the callback calulation without affecting the state vector.
- Parameters:
callback (
qibo.callbacks.Callback) – Callback object to calculate.
Fusion gate#
- class qibo.gates.FusedGate(*q)[source]#
Collection of gates that will be fused and applied as single gate during simulation. This gate is constructed automatically by
qibo.models.circuit.Circuit.fuse()and should not be used by user.- matrix(backend=None)[source]#
Returns matrix representation of special gate.
- Parameters:
backend (
qibo.backends.abstract.Backend, optional) – backend to be used in the execution. IfNone, it uses the current backend. Defaults toNone.- Returns:
Matrix representation of special gate.
- Return type:
ndarray
IONQ Native gates#
GPI#
- class qibo.gates.GPI(q, phi, trainable=True)[source]#
The GPI gate.
Corresponds to the following unitary matrix
\[\begin{split}\begin{pmatrix} 0 & e^{- i \phi} \\ e^{i \phi} & 0 \\ \end{pmatrix}\end{split}\]- Parameters:
q (int) – the qubit id number.
phi (float) – phase.
trainable (bool) – whether gate parameters can be updated using
qibo.models.circuit.Circuit.set_parameters(). Defaults toTrue.
GPI2#
- class qibo.gates.GPI2(q, phi, trainable=True)[source]#
The GPI2 gate.
Corresponds to the following unitary matrix
\[\begin{split}\frac{1}{\sqrt{2}} \, \begin{pmatrix} 1 & -i e^{- i \phi} \\ -i e^{i \phi} & 1 \\ \end{pmatrix}\end{split}\]- Parameters:
q (int) – the qubit id number.
phi (float) – phase.
trainable (bool) – whether gate parameters can be updated using
qibo.models.circuit.Circuit.set_parameters(). Defaults toTrue.
Mølmer–Sørensen (MS)#
- class qibo.gates.MS(q0, q1, phi0, phi1, theta=1.5707963267948966, trainable=True)[source]#
The Mølmer–Sørensen (MS) gate is a two-qubit gate native to trapped ions.
Corresponds to the following unitary matrix
\[\begin{split}\begin{pmatrix} \cos(\theta / 2) & 0 & 0 & -i e^{-i( \phi_0 + \phi_1)} \sin(\theta / 2) \\ 0 & \cos(\theta / 2) & -i e^{-i( \phi_0 - \phi_1)} \sin(\theta / 2) & 0 \\ 0 & -i e^{i( \phi_0 - \phi_1)} \sin(\theta / 2) & \cos(\theta / 2) & 0 \\ -i e^{i( \phi_0 + \phi_1)} \sin(\theta / 2) & 0 & 0 & \cos(\theta / 2) \\ \end{pmatrix}\end{split}\]- Parameters:
q0 (int) – the first qubit to be swapped id number.
q1 (int) – the second qubit to be swapped id number.
phi0 (float) – first qubit’s phase.
phi1 (float) – second qubit’s phase
theta (float, optional) – arbitrary angle in the interval \(0 \leq \theta \leq \pi /2\). If \(\theta \rightarrow \pi / 2\), the fully-entangling MS gate is defined. Defaults to \(\pi / 2\).
trainable (bool) – whether gate parameters can be updated using
qibo.models.circuit.Circuit.set_parameters(). Defaults toTrue.
Quantinuum native gates#
U1q#
- class qibo.gates.U1q(q, theta, phi, trainable=True)[source]#
Native single-qubit gate in the Quantinuum platform.
Corresponds to the following unitary matrix:
\[\begin{split}\begin{pmatrix} \cos\left(\frac{\theta}{2}\right) & -i \, e^{-i \, \phi} \, \sin\left(\frac{\theta}{2}\right) \\ -i \, e^{i \, \phi} \, \sin\left(\frac{\theta}{2}\right) & \cos\left(\frac{\theta}{2}\right) \\ \end{pmatrix}\end{split}\]Note that \(U_{1q}(\theta, \phi) = U_{3}(\theta, \phi - \frac{\pi}{2}, \frac{\pi}{2} - \phi)\), where \(U_{3}\) is
qibo.gates.U3.- Parameters:
q (int) – the qubit id number.
theta (float) – first rotation angle.
phi (float) – second rotation angle.
trainable (bool) – whether gate parameters can be updated using
qibo.models.circuit.Circuit.set_parameters(). Defaults toTrue.
Note
The other Quantinuum single-qubit and two-qubit native gates are implemented in Qibo as:
Pauli-\(Z\) rotation:
qibo.gates.RZArbitrary \(ZZ\) rotation:
qibo.gates.RZZFully-entangling \(ZZ\)-interaction: \(R_{ZZ}(\pi/2)\)
IQM native gates#
Phase-\(RX\)#
- class qibo.gates.PRX(q, theta, phi, trainable=True)[source]#
Phase \(RX\) gate.
Corresponds to the following unitary matrix
\[\begin{split}\begin{pmatrix} \cos{(\theta / 2)} & -i e^{-i \phi} \sin{(\theta / 2)} \\ -i e^{i \phi} \sin{(\theta / 2)} & \cos{(\theta / 2)} \end{pmatrix}\end{split}\]- Parameters:
q (int) – the qubit id number.
theta (float) – the first angle corresponding to a rotation angle.
phi (float) – the second angle correspoding to a phase angle.
trainable (bool) – whether gate parameters can be updated using
qibo.models.circuit.Circuit.set_parameters(). Defaults toTrue.
Note
The other IQM two-qubit native gate is implemented in Qibo as:
Controlled-\(Z\) rotation:
qibo.gates.CZ