1. Superposition
A qubit starts in a definite state, . One gate is enough to put it into
superposition: a state with two amplitudes at once. Measurement then returns
0 or 1 at random, with probabilities fixed by those amplitudes.
1. The idea
The Hadamard gate sends to an equal mixture of and
. Nothing in the circuit prefers one outcome. About half the shots
are 0 and half are 1, and the split gets closer to even as you take more
shots.
2. The code
import numpy as np
from fqkit import QuantumCircuit, Hadamard, run, measure_all
np.set_printoptions(precision=4, suppress=True)
qc = QuantumCircuit(1)
qc.add_gate(Hadamard(), [0])
state = run(qc)
print("Amplitudes :", np.round(state, 4))
print("Probabilities:", np.round(np.abs(state) ** 2, 4))
print("Counts :", measure_all(state, shots=1024))3. The output
Amplitudes : [0.7071+0.j 0.7071+0.j]
Probabilities: [0.5 0.5]
Counts : {'0': 509, '1': 515}The probabilities are exact. The counts are one sample of 1024 shots; yours will differ by a few tens.
4. The explanation
- Before the gate, the state vector is
[1, 0]: outcome0with certainty. - Hadamard writes the same amplitude, , onto both entries.
- A probability is an amplitude’s squared length. , so each outcome has probability one half.
measure_alldraws bitstrings from that distribution. It does not change the state vector you already printed.
5. The math
💡
Key insight: superposition is a pair of amplitudes, not a hidden bit. The simulator stores both numbers. A measurement throws one of them away and reports the bit that remains.
Try it yourself
- Delete the Hadamard and run the circuit again. Which probability becomes 1?
- Change
shotsto10000. How much closer is the split to 5120 / 5120? - Continue with Interference, where a second Hadamard makes those two amplitudes cancel.
Last updated on