Measurement
Measurement turns a qubit into a classical bit per the Born rule; learn to map qubits to bits, read bitstrings in the right order, and use measure_all.
Projective measurement in the computational basis gives outcome $k$ with probability $|\langle k|\psi\rangle|^2$ (the Born Rule) and projects the state onto $|k\rangle$. In Qiskit, qc.measure(q, c) measures qubit $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. The code runs on your machine or in Colab, not inside WordPress.
Code
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 code explanation
| Line | Explanation |
|---|---|
| 1-2 | Import the circuit, the transpile function and the AerSimulator simulator. |
| 4 | A circuit with two qubits and two classical bits. |
| 5 | X on q0: its measurement result is always 1. |
| 6 | H on q1: its measurement result is equally random. |
| 7-8 | Measure each qubit into its corresponding classical bit. |
| 10 | Create the simulator. |
| 11 | Transpile the circuit for the simulator and run it for 1000 shots with a fixed seed. |
| 12 | Extract the counts from the result. |
| 13 | Print the raw dictionary. |
| 14-15 | Split each string: the first character is q1 and the last is q0. |
| 17-19 | A second circuit that uses measure_all to add a meas register automatically. |
| 20 | Print the number of classical bits and the name of the created register. |
Expected output (from an actual run)
{'11': 473, '01': 527}
q1=0 q0=1 : 527
q1=1 q0=1 : 473
2 ['meas']
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.
Try it yourself
Related topics
References
- IBM Quantum (2025). Qiskit SDK API documentation. IBM Quantum Documentation quantum.cloud.ibm.com
- Qiskit contributors (2025). Qiskit Aer documentation. GitHub Pages qiskit.github.io
- 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.
