QuantumCircuit¶

class
QuantumCircuit
(*regs, name=None, global_phase=0, metadata=None)[source]¶ Bases:
object
Create a new circuit.
A circuit is a list of instructions bound to some registers.
 Parameters
regs (list(
Register
) or list(int
) or list(list(Bit
))) –The registers to be included in the circuit.
If a list of
Register
objects, represents theQuantumRegister
and/orClassicalRegister
objects to include in the circuit.For example:
QuantumCircuit(QuantumRegister(4))
QuantumCircuit(QuantumRegister(4), ClassicalRegister(3))
QuantumCircuit(QuantumRegister(4, 'qr0'), QuantumRegister(2, 'qr1'))
If a list of
int
, the amount of qubits and/or classical bits to include in the circuit. It can either be a single int for just the number of quantum bits, or 2 ints for the number of quantum bits and classical bits, respectively.For example:
QuantumCircuit(4) # A QuantumCircuit with 4 qubits
QuantumCircuit(4, 3) # A QuantumCircuit with 4 qubits and 3 classical bits
If a list of python lists containing
Bit
objects, a collection ofBit
s to be added to the circuit.
name (str) – the name of the quantum circuit. If not set, an automatically generated string will be assigned.
global_phase (float or ParameterExpression) – The global phase of the circuit in radians.
metadata (dict) – Arbitrary key value metadata to associate with the circuit. This gets stored as freeform data in a dict in the
metadata
attribute. It will not be directly used in the circuit.
 Raises
