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 to gates.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:

str

draw_label

Optional label for drawing the gate in a circuit with qibo.models.Circuit.draw().

Type:

str

is_controlled_by

True if the gate was created using the qibo.gates.abstract.Gate.controlled_by() method, otherwise False.

Type:

bool

init_args

Arguments used to initialize the gate.

Type:

list

init_kwargs

Arguments used to initialize the gate.

Type:

dict

target_qubits

Tuple with ids of target qubits.

Type:

tuple

control_qubits

Tuple with ids of control qubits sorted in increasing order.

Type:

tuple

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:

True if the gates commute, False otherwise.

Return type:

bool

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.RX gate will change the sign of its parameter at the time of application. However, if the parameter is updated after that, for example using qibo.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 if method="standard"). Without free qubits no (dirty) auxiliary qubits are used. The decomposition is applied recursively, so that the returned gates are controlled by at most one qubit.

Parameters:
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:

float

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(), then qibo.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. If None, it uses the current backend. Defaults to None.

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.Gate object 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 in init_kwargs. The values are restricted to JSON-compatible scalar types. Measurement gates additionally carry a measurement_result entry. The dictionary can be loaded back into a gate with qibo.gates.Gate.from_dict().

Returns:

JSON-compatible representation of the gate.

Return type:

dict

property target_qubits: tuple[int, ...]

Tuple with ids of target qubits.

to_json()[source]

Dump gate to JSON.

Note

Consider using raw() directly.

Single qubit gates#

Hadamard (H)#

class qibo.gates.H(q)[source]#

The Hadamard gate.

Corresponds to the following unitary matrix

\[\begin{split}\frac{1}{\sqrt{2}} \, \begin{pmatrix} 1 & 1 \\ 1 & -1 \\ \end{pmatrix}\end{split}\]
Parameters:

q (int) – the qubit id number.

property clifford: bool#

Return boolean value representing if a Gate is Clifford or not.

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.

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 clifford: bool#

Return boolean value representing if a Gate is Clifford or not.

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 if method="standard"). Without free qubits no (dirty) auxiliary qubits are used. The decomposition is applied recursively, so that the returned gates are controlled by at most one qubit.

Parameters:
Returns:

Gates that have the same effect as applying the original gate.

Return type:

List[qibo.gates.abstract.Gate]

basis_rotation()[source]#

Transformation required to rotate the basis for measuring the gate.

Return type:

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.

property clifford: bool#

Return boolean value representing if a Gate is Clifford or not.

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.

basis_rotation()[source]#

Transformation required to rotate the basis for measuring the gate.

Return type:

Gate

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 clifford: bool#

Return boolean value representing if a Gate is Clifford or not.

property hamming_weight: bool#

Return boolean value representing if a Gate is Hamming-weight-preserving or not.

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.

basis_rotation()[source]#

Transformation required to rotate the basis for measuring the gate.

Return type:

None

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 clifford: bool#

Return boolean value representing if a Gate is Clifford or not.

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.RX gate. More precisely, \(\sqrt{X} = e^{i \pi / 4} \, \text{RX}(\pi / 2)\).

Parameters:
  • free (int) –

  • method (str) –

Return type:

list[qibo.gates.abstract.Gate]

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.

property clifford: bool#

Return boolean value representing if a Gate is Clifford or not.

property hamming_weight: bool#

Return boolean value representing if a Gate is Hamming-weight-preserving or not.

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.

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.

property hamming_weight: bool#

Return boolean value representing if a Gate is Hamming-weight-preserving or not.

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.

Identity (I)#

class qibo.gates.I(*q)[source]#

The identity gate.

Parameters:

*q (int) – the qubit id numbers.

property clifford: bool#

Return boolean value representing if a Gate is Clifford or not.

property hamming_weight: bool#

Return boolean value representing if a Gate is Hamming-weight-preserving or not.

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.

Align (A)#

class qibo.gates.Align(q, delay=0, trainable=False)[source]#

