Quantum in 7 Sessions › Session 5
Session 5 — Algorithms: search and phase estimation
Session 5 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 chapters first. The summary below is a reminder, not a replacement.
2. Key ideas
- An oracle marks the answer by flipping the sign of its amplitude. Probabilities do not change yet.
- The diffusion step reflects all amplitudes about their average. The marked amplitude grows and the others shrink.
- For four states, one round is enough to make the marked state certain. In general Grover needs about √N rounds, a quadratic saving in oracle calls for unstructured search. It does not “read all answers at once”.
- Phase estimation uses the same idea in a different way: a controlled operation writes the phase of an eigenvalue onto helper qubits, where it can be read out.
3. Worked example
Work through this with pen and paper before touching the code.
Grover on four states, by hand. Suppose the marked state is |11⟩.
- After the first H gates, all four amplitudes are ½.
- Oracle: the marked amplitude becomes −½. The amplitudes are (½, ½, ½, −½).
- Average: (½ + ½ + ½ − ½)/4 = ¼.
- Diffusion replaces each amplitude a by 2 × average − a. Unmarked: 2 × ¼ − ½ = 0. Marked: 2 × ¼ − (−½) = 1.
The final amplitudes are (0, 0, 0, 1), so the marked state is measured with certainty after one round.
4. Try it
Search four states for the marked one, |11⟩, with a single Grover round. Write down what you expect before you run the code.
from qiskit import QuantumCircuit from qiskit_aer import AerSimulator qc = QuantumCircuit(2, 2) qc.h([0, 1]) # equal superposition of 00, 01, 10, 11 qc.cz(0, 1) # oracle: flip the sign of the marked state |11> qc.h([0, 1]) # diffusion: reflect about the average amplitude qc.x([0, 1]) qc.cz(0, 1) qc.x([0, 1]) qc.h([0, 1]) qc.measure([0, 1], [0, 1]) print(AerSimulator(seed_simulator=7).run(qc, shots=1000).result().get_counts())
Output (counts will vary slightly from run to run):
{'11': 1000}
What to notice
All 1,000 shots return 11. After the first H gates every state had probability ¼. The oracle changed only a sign, and the diffusion step converted that hidden sign into a certain answer. This is the interference from Session 2, used deliberately.
5. Second experiment
Apply the Grover round twice instead of once. Predict first.
from qiskit import QuantumCircuit
from qiskit_aer import AerSimulator
def grover_round(qc):
qc.cz(0, 1) # oracle for |11>
qc.h([0, 1]); qc.x([0, 1])
qc.cz(0, 1) # diffusion
qc.x([0, 1]); qc.h([0, 1])
qc = QuantumCircuit(2, 2)
qc.h([0, 1])
grover_round(qc)
grover_round(qc) # one round too many
qc.measure([0, 1], [0, 1])
print(AerSimulator(seed_simulator=7).run(qc, shots=1000).result().get_counts())
Output:
{'01': 267, '11': 244, '00': 260, '10': 229}
What it shows
All four outcomes now appear about equally: the advantage is gone. The second round rotated the state past the answer. More rounds are not better; there is a correct number, and for four states it is one.
6. Common mistakes and tips
Common mistakes
- Saying Grover “tries all answers at once”. It rotates amplitude toward the answer step by step.
- Running more rounds to be safe. Too many rounds rotate the state past the answer and the success probability falls.
- Forgetting that the oracle changes only a sign. On its own it changes no probability.
Tips
- For N items use about (π/4)√N rounds. For N = 4 that is one.
- Check your oracle separately: prepare the marked state directly and confirm that only its sign flips.
- Do the amplitude arithmetic for N = 4 by hand once. It makes every larger case easier to believe.
7. Check yourself
Answer each question on paper, then open the answer to compare.
- Change the oracle so that it marks |01⟩ instead (hint: an X gate on one qubit before and after the CZ). Predict the result.
Show answer
Put an X on one qubit before and after the oracle’s CZ. With the X on qubit 1 the output is 01; with the X on qubit 0 it is 10 (remember Qiskit prints qubit 0 on the right).
- What happens if you apply the Grover round twice on four states? Predict, then test.
Show answer
The success probability drops back to about 25%, as the second experiment shows.
- Why does the oracle alone not help, even though it “knows” the answer?
Show answer
The oracle only flips a sign, which leaves all four probabilities at ¼. Interference in the diffusion step is what turns the sign into a measurable difference.
8. Do a project
Pick one. Each follows the routine from Guide 1: predict, run the baseline, change one variable, analyze.
← Session 4Session 6: Near-term methods: Hamiltonians and variational circuits →
For instructors: Teacher notes for Session 5