Measure qubits
Package versions
The code on this page was developed using the following requirements. We recommend using these versions or newer.
qiskit[all]~=2.1.1
To get information about a qubit's state, you can measure it onto a classical bit. In Qiskit, measurements are performed in the computational basis, that is, the single-qubit Pauli- basis. Therefore, a measurement yields 0 or 1, depending on the overlap with the Pauli- eigenstates and :
Mid-circuit measurements
Mid-circuit measurements are a key component of dynamic circuits. Prior to qiskit-ibm-runtime
v0.43.0, measure
was the only measurement instruction in Qiskit. Mid-circuit measurements, however, have different tuning requirements than terminal measurements (measurements that happen at the end of a circuit). For example, you need to consider the instruction duration when tuning a mid-circuit measurement because longer instructions cause noisier circuits. You don't need to consider instruction duration for terminal measurements because there are no instructions after terminal measurements.
In qiskit-ibm-runtime
v0.43.0, the MidCircuitMeasure
(link to API) instruction was introduced. As the name suggests, it is a new measurement instruction that is optimized for mid-circuit on IBM QPUs.
Apply a measurement to a circuit
There are several ways to apply measurements to a circuit:
QuantumCircuit.measure
method
Use the measure
method to measure a QuantumCircuit
.
Examples:
from qiskit import QuantumCircuit
qc = QuantumCircuit(5, 5)
qc.x(0)
qc.x(1)
qc.x(4)
qc.measure(
range(5), range(5)
) # Measures all qubits into the corresponding clbit.
Output:
<qiskit.circuit.instructionset.InstructionSet at 0x7f72ed842560>
from qiskit import QuantumCircuit
qc = QuantumCircuit(3, 1)
qc.x([0, 2])
qc.measure(1, 0) # Measure qubit 1 into the classical bit 0.
Output:
<qiskit.circuit.instructionset.InstructionSet at 0x7f7320e529b0>
Measure
class
The Qiskit Measure class measures the specified qubits.
from qiskit.circuit import Measure
qc = QuantumCircuit(3, 1)
qc.x([0, 1])
qc.append(Measure(), [0], [0]) # measure qubit 0 into clbit 0
Output:
<qiskit.circuit.instructionset.InstructionSet at 0x7f7320e52e90>
QuantumCircuit.measure_all
method
To measure all qubits into the corresponding classical bits, use the measure_all
method. By default, this method adds new classical bits in a ClassicalRegister
to store these measurements.
from qiskit import QuantumCircuit
qc = QuantumCircuit(3, 1)
qc.x([0, 2])
qc.measure_all() # Measure all qubits.
QuantumCircuit.measure_active
method
To measure all qubits that are not idle, use the measure_active
method. This method creates a new ClassicalRegister
with a size equal to the number of non-idle qubits being measured.
from qiskit import QuantumCircuit
qc = QuantumCircuit(3, 1)
qc.x([0, 2])
qc.measure_active() # Measure qubits that are not idle, i.e., qubits 0 and 2.
MidCircuitMeasure
method
Use MidCircuitMeasure
(NEED LINK) to apply a mid-circuit measurement. While you can use QuantumCircuit.measure
for a mid-circuit measurement, MidCircuitMeasure
is designed specifically for mid-circuit measurements, and is therefore typically a better choice. For example, it adds less overhead to your circuit than when using QuantumCircuit.measure`.
from qiskit import QuantumCircuit
from qiskit_ibm_runtime.circuit import MidCircuitMeasure
from qiskit.circuit import Measure
circ = QuantumCircuit(2, 2)
circ.x(0)
circ.append(MidCircuitMeasure(), [0], [0])
circ.append(MidCircuitMeasure("measure_3"), [0], [1])
circ.measure([0], [0])
circ.measure_all()
print(circ.draw())
- Circuits that contain operations after a measurement are called dynamic circuits. Not all QPUs support these.
- There must be at least one classical register in order to use measurements.
- The Sampler primitive requires circuit measurements. You can add circuit measurements with the Estimator primitive, but they are ignored.
Next steps
Measure
classmeasure_all
methodmeasure_active
methodrandom_circuit
method