Aligns proceeding qubit operations and (optionally) waits delay amount of time.

Note

For this gate, the trainable parameter is by default set to False.

Parameters:
  • q (int) – The qubit ID.

  • delay (int, optional) – The time (in ns) for which to delay circuit execution on the specified qubits. Defaults to \(0\).

  • trainable (bool) –

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 example gates.M(*[0, 1, 4]) or gates.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 True the collapsed state vector is returned. If False the measurement result is returned.

  • basis (qibo.gates.Gate or 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 to qibo.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 p0 but controls the 1->0 bitflip probability. If p1 is None then p0 will 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 a ValueError is 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(), then qibo.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. If None, it uses the current backend. Defaults to None.

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:
property hamming_weight: bool#

Return boolean value representing if a Gate is Hamming-weight-preserving or not.

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.

generator(backend=None)[source]#

This function returns the gate’s generator.

Returns:

generator.

Return type:

ArrayLike

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:
property hamming_weight: bool#

Return boolean value representing if a Gate is Hamming-weight-preserving or not.

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.

generator(backend=None)[source]#

This function returns the gate’s generator.

Returns:

generator.

Return type:

ArrayLike

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:
property hamming_weight: bool#

Return boolean value representing if a Gate is Hamming-weight-preserving or not.

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.

generator(backend=None)[source]#

This function returns the gate’s generator.

Returns:

generator.

Return type:

ArrayLike

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:
property hamming_weight: bool#

Return boolean value representing if a Gate is Hamming-weight-preserving or not.

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.

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:
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.

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.RZ and :class`qibo.gates.SX`.

Parameters:
  • free (int) –

  • method (str) –

Return type:

list[qibo.gates.abstract.Gate]

property hamming_weight: bool#

Return boolean value representing if a Gate is Hamming-weight-preserving or not.

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.

Two qubit gates#

Controlled-NOT (CNOT)#

class qibo.gates.CNOT(q0, q1)[source]#

The Controlled-NOT gate.

Corresponds to the following unitary matrix

\[\begin{split}\begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \\ \end{pmatrix}\end{split}\]
Parameters:
  • q0 (int) – the control qubit id number.

  • q1 (int) – the target qubit id number.

property clifford: bool#

Return boolean value representing if a Gate is Clifford or not.

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.

Controlled-Y (CY)#

class qibo.gates.CY(q0, q1)[source]#

The Controlled-\(Y\) gate.

Corresponds to the following unitary matrix

\[\begin{split}\begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & -i \\ 0 & 0 & i & 0 \\ \end{pmatrix}\end{split}\]
Parameters:
  • q0 (int) – the control qubit id number.

  • q1 (int) – the target qubit id number.

property clifford: bool#

Return boolean value representing if a Gate is Clifford or not.

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.

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}\]
Parameters:
  • q0 (int) – the control qubit id number.

  • q1 (int) – the target qubit id number.

property clifford: bool#

Return boolean value representing if a Gate is Clifford or not.

property hamming_weight: bool#

Return boolean value representing if a Gate is Hamming-weight-preserving or not.

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.

Controlled-Hadamard (CH)#

class qibo.gates.CH(q0, q1)[source]#
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.

Controlled-Square Root of X (CSX)#

class qibo.gates.CSX(q0, q1)[source]#

The Controlled-\(\sqrt{X}\) gate.

Corresponds to the following unitary matrix

\[\begin{split}\begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & e^{i\pi/4} & e^{-i\pi/4} \\ 0 & 0 & e^{-i\pi/4} & e^{i\pi/4} \\ \end{pmatrix}\end{split}\]
Parameters:
  • q0 (int) – the control qubit id number.

  • q1 (int) – the target 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.

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:
property hamming_weight: bool#

Return boolean value representing if a Gate is Hamming-weight-preserving or not.

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.

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.RZ gate.

Parameters:
property hamming_weight: bool#

