Superposition and measurement
Superposition means a state with amplitudes for several outcomes. Measurement turns amplitudes into probabilities by squaring their size. Simulate it in numpy.
In 60 seconds
Superposition and measurement
Superposition means a state with amplitudes for several outcomes. Measurement turns amplitudes into probabilities by squaring their size. Simulate it in numpy.
From amplitudes to probabilities
| State | P(0) | P(1) |
|---|---|---|
| 1|0⟩ + 0|1⟩ | 1 | 0 |
| 0.6|0⟩ + 0.8|1⟩ | 0.36 | 0.64 |
| (1/√2)|0⟩ + (1/√2)|1⟩ | 0.5 | 0.5 |
| (1/√2)|0⟩ − (1/√2)|1⟩ | 0.5 | 0.5 |
| (1/√2)|0⟩ + (i/√2)|1⟩ | 0.5 | 0.5 (|i|² = 1) |
Measurement is random, but predictable on average
- 1
Prepare
Set up the qubits in a known starting state, usually all |0⟩. - 2
Apply gates
Change the amplitudes in a controlled way. - 3
Measure
Each qubit gives 0 or 1. The superposition is gone. - 4
Repeat
Run many shots and count the results to estimate the probabilities.
Interference: why it is not just a coin
import numpy as np
H = np.array([[1, 1],
[1, -1]]) / np.sqrt(2)
zero = np.array([1.0, 0.0])
after_one = H @ zero
after_two = H @ after_one
print("after H once :", np.round(after_one, 3), "probabilities", np.round(np.abs(after_one) ** 2, 3))
print("after H twice:", np.round(after_two, 3), "probabilities", np.round(np.abs(after_two) ** 2, 3))np.array([0.0, 1.0]) instead of zero. What does one H give? What do two give?Watch and read more
Lab
A numpy function that turns amplitudes into measurement probabilities, a simulated 1000-shot experiment, and a state you design.
The problem
Run this in the browser, or with Python 3 and numpy.
- Finish
measurement_probs(state): it gets a numpy array of amplitudes (real or complex, any length) and returns the array of probabilities. Use the squared magnitude. - Run the starter. It measures the state 1000 times with a fixed random seed and prints the counts. Do the counts match your probabilities roughly?
- Change
my_stateto a valid state that gives 1 with probability 0.9. - Write two sentences: why are the counts not exactly equal to 1000 × probability?
Practice lab
Edit the code and run it. Python runs in your browser — nothing is sent to our server.
No output. Add print(...) to see values.
You are done when
Hard questions
Try to answer before you reveal. If you can answer these, you understood the lesson.
Q1Your my_state could be [√0.1, √0.9] or [√0.1, −√0.9]. Are these the same state?Reveal
They give the same chances when measured directly, so this experiment cannot tell them apart. But they are different states: the relative sign changes how they interfere. For example, after a Hadamard gate they give different measurement chances. Only a global phase, multiplying the whole state by the same number, makes no measurable difference.
Q2If you run 100 shots instead of 1000, what changes and why?Reveal
The counts will wander further from the true probabilities as a fraction. The typical error of an estimated probability shrinks roughly as 1/√shots, so 100 shots gives errors about three times bigger than 1000 shots. The true probabilities do not change; only our estimate of them gets noisier.
Questions people ask
Does measurement need a human to look?
No. "Measurement" means the qubit interacts with a measuring device in a way that records 0 or 1. No human or mind is needed. A detector does it on its own.
Why square the amplitude? Why not use it directly?
Because amplitudes can be negative or complex, and probabilities cannot. Squaring the size always gives a number from 0 to 1. This rule has been tested in a very large number of experiments, and it is what makes interference possible: amplitudes add first, and only then do you square.
What does it mean if two states differ only by a phase?
If the whole state is multiplied by the same phase (for example by −1), nothing measurable changes. That is a "global phase". But a phase on just one part, like the minus sign in (1/√2)|0⟩ − (1/√2)|1⟩, is a "relative phase", and it does change what gates later do.
How many shots do I need?
More shots give better estimates. The error of an estimated probability shrinks roughly like 1/√shots, so four times more shots halves the error. A few thousand shots is common for small experiments.
Lesson test
5 questions. Get 3 right (60%) to pass and complete this lesson.