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 free-form 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('mpl')
(Source code, png, hires.png, pdf)
Construct a 5-qubit GHZ circuit.
from qiskit import QuantumCircuit qc = QuantumCircuit(5) qc.h(0) qc.cx(0, range(1, 5)) qc.measure_all()
Construct a 4-qubit Bernstein-Vazirani 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('mpl')
(Source code, png, hires.png, pdf)
Methods
Add Bits to the circuit.
Register a low-level, 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.
Apply
BreakLoopOp
.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
CCZGate
.Apply
CHGate
.Clear all instructions in self.
Return the current number of instances of this class, useful for auto naming.
Return the prefix to use for auto naming.
Apply
CXGate
.Compose circuit with
other
circuit or instruction, optionally permuting wires.Apply
ContinueLoopOp
.Control this circuit on
num_ctrl_qubits
qubits.Copy the circuit.
Return a copy of self with the same structure but empty.
Count each operation kind in the circuit.
Apply
CPhaseGate
.Apply
CRXGate
.Apply
CRYGate
.Apply
CRZGate
.Apply
CSGate
.Apply
CSdgGate
.Apply
CSwapGate
.Apply
CSXGate
.Apply
CUGate
.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
.Find locations in the circuit which can be used to reference a given
Bit
.Create a
for
loop on this circuit.Apply
CSwapGate
.Construct a circuit from an iterable of CircuitInstructions.
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.
Return True if the circuit has a calibration defined for the instruction context.
Test if this circuit has the register r.
Apply
IGate
.Apply
IGate
.Apply
IfElseOp
.Create an
if
statement on this circuit.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 Multiple-Controlled X rotation gate
Apply Multiple-Controlled Y rotation gate
Apply Multiple-Controlled Z rotation gate
Apply
MCXGate
.Apply
MCXGate
.Measure quantum bit into classical bit (tuples).
Adds measurement to all non-idle qubits.
Adds measurement to all qubits.
Apply
MSGate
.How many non-entangled subcircuits can the circuit be factored to.
Return number of non-local 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
.Prepare qubits in a specific state.
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 measurements and barriers on all qubits if they are present.
Repeat this circuit
reps
times.Reset the quantum bit(s) to their default state.
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 stabilizer simulator quantum state as a Clifford.
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 StabilizerState.
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 instructions in circuit.
Take a statevector snapshot of the internal simulator representation.
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
.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 specified by
obj
toqubits
.Create a
while
loop on this circuit.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.
- Return type
List
[AncillaQubit
]
- calibrations¶
Return calibration dictionary.
The custom pulse definition of a given gate is of the form
{'gate_name': {(qubits, params): schedule}}
- Return type
dict
- clbits¶
Returns a list of classical bits in the order that the registers were added.
- Return type
List
[Clbit
]
- data¶
Return the circuit data (instructions and context).
- Returns
a list-like object containing the
CircuitInstruction
s for each instruction.- Return type
QuantumCircuitData
- extension_lib = 'include "qelib1.inc";'¶
- global_phase¶
Return the global phase of the circuit in radians.
- Return type
Union
[ParameterExpression
,float
]
- header = 'OPENQASM 2.0;'¶
- instances = 2293¶
- 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.- Return type
dict
- num_ancillas¶
Return the number of ancilla qubits.
- Return type
int
- num_clbits¶
Return number of classical bits.
- Return type
int
- num_parameters¶
The number of parameter objects in the circuit.
- Return type
int
- num_qubits¶
Return number of qubits.
- Return type
int
- op_start_times¶
Return a list of operation start times.
This attribute is enabled once one of scheduling analysis passes runs on the quantum circuit.
- Return type
List
[int
]- Returns
List of integers representing instruction start times. The index corresponds to the index of instruction in
QuantumCircuit.data
.- Raises
AttributeError – When circuit is not scheduled.
- parameters¶
The parameters defined in the circuit.
This attribute returns the
Parameter
objects in the circuit sorted alphabetically. Note that parameters instantiated with aParameterVector
are still sorted numerically.Examples
The snippet below shows that insertion order of parameters does not matter.
>>> from qiskit.circuit import QuantumCircuit, Parameter >>> a, b, elephant = Parameter("a"), Parameter("b"), Parameter("elephant") >>> circuit = QuantumCircuit(1) >>> circuit.rx(b, 0) >>> circuit.rz(elephant, 0) >>> circuit.ry(a, 0) >>> circuit.parameters # sorted alphabetically! ParameterView([Parameter(a), Parameter(b), Parameter(elephant)])
Bear in mind that alphabetical sorting might be unituitive when it comes to numbers. The literal “10” comes before “2” in strict alphabetical sorting.
>>> from qiskit.circuit import QuantumCircuit, Parameter >>> angles = [Parameter("angle_1"), Parameter("angle_2"), Parameter("angle_10")] >>> circuit = QuantumCircuit(1) >>> circuit.u(*angles, 0) >>> circuit.draw() ┌─────────────────────────────┐ q: ┤ U(angle_1,angle_2,angle_10) ├ └─────────────────────────────┘ >>> circuit.parameters ParameterView([Parameter(angle_1), Parameter(angle_10), Parameter(angle_2)])
To respect numerical sorting, a
ParameterVector
can be used.>>> from qiskit.circuit import QuantumCircuit, Parameter, ParameterVector >>> x = ParameterVector("x", 12) >>> circuit = QuantumCircuit(1) >>> for x_i in x: ... circuit.rx(x_i, 0) >>> circuit.parameters ParameterView([ ParameterVectorElement(x[0]), ParameterVectorElement(x[1]), ParameterVectorElement(x[2]), ParameterVectorElement(x[3]), ..., ParameterVectorElement(x[11]) ])
- Return type
ParameterView
- Returns
The sorted
Parameter
objects in the circuit.
- prefix = 'circuit'¶