Circuit Library
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 ([label, ctrl_state]) | The X gate controlled on 3 qubits. |
C3SXGate ([label, ctrl_state]) | The 3-qubit controlled sqrt-X gate. |
C4XGate ([label, ctrl_state]) | The 4-qubit controlled X gate. |
CCXGate ([label, ctrl_state]) | CCX gate, also known as Toffoli gate. |
DCXGate () | Double-CNOT gate. |
CHGate ([label, ctrl_state]) | 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. |
CSwapGate ([label, ctrl_state]) | Controlled-SWAP gate, also known as the Fredkin gate. |
CSXGate ([label, ctrl_state]) | 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, ctrl_state]) | Controlled-U3 gate (3-parameter two-qubit gate). |
CXGate ([label, ctrl_state]) | Controlled-X gate. |
CYGate ([label, ctrl_state]) | Controlled-Y gate. |
CZGate ([label, ctrl_state]) | Controlled-Z gate. |
HGate ([label]) | Single-qubit Hadamard gate. |
IGate ([label]) | Identity gate. |
MSGate (num_qubits, theta[, label]) | MSGate has been deprecated. |
PhaseGate (theta[, label]) | Single-qubit rotation about the Z axis. |
RCCXGate ([label]) | The simplified Toffoli gate, also referred to as Margolus gate. |
RC3XGate ([label]) | The simplified 3-controlled Toffoli gate. |
RGate (theta, phi[, label]) | Rotation θ around the cos(φ)x + sin(φ)y axis. |
RXGate (theta[, label]) | Single-qubit rotation about the X axis. |
RXXGate (theta[, label]) | A parametric 2-qubit interaction (rotation about XX). |
RYGate (theta[, label]) | Single-qubit rotation about the Y axis. |
RYYGate (theta[, label]) | A parametric 2-qubit interaction (rotation about YY). |
RZGate (phi[, label]) | Single-qubit rotation about the Z axis. |
RZZGate (theta[, label]) | A parametric 2-qubit interaction (rotation about ZZ). |
RZXGate (theta[, label]) | A parametric 2-qubit interaction (rotation about ZX). |
XXPlusYYGate (theta[, beta, label]) | XX+YY interaction gate. |
XXMinusYYGate (theta[, beta, label]) | XX-YY interaction gate. |
ECRGate () | An echoed cross-resonance gate. |
SGate ([label]) | Single qubit S gate (Z**0.5). |
SdgGate ([label]) | Single qubit S-adjoint gate (~Z**0.5). |
SwapGate ([label]) | The SWAP gate. |
iSwapGate ([label]) | iSWAP gate. |
SXGate ([label]) | The single-qubit Sqrt(X) gate (). |
SXdgGate ([label]) | The inverse single-qubit Sqrt(X) gate. |
TGate ([label]) | Single qubit T gate (Z**0.25). |
TdgGate ([label]) | Single qubit T-adjoint gate (~Z**0.25). |
UGate (theta, phi, lam[, label]) | Generic single-qubit rotation gate with 3 Euler angles. |
U1Gate (theta[, label]) | Single-qubit rotation about the Z axis. |
U2Gate (phi, lam[, label]) | Single-qubit rotation about the X+Z axis. |
U3Gate (theta, phi, lam[, label]) | Generic single-qubit rotation gate with 3 Euler angles. |
XGate ([label]) | The single-qubit Pauli-X gate (). |
YGate ([label]) | The single-qubit Pauli-Y gate (). |
ZGate ([label]) | The single-qubit Pauli-Z 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.
Barrier (num_qubits[, label]) | Barrier instruction. |
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. | ||
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, ctrl_state]) | 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 $ | v | $ is angle of rotation in radians. |
PauliGate (label) | A multi-qubit Pauli gate. | ||
LinearFunction (linear[, validate_input]) | A linear reversible circuit on n qubits. |
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. |
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]) | Phase Oracle. |
EvolvedOperatorAnsatz ([operators, reps, ...]) | The evolved operator ansatz. |
PauliEvolutionGate (operator[, time, label, ...]) | Time-evolution of an operator consisting of Paulis. |
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. |
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. |
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
templates.nct.template_nct_2a_1 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_2a_2 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_2a_3 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_4a_1 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_4a_2 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_4a_3 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_4b_1 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_4b_2 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_5a_1 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_5a_2 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_5a_3 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_5a_4 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_6a_1 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_6a_2 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_6a_3 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_6a_4 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_6b_1 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_6b_2 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_6c_1 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_7a_1 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_7b_1 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_7c_1 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_7d_1 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_7e_1 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_9a_1 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_9c_1 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_9c_2 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_9c_3 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_9c_4 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_9c_5 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_9c_6 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_9c_7 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_9c_8 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_9c_9 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_9c_10 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_9c_11 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_9c_12 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_9d_1 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_9d_2 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_9d_3 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_9d_4 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_9d_5 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_9d_6 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_9d_7 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_9d_8 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_9d_9 () | returnstemplate as a quantum circuit. |
templates.nct.template_nct_9d_10 () | returnstemplate as a quantum circuit. |
Clifford template circuits
Template circuits over Clifford gates.
clifford_2_1 () | returnstemplate as a quantum circuit. |
clifford_2_2 () | returnstemplate as a quantum circuit. |
clifford_2_3 () | returnstemplate as a quantum circuit. |
clifford_2_4 () | returnstemplate as a quantum circuit. |
clifford_3_1 () | returnstemplate as a quantum circuit. |
clifford_4_1 () | returnstemplate as a quantum circuit. |
clifford_4_2 () | returnstemplate as a quantum circuit. |
clifford_4_3 () | returnstemplate as a quantum circuit. |
clifford_4_4 () | returnstemplate as a quantum circuit. |
clifford_5_1 () | returnstemplate as a quantum circuit. |
clifford_6_1 () | returnstemplate as a quantum circuit. |
clifford_6_2 () | returnstemplate as a quantum circuit. |
clifford_6_3 () | returnstemplate as a quantum circuit. |
clifford_6_4 () | returnstemplate as a quantum circuit. |
clifford_6_5 () | returnstemplate as a quantum circuit. |
clifford_8_1 () | returnstemplate as a quantum circuit. |
clifford_8_2 () | returnstemplate as a quantum circuit. |
clifford_8_3 () | returnstemplate as a quantum circuit. |
RZXGate template circuits
Template circuits with RZXGate
.
rzx_yz ([theta]) | Template for CX - RYGate - CX. |
rzx_xz ([theta]) | Template for CX - RXGate - CX. |
rzx_cy ([theta]) | Template for CX - RYGate - CX. |
rzx_zz1 ([theta]) | Template for CX - RZGate - CX. |
rzx_zz2 ([theta]) | Template for CX - RZGate - CX. |
rzx_zz3 ([theta]) | Template for CX - RZGate - CX. |