Grover Search (Toy Example)
Track: Quantum Programming · Difficulty: Beginner · Est: 15 min
Grover Search (Toy Example)
Overview
This page demonstrates a small, concrete version of Grover’s algorithm:
- a very small search space (two qubits)
- an oracle that “marks” one answer
- one round of amplitude amplification
This is intentionally a toy. The goal is to see amplitude amplification in code and in measurement results, not to claim scalability.
Conceptual Mapping
From the Algorithms overview and Grover intuition:
- you start in a uniform superposition over candidates
- an oracle flips the phase of the “marked” answer
- a diffuser amplifies the marked answer’s probability
From Foundations:
- probabilities come from amplitudes
- gates shape amplitudes through interference
In code:
hgates create the uniform superposition- the oracle is a small subcircuit
- the diffuser is another subcircuit
- measurement shows whether probability mass moved toward the marked state
Code Walkthrough
We will search over 4 items (2 qubits) and mark |11⟩ as the target.
from qiskit import QuantumCircuit
from qiskit_aer import AerSimulator
qc = QuantumCircuit(2, 2)
# 1) Start in uniform superposition
qc.h(0)
qc.h(1)
# 2) Oracle: mark |11> by flipping its phase (CZ does that)
qc.cz(0, 1)
# 3) Diffuser (inversion about the mean) for 2 qubits
qc.h(0)
qc.h(1)
qc.x(0)
qc.x(1)
qc.cz(0, 1)
qc.x(0)
qc.x(1)
qc.h(0)
qc.h(1)
# 4) Measure
qc.measure(0, 0)
qc.measure(1, 1)
sim = AerSimulator()
counts = sim.run(qc, shots=1000).result().get_counts()
print(counts)Line by line (conceptual):
- The first two
hgates create an equal-weight superposition over00, 01, 10, 11. cz(0, 1)is the oracle here: it marks|11⟩by applying a phase flip only to that basis state.- The diffuser is a fixed pattern of gates that increases the marked state’s amplitude (for a small system, one iteration is enough to see the effect).
- Measurement shows where probability mass ended up.
Results & Interpretation
Without Grover steps (just h then measure), counts would be roughly uniform across four outcomes.
With one oracle + diffuser round, you should see the marked state 11 appear more often.
Interpretation:
- the oracle does not “tell you the answer” directly
- it changes the phase of the marked state
- the diffuser turns that phase information into amplitude change
- measurement frequencies reflect those changed amplitudes
Why this is a toy:
- real Grover setups require carefully defined oracles
- the number of iterations depends on problem size
- hardware noise can overwhelm amplitude amplification for larger circuits
The learning goal is: interference can concentrate probability on a desired answer.
Turtle Tip
An oracle is not a magic lookup. It’s a phase-marking rule. Grover works because the diffuser converts phase differences into probability differences.
Common Pitfalls
- Thinking the oracle must output the answer; in Grover it only marks candidates.
- Forgetting that Grover is probabilistic: you still measure and interpret distributions.
- Overgeneralizing toy results to large problems; scaling introduces deeper circuits and more sensitivity to noise.
- Mixing up which bitstring is marked due to measurement bit ordering.
Quick Check
- In this toy example, what does the oracle do to the marked state?
- What is the purpose of the diffuser?
- Why does the output appear as a distribution rather than a single deterministic value?
What’s Next
You’ve now run experiments that show core quantum behaviors in code: sampling, amplitudes vs probabilities, entanglement correlations, and amplitude amplification. Next we can start mini-projects that combine these ideas (for example, small algorithm demonstrations with careful result interpretation and sanity checks).
