Circuit Library
qiskit.circuit.library
The circuit library is a collection of well-studied and valuable circuits, directives, and gates. We call them valuable for different reasons, for instance they can serve as building blocks for algorithms or they are circuits that we think are hard to simulate classically.
Each element can be plugged into a circuit using the QuantumCircuit.append()
method and so the circuit library allows users to program at higher levels of abstraction. For example, to append a multi-controlled CNOT:
from qiskit.circuit.library import MCXGate
gate = MCXGate(4)
from qiskit import QuantumCircuit
circuit = QuantumCircuit(5)
circuit.append(gate, [0, 1, 4, 2, 3])
circuit.draw('mpl')
The library is organized in several sections.
Standard gates
These operations are reversible unitary gates and they all subclass Gate
. As a consequence, they all have the methods to_matrix()
, power()
, and control()
, which we can generally only apply to unitary operations.
For example:
from qiskit.circuit.library import XGate
gate = XGate()
print(gate.to_matrix()) # X gate
print(gate.power(1/2).to_matrix()) # √X gate
print(gate.control(1).to_matrix()) # CX (controlled X) gate
[[0.+0.j 1.+0.j]
[1.+0.j 0.+0.j]]
[[0.5+0.5j 0.5-0.5j]
[0.5-0.5j 0.5+0.5j]]
[[1.+0.j 0.+0.j 0.+0.j 0.+0.j]
[0.+0.j 0.+0.j 0.+0.j 1.+0.j]
[0.+0.j 0.+0.j 1.+0.j 0.+0.j]
[0.+0.j 1.+0.j 0.+0.j 0.+0.j]]
C3XGate (*args[, _force_mutable]) | The X gate controlled on 3 qubits. |
C3SXGate (*args[, _force_mutable]) | The 3-qubit controlled sqrt-X gate. |
C4XGate (*args[, _force_mutable]) | The 4-qubit controlled X gate. |
CCXGate (*args[, _force_mutable]) | CCX gate, also known as Toffoli gate. |
DCXGate (*args[, _force_mutable]) | Double-CNOT gate. |
CHGate (*args[, _force_mutable]) | Controlled-Hadamard gate. |
CPhaseGate (theta[, label, ctrl_state, ...]) | Controlled-Phase gate. |
CRXGate (theta[, label, ctrl_state, ...]) | Controlled-RX gate. |
CRYGate (theta[, label, ctrl_state, ...]) | Controlled-RY gate. |
CRZGate (theta[, label, ctrl_state, ...]) | Controlled-RZ gate. |
CSGate (*args[, _force_mutable]) | Controlled-S gate. |
CSdgGate (*args[, _force_mutable]) | Controlled-S^dagger gate. |
CSwapGate (*args[, _force_mutable]) | Controlled-SWAP gate, also known as the Fredkin gate. |
CSXGate (*args[, _force_mutable]) | Controlled-√X gate. |
CUGate (theta, phi, lam, gamma[, label, ...]) | Controlled-U gate (4-parameter two-qubit gate). |
CU1Gate (theta[, label, ctrl_state, ...]) | Controlled-U1 gate. |
CU3Gate (theta, phi, lam[, label, ...]) | Controlled-U3 gate (3-parameter two-qubit gate). |
CXGate (*args[, _force_mutable]) | Controlled-X gate. |
CYGate (*args[, _force_mutable]) | Controlled-Y gate. |
CZGate (*args[, _force_mutable]) | Controlled-Z gate. |
CCZGate (*args[, _force_mutable]) | CCZ gate. |
ECRGate (*args[, _force_mutable]) | An echoed cross-resonance gate. |
HGate (*args[, _force_mutable]) | Single-qubit Hadamard gate. |
IGate (*args[, _force_mutable]) | Identity gate. |
MSGate (num_qubits, theta[, label]) | MSGate has been deprecated. |
PhaseGate (theta[, label, duration, unit]) | Single-qubit rotation about the Z axis. |
RCCXGate (*args[, _force_mutable]) | The simplified Toffoli gate, also referred to as Margolus gate. |
RC3XGate (*args[, _force_mutable]) | The simplified 3-controlled Toffoli gate. |
RGate (theta, phi[, label, duration, unit]) | Rotation θ around the cos(φ)x + sin(φ)y axis. |
RXGate (theta[, label, duration, unit]) | Single-qubit rotation about the X axis. |
RXXGate (theta[, label, duration, unit]) | A parametric 2-qubit interaction (rotation about XX). |
RYGate (theta[, label, duration, unit]) | Single-qubit rotation about the Y axis. |
RYYGate (theta[, label, duration, unit]) | A parametric 2-qubit interaction (rotation about YY). |
RZGate (phi[, label, duration, unit]) | Single-qubit rotation about the Z axis. |
RZZGate (theta[, label, duration, unit]) | A parametric 2-qubit interaction (rotation about ZZ). |
RZXGate (theta[, label, duration, unit]) | A parametric 2-qubit interaction (rotation about ZX). |
XXMinusYYGate (theta[, beta, label, ...]) | XX-YY interaction gate. |
XXPlusYYGate (theta[, beta, label, duration, ...]) | XX+YY interaction gate. |
SGate (*args[, _force_mutable]) | Single qubit S gate (Z**0.5). |
SdgGate (*args[, _force_mutable]) | Single qubit S-adjoint gate (~Z**0.5). |
SwapGate (*args[, _force_mutable]) | The SWAP gate. |
iSwapGate (*args[, _force_mutable]) | iSWAP gate. |
SXGate (*args[, _force_mutable]) | The single-qubit Sqrt(X) gate (). |
SXdgGate (*args[, _force_mutable]) | The inverse single-qubit Sqrt(X) gate. |
TGate (*args[, _force_mutable]) | Single qubit T gate (Z**0.25). |
TdgGate (*args[, _force_mutable]) | Single qubit T-adjoint gate (~Z**0.25). |
UGate (theta, phi, lam[, label, duration, unit]) | Generic single-qubit rotation gate with 3 Euler angles. |
U1Gate (theta[, label, duration, unit]) | Single-qubit rotation about the Z axis. |
U2Gate (phi, lam[, label, duration, unit]) | Single-qubit rotation about the X+Z axis. |
U3Gate (theta, phi, lam[, label, duration, unit]) | Generic single-qubit rotation gate with 3 Euler angles. |
XGate (*args[, _force_mutable]) | The single-qubit Pauli-X gate (). |
YGate (*args[, _force_mutable]) | The single-qubit Pauli-Y gate (). |
ZGate (*args[, _force_mutable]) | The single-qubit Pauli-Z gate (). |
GlobalPhaseGate (phase[, label, duration, unit]) | The global phase gate (). |
Standard Directives
Directives are operations to the quantum stack that are meant to be interpreted by the backend or the transpiler. In general, the transpiler or backend might optionally ignore them if there is no implementation for them.
Standard Operations
Operations are non-reversible changes in the quantum state of the circuit.
Generalized Gates
These “gates” (many are QuantumCircuit
subclasses) allow to set the amount of qubits involved at instantiation time.
from qiskit.circuit.library import Diagonal
diagonal = Diagonal([1, 1])
print(diagonal.num_qubits)
diagonal = Diagonal([1, 1, 1, 1])
print(diagonal.num_qubits)
1
2
Diagonal (diag) | Diagonal circuit. |
DiagonalGate (diag) | Gate implementing a diagonal transformation. |
MCMT (gate, num_ctrl_qubits, num_target_qubits) | The multi-controlled multi-target gate, for an arbitrary singly controlled target gate. |
MCMTVChain (gate, num_ctrl_qubits, ...) | The MCMT implementation using the CCX V-chain. |
Permutation (num_qubits[, pattern, seed]) | An n_qubit circuit that permutes qubits. |
PermutationGate (pattern) | A gate that permutes qubits. |
GMS (num_qubits, theta) | Global Mølmer–Sørensen gate. |
GR (num_qubits, theta, phi) | Global R gate. |
GRX (num_qubits, theta) | Global RX gate. |
GRY (num_qubits, theta) | Global RY gate. |
GRZ (num_qubits, phi) | Global RZ gate. |
MCPhaseGate (lam, num_ctrl_qubits[, label, ...]) | Multi-controlled-Phase gate. |
MCXGate ([num_ctrl_qubits, label, ...]) | The general, multi-controlled X gate. |
MCXGrayCode ([num_ctrl_qubits, label, ...]) | Implement the multi-controlled X gate using the Gray code. |
MCXRecursive ([num_ctrl_qubits, label, ...]) | Implement the multi-controlled X gate using recursion. |
MCXVChain ([num_ctrl_qubits, dirty_ancillas, ...]) | Implement the multi-controlled X gate using a V-chain of CX gates. |
RVGate (v_x, v_y, v_z[, basis]) | Rotation around arbitrary rotation axis where is angle of rotation in radians. |
PauliGate (label) | A multi-qubit Pauli gate. |
LinearFunction (linear[, validate_input]) | A linear reversible circuit on n qubits. |
Isometry (isometry, num_ancillas_zero, ...[, ...]) | Decomposition of arbitrary isometries from to qubits. |
UnitaryGate (data[, label, check_input, ...]) | Class quantum gates specified by a unitary matrix. |
UCGate (gate_list[, up_to_diagonal]) | Uniformly controlled gate (also called multiplexed gate). |
UCPauliRotGate (angle_list, rot_axis) | Uniformly controlled Pauli rotations. |
UCRXGate (angle_list) | Uniformly controlled Pauli-X rotations. |
UCRYGate (angle_list) | Uniformly controlled Pauli-Y rotations. |
UCRZGate (angle_list) | Uniformly controlled Pauli-Z rotations. |
Boolean Logic Circuits
These are QuantumCircuit
subclasses that implement boolean logic operations, such as the logical or of a set of qubit states.
AND (num_variable_qubits[, flags, mcx_mode]) | A circuit implementing the logical AND operation on a number of qubits. |
OR (num_variable_qubits[, flags, mcx_mode]) | A circuit implementing the logical OR operation on a number of qubits. |
XOR (num_qubits[, amount, seed]) | An n_qubit circuit for bitwise xor-ing the input with some integer amount . |
InnerProduct (num_qubits) | A 2n-qubit Boolean function that computes the inner product of two n-qubit vectors over . |
Basis Change Circuits
These circuits allow basis transformations of the qubit states. For example, in the case of the Quantum Fourier Transform (QFT), it transforms between the computational basis and the Fourier basis.
QFT ([num_qubits, approximation_degree, ...]) | Quantum Fourier Transform Circuit. |
QFTGate (num_qubits) | Quantum Fourier Transform Gate. |
Arithmetic Circuits
These QuantumCircuit
s perform classical arithmetic, such as addition or multiplication.
Amplitude Functions
LinearAmplitudeFunction (num_state_qubits, ...) | A circuit implementing a (piecewise) linear function on qubit amplitudes. |
Functional Pauli Rotations
FunctionalPauliRotations ([num_state_qubits, ...]) | Base class for functional Pauli rotations. |
LinearPauliRotations ([num_state_qubits, ...]) | Linearly-controlled X, Y or Z rotation. |
PolynomialPauliRotations ([num_state_qubits, ...]) | A circuit implementing polynomial Pauli rotations. |
PiecewiseLinearPauliRotations ([...]) | Piecewise-linearly-controlled Pauli rotations. |
PiecewisePolynomialPauliRotations ([...]) | Piecewise-polynomially-controlled Pauli rotations. |
PiecewiseChebyshev (f_x[, degree, ...]) | Piecewise Chebyshev approximation to an input function. |
Adders
DraperQFTAdder (num_state_qubits[, kind, name]) | A circuit that uses QFT to perform in-place addition on two qubit registers. |
CDKMRippleCarryAdder (num_state_qubits[, ...]) | A ripple-carry circuit to perform in-place addition on two qubit registers. |
VBERippleCarryAdder (num_state_qubits[, ...]) | The VBE ripple carry adder [1]. |
WeightedAdder ([num_state_qubits, weights, name]) | A circuit to compute the weighted sum of qubit registers. |
Multipliers
HRSCumulativeMultiplier (num_state_qubits[, ...]) | A multiplication circuit to store product of two input registers out-of-place. |
RGQFTMultiplier (num_state_qubits[, ...]) | A QFT multiplication circuit to store product of two input registers out-of-place. |
Comparators
IntegerComparator ([num_state_qubits, value, ...]) | Integer Comparator. |
Functions on binary variables
QuadraticForm ([num_result_qubits, ...]) | Implements a quadratic form on binary variables encoded in qubit registers. |
Other arithmetic functions
ExactReciprocal (num_state_qubits, scaling[, ...]) | Exact reciprocal |
Particular Quantum Circuits
FourierChecking (f, g) | Fourier checking circuit. |
GraphState (adjacency_matrix) | Circuit to prepare a graph state. |
HiddenLinearFunction (adjacency_matrix) | Circuit to solve the hidden linear function problem. |
IQP (interactions) | Instantaneous quantum polynomial (IQP) circuit. |
QuantumVolume (num_qubits[, depth, seed, ...]) | A quantum volume model circuit. |
PhaseEstimation (num_evaluation_qubits, unitary) | Phase Estimation circuit. |
GroverOperator (oracle[, state_preparation, ...]) | The Grover operator. |
PhaseOracle (expression[, synthesizer, var_order]) | Phase Oracle. |
PauliEvolutionGate (operator[, time, label, ...]) | Time-evolution of an operator consisting of Paulis. |
HamiltonianGate (data, time[, label]) | Class for representing evolution by a Hamiltonian operator as a gate. |
UnitaryOverlap (unitary1, unitary2[, ...]) | Circuit that returns the overlap between two unitaries . |
N-local circuits
These BlueprintCircuit
subclasses are used as parameterized models (a.k.a. ansatzes or variational forms) in variational algorithms. They are heavily used in near-term algorithms in e.g. Chemistry, Physics or Optimization.
NLocal ([num_qubits, rotation_blocks, ...]) | The n-local circuit class. |
TwoLocal ([num_qubits, rotation_blocks, ...]) | The two-local circuit. |
PauliTwoDesign ([num_qubits, reps, seed, ...]) | The Pauli Two-Design ansatz. |
RealAmplitudes ([num_qubits, entanglement, ...]) | The real-amplitudes 2-local circuit. |
EfficientSU2 ([num_qubits, su2_gates, ...]) | The hardware efficient SU(2) 2-local circuit. |
EvolvedOperatorAnsatz ([operators, reps, ...]) | The evolved operator ansatz. |
ExcitationPreserving ([num_qubits, mode, ...]) | The heuristic excitation-preserving wave function ansatz. |
QAOAAnsatz ([cost_operator, reps, ...]) | A generalized QAOA quantum circuit with a support of custom initial states and mixers. |
Data encoding circuits
These BlueprintCircuit
encode classical data in quantum states and are used as feature maps for classification.
PauliFeatureMap ([feature_dimension, reps, ...]) | The Pauli Expansion circuit. |
ZFeatureMap (feature_dimension[, reps, ...]) | The first order Pauli Z-evolution circuit. |
ZZFeatureMap (feature_dimension[, reps, ...]) | Second-order Pauli-Z evolution circuit. |
StatePreparation (params[, num_qubits, ...]) | Complex amplitude state preparation. |
Initialize (params[, num_qubits, normalize]) | Complex amplitude initialization. |
Template circuits
Templates are functions that return circuits that compute the identity. They are used at circuit optimization where matching part of the template allows the compiler to replace the match with the inverse of the remainder from the template.
In this example, the identity constant in a template is checked:
from qiskit.circuit.library.templates import template_nct_4b_1
from qiskit.quantum_info import Operator
import numpy as np
template = template_nct_4b_1()
identity = np.identity(2 ** len(template.qubits), dtype=complex)
data = Operator(template).data
np.allclose(data, identity) # True, template_nct_4b_1 is the identity
NCT (Not-CNOT-Toffoli) template circuits
Template circuits for XGate
, CXGate
, and CCXGate
(Toffoli) gates.
Reference: Maslov, D. and Dueck, G. W. and Miller, D. M., Techniques for the synthesis of reversible Toffoli networks, 2007 http://dx.doi.org/10.1145/1278349.1278355
template_nct_2a_1
qiskit.circuit.library.templates.nct.template_nct_2a_1()
Returns
template as a quantum circuit.
Return type
template_nct_2a_2
qiskit.circuit.library.templates.nct.template_nct_2a_2()
Returns
template as a quantum circuit.
Return type
template_nct_2a_3
qiskit.circuit.library.templates.nct.template_nct_2a_3()
Returns
template as a quantum circuit.
Return type
template_nct_4a_1
qiskit.circuit.library.templates.nct.template_nct_4a_1()
Returns
template as a quantum circuit.
Return type
template_nct_4a_2
qiskit.circuit.library.templates.nct.template_nct_4a_2()
Returns
template as a quantum circuit.
Return type
template_nct_4a_3
qiskit.circuit.library.templates.nct.template_nct_4a_3()
Returns
template as a quantum circuit.
Return type
template_nct_4b_1
qiskit.circuit.library.templates.nct.template_nct_4b_1()
Returns
template as a quantum circuit.
Return type
template_nct_4b_2
qiskit.circuit.library.templates.nct.template_nct_4b_2()
Returns
template as a quantum circuit.
Return type
template_nct_5a_1
qiskit.circuit.library.templates.nct.template_nct_5a_1()
Returns
template as a quantum circuit.
Return type
template_nct_5a_2
qiskit.circuit.library.templates.nct.template_nct_5a_2()
Returns
template as a quantum circuit.
Return type
template_nct_5a_3
qiskit.circuit.library.templates.nct.template_nct_5a_3()
Returns
template as a quantum circuit.
Return type
template_nct_5a_4
qiskit.circuit.library.templates.nct.template_nct_5a_4()
Returns
template as a quantum circuit.
Return type
template_nct_6a_1
qiskit.circuit.library.templates.nct.template_nct_6a_1()
Returns
template as a quantum circuit.
Return type
template_nct_6a_2
qiskit.circuit.library.templates.nct.template_nct_6a_2()
Returns
template as a quantum circuit.
Return type
template_nct_6a_3
qiskit.circuit.library.templates.nct.template_nct_6a_3()
Returns
template as a quantum circuit.
Return type
template_nct_6a_4
qiskit.circuit.library.templates.nct.template_nct_6a_4()
Returns
template as a quantum circuit.
Return type
template_nct_6b_1
qiskit.circuit.library.templates.nct.template_nct_6b_1()
Returns
template as a quantum circuit.
Return type
template_nct_6b_2
qiskit.circuit.library.templates.nct.template_nct_6b_2()
Returns
template as a quantum circuit.
Return type
template_nct_6c_1
qiskit.circuit.library.templates.nct.template_nct_6c_1()
Returns
template as a quantum circuit.
Return type
template_nct_7a_1
qiskit.circuit.library.templates.nct.template_nct_7a_1()
Returns
template as a quantum circuit.
Return type
template_nct_7b_1
qiskit.circuit.library.templates.nct.template_nct_7b_1()
Returns
template as a quantum circuit.
Return type
template_nct_7c_1
qiskit.circuit.library.templates.nct.template_nct_7c_1()
Returns
template as a quantum circuit.
Return type
template_nct_7d_1
qiskit.circuit.library.templates.nct.template_nct_7d_1()
Returns
template as a quantum circuit.
Return type
template_nct_7e_1
qiskit.circuit.library.templates.nct.template_nct_7e_1()
Returns
template as a quantum circuit.
Return type
template_nct_9a_1
qiskit.circuit.library.templates.nct.template_nct_9a_1()
Returns
template as a quantum circuit.
Return type
template_nct_9c_1
qiskit.circuit.library.templates.nct.template_nct_9c_1()
Returns
template as a quantum circuit.
Return type
template_nct_9c_2
qiskit.circuit.library.templates.nct.template_nct_9c_2()
Returns
template as a quantum circuit.
Return type
template_nct_9c_3
qiskit.circuit.library.templates.nct.template_nct_9c_3()
Returns
template as a quantum circuit.
Return type
template_nct_9c_4
qiskit.circuit.library.templates.nct.template_nct_9c_4()
Returns
template as a quantum circuit.
Return type
template_nct_9c_5
qiskit.circuit.library.templates.nct.template_nct_9c_5()
Returns
template as a quantum circuit.
Return type
template_nct_9c_6
qiskit.circuit.library.templates.nct.template_nct_9c_6()
Returns
template as a quantum circuit.
Return type
template_nct_9c_7
qiskit.circuit.library.templates.nct.template_nct_9c_7()
Returns
template as a quantum circuit.
Return type
template_nct_9c_8
qiskit.circuit.library.templates.nct.template_nct_9c_8()
Returns
template as a quantum circuit.
Return type
template_nct_9c_9
qiskit.circuit.library.templates.nct.template_nct_9c_9()
Returns
template as a quantum circuit.
Return type
template_nct_9c_10
qiskit.circuit.library.templates.nct.template_nct_9c_10()
Returns
template as a quantum circuit.
Return type
template_nct_9c_11
qiskit.circuit.library.templates.nct.template_nct_9c_11()
Returns
template as a quantum circuit.
Return type
template_nct_9c_12
qiskit.circuit.library.templates.nct.template_nct_9c_12()
Returns
template as a quantum circuit.
Return type
template_nct_9d_1
qiskit.circuit.library.templates.nct.template_nct_9d_1()
Returns
template as a quantum circuit.
Return type
template_nct_9d_2
qiskit.circuit.library.templates.nct.template_nct_9d_2()
Returns
template as a quantum circuit.
Return type
template_nct_9d_3
qiskit.circuit.library.templates.nct.template_nct_9d_3()
Returns
template as a quantum circuit.
Return type
template_nct_9d_4
qiskit.circuit.library.templates.nct.template_nct_9d_4()
Returns
template as a quantum circuit.
Return type
template_nct_9d_5
qiskit.circuit.library.templates.nct.template_nct_9d_5()
Returns
template as a quantum circuit.
Return type
template_nct_9d_6
qiskit.circuit.library.templates.nct.template_nct_9d_6()
Returns
template as a quantum circuit.
Return type
template_nct_9d_7
qiskit.circuit.library.templates.nct.template_nct_9d_7()
Returns
template as a quantum circuit.
Return type
template_nct_9d_8
qiskit.circuit.library.templates.nct.template_nct_9d_8()
Returns
template as a quantum circuit.
Return type
template_nct_9d_9
qiskit.circuit.library.templates.nct.template_nct_9d_9()
Returns
template as a quantum circuit.
Return type
template_nct_9d_10
qiskit.circuit.library.templates.nct.template_nct_9d_10()
Returns
template as a quantum circuit.
Return type
Clifford template circuits
Template circuits over Clifford gates.
clifford_2_1
clifford_2_2
clifford_2_3
clifford_2_4
clifford_3_1
clifford_4_1
clifford_4_2
clifford_4_3
clifford_4_4
clifford_5_1
clifford_6_1
clifford_6_2
clifford_6_3
clifford_6_4
clifford_6_5
clifford_8_1
clifford_8_2
clifford_8_3
RZXGate template circuits
Template circuits with RZXGate
.