Skip to main content
This project is in beta. Found a bug? Open a GitHub issue. Question or idea? Ask in Discussions — we'd love your feedback!

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.5.2

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-ZZ basis. Therefore, a measurement yields 0 or 1, depending on the overlap with the Pauli-ZZ eigenstates ∣0⟩|0\rangle and ∣1⟩|1\rangle:

∣q⟩→measure{0(outcome+1),with probability p0=∣⟨q∣0⟩∣2,1(outcome−1),with probability p1=∣⟨q∣1⟩∣2.|q\rangle \xrightarrow{measure}\begin{cases} 0 (\text{outcome}+1), \text{with probability } p_0=|\langle q|0\rangle|^{2}\text{,} \\ 1 (\text{outcome}-1), \text{with probability } p_1=|\langle q|1\rangle|^{2}\text{.} \end{cases}

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:

# Added by doQumentation — installs the packages this notebook needs if they are missing
import importlib.util

_needed = {"qiskit": "qiskit"}
_missing = [pip for module, pip in _needed.items()
if importlib.util.find_spec(module) is None]
# One at a time, so a package that fails to install does not block the others
for _pip in _missing:
%pip install -q {_pip}
if not _missing:
print("\u2713 All required packages are installed")
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.
<qiskit.circuit.instructionset.InstructionSet at 0x1068db370>
from qiskit import QuantumCircuit

qc = QuantumCircuit(3, 1)
qc.x([0, 2])
qc.measure(1, 0) # Measure qubit 1 into the classical bit 0.
<qiskit.circuit.instructionset.InstructionSet at 0x1068da4a0>

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
<qiskit.circuit.instructionset.InstructionSet at 0x1068daef0>

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, that is, qubits 0 and 2.

Next steps​

Recommendations