CircuitError – if the circuit name, if given, is not valid.
Examples
Construct a simple Bell state circuit.
from qiskit import QuantumCircuit qc = QuantumCircuit(2, 2) qc.h(0) qc.cx(0, 1) qc.measure([0, 1], [0, 1]) qc.draw()
┌───┐ ┌─┐ q_0: ┤ H ├──■──┤M├─── └───┘┌─┴─┐└╥┘┌─┐ q_1: ─────┤ X ├─╫─┤M├ └───┘ ║ └╥┘ c: 2/═══════════╩══╩═ 0 1
Construct a 5qubit GHZ circuit.
from qiskit import QuantumCircuit qc = QuantumCircuit(5) qc.h(0) qc.cx(0, range(1, 5)) qc.measure_all()
Construct a 4qubit BernsteinVazirani circuit using registers.
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit qr = QuantumRegister(3, 'q') anc = QuantumRegister(1, 'ancilla') cr = ClassicalRegister(3, 'c') qc = QuantumCircuit(qr, anc, cr) qc.x(anc[0]) qc.h(anc[0]) qc.h(qr[0:3]) qc.cx(qr[0:3], anc[0]) qc.h(qr[0:3]) qc.barrier(qr) qc.measure(qr, cr) qc.draw()
┌───┐ ┌───┐ ░ ┌─┐ q_0: ┤ H ├───────■──┤ H ├───────────░─┤M├────── ├───┤ │ └───┘┌───┐ ░ └╥┘┌─┐ q_1: ┤ H ├───────┼────■──┤ H ├──────░──╫─┤M├─── ├───┤ │ │ └───┘┌───┐ ░ ║ └╥┘┌─┐ q_2: ┤ H ├───────┼────┼────■──┤ H ├─░──╫──╫─┤M├ ├───┤┌───┐┌─┴─┐┌─┴─┐┌─┴─┐└───┘ ░ ║ ║ └╥┘ ancilla_0: ┤ X ├┤ H ├┤ X ├┤ X ├┤ X ├─────────╫──╫──╫─ └───┘└───┘└───┘└───┘└───┘ ║ ║ ║ c: 3/══════════════════════════════════╩══╩══╩═ 0 1 2
Methods
Add Bits to the circuit.
Register a lowlevel, custom pulse definition for the given gate.
Add registers.
Append one or more instructions to the end of the circuit, modifying the circuit in place.
Assign parameters to new parameters or values.
Apply
Barrier
.Assign numeric parameters to values yielding a new circuit.
Best effort to cast value to type.
Converts several classical bit representations (such as indexes, range, etc.) into a list of classical bits.
Apply
CCXGate
.Apply
CHGate
.Return the current number of instances of this class, useful for auto naming.
Return the prefix to use for auto naming.
Apply
CXGate
.DEPRECATED  Returns rhs appended to self if self contains compatible registers.
Compose circuit with
other
circuit or instruction, optionally permuting wires.Control this circuit on
num_ctrl_qubits
qubits.Copy the circuit.
Count each operation kind in the circuit.
Apply
CPhaseGate
.Apply
CRXGate
.Apply
CRYGate
.Apply
CRZGate
.Apply
CSwapGate
.Apply
CSXGate
.Apply
CUGate
.Apply
CU1Gate
.Apply
CU3Gate
.Apply
CXGate
.Apply
CYGate
.Apply
CZGate
.Apply
DCXGate
.Call a decomposition pass on this circuit, to decompose one level (shallow decompose).
Apply
Delay
.Return circuit depth (i.e., length of critical path).
Attach a diagonal gate to a circuit.
Draw the quantum circuit.
Apply
ECRGate
.DEPRECATED  Append QuantumCircuit to the RHS if it contains compatible registers.
Apply
CSwapGate
.Take in a QASM file and generate a QuantumCircuit object.
Take in a QASM string and generate a QuantumCircuit object.
Get instructions matching name.
Apply
HGate
.Apply hamiltonian evolution to qubits.
Test if this circuit has the register r.
Apply
IGate
.Apply
IGate
.Initialize qubits in a specific state.
Invert (take adjoint of) this circuit.
Attach an arbitrary isometry from m to n qubits to a circuit.
Attach an arbitrary isometry from m to n qubits to a circuit.
Apply
iSwapGate
.Apply
MCPhaseGate
.Apply MultipleControlled X rotation gate
Apply MultipleControlled Y rotation gate
Apply MultipleControlled Z rotation gate
Apply
MCXGate
.Apply
MCU1Gate
.Apply
MCXGate
.Measure quantum bit into classical bit (tuples).
Adds measurement to all nonidle qubits.
Adds measurement to all qubits.
Apply
MSGate
.How many nonentangled subcircuits can the circuit be factored to.
Return number of nonlocal gates (i.e.
Computes the number of tensor factors in the unitary (quantum) part of the circuit only.
Computes the number of tensor factors in the unitary (quantum) part of the circuit only.
Apply
PhaseGate
.Apply
PauliGate
.Raise this circuit to the power of
power
.Return OpenQASM string.
Converts several qubit representations (such as indexes, range, etc.) into a list of qubits.
Return the duration between the start and stop time of the first and last instructions, excluding delays, over the supplied qubits.
Return the start time of the first instruction, excluding delays, over the supplied qubits.
Return the stop time of the last instruction, excluding delays, over the supplied qubits.
Apply
RGate
.Apply
RC3XGate
.Apply
RCCXGate
.Removes final measurement on all qubits if they are present.
Repeat this circuit
reps
times.Reset q.
Return a circuit with the opposite order of wires.
Reverse the circuit by reversing the order of instructions.
Apply
RVGate
.Apply
RXGate
.Apply
RXXGate
.Apply
RYGate
.Apply
RYYGate
.Apply
RZGate
.Apply
RZXGate
.Apply
RZZGate
.Apply
SGate
.Save complex statevector amplitudes.
Save squared statevector amplitudes (probabilities).
Save the current simulator quantum state as a density matrix.
Save the expectation value of a Hermitian operator.
Save the expectation value of a Hermitian operator.
Save the current simulator quantum state as a matrix product state.
Save measurement outcome probabilities vector.
Save measurement outcome probabilities vector.
Save the current stabilizer simulator quantum state as a Clifford.
Save the current simulator quantum state.
Save the current simulator quantum state as a statevector.
Save the current simulator quantum state as a statevector as a dict.
Save the current state of the superop simulator.
Save the current state of the unitary simulator.
Apply
SdgGate
.Set the density matrix state of the simulator.
Set the matrix product state of the simulator.
Set the Clifford stabilizer state of the simulator.
Set the statevector state of the simulator.
Set the superop state of the simulator.
Set the state state of the simulator.
Returns total number of gate operations in circuit.
Take a statevector snapshot of the internal simulator representation.
Take a density matrix snapshot of simulator state.
Take a snapshot of expectation value <O> of an Operator.
Take a probability snapshot of the simulator state.
Take a stabilizer snapshot of the simulator state.
Take a statevector snapshot of the simulator state.
Decompose an arbitrary 2*2 unitary into three rotation gates.
Apply
SwapGate
.Apply
SXGate
.Apply
SXdgGate
.Apply
TGate
.Apply
TdgGate
.Tensor
self
withother
.Create a Gate out of this circuit.
Create an Instruction out of this circuit.
Apply
CCXGate
.Apply
UGate
.Apply
U1Gate
.Apply
U2Gate
.Apply
U3Gate
.Attach a uniformly controlled gates (also called multiplexed gates) to a circuit.
Attach a uniformly controlled (also called multiplexed) Rx rotation gate to a circuit.
Attach a uniformly controlled (also called multiplexed) Ry rotation gate to a circuit.
Attach a uniformly controlled (also called multiplexed gates) Rz rotation gate to a circuit.
Apply unitary gate to q.
Return number of qubits plus clbits in circuit.
Apply
XGate
.Apply
YGate
.Apply
ZGate
.Attributes

ancillas
¶ Returns a list of ancilla bits in the order that the registers were added.

calibrations
¶ Return calibration dictionary.
 The custom pulse definition of a given gate is of the form
{‘gate_name’: {(qubits, params): schedule}}

clbits
¶ Returns a list of classical bits in the order that the registers were added.

data
¶ Return the circuit data (instructions and context).
 Returns
a listlike object containing the tuples for the circuit’s data.
Each tuple is in the format
(instruction, qargs, cargs)
, where instruction is an Instruction (or subclass) object, qargs is a list of Qubit objects, and cargs is a list of Clbit objects. Return type
QuantumCircuitData

extension_lib
= 'include "qelib1.inc";'¶

global_phase
¶ Return the global phase of the circuit in radians.

header
= 'OPENQASM 2.0;'¶

instances
= 16¶

metadata
¶ The user provided metadata associated with the circuit
The metadata for the circuit is a user provided
dict
of metadata for the circuit. It will not be used to influence the execution or operation of the circuit, but it is expected to be passed between all transforms of the circuit (ie transpilation) and that providers will associate any circuit metadata with the results it returns from execution of that circuit.

num_ancillas
¶ Return the number of ancilla qubits.

num_clbits
¶ Return number of classical bits.

num_parameters
¶ Convenience function to get the number of parameter objects in the circuit.

num_qubits
¶ Return number of qubits.

parameters
¶ Convenience function to get the parameters defined in the parameter table.

prefix
= 'circuit'¶

qubits
¶ Returns a list of quantum bits in the order that the registers were added.