Quantum computing and AIMiddleLesson 3046 min read

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.

Lesson in motion

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.

1/4
In simple words
Think of two ripples on a pond meeting. Where two high points meet, the wave gets taller. Where a high point meets a low point, they cancel and the water is flat. Amplitudes behave a bit like these ripples: they can add up or cancel. Only when you measure do you get a plain answer, and the tall places are where answers are most likely.
A state like a|0⟩ + b|1⟩, where both a and b are not zero, is called a superposition. It is not "secretly 0" or "secretly 1". It is a definite state of its own, and it is described fully by the amplitudes.

From amplitudes to probabilities

The rule, called the Born rule, is simple: the probability of an outcome is the squared magnitude of its amplitude. For a real number, that is just the number squared. For a complex number such as a + bi, the magnitude squared is a² + b².
StateP(0)P(1)
1|0⟩ + 0|1⟩10
0.6|0⟩ + 0.8|1⟩0.360.64
(1/√2)|0⟩ + (1/√2)|1⟩0.50.5
(1/√2)|0⟩ − (1/√2)|1⟩0.50.5
(1/√2)|0⟩ + (i/√2)|1⟩0.50.5 (|i|² = 1)
The last three rows give the same chances, yet they are different states. The difference is in the sign or phase of the amplitudes. Phase does not change the chances of a single measurement, but it changes how amplitudes interfere when gates act on the qubit.

Measurement is random, but predictable on average

Measure one qubit in state 0.6|0⟩ + 0.8|1⟩ and you get 0 or 1; you cannot predict which. Prepare the same state 1000 times and measure each time, and you get 1 roughly 640 times. Physicists call each run a shot. Real quantum programs are run for many shots, and the result is a count of outcomes.
  1. 1

    Prepare

    Set up the qubits in a known starting state, usually all |0⟩.
  2. 2

    Apply gates

    Change the amplitudes in a controlled way.
  3. 3

    Measure

    Each qubit gives 0 or 1. The superposition is gone.
  4. 4

    Repeat

    Run many shots and count the results to estimate the probabilities.

Interference: why it is not just a coin

The Hadamard gate, written H, turns |0⟩ into an equal superposition. If a qubit were just a fair coin, doing H twice would still give a 50/50 coin. But it does not. H applied twice gives back |0⟩ exactly, because the amplitudes for 1 cancel out. Try it:
Python: Hadamard twice, and interferencepython
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))
After one H, the chances are 50/50. After two, the chance of 0 is back to 1. The two paths that lead to 1 have amplitudes +½ and −½, and they cancel. This interference is what quantum algorithms use: arrange things so wrong answers cancel and right answers add up.
Watch out
A quantum computer does not "try every answer at once and read them all". You only ever read one outcome per shot. Speed-ups, where they exist, come from cleverly designed interference, and only some problems allow that.
Try this. In the code, start from 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.

~25 min

The problem

Run this in the browser, or with Python 3 and numpy.

  1. 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.
  2. 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?
  3. Change my_state to a valid state that gives 1 with probability 0.9.
  4. 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.

Output

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.

Please sign in to continue.

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.

Sign in to take the test and save your progress