OQCL   HomeStart Here7 SessionsTextbookProjectsQUEST LabSubmitUse CasesInstructor GuideReferences

Quantum in 7 Sessions › Session 5

1234567

Session 5 — Algorithms: search and phase estimation

Session 5 of 7Self-studySimulator only

By the end of this session you can explain Grover’s search as a sign flip followed by a reflection, run it on four states, and describe what phase estimation measures.

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

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⟩.

  1. After the first H gates, all four amplitudes are ½.
  2. Oracle: the marked amplitude becomes −½. The amplitudes are (½, ½, ½, −½).
  3. Average: (½ + ½ + ½ − ½)/4 = ¼.
  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

Tips

7. Check yourself

Answer each question on paper, then open the answer to compare.

  1. 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).

  2. 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.

  3. 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