Quantum Circuits (qiskit.circuit)


The fundamental element of quantum computing is the quantum circuit. A quantum circuit is a computational routine consisting of coherent quantum operations on quantum data, such as qubits. It is an ordered sequence of quantum gates, measurements and resets, which may be conditioned on real-time classical computation. A set of quantum gates is said to be universal if any unitary transformation of the quantum data can be efficiently approximated arbitrarily well as as sequence of gates in the set. Any quantum program can be represented by a sequence of quantum circuits and classical near-time computation.

In Qiskit, this core element is represented by the QuantumCircuit class. Below is an example of a quantum circuit that makes a three-qubit GHZ state defined as:

\[|\psi\rangle = \left(|000\rangle+|111\rangle\right)/\sqrt{2}\]
from qiskit import QuantumCircuit
# Create a circuit with a register of three qubits
circ = QuantumCircuit(3)
# H gate on qubit 0, putting this qubit in a superposition of |0> + |1>.
# A CX (CNOT) gate on control qubit 0 and target qubit 1 generating a Bell state., 1)
# CX (CNOT) gate on control qubit 0 and target qubit 2 resulting in a GHZ state., 2)
# Draw the circuit

(Source code)


Supplementary Information

Quantum Circuit with conditionals

When building a quantum circuit, there can be interest in applying a certain gate only if a classical register has a specific value. This can be done with the InstructionSet.c_if() method.

In the following example, we start with a single-qubit circuit formed by only a Hadamard gate (HGate), in which we expect to get \(|0\rangle\) and \(|1\rangle\) with equal probability.

from qiskit import BasicAer, transpile, QuantumRegister, ClassicalRegister, QuantumCircuit

qr = QuantumRegister(1)
cr = ClassicalRegister(1)
qc = QuantumCircuit(qr, cr)
qc.measure(0, 0)

(Source code)

backend = BasicAer.get_backend('qasm_simulator')
tqc = transpile(qc, backend)
counts =

{'0': 524, '1': 500}

Now, we add an XGate only if the value of the ClassicalRegister is 0. That way, if the state is \(|0\rangle\), it will be changed to \(|1\rangle\) and if the state is \(|1\rangle\), it will not be changed at all, so the final state will always be \(|1\rangle\).

from qiskit import BasicAer, transpile, QuantumRegister, ClassicalRegister, QuantumCircuit

qr = QuantumRegister(1)
cr = ClassicalRegister(1)
qc = QuantumCircuit(qr, cr)
qc.measure(0, 0)

qc.x(0).c_if(cr, 0)
qc.measure(0, 0)


(Source code)

backend = BasicAer.get_backend('qasm_simulator')
tqc = transpile(qc, backend)
counts =

{'1': 1024}
Quantum Circuit Properties

When constructing quantum circuits, there are several properties that help quantify the «size» of the circuits, and their ability to be run on a noisy quantum device. Some of these, like number of qubits, are straightforward to understand, while others like depth and number of tensor components require a bit more explanation. Here we will explain all of these properties, and, in preparation for understanding how circuits change when run on actual devices, highlight the conditions under which they change.

Consider the following circuit:

from qiskit import QuantumCircuit
qc = QuantumCircuit(12)
for idx in range(5):
   qc.h(idx), idx+5), 7)
qc.x(8), 9)
qc.x(7), 11)
qc.swap(6, 11)
qc.swap(6, 9)
qc.swap(6, 10)

(Source code)


From the plot, it is easy to see that this circuit has 12 qubits, and a collection of Hadamard, CNOT, X, and SWAP gates. But how to quantify this programmatically? Because we can do single-qubit gates on all the qubits simultaneously, the number of qubits in this circuit is equal to the width of the circuit:


We can also just get the number of qubits directly:



For a quantum circuit composed from just qubits, the circuit width is equal to the number of qubits. This is the definition used in quantum computing. However, for more complicated circuits with classical registers, and classically controlled gates, this equivalence breaks down. As such, from now on we will not refer to the number of qubits in a quantum circuit as the width.