Return boolean value representing if a Gate is Hamming-weight-preserving or not.

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.

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:
property hamming_weight: bool#

Return boolean value representing if a Gate is Hamming-weight-preserving or not.

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.

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.CRZ gate.

Parameters:
property hamming_weight: bool#

Return boolean value representing if a Gate is Hamming-weight-preserving or not.

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.

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:
property hamming_weight: bool#

Return boolean value representing if a Gate is Hamming-weight-preserving or not.

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.

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:
  • q0 (int) – the first qubit to be swapped id number.

  • q1 (int) – the second qubit to be swapped id number.

property clifford: bool#

Return boolean value representing if a Gate is Clifford or not.

property hamming_weight: bool#

Return boolean value representing if a Gate is Hamming-weight-preserving or not.

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.

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:
  • q0 (int) – the first qubit to be swapped id number.

  • q1 (int) – the second qubit to be swapped id number.

property clifford: bool#

Return boolean value representing if a Gate is Clifford or not.

property hamming_weight: bool#

Return boolean value representing if a Gate is Hamming-weight-preserving or not.

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.

Square root of iSwap (SiSWAP)#

class qibo.gates.SiSWAP(q0, q1)[source]#

The \(\sqrt{\text{iSWAP}}\) gate.

Corresponds to the following unitary matrix

\[\begin{split}\begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1/\sqrt{2} & i/\sqrt{2} & 0 \\ 0 & i/\sqrt{2} & 1/\sqrt{2} & 0 \\ 0 & 0 & 0 & 1 \\ \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.

property hamming_weight: bool#

Return boolean value representing if a Gate is Hamming-weight-preserving or not.

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:
  • q0 (int) – the first qubit to be f-swapped id number.

  • q1 (int) – the second qubit to be f-swapped id number.

property clifford: bool#

Return boolean value representing if a Gate is Clifford or not.

property hamming_weight: bool#

Return boolean value representing if a Gate is Hamming-weight-preserving or not.

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.

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 to True.

property hamming_weight: bool#

Return boolean value representing if a Gate is Hamming-weight-preserving or not.

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.fSim gate.

Parameters:
  • q0 (int) – the first qubit to be swapped id number.

  • q1 (int) – the second qubit to be swapped id number.

property hamming_weight: bool#

Return boolean value representing if a Gate is Hamming-weight-preserving or not.

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 to True.

property hamming_weight: bool#

Return boolean value representing if a Gate is Hamming-weight-preserving or not.

property parameters: tuple[float, ...]#

Returns a tuple containing the current value of gate’s parameters.

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:
property hamming_weight: bool#

Return boolean value representing if a Gate is Hamming-weight-preserving or not.

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.

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:
property hamming_weight: bool#

Return boolean value representing if a Gate is Hamming-weight-preserving or not.

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.

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:
property hamming_weight: bool#

Return boolean value representing if a Gate is Hamming-weight-preserving or not.

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.

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.RX gate.

Parameters:
property hamming_weight: bool#

Return boolean value representing if a Gate is Hamming-weight-preserving or not.

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:
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 \(\text{R_{XX-YY}}\) up to global phase.

This decomposition has a global phase difference with respect to the original gate due to a phase difference in \(\left(\sqrt{X}\right)^{\dagger}\).

Parameters:

method (str) –

Return type:

list[qibo.gates.abstract.Gate]

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:
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).

Parameters:
  • free (int) –

  • method (str) –

Return type:

list[qibo.gates.abstract.Gate]

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.GIVENS gate. However, we point out that this definition is not unique.

Parameters:
property hamming_weight: bool#

Return boolean value representing if a Gate is Hamming-weight-preserving or not.

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}\]
Parameters:
  • q0 (int) – the first qubit id number.

  • q1 (int) – the second qubit id number.

property clifford: bool#

Return boolean value representing if a Gate is Clifford or not.

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.

Parameters:
  • free (int) –

  • method (str) –

