Qiskit Fundamentals

Lesson 5 of 8

Measurement and Running on a Simulator

Statevector gives exact probabilities, but that's only possible on a simulator that can peek at the math directly. Real quantum hardware (and a more realistic simulator) only ever gives you measurement outcomes, one bitstring per run. This lesson runs a circuit many times and looks at the resulting distribution.

Running with shots

from qiskit import QuantumCircuit
from qiskit_aer import AerSimulator

qc = QuantumCircuit(2, 2)
qc.h(0)
qc.cx(0, 1)
qc.measure([0, 1], [0, 1])

simulator = AerSimulator()
result = simulator.run(qc, shots=1024).result()
counts = result.get_counts()
print(counts)  # e.g. {'00': 512, '11': 512}
  • AerSimulator() is a simulator that behaves like real hardware: it doesn't hand you the exact quantum state, only the outcome of measuring it, one shot at a time.
  • shots=1024 runs the entire circuit 1024 times independently (each one a fresh superposition, fresh measurement), since a single measurement only ever gives you one bitstring, you need many repetitions to see the underlying probability distribution.
  • result.get_counts() returns a dictionary mapping each observed bitstring to how many of the 1024 shots produced it.

Statistical noise

Run the code above a few times, the exact counts won't be identically 512/512 every time, maybe 498/526, this is expected: with a finite number of shots, you're estimating the true 50/50 probability, not measuring it exactly. More shots narrow that estimate, at the cost of more computation (or, on real hardware, more time and cost).

# fewer shots = noisier estimate of the true probabilities
result_small = simulator.run(qc, shots=10).result()
print(result_small.get_counts())  # could easily be lopsided, e.g. {'00': 7, '11': 3}

NOTE

Notice counts still only ever contains '00' and '11', exactly the entanglement result predicted in the previous lesson's Statevector probabilities, just observed empirically through repeated measurement instead of read directly from the math.

📝 Measurement and Simulation Quiz

Passing score: 70%
  1. 1.Why does simulator.run(qc, shots=1024) run the circuit 1024 times instead of once?

  2. 2.With a small number of shots, the observed counts can be noticeably lopsided compared to the true probabilities.

  3. 3.result.get_____() returns a dict mapping each observed bitstring to how many shots produced it.