Quantum in 7 Sessions › Session 2
Session 2 — Measurement, gates, and interference
Session 2 of 7Self-studySimulator only
On this page: Read · Key ideas · Worked example · Try it · Second experiment · Common mistakes · Check yourself · Project
1. Read
Read the chapter first. The summary below is a reminder, not a replacement.
2. Key ideas
- A gate is a reversible (unitary) operation on the state. X swaps |0⟩ and |1⟩. Z flips the sign of the |1⟩ amplitude. H turns |0⟩ into an equal superposition.
- Measurement is sampling: each shot gives 0 or 1, and you estimate probabilities from many shots. More shots give a better estimate, not a different state.
- Applying H twice returns the qubit to |0⟩. The two paths to |1⟩ have opposite signs and cancel. That cancellation is interference.
- A Z gate between the two H gates changes only a phase, yet it flips the final answer from 0 to 1. Phase is invisible until interference makes it visible.
3. Worked example
Work through this with pen and paper before touching the code.
Follow the amplitudes through H, Z, H starting from |0⟩.
- H|0⟩ = (|0⟩ + |1⟩)/√2.
- Z flips the sign of |1⟩: (|0⟩ − |1⟩)/√2. The probabilities are still 50/50.
- Apply H to each part: H|0⟩ = (|0⟩ + |1⟩)/√2 and H|1⟩ = (|0⟩ − |1⟩)/√2.
- Combine: ½[(|0⟩ + |1⟩) − (|0⟩ − |1⟩)] = ½[2|1⟩] = |1⟩.
The two |0⟩ contributions cancel and the two |1⟩ contributions add. Without the Z, the signs are the other way round and the result is |0⟩.
4. Try it
Run three small circuits and compare them. Predict each result before you look. Write down what you expect before you run the code.
from qiskit import QuantumCircuit
from qiskit_aer import AerSimulator
sim = AerSimulator(seed_simulator=7)
def run(build, label):
qc = QuantumCircuit(1, 1)
build(qc)
qc.measure(0, 0)
print(label, sim.run(qc, shots=1000).result().get_counts())
run(lambda qc: qc.h(0), "H ") # about half 0, half 1
run(lambda qc: (qc.h(0), qc.h(0)), "H, H ") # always 0
run(lambda qc: (qc.h(0), qc.z(0), qc.h(0)), "H, Z, H ") # always 1
Output (counts will vary slightly from run to run):
H {'1': 486, '0': 514}
H, H {'0': 1000}
H, Z, H {'1': 1000}
What to notice
One H gives a random-looking result. Two H gates give 0 every time, so the randomness was not noise: it was a superposition that could be undone. Inserting Z, which changes no probability on its own, turns the answer into 1 every time.
5. Second experiment
Replace the Z in the middle with X and predict the result first. Predict first.
from qiskit import QuantumCircuit from qiskit_aer import AerSimulator qc = QuantumCircuit(1, 1) qc.h(0) qc.x(0) # X instead of Z in the middle qc.h(0) qc.measure(0, 0) print(AerSimulator(seed_simulator=7).run(qc, shots=1000).result().get_counts())
Output:
{'0': 1000}
What it shows
The answer is 0 every time. X swaps the two amplitudes of H|0⟩, but they are equal, so nothing changes and the second H returns the qubit to |0⟩. Equivalently, HXH = Z, and Z leaves |0⟩ alone.
6. Common mistakes and tips
Common mistakes
- Reading H as “makes the qubit random”. A random bit stays random after a second H; a superposition does not.
- Thinking more shots change the state. Shots only improve your estimate of the same probabilities.
- Assuming Z “does nothing” because it leaves the probabilities of |0⟩ and |1⟩ unchanged.
Tips
- When unsure, track amplitudes line by line as in the worked example. Three lines of algebra beat guessing.
- Sampling error shrinks like 1/√shots. To halve the error you need four times the shots.
- Useful identities: HH = I, HZH = X, HXH = Z.
7. Check yourself
Answer each question on paper, then open the answer to compare.
- If H made the qubit “randomly 0 or 1”, what would H followed by H give? Why is that not what happens?
Show answer
A random bit would still be random after the second H, giving about 50/50. The experiment gives 0 every time, so the state after one H was a definite superposition, not a random bit.
- What does X do between the two H gates instead of Z? Predict, then test.
Show answer
Always 0. See the second experiment: X swaps two equal amplitudes, which changes nothing.
- How many shots do you need before P(0) for a single H is reliably within 0.05 of 0.5?
Show answer
The standard error of P(0) is √(0.25/N). Two standard errors within 0.05 needs N of about 400. With 100 shots you will often miss by more than 0.05.
8. Do a project
Pick one. Each follows the routine from Guide 1: predict, run the baseline, change one variable, analyze.
← Session 1Session 3: Entanglement →
For instructors: Teacher notes for Session 2