Return type:

list[qibo.gates.abstract.Gate]

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:
  • q0 (int) – the first control qubit id number.

  • q1 (int) – the second control qubit id number.

  • q2 (int) – the target 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.

congruent()[source]#

Congruent representation of TOFFOLI gate.

The congruent representation is based on Sec. 6.2 of arXiv:9503016. The sequence of the gates produced here has the same effect as TOFFOLI with the phase of the |101> state reversed.

Returns:

List with RY and CNOT gates that have the same effect as applying the original TOFFOLI gate.

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:
  • q0 (int) – the first control qubit id number.

  • q1 (int) – the second control qubit id number.

  • q2 (int) – the target qubit id number.

property hamming_weight: bool#

Return boolean value representing if a Gate is Hamming-weight-preserving or not.

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.

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:
  • q0 (int) – the first control qubit id number.

  • q1 (int) – the second control qubit id number.

  • q2 (int) – the target qubit id number.

  • theta (float) –

  • trainable (bool) –

property hamming_weight: bool#

Return boolean value representing if a Gate is Hamming-weight-preserving or not.

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.CNOT gates 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.

property clifford: bool#

Return boolean value representing if a Gate is Clifford or not.

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_in containing \(m\) qubits and a register named qubits_out containing \(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:
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.Gate object 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 axis of 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.RY or qibo.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.CNOT gates 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:
  • controls (tuple or list) – ids of the control qubits.

  • 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 to True.

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.Gate object 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 to True.

  • 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 if unitary is an unitary operator. If False, check is not performed and unitary attribute defaults to False. Note that, even when the check is performed, there is no enforcement. This allows the user to create non-unitary gates. Default is True.

property parameters: tuple[float, ...]#

Returns a tuple containing the current value of gate’s parameters.

property clifford: bool#

Return boolean value representing if a Gate is Clifford or not.

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.Gate object 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:

Note

Like every qibo.gates.abstract.SpecialGate, a barrier is treated as acting on all qubits of the circuit by qibo.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: ───────░─
property clifford: bool#

Barriers are the identity operation, hence trivially Clifford.

property qasm_label: str#

OpenQASM (Open Quantum Assembly Language) keyword for the barrier.

apply(backend, state, nqubits)[source]#

Return state unchanged, since a barrier does not act on the state.

apply_clifford(backend, state, nqubits)[source]#

Return state unchanged, since a barrier does not act on the state.

on_qubits(qubit_map)[source]#

Creates the same barrier acting on different qubits.

Parameters:

qubit_map (dict) – Dictionary mapping original qubit indices to new ones. Every qubit of the barrier must appear as a key.

Returns:

Barrier acting on the mapped qubits.

Return type:

qibo.gates.Barrier

Callback gate#

class qibo.gates.CallbackGate(callback)[source]#

Calculates a qibo.callbacks.Callback at 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.

can_fuse(gate, max_qubits)[source]#

Check if two gates can be fused.

matrix(backend=None)[source]#

Returns matrix representation of special gate.

Parameters:

backend (qibo.backends.abstract.Backend, optional) – backend to be used in the execution. If None, it uses the current backend. Defaults to None.

Returns:

Matrix representation of special gate.

Return type:

ndarray

fuse(gate)[source]#

Fuses two gates.

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:
property qasm_label: 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.

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:
property qasm_label: 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 clifford: bool#

Return boolean value representing if a Gate is Clifford or not.

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 to True.

property hamming_weight: bool#

Return boolean value representing if a Gate is Hamming-weight-preserving or not.

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.

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:
property hamming_weight: bool#

Return boolean value representing if a Gate is Hamming-weight-preserving or not.

Note

The other Quantinuum single-qubit and two-qubit native gates are implemented in Qibo as:

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:
property hamming_weight: bool#

Return boolean value representing if a Gate is Hamming-weight-preserving or not.

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.

Note

The other IQM two-qubit native gate is implemented in Qibo as: