Quantum Circuits (qiskit.circuit
)#
Overview#
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 a 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:
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>.
circ.h(0)
# A CX (CNOT) gate on control qubit 0 and target qubit 1 generating a Bell state.
circ.cx(0, 1)
# CX (CNOT) gate on control qubit 0 and target qubit 2 resulting in a GHZ state.
circ.cx(0, 2)
# Draw the circuit
circ.draw('mpl')

Supplementary Information#
Quantum Circuit API#
Quantum Circuit Construction#
|
Create a new circuit. |
|
Implement a quantum register. |
|
Implement a quantum bit. |
|
Implement a classical register. |
|
Implement a classical bit. |
|
Implement an ancilla register. |
|
A qubit used as ancillary qubit. |
|
A single instruction in a |
|
Implement a generic register. |
|
Implement a generic bit. |
Gates and Instructions#
|
Unitary gate. |
|
Controlled unitary gate. |
|
Do nothing and just delay/wait/idle for a specified duration. |
|
Generic quantum instruction. |
|
Instruction collection, and their contexts. |
Quantum Operation Interface Class. |
|
|
A library providing a one-way mapping of Gates to their equivalent implementations as QuantumCircuits. |
Control Flow Operations#
|
Abstract class to encapsulate all control flow operations. |
|
A circuit operation which executes a program ( |
|
A circuit operation which repeatedly executes a subcircuit ( |
|
A circuit operation which repeatedly executes a subcircuit ( |
|
A circuit operation that executes one particular circuit block based on matching a given |
|
A circuit operation which, when encountered, jumps to the end of the nearest enclosing loop. |
|
A circuit operation which, when encountered, moves to the next iteration of the nearest enclosing loop. |
The SwitchCaseOp
also understands a special value:
- qiskit.circuit.CASE_DEFAULT#
A special object that represents the « default » case of a switch statement. If you use this as a case target, it must be the last case, and will match anything that wasn’t already matched. For example:
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister from qiskit.circuit import SwitchCaseOp, CASE_DEFAULT body0 = QuantumCircuit(2, 2) body0.x(0) body1 = QuantumCircuit(2, 2) body1.z(0) body2 = QuantumCircuit(2, 2) body2.cx(0, 1) qr, cr = QuantumRegister(2), ClassicalRegister(2) qc = QuantumCircuit(qr, cr) qc.switch(cr, [(0, body0), (1, body1), (CASE_DEFAULT, body2)], qr, cr)
When using the builder interface of
QuantumCircuit.switch()
, this can also be accessed as theDEFAULT
attribute of the bound case-builder object, such as:from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister qr, cr = QuantumRegister(2), ClassicalRegister(2) qc = QuantumCircuit(qr, cr) with qc.switch(cr) as case: with case(0): qc.x(0) with case(1): qc.z(0) with case(case.DEFAULT): qc.cx(0, 1)
Parametric Quantum Circuits#
|
Parameter Class for variable parameters. |
|
ParameterVector class to quickly generate lists of parameters. |
|
ParameterExpression class to enable creating expressions of Parameters. |
Random Circuits#
- qiskit.circuit.random.random_circuit(num_qubits, depth, max_operands=4, measure=False, conditional=False, reset=False, seed=None)[source]#
Generate random circuit of arbitrary size and form.
This function will generate a random circuit by randomly selecting gates from the set of standard gates in
qiskit.extensions
. For example:from qiskit.circuit.random import random_circuit circ = random_circuit(2, 2, measure=True) circ.draw(output='mpl')
- Paramètres:
num_qubits (int) – number of quantum wires
depth (int) – layers of operations (i.e. critical path length)
max_operands (int) – maximum qubit operands of each gate (between 1 and 4)
measure (bool) – if True, measure all qubits at the end
conditional (bool) – if True, insert middle measurements and conditionals
reset (bool) – if True, insert middle resets
seed (int) – sets random seed (optional)
- Renvoie:
constructed circuit
- Type renvoyé:
- Lève:
CircuitError – when invalid options given
Exceptions#
Almost all circuit functions and methods will raise a CircuitError
when encountering an error
that is particular to usage of Qiskit (as opposed to regular typing or indexing problems, which will
typically raise the corresponding standard Python error).