It is also straightforward to get the number and type of the gates in a circuit using QuantumCircuit.count_ops():

OrderedDict([('cx', 8), ('h', 5), ('x', 3), ('swap', 3)])

We can also get just the raw count of operations by computing the circuits QuantumCircuit.size():


A particularly important circuit property is known as the circuit depth. The depth of a quantum circuit is a measure of how many «layers» of quantum gates, executed in parallel, it takes to complete the computation defined by the circuit. Because quantum gates take time to implement, the depth of a circuit roughly corresponds to the amount of time it takes the quantum computer to execute the circuit. Thus, the depth of a circuit is one important quantity used to measure if a quantum circuit can be run on a device.

The depth of a quantum circuit has a mathematical definition as the longest path in a directed acyclic graph (DAG). However, such a definition is a bit hard to grasp, even for experts. Fortunately, the depth of a circuit can be easily understood by anyone familiar with playing Tetris. Lets see how to compute this graphically:


We can verify our graphical result using QuantumCircuit.depth():


Quantum Circuit API

Quantum Circuit Construction

QuantumCircuit(*regs[, name, global_phase, ...])

Create a new circuit.

QuantumRegister([size, name, bits])

Implement a quantum register.

Qubit([register, index])

Implement a quantum bit.

ClassicalRegister([size, name, bits])

Implement a classical register.

Clbit([register, index])

Implement a classical bit.

AncillaRegister([size, name, bits])

Implement an ancilla register.

AncillaQubit([register, index])

A qubit used as ancillary qubit.

CircuitInstruction(operation[, qubits, clbits])

A single instruction in a QuantumCircuit, comprised of the operation and various operands.

Register([size, name, bits])

Implement a generic register.

Bit([register, index])

Implement a generic bit.

Gates and Instructions

Gate(name, num_qubits, params[, label])

Unitary gate.

ControlledGate(name, num_qubits, params[, ...])

Controlled unitary gate.

Delay(duration[, unit])

Do nothing and just delay/wait/idle for a specified duration.

Instruction(name, num_qubits, num_clbits, params)

Generic quantum instruction.

InstructionSet([circuit_cregs, ...])

Instruction collection, and their contexts.


Quantum Operation Interface Class.

EquivalenceLibrary(*[, base])

A library providing a one-way mapping of Gates to their equivalent implementations as QuantumCircuits.

Control Flow Operations

ControlFlowOp(name, num_qubits, num_clbits, ...)

Abstract class to encapsulate all control flow operations.

IfElseOp(condition, true_body[, false_body, ...])

A circuit operation which executes a program (true_body) if a provided condition (condition) evaluates to true, and optionally evaluates another program (false_body) otherwise.

WhileLoopOp(condition, body[, label])

A circuit operation which repeatedly executes a subcircuit (body) until a condition (condition) evaluates as False.

ForLoopOp(indexset, loop_parameter, body[, ...])

A circuit operation which repeatedly executes a subcircuit (body) parameterized by a parameter loop_parameter through the set of integer values provided in indexset.

SwitchCaseOp(target, cases, *[, label])

A circuit operation that executes one particular circuit block based on matching a given target against an ordered list of values.

BreakLoopOp(num_qubits, num_clbits[, label])

A circuit operation which, when encountered, jumps to the end of the nearest enclosing loop.

ContinueLoopOp(num_qubits, num_clbits[, label])

A circuit operation which, when encountered, moves to the next iteration of the nearest enclosing loop.

The SwitchCaseOp also understands a special value:

Parametric Quantum Circuits

Parameter(name[, uuid])

Parameter Class for variable parameters.

ParameterVector(name[, length])

ParameterVector class to quickly generate lists of parameters.

ParameterExpression(symbol_map, expr)

ParameterExpression class to enable creating expressions of Parameters.

Random Circuits

random.random_circuit(num_qubits, depth[, ...])

Generate random circuit of arbitrary size and form.