Code exampleBeginnerQiskit

Measurement

Measurement

Measurement turns a qubit into a classical bit according to the Born rule; we learn to map qubits to bits, read strings in the correct order, and use measure_all.

Projective measurement in the computational basis gives outcome $k$ with probability $|\langle k|\psi\rangle|^2$ (the Born RuleBorn Rule: The Born rule states that the probability of measurement outcome x equals the squared absolute value of the amplitude: p(x)=⟨ xψ⟩^2.) and projects the state onto $|k\rangle$. In Qiskit, qc.measure(q, c) measures qubitQubit: The qubit is the unit of quantum information: a two-level system whose state is a unit vector α0⟩+β1⟩ in ℂ^2. $q$ and stores its result in classical bit $c$.

In the example: $q_0$ is in state $|1\rangle$ (after X), so its result is always 1, and $q_1$ is in $|+\rangle$ (after H), so its result is equally random. That is why only two strings appear: “01” and “11”. The string is read from right to left: the last character is bit 0. This is a common source of errors when comparing results with textbooks that write the first qubit on the left.

Counts come from a finite number of Shots, so they are a statistical estimate of the probabilities: with 1000 shots and probability $1/2$ the standard deviation is about 16 shots. We fix the seed seed_simulator so that the results are reproducible.

The measure_all() method adds a Barrier and a new classical register named meas, sized to the number of qubits, and measures all qubits into it; this is the usual style with StatevectorSampler. Execution here is on a classical simulator (AerSimulator or the Statevector primitives), not on a real quantum computer.

Requirements: pip install qiskit qiskit-aer

Code

Qiskit
from qiskit import QuantumCircuit, transpile
from qiskit_aer import AerSimulator

qc = QuantumCircuit(2, 2)
qc.x(0)
qc.h(1)
qc.measure(0, 0)
qc.measure(1, 1)

sim = AerSimulator()
result = sim.run(transpile(qc, sim), shots=1000, seed_simulator=11).result()
counts = result.get_counts()
print(counts)
for bits, n in sorted(counts.items()):
    print(f"q1={bits[0]} q0={bits[1]} : {n}")

qm = QuantumCircuit(2)
qm.h(0)
qm.measure_all()
print(qm.num_clbits, [c.name for c in qm.cregs])

Line-by-line explanation of the code

LineExplanation
1-2Import the circuit, the transpile function and the AerSimulator simulator.
4A circuit with two qubits and two classical bits.
5X on q0: its measurement result is always 1.
6H on q1: its measurement result is equally random.
7-8Measure each qubit into its corresponding classical bit.
10Create the simulator.
11Transpile the circuit for the simulator and run it for 1000 shots with a fixed seed.
12Extract the counts from the result.
13Print the raw dictionary.
14-15Split each string: the first character is q1 and the last is q0.
17-19A second circuit that uses measure_all to add a meas register automatically.
20Print the number of classical bits and the name of the created register.

Output (from an actual run)

{'11': 473, '01': 527}
q1=0 q0=1 : 527
q1=1 q0=1 : 473
2 ['meas']

Run with Python 3.13.18 on 2026-11-20 08:14 UTC.

The counts were produced by AerSimulator with seed_simulator=11; they will differ with another seed, but 01 and 11 are the only possible outcomes.

References

  1. IBM Quantum (2025). Qiskit SDK API documentation. IBM Quantum Documentation quantum.cloud.ibm.com
  2. Qiskit contributors (2025). Qiskit Aer documentation. GitHub Pages qiskit.github.io
  3. Michael A. Nielsen; Isaac L. Chuang (2010). Quantum Computation and Quantum Information (10th Anniversary Edition). Cambridge University Press doi:10.1017/CBO9780511976667The standard reference for quantum computing; the Crossref record gives 2012 for the Cambridge Core online edition.