Lesson 16: Grover search
Finding a marked item among N in about √N steps, as a rotation
The interactive version checks every answer, gives hints and saves your progress in this browser. This page is the same lesson on one page, with the answers folded away.
1 / 14 · Learn
Before you start
Lesson 14 counted queries, the calls an algorithm makes to an oracle, and used phase kickback (Lesson 9) to turn an oracle’s answer into a sign. This lesson puts both to work on search: a quantum computer can find one marked item among with about queries, where a classical search needs about .
First the two steps, the oracle and the diffusion step, as operations on amplitudes. Then the same steps as two mirrors in a plane, which tells you how many iterations to run. Last, Grover circuits in the editor and in Qiskit.
By the end of this lesson you can:
- apply the phase oracle and the diffusion step to a list of amplitudes, and explain the diffusion step as an inversion about the mean;
- build the diffusion step from , and a multi-controlled , and say why its extra minus sign doesn’t matter;
- draw one Grover iteration as two reflections in the plane of and , and compute the success chance after iterations;
- choose the number of iterations, and explain why more is worse;
- compare Grover search with classical search, and run it in Qiskit.
Check yourself
A warm-up: make a guess before reading on. The lesson comes back to it.
Four items, to , and only is marked. Grover search starts with all four in equal superposition, applies the oracle once and the diffusion step once, then measures both qubits. What is the chance that it finds ?
- A: no better than a random guess
- B
- C
- D: it finds every time
Show the answer
Answer: D. : it finds every time
After the oracle the amplitudes are on , , and on . Their mean is . The diffusion step reflects each amplitude about the mean, : gets and each other item . One query, and the answer is certain.
Why not the others
- A. The oracle alone would change no probability: it only flips the sign of . The diffusion step turns that sign into probability. After the oracle the amplitudes are three s and one , with mean ; reflecting each about the mean gives for and for the rest.
- B. One iteration doesn’t add a fixed share of probability. Work out the amplitudes: after the oracle they are , , and on , with mean . The diffusion step sends each to : 1 for , 0 for the others.
- C. It is natural to expect a gradual climb that reaches certainty only after many steps. For four items one step is already exact: the diffusion step sends every amplitude to , which gives amplitude 1 and the other three amplitude 0.
Check yourself
A warm-up: make a guess before reading on. The lesson comes back to it.
Grover search over items with one marked item. After 2 iterations a measurement finds the marked item with probability 94.5%, higher than after any smaller number of iterations. What happens to that chance if you run one more iteration before measuring?
- AIt rises, closer to 100%
- BIt stays at about 94.5%: the state has settled
- CIt falls, to about 33%
- DIt falls all the way back to , where it started
Show the answer
Answer: C. It falls, to about 33%
gives . After 2 iterations the state is at from , near . One more iteration turns it to , well past , and the chance drops to 33%.
Why not the others
- A. It seems that more of a good thing must help. But every iteration turns the state by the same angle, , in a plane. After 2 iterations it is close to the marked direction, and one more turn carries it past: the chance falls to 33%.
- B. Nothing settles. Every iteration is the same rotation by , applied again, so the state keeps turning, past the marked direction, and the chance falls to 33%.
- D. One more iteration turns the state only further, not all the way back. The chance falls to 33%, still well above .
2 / 14 · Learn
The search problem
There are items, labelled by -bit strings , and a function with for the marked items and for the rest. Nothing else is known: the items are in no useful order, and is an oracle that you can only query. This is unstructured search.
With one marked item, a classical search checks items one at a time, and each check rules out a single item. On average it needs about checks. In the worst case it needs : if the first all fail, the last item must be the one.
The setting covers more than lists. Any problem whose answers are easy to check but hard to find, such as a key that decrypts a message, is a search of this kind, with the checking done by a circuit (Lesson 18 builds one).
Check yourself
A list of 64 items, in no particular order, has exactly one marked item. You can check one item at a time to see whether it is the marked one. How many checks might you need, in the worst case, to be certain which item is marked?
Hints
- What does one check rule out?
- Think about the unluckiest order: every check fails until few items are left. When can you stop?
Show the answer
Answer:
Each check rules out one item. In the worst case the first 63 all fail, and then the last item must be the marked one, so checks always suffice and fewer might not. On average you need about .
Common slips
- : You never need to check the last item. Exactly one is marked, so if the first 63 checks all say no, the last item must be it.
- : About is the average: often the marked item turns up early. The worst case is that the first 63 checks all fail.
- : That would be a binary search, which needs a sorted list and questions like “is it in the first half?”. Here a check only answers “is this the one?”, so each check rules out a single item.
The quantum version of a check is a phase oracle. With the target qubit in , the oracle kicks back a sign, as in Lessons 9 and 14. Leaving the target out of the notation,
flips the sign of every marked item and leaves the others alone. Grover search starts in the equal superposition
so a single query acts on every item at once. The catch: a sign is invisible to a measurement, because . A second step has to turn the sign into probability.
Check yourself
Four items, and the oracle marks . Enter as four amplitudes, to .
Hints
- gives every item amplitude .
- multiplies by : only has .
Show the answer
Answer:
Every amplitude of is . The oracle multiplies the marked one, , by and leaves the other three alone.
Check yourself
Right after that oracle call, before any other step, you measure both qubits. What is the chance of the result , the marked item?
Hint
- The probability is the squared magnitude of the amplitude.
Show the answer
Answer:
The amplitude of is , and : the same as before the oracle. The oracle has marked the item, but no measurement can see the mark yet.
Common slips
- : The oracle marks with a sign, but a sign changes no probability: , the same as every other item.
- : A negative amplitude still has a positive probability: .
3 / 14 · Learn
Inversion about the mean
The second step is the diffusion step,
Its effect is easiest to see on amplitudes. For a state , the overlap is , where is the mean amplitude, and so
Every amplitude is reflected about the mean, which is why is also called inversion about the mean. An amplitude a little above the mean ends a little below it; one far below ends far above. After the oracle, the marked amplitude is the one far below: it is negative and the rest are positive. Reflecting about the mean throws it high above the others.
At the start
After the oracle
After the diffusion
Eight items, one marked (amber). The oracle flips the marked amplitude to and lowers the mean (dashed) to . The diffusion step mirrors every bar in that line: the marked one, below the mean, ends above it, at .
Check yourself
Four items with marked. After the oracle the amplitudes are . What is the amplitude of after the diffusion step?
Hints
- Find the mean of the four amplitudes first.
- The diffusion step sends to .
Show the answer
Answer:
The mean is . The diffusion step sends to , and each to . The state is now exactly .
Common slips
- : That leaves the amplitude where it was. moves every amplitude to , and is far below the mean.
- : That only undoes the oracle’s sign flip, a reflection about zero. reflects about the mean, : .
- : That is the mean of the four amplitudes. reflects each amplitude about it: .
- : That is the new amplitude of each unmarked item, . The marked one starts below the mean, so it ends above: .
To build as a circuit, start with a sign flip on one basis state. A controlled-, written , flips the sign of and nothing else; with more controls (, and so on) a multi-controlled flips the sign of only. Put on every qubit before and after, and the flip moves to :
Hadamards on both sides turn into , which gives . The extra multiplies the whole state, a global phase, so no measurement can tell this circuit from .
Check yourself
Which circuit on 3 qubits applies the diffusion step , up to a global phase?
- A on every qubit, on every qubit, a , on every qubit, on every qubit
- B on every qubit
- C on every qubit, a , on every qubit
- D on every qubit, a , on every qubit
Show the answer
Answer: A. on every qubit, on every qubit, a , on every qubit, on every qubit
on every qubit, a , and again flip the sign of only: that is . Hadamards on both sides turn into , which gives . The overall is a global phase, so no measurement can tell this circuit from .
Why not the others
- B. A layer of Hadamards is not a reflection about : it sends to , while leaves alone. The Hadamards only change basis. The reflection itself is a sign flip on , made between two Hadamard layers.
- C. A flips the sign of , so this reflects about , a state full of minus signs, not . The layers move the flip onto , which the Hadamards then turn into .
- D. That flips the sign of alone, so its mirror passes through , not . Hadamards on both sides move the mirror to .
4 / 14 · Explore
Step by step
Check yourself
In the lab, with eight items and one marked, press Start over, then press One iteration (O, then D) several times, reading the chance each time. It rises, then falls. After how many iterations does it reach its first peak?
Hint
- Press Start over, then One iteration (O, then D), and note the chance after each press.
Show the answer
Answer:
The chance goes 12.5%, 78.1%, 94.5%, then down to 33%: the first peak is at two iterations. It climbs again later, to 99.98% after six, but that costs three times as many queries. A classical search of eight items needs about four checks on average.
Common slips
- : Look again at the third iteration: the chance falls from 94.5% to 33%. More iterations don’t always help.
- : One iteration gives 78.1%, but a second raises it to 94.5%.
- : Four is , the classical average. In the lab the chance peaks at 94.5% after two iterations and has fallen to about 1% by the fourth.
Check yourself
Start over, press Apply oracle O and watch the mean line, then press Apply diffusion D and watch it again. What does the diffusion step do to the mean amplitude?
- ANothing: the mean stays where it is
- BIt raises the mean, since the marked bar shoots up
- CIt lowers the mean, since most of the bars get shorter
- DIt sets the mean to zero
Show the answer
Answer: A. Nothing: the mean stays where it is
Each amplitude goes to , so the new mean is . The oracle is the step that moves the mean: it lowers it, by flipping one positive amplitude to negative.
Why not the others
- B. The marked bar rises a lot, but the seven others each fall a little, and the mean line doesn’t move. Each bar ends as far from the mean as it started, on the other side, so the gains and losses cancel.
- C. The seven unmarked bars do get shorter, but the marked bar rises by just as much in total. Reflecting every bar in the mean line leaves the line where it was.
- D. Reflecting about the mean doesn’t move the mean anywhere: each amplitude goes to , and the average of those is .
5 / 14 · Learn
Two mirrors make a rotation
The bars hide a simpler picture. Say of the items are marked, and define two states: , the equal superposition of the marked items, and , the equal superposition of the other . They are orthogonal, and
with , the part of . So is a unit vector in the plane of (across) and (up), at the angle above . Neither nor ever takes the state out of this plane: all marked items keep one shared amplitude, as do all unmarked ones. A state at angle finds a marked item with chance .
Check yourself
Grover search over items with one marked item. What is the angle between and , in degrees?
Hints
- . What is the part?
- The part is , so .
Show the answer
Answer:
, so .
Common slips
- : That uses . The part of is an amplitude, , so .
- : That is in radians (or , which is close to it when is small). The question asks for degrees: .
- : That is , the turn made by one iteration. is the angle between and .
- : That is the angle between and . is measured from , and is the part of .
Now the two steps. The oracle flips the sign of the part and keeps the part: in the plane it is the mirror in . The diffusion step keeps and flips everything orthogonal to it: it is the mirror in .
Two mirrors in a row make a rotation by twice the angle between them. These mirrors meet at , so one iteration, , turns the state by toward . From , at , iterations reach :
The chance of a marked item after iterations is .
The oracle step
The diffusion step
One iteration
Eight items, one marked, so . The oracle mirrors the state in , from to . The diffusion step mirrors it in the line of , from to . Together: a turn of .
Check yourself
Grover search over items with one marked item. What is the chance that a measurement finds the marked item after one iteration? Give a decimal.
Hints
- Find the start angle from .
- Each iteration turns the state by , so after iterations it is at from . The chance is of that angle.
- Here , so the angle is .
Show the answer
Answer:
gives . After one iteration the state is at from , so the chance is .
Common slips
- : That is , the marked item’s amplitude. The chance is its square, .
- : That puts the state at . It starts at , not at 0, so after one iteration it is at .
- : That is the small-angle estimate with . At the angle is too large for it: take itself.
6 / 14 · Explore
Watching the turn
Predict, then see
Two qubits, and three of the four items are marked: , and . Before any step, a measurement finds a marked item 75% of the time. What does one Grover iteration do to that chance?
- AIt drops to 0%
- BIt rises to 100%
- CIt rises a little, to about 90%
- DIt stays at 75%: with so many marked items the steps do nothing
Why did the chance fall to zero? Use the angle picture, the amplitudes and their mean, or both.
Show what happens
What happens: A. It drops to 0%
With and , , so : is already closer to than to . One iteration turns it by , to , which is : the unmarked item, with certainty. In amplitudes: after the oracle they are , with mean , and reflecting about the mean gives on and 0 on the rest. The circuit applies , so it shows .
Why not the others
- B. With one marked item among four, one iteration was perfect, so it seems more marked items can only help. But the circuit ends in , the one unmarked item.
- C. The circuit shows the chance falling, not rising: at the end every amplitude is 0 except the one on , the unmarked item.
- D. The steps still act. The oracle makes three of the four amplitudes negative, and the diffusion step then moves all the amplitude onto .
What if half the items or more are marked?
Then , and measuring already finds a marked item at least half the time. One iteration doesn’t help there: it lowers the chance (at exactly half, it leaves it at ). So skip the iterations: measure , which picks an item at random, and check the result with one more query.
Check yourself
In the lab, mark two of the eight items. Now one iteration finds a marked item with certainty. Why?
- AWith two marked items, every number of iterations gives certainty
- BTwice as many marked items halve the number of iterations: two become one
- C, as for one marked item among four, so and one iteration reaches
- DThe oracle now flips two signs, so each iteration does twice the work
Show the answer
Answer: C. , as for one marked item among four, so and one iteration reaches
gives . One iteration turns by , to : exactly . A second iteration would overshoot to , with chance .
Why not the others
- A. Press One iteration (O, then D) again: the chance falls to 25%. The state turns past to , and .
- B. The count shrinks like , not : twice as many marked items need about as many iterations. What decides it here is the angle: , so and one iteration lands on exactly .
- D. Twice the work per step would double the gain, and with one marked item one iteration gave 78.1%: double that is more than 100%. The real reason is the angle: , so and one turn of reaches .
7 / 14 · Learn
When to stop
You want as close to as possible, so solve and round:
The best whole leaves the state within of , so the chance is at least . When is much smaller than , (in radians), and the rule becomes
For one marked item among , about a million, that is 804 iterations, each with one query. A classical search needs about 524,288 checks on average.
One marked item among eight, so the chance after iterations is with . It climbs from 12.5% to 78.1% after one iteration and 94.5% after two (the first peak, in amber), then falls to 33% after three. It keeps rising and falling as the state goes round the circle.
More iterations are not better. The state keeps turning by , so after the first peak it moves past and the chance falls again, all the way to nearly zero, before rising once more. Grover search is a rotation, not a climb: you must know roughly how many marked items there are, compute , and stop. Lesson 17 shows what to do when you don’t know .
Check yourself
Grover search over items with one marked item. How many iterations bring the chance of finding a marked item to its first peak?
Hints
- Find from , in radians.
- You want as close as possible to : , rounded to a whole number.
- Here radians, so .
Show the answer
Answer:
gives radians, about . Then , which rounds to 3. Check: , close to , and the chance is 96.1%.
Common slips
- : That is , the average for a classical search. Grover search needs only about iterations.
- : One more than the best overshoots. After 4 iterations the state is at , further from than , and the chance drops from 96.1% to 58.2%.
- : One fewer stops short: after 2 iterations the state is at , and the chance is 90.8%, against 96.1% after 3.
8 / 14 · Worked example
Worked example: how many iterations?
The angle picture turns “how many iterations?” into a few lines of arithmetic. Use radians in the formula for and degrees for the picture.
Worked example
One marked item among
Find the start angle, the best number of iterations and the chance of success. Then do the same for .
Start angle
, so radians, about . For a small angle , and indeed .
Best count
, which rounds to . The shortcut gives the same whole number.
Chance
After 6 iterations the state is at from , so the chance is . For comparison, 5 iterations reach (chance 0.9635) and 7 reach (0.9074). The best stops a little past , and both neighbours are worse.
Your turn: start angle
Now one marked item among .
Your turn
What is for one marked item among ?
Hint
- is the part of , the amplitude .
Show the answer
Answer:
, so radians, about .
Common slips
- : That is , the chance of the marked item in . is its amplitude, the square root: .
- : is at most 1. That is ; turn it over: .
Your turn: best count
Your turn
With radians, how many iterations bring the chance to its first peak?
Hint
- Compute and round.
Show the answer
Answer:
, which rounds to 25. Then and the chance is about 0.9995. A list 16 times longer than 64 needed about 4 times as many iterations: 25 against 6.
Common slips
- : 32 is , without the factor . The state must turn through about in steps of : . After 32 iterations it is at , well past , with chance 0.80.
- : That is , the classical average. Grover search needs about iterations.
- : Close, but rounds up. 25 iterations reach (chance 0.9995); 24 stop at (0.9985).
One more, fully worked: a list 64 times longer than the first.
Another worked example
Another example:
Start angle
, so radians, about .
Best count
, which rounds to 50. The shortcut gives : also 50.
Chance
, so the chance is . A list 64 times longer than needed about 8 times as many iterations, 50 against 6: the square root at work.
Check yourself
Grover search over items with one marked item. How many iterations bring the chance of finding a marked item to its first peak?
Hints
- Find from , in radians.
- You want as close as possible to : , rounded to a whole number.
- Here radians, so .
Show the answer
Answer:
gives radians, about . Then , which rounds to 25. Check: , close to , and the chance is 99.9%.
Common slips
- : That is , the average for a classical search. Grover search needs only about iterations.
- : One more than the best overshoots. After 26 iterations the state is at , further from than , and the chance drops from 99.9% to 99.3%.
- : is how the count grows, but the constant matters. The state must turn through about , in steps of , which takes about iterations. 32 iterations give only 80.2%.
- : One fewer stops short: after 24 iterations the state is at , and the chance is 99.8%, against 99.9% after 25.
9 / 14 · Learn
How big is the speedup?
Grover search needs about queries where a classical search needs about . With that is about against : the square root halves the exponent. That is a quadratic speedup. Both counts still grow exponentially with the number of bits , so Grover search doesn’t make an exponential search easy; it makes it less hard.
It also can’t be beaten. It is proved that any quantum algorithm for unstructured search needs on the order of queries, so Grover search is optimal up to a constant factor. The exponential gaps of Lesson 15 come from structure in the problem, a promise about . Unstructured search has none.
Queries against the number of bits , on a log scale. Classical search grows like , Grover search like : both are straight lines, one half as steep. At : 524,288 against 804.
Check yourself
A secret key has 128 bits, so there are possible keys, and an oracle recognises the right one. About how many oracle queries does Grover search need to find the key?
- AOne: a single query tests every key at once
- BAbout (more exactly )
- CAbout , half the keys
- DAbout 128, one query per bit of the key
Show the answer
Answer: B. About (more exactly )
With one marked item among , Grover search needs about queries, against about for a classical search. The square root halves the exponent: a quadratic speedup, not an exponential one.
Why not the others
- A. One query does act on every key in superposition, but all it does is flip the sign of the right one, which no measurement can see. Turning that sign into a high chance takes about rounds of oracle and diffusion.
- C. That is the classical average: test keys one at a time and you expect to go through half of them. Grover search needs about the square root of , which is .
- D. That would be an exponential speedup, and Grover search doesn’t give one. Each iteration turns the state by about radians, so it takes about queries: the square root of , not its logarithm.
10 / 14 · Learn
Grover circuits and code
With two qubits the whole algorithm is small. To mark the oracle is a single . The diffusion step is on both, on both, a , on both, on both. Since and one item is marked, one iteration is exact. In the editor a control dot and a in the same column make a .
Check yourself
The first two columns make and apply the oracle for . Add the diffusion step in the five empty columns, so that the circuit ends in (up to a global phase).
Build it in the circuit builder (gates: H, X, Z, control).
Hints
- The diffusion step is , , a sign flip on , , , each on both qubits.
- Column 3: on both. Column 4: on both. Column 5: a dot on one qubit and on the other.
Show the answer
One circuit that works: H on q0 and q1, then CZ q0→q1, then H on q0 and q1, then X on q0 and q1, then CZ q0→q1, then X on q0 and q1, then H on q0 and q1.
After the oracle the state is . , , , , applies , which reflects every amplitude about the mean and adds a global : the result is , and a measurement gives every time.
In Qiskit the multi-controlled on three qubits is qc.ccz(0, 1, 2). For more qubits, qc.mcp(np.pi, controls, target) applies a phase of , that is a factor , when every control and the target are 1, which is a multi-controlled . An oracle for one marked string puts on each qubit whose bit is 0, applies the multi-controlled , and undoes the s.
When you read the counts, remember Qiskit’s bit order from Lesson 12: qubit 0 is the rightmost bit, so the marked item prints as 1101.
Check yourself
A Qiskit program runs Grover search on 4 qubits with the marked item (in lesson order, qubit 0 first). It applies 3 iterations and measures every qubit, with 1,000 shots. Which bitstring tops the printed counts, with about 961 of them?
- ANo string stands out: all 16 appear about equally often
- B
1000 - C
0111 - D
1110
Show the answer
Answer: D. 1110
3 iterations put probability 96.1% on . Qiskit writes qubit 0 as the rightmost bit, so it prints that result as 1110, about 961 times in 1,000 shots.
Why not the others
- A. The oracle and diffusion steps don’t just spread amplitude around. After 3 iterations the marked item has probability 96.1%, so about 961 of the 1,000 shots give it.
- B. That flips every bit, as if the gates in the oracle changed the answer. They are undone right after the multi-controlled ; they only move the sign flip onto . The result is , which Qiskit prints as
1110. - C. That reads Qiskit’s string as a ket. Qiskit prints qubit 0 last, so shows up as
1110: the same bits in reverse order.
Check yourself
Three qubits, marked item , and the oracle is written for you. Fill in diffusion and apply two iterations, each the oracle and then the diffusion step. Two iterations are the best for eight items, so the marked item should turn up in about 94.5% of the shots.
from qiskit import QuantumCircuit
def oracle(qc):
# flip the sign of |110> (lesson order: qubit 0 first)
qc.x(2)
qc.ccz(0, 1, 2)
qc.x(2)
def diffusion(qc):
# reflect about the mean: H, X, CCZ, X, H on all three qubits
pass
qc = QuantumCircuit(3)
qc.h(range(3))
# apply two Grover iterations here
qc.measure_all()Write the program in the app’s code editor (Python with Qiskit’s circuit methods), run it, and check it.
Hints
- The diffusion step is , , , , , each layer on all three qubits:
qc.h(range(3)),qc.x(range(3)),qc.ccz(0, 1, 2). - For two iterations, loop twice:
for step in range(2):withoracle(qc)anddiffusion(qc)inside.
Show the answer
One program that works:
from qiskit import QuantumCircuit
def oracle(qc):
# flip the sign of |110> (lesson order: qubit 0 first)
qc.x(2)
qc.ccz(0, 1, 2)
qc.x(2)
def diffusion(qc):
qc.h(range(3))
qc.x(range(3))
qc.ccz(0, 1, 2)
qc.x(range(3))
qc.h(range(3))
qc = QuantumCircuit(3)
qc.h(range(3))
for step in range(2):
oracle(qc)
diffusion(qc)
qc.measure_all()Two iterations put the state at , with chance for the marked item. Qiskit prints as 011, so 011 should get about 945 of the 1,000 shots, and each other string about 8.
11 / 14 · Practice
Practice
Three items, each with new numbers when it comes back. Hints are there if you need them.
Check yourself
Grover search over items, one of them marked. Starting from , apply the oracle once and then the diffusion step once. What is the amplitude of each unmarked item now? A decimal to three places is enough.
Hints
- Write down the amplitudes after the oracle, and find their mean.
- The diffusion step sends every amplitude to , with the mean after the oracle.
- Here every amplitude starts at , and after the oracle the mean is .
Show the answer
Answer:
Every amplitude starts at . The oracle makes the marked one , so the mean drops to . Reflecting about it gives for each unmarked item.
Common slips
- : That leaves the unmarked amplitudes where they were. moves every amplitude: each sits a little above the mean , so it ends a little below it.
- : That is the mean after the oracle. reflects each amplitude about it, : .
- : That is the marked item’s new amplitude. An unmarked item starts above the mean, at , so it is reflected to just below it: .
Check yourself
Grover search over items with one marked item. What is the chance that a measurement finds the marked item after one iteration? Give a decimal.
Hints
- Find the start angle from .
- Each iteration turns the state by , so after iterations it is at from . The chance is of that angle.
- Here , so the angle is .
Show the answer
Answer:
gives . After one iteration the state is at from , so the chance is .
Common slips
- : That is , the marked item’s amplitude. The chance is its square, .
- : That puts the state at . It starts at , not at 0, so after one iteration it is at .
Check yourself
Grover search over items with one marked item. How many iterations bring the chance of finding a marked item to its first peak?
Hints
- Find from , in radians.
- You want as close as possible to : , rounded to a whole number.
- Here radians, so .
Show the answer
Answer:
gives radians, about . Then , which rounds to 8. Check: , close to , and the chance is 99.6%.
Common slips
- : That is , the average for a classical search. Grover search needs only about iterations.
- : The shortcut is a good estimate, but here it rounds to the wrong side. The exact rule rounds , which gives 8: chance 99.6%, against 98.8% for 9.
- : is how the count grows, but the constant matters. The state must turn through about , in steps of , which takes about iterations. 11 iterations give only 79.9%.
- : One fewer stops short: after 7 iterations the state is at , and the chance is 94.2%, against 99.6% after 8.
12 / 14 · Explain
Explain it
Explain it
Why does Grover search need about iterations to find one marked item among , and why does running many more than that make things worse?
Show a model answer
The state never leaves the plane of and . It starts as , at the angle from with , so radians. Each iteration is two reflections, in (the oracle) and in (the diffusion step), and two reflections make a rotation by twice the angle between the mirrors, . To reach the state must turn through about , which takes about iterations. The rotation doesn’t stop at : further iterations carry the state past it, and the chance falls again.
A good answer covers
- The state starts at a small angle from , with , so
- Each iteration (two reflections) turns the state by the same angle,
- Turning through about therefore takes about iterations
- More iterations keep turning the state, past , so the chance falls again
13 / 14 · Mastery check
Mastery check
Check yourself
Four items, to , and only is marked. Grover search starts with all four in equal superposition, applies the oracle once and the diffusion step once, then measures both qubits. What is the chance that it finds ?
- A: no better than a random guess
- B
- C
- D: it finds every time
Show the answer
Answer: D. : it finds every time
After the oracle the amplitudes are on , , and on . Their mean is . The diffusion step reflects each amplitude about the mean, : gets and each other item . One query, and the answer is certain.
Why not the others
- A. The oracle alone would change no probability: it only flips the sign of . The diffusion step turns that sign into probability. After the oracle the amplitudes are three s and one , with mean ; reflecting each about the mean gives for and for the rest.
- B. One iteration doesn’t add a fixed share of probability. Work out the amplitudes: after the oracle they are , , and on , with mean . The diffusion step sends each to : 1 for , 0 for the others.
- C. It is natural to expect a gradual climb that reaches certainty only after many steps. For four items one step is already exact: the diffusion step sends every amplitude to , which gives amplitude 1 and the other three amplitude 0.
Check yourself
Which circuit on 2 qubits applies the diffusion step , up to a global phase?
- A on every qubit, a , on every qubit
- B on every qubit
- C on every qubit, a , on every qubit
- D on every qubit, on every qubit, a , on every qubit, on every qubit
Show the answer
Answer: D. on every qubit, on every qubit, a , on every qubit, on every qubit
on every qubit, a , and again flip the sign of only: that is . Hadamards on both sides turn into , which gives . The overall is a global phase, so no measurement can tell this circuit from .
Why not the others
- A. That flips the sign of alone, so its mirror passes through , not . Hadamards on both sides move the mirror to .
- B. A layer of Hadamards is not a reflection about : it sends to , while leaves alone. The Hadamards only change basis. The reflection itself is a sign flip on , made between two Hadamard layers.
- C. A flips the sign of , so this reflects about , a state full of minus signs, not . The layers move the flip onto , which the Hadamards then turn into .
Check yourself
Grover search over items with one marked item. What is the chance that a measurement finds the marked item after three iterations? Give a decimal.
Hints
- Find the start angle from .
- Each iteration turns the state by , so after iterations it is at from . The chance is of that angle.
- Here , so the angle is .
Show the answer
Answer:
gives . After three iterations the state is at from , so the chance is .
Common slips
- : That is , the marked item’s amplitude. The chance is its square, .
- : That puts the state at . It starts at , not at 0, so after three iterations it is at .
- : That turns the state by per iteration. One iteration is two reflections, and two reflections turn by twice the angle between the mirrors: .
Check yourself
Grover search over items with one marked item. After 4 iterations a measurement finds the marked item with probability 99.9%, higher than after any smaller number of iterations. What happens to that chance if you run one more iteration before measuring?
- AIt rises, closer to 100%
- BIt stays at about 99.9%: the state has settled
- CIt falls, to about 86%
- DIt falls all the way back to , where it started
Show the answer
Answer: C. It falls, to about 86%
gives . After 4 iterations the state is at from , near . One more iteration turns it to , well past , and the chance drops to 86%.
Why not the others
- A. It seems that more of a good thing must help. But every iteration turns the state by the same angle, , in a plane. After 4 iterations it is close to the marked direction, and one more turn carries it past: the chance falls to 86%.
- B. Nothing settles. Every iteration is the same rotation by , applied again, so the state keeps turning, past the marked direction, and the chance falls to 86%.
- D. One more iteration turns the state only further, not all the way back. The chance falls to 86%, still well above .
Check yourself
Grover search over items with 2 marked items. How many iterations bring the chance of finding a marked item to its first peak?
Hints
- Find from , in radians.
- You want as close as possible to : , rounded to a whole number.
- Here radians, so .
Show the answer
Answer:
gives radians, about . Then , which rounds to 12. Check: , close to , and the chance is 100%.
Common slips
- : That is the count for one marked item, . With 2 marked items the start angle is larger, , so fewer iterations reach .
- : The shortcut is a good estimate, but here it rounds to the wrong side. The exact rule rounds , which gives 12: chance 100%, against 98.6% for 13.
- : is how the count grows, but the constant matters. The state must turn through about , in steps of , which takes about iterations. 16 iterations give only 77.6%.
- : One fewer stops short: after 11 iterations the state is at , and the chance is 98.3%, against 100% after 12.
Check yourself
A secret key has 64 bits, so there are possible keys, and an oracle recognises the right one. About how many oracle queries does Grover search need to find the key?
- AAbout 64, one query per bit of the key
- BAbout , half the keys
- COne: a single query tests every key at once
- DAbout (more exactly )
Show the answer
Answer: D. About (more exactly )
With one marked item among , Grover search needs about queries, against about for a classical search. The square root halves the exponent: a quadratic speedup, not an exponential one.
Why not the others
- A. That would be an exponential speedup, and Grover search doesn’t give one. Each iteration turns the state by about radians, so it takes about queries: the square root of , not its logarithm.
- B. That is the classical average: test keys one at a time and you expect to go through half of them. Grover search needs about the square root of , which is .
- C. One query does act on every key in superposition, but all it does is flip the sign of the right one, which no measurement can see. Turning that sign into a high chance takes about rounds of oracle and diffusion.
Check yourself
A Qiskit program runs Grover search on 4 qubits with the marked item (in lesson order, qubit 0 first). It applies 3 iterations and measures every qubit, with 1,000 shots. Which bitstring tops the printed counts, with about 961 of them?
- A
1000 - B
0001 - CNo string stands out: all 16 appear about equally often
- D
0111
Show the answer
Answer: B. 0001
3 iterations put probability 96.1% on . Qiskit writes qubit 0 as the rightmost bit, so it prints that result as 0001, about 961 times in 1,000 shots.
Why not the others
- A. That reads Qiskit’s string as a ket. Qiskit prints qubit 0 last, so shows up as
0001: the same bits in reverse order. - C. The oracle and diffusion steps don’t just spread amplitude around. After 3 iterations the marked item has probability 96.1%, so about 961 of the 1,000 shots give it.
- D. That flips every bit, as if the gates in the oracle changed the answer. They are undone right after the multi-controlled ; they only move the sign flip onto . The result is , which Qiskit prints as
0001.
14 / 14 · Recap
Recap
From memory
Before reading the summary, write down what you remember. Pulling it back from memory is what makes it last.
What do the oracle and the diffusion step do to the amplitudes? What does one iteration do for four items with one marked?
Show the key points
- The oracle flips the sign of every marked amplitude; on its own it changes no probability
- The diffusion step reflects every amplitude about the mean:
- For four items with one marked, one iteration gives the marked item amplitude 1
Draw one iteration in the plane of and . Where does the state start, and what do the two steps do?
Show the key points
- starts at the angle from , with
- The oracle mirrors the state in and the diffusion step in ; together they turn it by
- After iterations the chance of a marked item is
How many iterations should you run, what goes wrong with more, and how big is the speedup?
Show the key points
- The that puts nearest , about
- More iterations turn the state past , and the chance falls
- About queries against about : a quadratic speedup, not an exponential one, and the best possible
Key ideas
Unstructured search: with one marked item among , a classical search needs about checks on average and in the worst case.
The phase oracle flips the sign of every marked item. On its own it changes no probability.
The diffusion step reflects every amplitude about the mean. The circuit , , multi-controlled , , gives , which is up to a global phase.
In the plane of and , sits at the angle from , with . The two steps are mirrors; one iteration turns the state by , and iterations succeed with chance .
Run about iterations and stop, since more turn the state past . The gain over classical search is quadratic, not exponential, and no quantum algorithm does better.
Go further
Free resources for when you want more than this lesson covers.
- Fundamentals of quantum algorithms: Grover’s algorithmJohn Watrous, IBM Quantum · Course
A video and written lesson that analyses the algorithm with the same two reflections and works out its success chance exactly.
- Grover’s algorithm (tutorial)IBM Quantum · Interactive
A Qiskit tutorial that builds the oracle and the Grover operator and then runs the search on IBM quantum hardware.
- A fast quantum mechanical algorithm for database searchLov K. Grover · Article
The 1996 paper that introduced the algorithm, short and readable once you know inversion about the mean.
- Grover’s algorithmWikipedia · Article
The algorithm, its geometric proof, its applications and the proof that no quantum search can be faster.