Skip to the content
Kets & Gates homeOpen the app

Lesson 16: Grover search

Finding a marked item among N in about √N steps, as a rotation

about 45 min14 partsbest after lesson 14

Start the interactive lesson

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 NN with about N\sqrt N queries, where a classical search needs about N/2N/2.

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 HH, XX and a multi-controlled ZZ, and say why its extra minus sign doesn’t matter;
  • draw one Grover iteration as two reflections in the plane of ∣good⟩\ket{\text{good}} and ∣bad⟩\ket{\text{bad}}, and compute the success chance after kk 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, ∣00⟩\ket{00} to ∣11⟩\ket{11}, and only ∣10⟩\ket{10} 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 1010?

  1. A14\tfrac14: no better than a random guess
  2. B12\tfrac12
  3. C34\tfrac34
  4. D11: it finds 1010 every time
Show the answer

Answer: D. 11: it finds 1010 every time

After the oracle the amplitudes are 12\tfrac12 on ∣00⟩\ket{00}, ∣01⟩\ket{01}, ∣11⟩\ket{11} and −12-\tfrac12 on ∣10⟩\ket{10}. Their mean is 14\tfrac14. The diffusion step reflects each amplitude about the mean, a→2⋅14−aa \to 2\cdot\tfrac14 - a: ∣10⟩\ket{10} gets 12+12=1\tfrac12 + \tfrac12 = 1 and each other item 12−12=0\tfrac12 - \tfrac12 = 0. 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 ∣10⟩\ket{10}. The diffusion step turns that sign into probability. After the oracle the amplitudes are three 12\tfrac12⁠s and one −12-\tfrac12, with mean 14\tfrac14; reflecting each about the mean gives 2⋅14+12=12\cdot\tfrac14 + \tfrac12 = 1 for ∣10⟩\ket{10} and 2⋅14−12=02\cdot\tfrac14 - \tfrac12 = 0 for the rest.
  • B. One iteration doesn’t add a fixed share of probability. Work out the amplitudes: after the oracle they are 12\tfrac12, 12\tfrac12, 12\tfrac12 and −12-\tfrac12 on ∣10⟩\ket{10}, with mean 14\tfrac14. The diffusion step sends each aa to 2⋅14−a2\cdot\tfrac14 - a: 1 for ∣10⟩\ket{10}, 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 aa to 2⋅14−a2\cdot\tfrac14 - a, which gives ∣10⟩\ket{10} 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 N=8N = 8 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?

  1. AIt rises, closer to 100%
  2. BIt stays at about 94.5%: the state has settled
  3. CIt falls, to about 33%
  4. DIt falls all the way back to 1/81/8, where it started
Show the answer

Answer: C. It falls, to about 33%

sin⁡θ=1/8\sin\theta = 1/\sqrt{8} gives θ≈20.70∘\theta \approx 20.70^\circ. After 2 iterations the state is at 5θ≈103.5∘5\theta \approx 103.5^\circ from ∣bad⟩\ket{\text{bad}}, near 90∘90^\circ. One more iteration turns it to 7θ≈144.9∘7\theta \approx 144.9^\circ, well past 90∘90^\circ, 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, 2θ2\theta, 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 2θ2\theta, 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 2θ2\theta further, not all the way back. The chance falls to 33%, still well above 1/81/8.

2 / 14 · Learn

The search problem

There are N=2nN = 2^n items, labelled by nn⁠-bit strings xx, and a function ff with f(x)=1f(x) = 1 for the marked items and f(x)=0f(x) = 0 for the rest. Nothing else is known: the items are in no useful order, and ff 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 N/2N/2 checks. In the worst case it needs N−1N - 1: if the first N−1N - 1 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
  1. What does one check rule out?
  2. Think about the unluckiest order: every check fails until few items are left. When can you stop?
Show the answer

Answer: 6363

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 N−1=63N - 1 = 63 checks always suffice and fewer might not. On average you need about N/2N/2.

Common slips

  • 6464: 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.
  • 3232: About N/2N/2 is the average: often the marked item turns up early. The worst case is that the first 63 checks all fail.
  • 66: 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 ∣−⟩\ket-, the oracle UfU_f kicks back a sign, as in Lessons 9 and 14. Leaving the target out of the notation,

O∣x⟩=(−1)f(x)∣x⟩.O\ket x = (-1)^{f(x)}\ket x.

OO flips the sign of every marked item and leaves the others alone. Grover search starts in the equal superposition

∣s⟩=H⊗n∣0…0⟩=1N∑x∣x⟩,\ket s = H^{\otimes n}\ket{0\ldots0} = \frac{1}{\sqrt N}\sum_x\ket x,

so a single query acts on every item at once. The catch: a sign is invisible to a measurement, because ∣−a∣2=∣a∣2\abs{-a}^2 = \abs{a}^2. A second step has to turn the sign into probability.

Check yourself

Four items, and the oracle marks ∣10⟩\ket{10}. Enter O∣s⟩O\ket s as four amplitudes, ∣00⟩\ket{00} to ∣11⟩\ket{11}.

Hints
  1. ∣s⟩\ket s gives every item amplitude 12\tfrac12.
  2. OO multiplies ∣x⟩\ket x by (−1)f(x)(-1)^{f(x)}: only ∣10⟩\ket{10} has f(x)=1f(x) = 1.
Show the answer

Answer: 12∣00⟩+12∣01⟩−12∣10⟩+12∣11⟩\frac{1}{2}|00\rangle + \frac{1}{2}|01\rangle - \frac{1}{2}|10\rangle + \frac{1}{2}|11\rangle

Every amplitude of ∣s⟩\ket s is 12\tfrac12. The oracle multiplies the marked one, ∣10⟩\ket{10}, by −1-1 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 1010, the marked item?

P(10)=  ?P(10) = \;?

Hint
  1. The probability is the squared magnitude of the amplitude.
Show the answer

Answer: P(10)=14P(10) = \frac{1}{4}

The amplitude of ∣10⟩\ket{10} is −12-\tfrac12, and ∣−12∣2=14\abs{-\tfrac12}^2 = \tfrac14: the same as before the oracle. The oracle has marked the item, but no measurement can see the mark yet.

Common slips

  • 11: The oracle marks ∣10⟩\ket{10} with a sign, but a sign changes no probability: ∣−12∣2=14\abs{-\tfrac12}^2 = \tfrac14, the same as every other item.
  • 00: A negative amplitude still has a positive probability: (−12)2=14(-\tfrac12)^2 = \tfrac14.

3 / 14 · Learn

Inversion about the mean

The second step is the diffusion step,

D=2∣s⟩⟨s∣−I.D = 2\ket s\bra s - I.

Its effect is easiest to see on amplitudes. For a state ∑xax∣x⟩\sum_x a_x\ket x, the overlap ⟨s|ψ⟩\braket{s}{\psi} is N aˉ\sqrt N\,\bar a, where aˉ\bar a is the mean amplitude, and so

D:ax  →  2aˉ−ax.D:\quad a_x \;\to\; 2\bar a - a_x.

Every amplitude is reflected about the mean, which is why DD 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.

Eight items, one marked (amber). The oracle flips the marked amplitude to −0.35-0.35 and lowers the mean (dashed) to 0.270.27. The diffusion step mirrors every bar in that line: the marked one, 0.620.62 below the mean, ends 0.620.62 above it, at 0.880.88.

Check yourself

Four items with ∣10⟩\ket{10} marked. After the oracle the amplitudes are 12,12,−12,12\tfrac12, \tfrac12, -\tfrac12, \tfrac12. What is the amplitude of ∣10⟩\ket{10} after the diffusion step?

Hints
  1. Find the mean of the four amplitudes first.
  2. The diffusion step sends aa to 2aˉ−a2\bar a - a.
Show the answer

Answer: 11

The mean is 14(12+12−12+12)=14\tfrac14(\tfrac12 + \tfrac12 - \tfrac12 + \tfrac12) = \tfrac14. The diffusion step sends −12-\tfrac12 to 2⋅14+12=12\cdot\tfrac14 + \tfrac12 = 1, and each 12\tfrac12 to 2⋅14−12=02\cdot\tfrac14 - \tfrac12 = 0. The state is now exactly ∣10⟩\ket{10}.

Common slips

  • −12-\frac{1}{2}: That leaves the amplitude where it was. DD moves every amplitude to 2aˉ−a2\bar a - a, and −12-\tfrac12 is far below the mean.
  • 12\frac{1}{2}: That only undoes the oracle’s sign flip, a reflection about zero. DD reflects about the mean, 14\tfrac14: 2⋅14−(−12)=12\cdot\tfrac14 - (-\tfrac12) = 1.
  • 14\frac{1}{4}: That is the mean of the four amplitudes. DD reflects each amplitude about it: 2⋅14−(−12)=12\cdot\tfrac14 - (-\tfrac12) = 1.
  • 00: That is the new amplitude of each unmarked item, 2⋅14−12=02\cdot\tfrac14 - \tfrac12 = 0. The marked one starts below the mean, so it ends above: 2⋅14+12=12\cdot\tfrac14 + \tfrac12 = 1.

To build DD as a circuit, start with a sign flip on one basis state. A controlled-ZZ, written CZCZ, flips the sign of ∣11⟩\ket{11} and nothing else; with more controls (CCZCCZ, and so on) a multi-controlled ZZ flips the sign of ∣1…1⟩\ket{1\ldots1} only. Put XX on every qubit before and after, and the flip moves to ∣0…0⟩\ket{0\ldots0}:

X⊗n (multi-controlled Z) X⊗n=I−2∣0…0⟩⟨0…0∣.\begin{aligned}&X^{\otimes n}\,(\text{multi-controlled } Z)\,X^{\otimes n} \\ &\qquad = I - 2\ket{0\ldots0}\bra{0\ldots0}.\end{aligned}

Hadamards on both sides turn ∣0…0⟩\ket{0\ldots0} into ∣s⟩\ket s, which gives I−2∣s⟩⟨s∣=−DI - 2\ket s\bra s = -D. The extra −1-1 multiplies the whole state, a global phase, so no measurement can tell this circuit from DD.

Check yourself

Which circuit on 3 qubits applies the diffusion step D=2∣s⟩⟨s∣−ID = 2\ket s\bra s - I, up to a global phase?

  1. AHH on every qubit, XX on every qubit, a CCZCCZ, XX on every qubit, HH on every qubit
  2. BHH on every qubit
  3. CHH on every qubit, a CCZCCZ, HH on every qubit
  4. DXX on every qubit, a CCZCCZ, XX on every qubit
Show the answer

Answer: A. HH on every qubit, XX on every qubit, a CCZCCZ, XX on every qubit, HH on every qubit

XX on every qubit, a CCZCCZ, and XX again flip the sign of ∣000⟩\ket{000} only: that is I−2∣000⟩⟨000∣I - 2\ket{000}\bra{000}. Hadamards on both sides turn ∣000⟩\ket{000} into ∣s⟩\ket s, which gives I−2∣s⟩⟨s∣=−DI - 2\ket s\bra s = -D. The overall −1-1 is a global phase, so no measurement can tell this circuit from DD.

Why not the others

  • B. A layer of Hadamards is not a reflection about ∣s⟩\ket s: it sends ∣s⟩\ket s to ∣000⟩\ket{000}, while DD leaves ∣s⟩\ket s alone. The Hadamards only change basis. The reflection itself is a sign flip on ∣000⟩\ket{000}, made between two Hadamard layers.
  • C. A CCZCCZ flips the sign of ∣111⟩\ket{111}, so this reflects about H⊗3∣111⟩H^{\otimes 3}\ket{111}, a state full of minus signs, not ∣s⟩\ket s. The XX layers move the flip onto ∣000⟩\ket{000}, which the Hadamards then turn into ∣s⟩\ket s.
  • D. That flips the sign of ∣000⟩\ket{000} alone, so its mirror passes through ∣000⟩\ket{000}, not ∣s⟩\ket s. Hadamards on both sides move the mirror to H⊗3∣000⟩=∣s⟩H^{\otimes 3}\ket{000} = \ket s.

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
  1. Press Start over, then One iteration (O, then D), and note the chance after each press.
Show the answer

Answer: 22

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

  • 33: Look again at the third iteration: the chance falls from 94.5% to 33%. More iterations don’t always help.
  • 11: One iteration gives 78.1%, but a second raises it to 94.5%.
  • 44: Four is N/2N/2, 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?

  1. ANothing: the mean stays where it is
  2. BIt raises the mean, since the marked bar shoots up
  3. CIt lowers the mean, since most of the bars get shorter
  4. DIt sets the mean to zero
Show the answer

Answer: A. Nothing: the mean stays where it is

Each amplitude goes to 2aˉ−a2\bar a - a, so the new mean is 2aˉ−aˉ=aˉ2\bar a - \bar a = \bar a. 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 aa goes to 2aˉ−a2\bar a - a, and the average of those is 2aˉ−aˉ=aˉ2\bar a - \bar a = \bar a.

5 / 14 · Learn

Two mirrors make a rotation

The bars hide a simpler picture. Say MM of the NN items are marked, and define two states: ∣good⟩\ket{\text{good}}, the equal superposition of the MM marked items, and ∣bad⟩\ket{\text{bad}}, the equal superposition of the other N−MN - M. They are orthogonal, and

∣s⟩=sin⁡θ ∣good⟩+cos⁡θ ∣bad⟩\ket s = \sin\theta\,\ket{\text{good}} + \cos\theta\,\ket{\text{bad}}

with sin⁡θ=M/N\sin\theta = \sqrt{M/N}, the ∣good⟩\ket{\text{good}} part of ∣s⟩\ket s. So ∣s⟩\ket s is a unit vector in the plane of ∣bad⟩\ket{\text{bad}} (across) and ∣good⟩\ket{\text{good}} (up), at the angle θ\theta above ∣bad⟩\ket{\text{bad}}. Neither OO nor DD ever takes the state out of this plane: all marked items keep one shared amplitude, as do all unmarked ones. A state at angle φ\varphi finds a marked item with chance sin⁡2φ\sin^2\varphi.

Check yourself

Grover search over N=16N = 16 items with one marked item. What is the angle θ\theta between ∣s⟩\ket s and ∣bad⟩\ket{\text{bad}}, in degrees?

θ=  ?\theta = \;?

Hints
  1. ∣s⟩=sin⁡θ∣good⟩+cos⁡θ∣bad⟩\ket s = \sin\theta\ket{\text{good}} + \cos\theta\ket{\text{bad}}. What is the ∣good⟩\ket{\text{good}} part?
  2. The ∣good⟩\ket{\text{good}} part is M/N\sqrt{M/N}, so θ=arcsin⁡1/16\theta = \arcsin\sqrt{1/16}.
Show the answer

Answer: θ=14.477\theta = 14.477

sin⁡θ=1/16=14\sin\theta = \sqrt{1/16} = \tfrac{1}{4}, so θ=arcsin⁡1/16≈14.48∘\theta = \arcsin\sqrt{1/16} \approx 14.48^\circ.

Common slips

  • 3.5833.583: That uses sin⁡θ=M/N\sin\theta = M/N. The ∣good⟩\ket{\text{good}} part of ∣s⟩\ket s is an amplitude, M/N\sqrt{M/N}, so sin⁡θ=1/16\sin\theta = \sqrt{1/16}.
  • 0.2530.253: That is θ\theta in radians (or sin⁡θ\sin\theta, which is close to it when θ\theta is small). The question asks for degrees: θ≈14.48∘\theta \approx 14.48^\circ.
  • 28.95528.955: That is 2θ2\theta, the turn made by one iteration. θ\theta is the angle between ∣s⟩\ket s and ∣bad⟩\ket{\text{bad}}.
  • 75.52275.522: That is the angle between ∣s⟩\ket s and ∣good⟩\ket{\text{good}}. θ\theta is measured from ∣bad⟩\ket{\text{bad}}, and sin⁡θ\sin\theta is the ∣good⟩\ket{\text{good}} part of ∣s⟩\ket s.

Now the two steps. The oracle flips the sign of the ∣good⟩\ket{\text{good}} part and keeps the ∣bad⟩\ket{\text{bad}} part: in the plane it is the mirror in ∣bad⟩\ket{\text{bad}}. The diffusion step keeps ∣s⟩\ket s and flips everything orthogonal to it: it is the mirror in ∣s⟩\ket s.

Two mirrors in a row make a rotation by twice the angle between them. These mirrors meet at θ\theta, so one iteration, G=DOG = DO, turns the state by 2θ2\theta toward ∣good⟩\ket{\text{good}}. From ∣s⟩\ket s, at θ\theta, kk iterations reach (2k+1)θ(2k+1)\theta:

Gk∣s⟩=sin⁡((2k+1)θ)∣good⟩+cos⁡((2k+1)θ)∣bad⟩.\begin{aligned}G^k\ket s &= \sin\big((2k+1)\theta\big)\ket{\text{good}} \\ &\quad + \cos\big((2k+1)\theta\big)\ket{\text{bad}}.\end{aligned}

The chance of a marked item after kk iterations is sin⁡2((2k+1)θ)\sin^2((2k+1)\theta).

Eight items, one marked, so θ≈20.7∘\theta \approx 20.7^\circ. The oracle mirrors the state in ∣bad⟩\ket{\text{bad}}, from θ\theta to −θ-\theta. The diffusion step mirrors it in the line of ∣s⟩\ket s, from −θ-\theta to 3θ3\theta. Together: a turn of 2θ≈41.4∘2\theta \approx 41.4^\circ.

Check yourself

Grover search over N=16N = 16 items with one marked item. What is the chance that a measurement finds the marked item after one iteration? Give a decimal.

P=  ?P = \;?

Hints
  1. Find the start angle from sin⁡θ=M/N\sin\theta = \sqrt{M/N}.
  2. Each iteration turns the state by 2θ2\theta, so after kk iterations it is at (2k+1)θ(2k+1)\theta from ∣bad⟩\ket{\text{bad}}. The chance is sin⁡2\sin^2 of that angle.
  3. Here θ≈14.48∘\theta \approx 14.48^\circ, so the angle is 3θ≈43.4∘3\theta \approx 43.4^\circ.
Show the answer

Answer: P=0.473P = 0.473

sin⁡θ=1/16\sin\theta = 1/\sqrt{16} gives θ≈14.48∘\theta \approx 14.48^\circ. After one iteration the state is at 3θ≈43.4∘3\theta \approx 43.4^\circ from ∣bad⟩\ket{\text{bad}}, so the chance is sin⁡2(43.4∘)≈0.4727\sin^2(43.4^\circ) \approx 0.4727.

Common slips

  • 1116\frac{11}{16}: That is sin⁡(3θ)\sin(3\theta), the marked item’s amplitude. The chance is its square, sin⁡2(3θ)\sin^2(3\theta).
  • 1564\frac{15}{64}: That puts the state at 2θ2\theta. It starts at θ\theta, not at 0, so after one iteration it is at (2⋅1+1)θ=3θ(2\cdot1 + 1)\theta = 3\theta.
  • 916\frac{9}{16}: That is the small-angle estimate sin⁡2x≈x2\sin^2 x \approx x^2 with x≈3/16x \approx 3/\sqrt{16}. At 43.4∘43.4^\circ the angle is too large for it: take sin⁡2\sin^2 itself.

6 / 14 · Explore

Watching the turn

Predict, then see

Two qubits, and three of the four items are marked: ∣01⟩\ket{01}, ∣10⟩\ket{10} and ∣11⟩\ket{11}. Before any step, a measurement finds a marked item 75% of the time. What does one Grover iteration do to that chance?

  1. AIt drops to 0%
  2. BIt rises to 100%
  3. CIt rises a little, to about 90%
  4. 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 M=3M = 3 and N=4N = 4, sin⁡θ=3/4\sin\theta = \sqrt{3/4}, so θ=60∘\theta = 60^\circ: ∣s⟩\ket s is already closer to ∣good⟩\ket{\text{good}} than to ∣bad⟩\ket{\text{bad}}. One iteration turns it by 2θ=120∘2\theta = 120^\circ, to 3θ=180∘3\theta = 180^\circ, which is −∣bad⟩-\ket{\text{bad}}: the unmarked item, with certainty. In amplitudes: after the oracle they are 12,−12,−12,−12\tfrac12, -\tfrac12, -\tfrac12, -\tfrac12, with mean −14-\tfrac14, and reflecting about the mean gives −1-1 on ∣00⟩\ket{00} and 0 on the rest. The circuit applies −D-D, so it shows +1+1.

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 ∣00⟩\ket{00}, the one unmarked item.
  • C. The circuit shows the chance falling, not rising: at the end every amplitude is 0 except the one on ∣00⟩\ket{00}, 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 ∣00⟩\ket{00}.
What if half the items or more are marked?

Then θ≥45∘\theta \ge 45^\circ, and measuring ∣s⟩\ket s 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 12\tfrac12). So skip the iterations: measure ∣s⟩\ket s, 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?

  1. AWith two marked items, every number of iterations gives certainty
  2. BTwice as many marked items halve the number of iterations: two become one
  3. CM/N=14M/N = \tfrac14, as for one marked item among four, so θ=30∘\theta = 30^\circ and one iteration reaches 3θ=90∘3\theta = 90^\circ
  4. DThe oracle now flips two signs, so each iteration does twice the work
Show the answer

Answer: C. M/N=14M/N = \tfrac14, as for one marked item among four, so θ=30∘\theta = 30^\circ and one iteration reaches 3θ=90∘3\theta = 90^\circ

sin⁡θ=2/8=12\sin\theta = \sqrt{2/8} = \tfrac12 gives θ=30∘\theta = 30^\circ. One iteration turns ∣s⟩\ket s by 60∘60^\circ, to 90∘90^\circ: exactly ∣good⟩\ket{\text{good}}. A second iteration would overshoot to 150∘150^\circ, with chance 14\tfrac14.

Why not the others

  • A. Press One iteration (O, then D) again: the chance falls to 25%. The state turns past 90∘90^\circ to 5θ=150∘5\theta = 150^\circ, and sin⁡2150∘=14\sin^2 150^\circ = \tfrac14.
  • B. The count shrinks like 1/M1/\sqrt M, not 1/M1/M: twice as many marked items need about 1/21/\sqrt2 as many iterations. What decides it here is the angle: sin⁡θ=2/8=12\sin\theta = \sqrt{2/8} = \tfrac12, so θ=30∘\theta = 30^\circ and one iteration lands on exactly 90∘90^\circ.
  • 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: sin⁡θ=2/8=12\sin\theta = \sqrt{2/8} = \tfrac12, so θ=30∘\theta = 30^\circ and one turn of 2θ2\theta reaches 90∘90^\circ.

7 / 14 · Learn

When to stop

You want (2k+1)θ(2k+1)\theta as close to 90∘90^\circ as possible, so solve (2k+1)θ=π/2(2k+1)\theta = \pi/2 and round:

k=round⁡(π4θ−12).k = \operatorname{round}\Big(\frac{\pi}{4\theta} - \frac12\Big).

The best whole kk leaves the state within θ\theta of ∣good⟩\ket{\text{good}}, so the chance is at least cos⁡2θ=1−M/N\cos^2\theta = 1 - M/N. When MM is much smaller than NN, θ≈M/N\theta \approx \sqrt{M/N} (in radians), and the rule becomes

k≈π4NM.k \approx \frac{\pi}{4}\sqrt{\frac{N}{M}}.

For one marked item among N=220N = 2^{20}, 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 kk iterations is sin⁡2((2k+1)θ)\sin^2((2k+1)\theta) with θ≈20.7∘\theta \approx 20.7^\circ. 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 2θ2\theta, so after the first peak it moves past ∣good⟩\ket{\text{good}} 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 kk, and stop. Lesson 17 shows what to do when you don’t know MM.

Check yourself

Grover search over N=16N = 16 items with one marked item. How many iterations bring the chance of finding a marked item to its first peak?

Hints
  1. Find θ\theta from sin⁡θ=M/N\sin\theta = \sqrt{M/N}, in radians.
  2. You want (2k+1)θ(2k+1)\theta as close as possible to π/2\pi/2: k=π/(4θ)−12k = \pi/(4\theta) - \tfrac12, rounded to a whole number.
  3. Here θ≈0.2527\theta \approx 0.2527 radians, so π/(4θ)−12≈2.61\pi/(4\theta) - \tfrac12 \approx 2.61.
Show the answer

Answer: 33

sin⁡θ=1/16=14\sin\theta = \sqrt{1/16} = \tfrac{1}{4} gives θ≈0.2527\theta \approx 0.2527 radians, about 14.48∘14.48^\circ. Then π/(4θ)−12≈2.61\pi/(4\theta) - \tfrac12 \approx 2.61, which rounds to 3. Check: 7θ≈101.3∘7\theta \approx 101.3^\circ, close to 90∘90^\circ, and the chance is 96.1%.

Common slips

  • 88: That is N/2N/2, the average for a classical search. Grover search needs only about π4N\tfrac{\pi}{4}\sqrt N iterations.
  • 44: One more than the best overshoots. After 4 iterations the state is at 130.3∘130.3^\circ, further from 90∘90^\circ than 101.3∘101.3^\circ, and the chance drops from 96.1% to 58.2%.
  • 22: One fewer stops short: after 2 iterations the state is at 72.4∘72.4^\circ, 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 kk and degrees for the picture.

Worked example

One marked item among N=64N = 64

Find the start angle, the best number of iterations and the chance of success. Then do the same for N=1024N = 1024.

  1. Start angle

    sin⁡θ=M/N=1/64=18\sin\theta = \sqrt{M/N} = \sqrt{1/64} = \tfrac18, so θ=arcsin⁡18≈0.1253\theta = \arcsin\tfrac18 \approx 0.1253 radians, about 7.18∘7.18^\circ. For a small angle θ≈sin⁡θ\theta \approx \sin\theta, and indeed 0.1253≈0.1250.1253 \approx 0.125.

  2. Best count

    k=π/(4θ)−12≈6.27−0.5=5.77k = \pi/(4\theta) - \tfrac12 \approx 6.27 - 0.5 = 5.77, which rounds to k=6k = 6. The shortcut π4N=π4⋅8≈6.28\tfrac{\pi}{4}\sqrt N = \tfrac{\pi}{4}\cdot 8 \approx 6.28 gives the same whole number.

  3. Chance

    After 6 iterations the state is at 13θ≈93.3∘13\theta \approx 93.3^\circ from ∣bad⟩\ket{\text{bad}}, so the chance is sin⁡2(93.3∘)≈0.9966\sin^2(93.3^\circ) \approx 0.9966. For comparison, 5 iterations reach 11θ≈79.0∘11\theta \approx 79.0^\circ (chance 0.9635) and 7 reach 15θ≈107.7∘15\theta \approx 107.7^\circ (0.9074). The best stops a little past 90∘90^\circ, and both neighbours are worse.

  4. Your turn: start angle

    Now one marked item among N=1024N = 1024.

    Your turn

    What is sin⁡θ\sin\theta for one marked item among N=1024N = 1024?

    sin⁡θ=  ?\sin\theta = \;?

    Hint
    1. sin⁡θ\sin\theta is the ∣good⟩\ket{\text{good}} part of ∣s⟩\ket s, the amplitude M/N\sqrt{M/N}.
    Show the answer

    Answer: sin⁡θ=132\sin\theta = \frac{1}{32}

    sin⁡θ=1/1024=132\sin\theta = \sqrt{1/1024} = \tfrac{1}{32}, so θ≈0.03126\theta \approx 0.03126 radians, about 1.79∘1.79^\circ.

    Common slips

    • 0.0010.001: That is M/NM/N, the chance of the marked item in ∣s⟩\ket s. sin⁡θ\sin\theta is its amplitude, the square root: 1/1024=132\sqrt{1/1024} = \tfrac{1}{32}.
    • 3232: sin⁡θ\sin\theta is at most 1. That is N/M\sqrt{N/M}; turn it over: M/N=132\sqrt{M/N} = \tfrac{1}{32}.
  5. Your turn: best count

    Your turn

    With θ≈0.03126\theta \approx 0.03126 radians, how many iterations bring the chance to its first peak?

    Hint
    1. Compute π/(4θ)−12\pi/(4\theta) - \tfrac12 and round.
    Show the answer

    Answer: 2525

    π/(4θ)−12≈25.13−0.5=24.63\pi/(4\theta) - \tfrac12 \approx 25.13 - 0.5 = 24.63, which rounds to 25. Then 51θ≈91.3∘51\theta \approx 91.3^\circ 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

    • 3232: 32 is N\sqrt N, without the factor π4\tfrac{\pi}{4}. The state must turn through about 90∘90^\circ in steps of 2θ2\theta: π/(4θ)−12≈24.6\pi/(4\theta) - \tfrac12 \approx 24.6. After 32 iterations it is at 65θ≈116.4∘65\theta \approx 116.4^\circ, well past 90∘90^\circ, with chance 0.80.
    • 512512: That is N/2N/2, the classical average. Grover search needs about π4N≈25\tfrac{\pi}{4}\sqrt N \approx 25 iterations.
    • 2424: Close, but π/(4θ)−12≈24.63\pi/(4\theta) - \tfrac12 \approx 24.63 rounds up. 25 iterations reach 51θ≈91.3∘51\theta \approx 91.3^\circ (chance 0.9995); 24 stop at 49θ≈87.7∘49\theta \approx 87.7^\circ (0.9985).

One more, fully worked: a list 64 times longer than the first.

Another worked example

Another example: N=4096N = 4096

  1. Start angle

    sin⁡θ=1/4096=164\sin\theta = 1/\sqrt{4096} = \tfrac{1}{64}, so θ≈0.01563\theta \approx 0.01563 radians, about 0.895∘0.895^\circ.

  2. Best count

    π/(4θ)−12≈50.26−0.5=49.76\pi/(4\theta) - \tfrac12 \approx 50.26 - 0.5 = 49.76, which rounds to 50. The shortcut gives π4⋅64≈50.3\tfrac{\pi}{4}\cdot 64 \approx 50.3: also 50.

  3. Chance

    101θ≈90.42∘101\theta \approx 90.42^\circ, so the chance is sin⁡2(90.42∘)≈0.99995\sin^2(90.42^\circ) \approx 0.99995. A list 64 times longer than N=64N = 64 needed about 8 times as many iterations, 50 against 6: the square root at work.

Check yourself

Grover search over N=1,024N = 1{,}024 items with one marked item. How many iterations bring the chance of finding a marked item to its first peak?

Hints
  1. Find θ\theta from sin⁡θ=M/N\sin\theta = \sqrt{M/N}, in radians.
  2. You want (2k+1)θ(2k+1)\theta as close as possible to π/2\pi/2: k=π/(4θ)−12k = \pi/(4\theta) - \tfrac12, rounded to a whole number.
  3. Here θ≈0.0313\theta \approx 0.0313 radians, so π/(4θ)−12≈24.63\pi/(4\theta) - \tfrac12 \approx 24.63.
Show the answer

Answer: 2525

sin⁡θ=1/1,024=132\sin\theta = \sqrt{1/1{,}024} = \tfrac{1}{32} gives θ≈0.0313\theta \approx 0.0313 radians, about 1.79∘1.79^\circ. Then π/(4θ)−12≈24.63\pi/(4\theta) - \tfrac12 \approx 24.63, which rounds to 25. Check: 51θ≈91.3∘51\theta \approx 91.3^\circ, close to 90∘90^\circ, and the chance is 99.9%.

Common slips

  • 512512: That is N/2N/2, the average for a classical search. Grover search needs only about π4N\tfrac{\pi}{4}\sqrt N iterations.
  • 2626: One more than the best overshoots. After 26 iterations the state is at 94.9∘94.9^\circ, further from 90∘90^\circ than 91.3∘91.3^\circ, and the chance drops from 99.9% to 99.3%.
  • 3232: N/M\sqrt{N/M} is how the count grows, but the constant matters. The state must turn through about 90∘90^\circ, in steps of 2θ2\theta, which takes about π4N/M\tfrac{\pi}{4}\sqrt{N/M} iterations. 32 iterations give only 80.2%.
  • 2424: One fewer stops short: after 24 iterations the state is at 87.7∘87.7^\circ, and the chance is 99.8%, against 99.9% after 25.

9 / 14 · Learn

How big is the speedup?

Grover search needs about π4N\tfrac{\pi}{4}\sqrt N queries where a classical search needs about N/2N/2. With N=2nN = 2^n that is about 2n/22^{n/2} against 2n−12^{n-1}: the square root halves the exponent. That is a quadratic speedup. Both counts still grow exponentially with the number of bits nn, 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 N\sqrt N 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 ff. Unstructured search has none.

Queries against the number of bits nn, on a log scale. Classical search grows like 2n−12^{n-1}, Grover search like 2n/22^{n/2}: both are straight lines, one half as steep. At n=20n = 20: 524,288 against 804.

Check yourself

A secret key has 128 bits, so there are N=2128N = 2^{128} possible keys, and an oracle recognises the right one. About how many oracle queries does Grover search need to find the key?

  1. AOne: a single query tests every key at once
  2. BAbout 2642^{64} (more exactly π4⋅264≈1.4×1019\tfrac{\pi}{4}\cdot 2^{64} \approx 1.4\times10^{19})
  3. CAbout 21272^{127}, half the keys
  4. DAbout 128, one query per bit of the key
Show the answer

Answer: B. About 2642^{64} (more exactly π4⋅264≈1.4×1019\tfrac{\pi}{4}\cdot 2^{64} \approx 1.4\times10^{19})

With one marked item among N=2128N = 2^{128}, Grover search needs about π4N=π4⋅264≈1.4×1019\tfrac{\pi}{4}\sqrt N = \tfrac{\pi}{4}\cdot 2^{64} \approx 1.4\times10^{19} queries, against about 21272^{127} 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 π4N\tfrac{\pi}{4}\sqrt N 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 NN, which is 2642^{64}.
  • D. That would be an exponential speedup, and Grover search doesn’t give one. Each iteration turns the state by about 2/N2/\sqrt N radians, so it takes about π4N=π4⋅264\tfrac{\pi}{4}\sqrt N = \tfrac{\pi}{4}\cdot 2^{64} queries: the square root of NN, not its logarithm.

10 / 14 · Learn

Grover circuits and code

With two qubits the whole algorithm is small. To mark ∣11⟩\ket{11} the oracle is a single CZCZ. The diffusion step is HH on both, XX on both, a CZCZ, XX on both, HH on both. Since N=4N = 4 and one item is marked, one iteration is exact. In the editor a control dot ∙\bullet and a ZZ in the same column make a CZCZ.

Check yourself

The first two columns make ∣s⟩\ket s and apply the oracle for ∣11⟩\ket{11}. Add the diffusion step in the five empty columns, so that the circuit ends in ∣11⟩\ket{11} (up to a global phase).

Build it in the circuit builder (gates: H, X, Z, control).

Hints
  1. The diffusion step is HH, XX, a sign flip on ∣11⟩\ket{11}, XX, HH, each on both qubits.
  2. Column 3: HH on both. Column 4: XX on both. Column 5: a dot on one qubit and ZZ 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 12(∣00⟩+∣01⟩+∣10⟩−∣11⟩)\tfrac12(\ket{00} + \ket{01} + \ket{10} - \ket{11}). HH, XX, CZCZ, XX, HH applies −D-D, which reflects every amplitude about the mean 14\tfrac14 and adds a global −1-1: the result is −∣11⟩-\ket{11}, and a measurement gives 1111 every time.

In Qiskit the multi-controlled ZZ on three qubits is qc.ccz(0, 1, 2). For more qubits, qc.mcp(np.pi, controls, target) applies a phase of π\pi, that is a factor −1-1, when every control and the target are 1, which is a multi-controlled ZZ. An oracle for one marked string puts XX on each qubit whose bit is 0, applies the multi-controlled ZZ, and undoes the XX⁠s.

When you read the counts, remember Qiskit’s bit order from Lesson 12: qubit 0 is the rightmost bit, so the marked item ∣1011⟩\ket{1011} prints as 1101.

Check yourself

A Qiskit program runs Grover search on 4 qubits with the marked item ∣0111⟩\ket{0111} (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?

  1. ANo string stands out: all 16 appear about equally often
  2. B1000
  3. C0111
  4. D1110
Show the answer

Answer: D. 1110

3 iterations put probability 96.1% on ∣0111⟩\ket{0111}. 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 XX gates in the oracle changed the answer. They are undone right after the multi-controlled ZZ; they only move the sign flip onto ∣0111⟩\ket{0111}. The result is ∣0111⟩\ket{0111}, which Qiskit prints as 1110.
  • C. That reads Qiskit’s string as a ket. Qiskit prints qubit 0 last, so ∣0111⟩\ket{0111} shows up as 1110: the same bits in reverse order.

Check yourself

Three qubits, marked item ∣110⟩\ket{110}, 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
  1. The diffusion step is HH, XX, CCZCCZ, XX, HH, each layer on all three qubits: qc.h(range(3)), qc.x(range(3)), qc.ccz(0, 1, 2).
  2. For two iterations, loop twice: for step in range(2): with oracle(qc) and diffusion(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 5θ≈103.5∘5\theta \approx 103.5^\circ, with chance sin⁡2103.5∘=121128≈0.945\sin^2 103.5^\circ = \tfrac{121}{128} \approx 0.945 for the marked item. Qiskit prints ∣110⟩\ket{110} 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 N=8N = 8 items, one of them marked. Starting from ∣s⟩\ket s, 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
  1. Write down the amplitudes after the oracle, and find their mean.
  2. The diffusion step sends every amplitude aa to 2aˉ−a2\bar a - a, with aˉ\bar a the mean after the oracle.
  3. Here every amplitude starts at 1/8≈0.3541/\sqrt{8} \approx 0.354, and after the oracle the mean is 0.354⋅68≈0.2650.354\cdot\tfrac{6}{8} \approx 0.265.
Show the answer

Answer: 0.1770.177

Every amplitude starts at 1/8≈0.3541/\sqrt{8} \approx 0.354. The oracle makes the marked one −0.354-0.354, so the mean drops to 0.354⋅68≈0.2650.354\cdot\tfrac{6}{8} \approx 0.265. Reflecting about it gives 2(0.265)−0.354≈0.1772(0.265) - 0.354 \approx 0.177 for each unmarked item.

Common slips

  • 24\frac{\sqrt{2}}{4}: That leaves the unmarked amplitudes where they were. DD moves every amplitude: each 0.3540.354 sits a little above the mean 0.2650.265, so it ends a little below it.
  • 382\frac{3}{8\sqrt{2}}: That is the mean after the oracle. DD reflects each amplitude about it, a→2aˉ−aa \to 2\bar a - a: 2(0.265)−0.3542(0.265) - 0.354.
  • 542\frac{5}{4\sqrt{2}}: That is the marked item’s new amplitude. An unmarked item starts above the mean, at 0.3540.354, so it is reflected to just below it: 2(0.265)−0.3542(0.265) - 0.354.

Check yourself

Grover search over N=8N = 8 items with one marked item. What is the chance that a measurement finds the marked item after one iteration? Give a decimal.

P=  ?P = \;?

Hints
  1. Find the start angle from sin⁡θ=M/N\sin\theta = \sqrt{M/N}.
  2. Each iteration turns the state by 2θ2\theta, so after kk iterations it is at (2k+1)θ(2k+1)\theta from ∣bad⟩\ket{\text{bad}}. The chance is sin⁡2\sin^2 of that angle.
  3. Here θ≈20.70∘\theta \approx 20.70^\circ, so the angle is 3θ≈62.1∘3\theta \approx 62.1^\circ.
Show the answer

Answer: P=0.781P = 0.781

sin⁡θ=1/8\sin\theta = 1/\sqrt{8} gives θ≈20.70∘\theta \approx 20.70^\circ. After one iteration the state is at 3θ≈62.1∘3\theta \approx 62.1^\circ from ∣bad⟩\ket{\text{bad}}, so the chance is sin⁡2(62.1∘)≈0.7813\sin^2(62.1^\circ) \approx 0.7813.

Common slips

  • 542\frac{5}{4\sqrt{2}}: That is sin⁡(3θ)\sin(3\theta), the marked item’s amplitude. The chance is its square, sin⁡2(3θ)\sin^2(3\theta).
  • 716\frac{7}{16}: That puts the state at 2θ2\theta. It starts at θ\theta, not at 0, so after one iteration it is at (2⋅1+1)θ=3θ(2\cdot1 + 1)\theta = 3\theta.

Check yourself

Grover search over N=128N = 128 items with one marked item. How many iterations bring the chance of finding a marked item to its first peak?

Hints
  1. Find θ\theta from sin⁡θ=M/N\sin\theta = \sqrt{M/N}, in radians.
  2. You want (2k+1)θ(2k+1)\theta as close as possible to π/2\pi/2: k=π/(4θ)−12k = \pi/(4\theta) - \tfrac12, rounded to a whole number.
  3. Here θ≈0.0885\theta \approx 0.0885 radians, so π/(4θ)−12≈8.37\pi/(4\theta) - \tfrac12 \approx 8.37.
Show the answer

Answer: 88

sin⁡θ=1/128\sin\theta = \sqrt{1/128} gives θ≈0.0885\theta \approx 0.0885 radians, about 5.07∘5.07^\circ. Then π/(4θ)−12≈8.37\pi/(4\theta) - \tfrac12 \approx 8.37, which rounds to 8. Check: 17θ≈86.2∘17\theta \approx 86.2^\circ, close to 90∘90^\circ, and the chance is 99.6%.

Common slips

  • 6464: That is N/2N/2, the average for a classical search. Grover search needs only about π4N\tfrac{\pi}{4}\sqrt N iterations.
  • 99: The shortcut π4N/M≈8.89\tfrac{\pi}{4}\sqrt{N/M} \approx 8.89 is a good estimate, but here it rounds to the wrong side. The exact rule rounds π/(4θ)−12≈8.37\pi/(4\theta) - \tfrac12 \approx 8.37, which gives 8: chance 99.6%, against 98.8% for 9.
  • 1111: N/M\sqrt{N/M} is how the count grows, but the constant matters. The state must turn through about 90∘90^\circ, in steps of 2θ2\theta, which takes about π4N/M\tfrac{\pi}{4}\sqrt{N/M} iterations. 11 iterations give only 79.9%.
  • 77: One fewer stops short: after 7 iterations the state is at 76.1∘76.1^\circ, and the chance is 94.2%, against 99.6% after 8.

12 / 14 · Explain

Explain it

Explain it

Why does Grover search need about N\sqrt N iterations to find one marked item among NN, and why does running many more than that make things worse?

Show a model answer

The state never leaves the plane of ∣good⟩\ket{\text{good}} and ∣bad⟩\ket{\text{bad}}. It starts as ∣s⟩\ket s, at the angle θ\theta from ∣bad⟩\ket{\text{bad}} with sin⁡θ=1/N\sin\theta = 1/\sqrt N, so θ≈1/N\theta \approx 1/\sqrt N radians. Each iteration is two reflections, in ∣bad⟩\ket{\text{bad}} (the oracle) and in ∣s⟩\ket s (the diffusion step), and two reflections make a rotation by twice the angle between the mirrors, 2θ2\theta. To reach ∣good⟩\ket{\text{good}} the state must turn through about 90∘90^\circ, which takes about (π/2)/(2θ)≈π4N(\pi/2)/(2\theta) \approx \tfrac{\pi}{4}\sqrt N iterations. The rotation doesn’t stop at ∣good⟩\ket{\text{good}}: further iterations carry the state past it, and the chance sin⁡2((2k+1)θ)\sin^2((2k+1)\theta) falls again.

A good answer covers

  • The state starts at a small angle θ\theta from ∣bad⟩\ket{\text{bad}}, with sin⁡θ=1/N\sin\theta = 1/\sqrt N, so θ≈1/N\theta \approx 1/\sqrt N
  • Each iteration (two reflections) turns the state by the same angle, 2θ2\theta
  • Turning through about 90∘90^\circ therefore takes about π4N\tfrac{\pi}{4}\sqrt N iterations
  • More iterations keep turning the state, past ∣good⟩\ket{\text{good}}, so the chance falls again

13 / 14 · Mastery check

Mastery check

Check yourself

Four items, ∣00⟩\ket{00} to ∣11⟩\ket{11}, and only ∣11⟩\ket{11} 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 1111?

  1. A14\tfrac14: no better than a random guess
  2. B12\tfrac12
  3. C34\tfrac34
  4. D11: it finds 1111 every time
Show the answer

Answer: D. 11: it finds 1111 every time

After the oracle the amplitudes are 12\tfrac12 on ∣00⟩\ket{00}, ∣01⟩\ket{01}, ∣10⟩\ket{10} and −12-\tfrac12 on ∣11⟩\ket{11}. Their mean is 14\tfrac14. The diffusion step reflects each amplitude about the mean, a→2⋅14−aa \to 2\cdot\tfrac14 - a: ∣11⟩\ket{11} gets 12+12=1\tfrac12 + \tfrac12 = 1 and each other item 12−12=0\tfrac12 - \tfrac12 = 0. 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 ∣11⟩\ket{11}. The diffusion step turns that sign into probability. After the oracle the amplitudes are three 12\tfrac12⁠s and one −12-\tfrac12, with mean 14\tfrac14; reflecting each about the mean gives 2⋅14+12=12\cdot\tfrac14 + \tfrac12 = 1 for ∣11⟩\ket{11} and 2⋅14−12=02\cdot\tfrac14 - \tfrac12 = 0 for the rest.
  • B. One iteration doesn’t add a fixed share of probability. Work out the amplitudes: after the oracle they are 12\tfrac12, 12\tfrac12, 12\tfrac12 and −12-\tfrac12 on ∣11⟩\ket{11}, with mean 14\tfrac14. The diffusion step sends each aa to 2⋅14−a2\cdot\tfrac14 - a: 1 for ∣11⟩\ket{11}, 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 aa to 2⋅14−a2\cdot\tfrac14 - a, which gives ∣11⟩\ket{11} amplitude 1 and the other three amplitude 0.

Check yourself

Which circuit on 2 qubits applies the diffusion step D=2∣s⟩⟨s∣−ID = 2\ket s\bra s - I, up to a global phase?

  1. AXX on every qubit, a CZCZ, XX on every qubit
  2. BHH on every qubit
  3. CHH on every qubit, a CZCZ, HH on every qubit
  4. DHH on every qubit, XX on every qubit, a CZCZ, XX on every qubit, HH on every qubit
Show the answer

Answer: D. HH on every qubit, XX on every qubit, a CZCZ, XX on every qubit, HH on every qubit

XX on every qubit, a CZCZ, and XX again flip the sign of ∣00⟩\ket{00} only: that is I−2∣00⟩⟨00∣I - 2\ket{00}\bra{00}. Hadamards on both sides turn ∣00⟩\ket{00} into ∣s⟩\ket s, which gives I−2∣s⟩⟨s∣=−DI - 2\ket s\bra s = -D. The overall −1-1 is a global phase, so no measurement can tell this circuit from DD.

Why not the others

  • A. That flips the sign of ∣00⟩\ket{00} alone, so its mirror passes through ∣00⟩\ket{00}, not ∣s⟩\ket s. Hadamards on both sides move the mirror to H⊗2∣00⟩=∣s⟩H^{\otimes 2}\ket{00} = \ket s.
  • B. A layer of Hadamards is not a reflection about ∣s⟩\ket s: it sends ∣s⟩\ket s to ∣00⟩\ket{00}, while DD leaves ∣s⟩\ket s alone. The Hadamards only change basis. The reflection itself is a sign flip on ∣00⟩\ket{00}, made between two Hadamard layers.
  • C. A CZCZ flips the sign of ∣11⟩\ket{11}, so this reflects about H⊗2∣11⟩H^{\otimes 2}\ket{11}, a state full of minus signs, not ∣s⟩\ket s. The XX layers move the flip onto ∣00⟩\ket{00}, which the Hadamards then turn into ∣s⟩\ket s.

Check yourself

Grover search over N=16N = 16 items with one marked item. What is the chance that a measurement finds the marked item after three iterations? Give a decimal.

P=  ?P = \;?

Hints
  1. Find the start angle from sin⁡θ=M/N\sin\theta = \sqrt{M/N}.
  2. Each iteration turns the state by 2θ2\theta, so after kk iterations it is at (2k+1)θ(2k+1)\theta from ∣bad⟩\ket{\text{bad}}. The chance is sin⁡2\sin^2 of that angle.
  3. Here θ≈14.48∘\theta \approx 14.48^\circ, so the angle is 7θ≈101.3∘7\theta \approx 101.3^\circ.
Show the answer

Answer: P=0.961P = 0.961

sin⁡θ=1/16\sin\theta = 1/\sqrt{16} gives θ≈14.48∘\theta \approx 14.48^\circ. After three iterations the state is at 7θ≈101.3∘7\theta \approx 101.3^\circ from ∣bad⟩\ket{\text{bad}}, so the chance is sin⁡2(101.3∘)≈0.9613\sin^2(101.3^\circ) \approx 0.9613.

Common slips

  • 0.980.98: That is sin⁡(7θ)\sin(7\theta), the marked item’s amplitude. The chance is its square, sin⁡2(7θ)\sin^2(7\theta).
  • 0.9970.997: That puts the state at 6θ6\theta. It starts at θ\theta, not at 0, so after three iterations it is at (2⋅3+1)θ=7θ(2\cdot3 + 1)\theta = 7\theta.
  • 0.7180.718: That turns the state by θ\theta per iteration. One iteration is two reflections, and two reflections turn by twice the angle between the mirrors: 2θ2\theta.

Check yourself

Grover search over N=32N = 32 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?

  1. AIt rises, closer to 100%
  2. BIt stays at about 99.9%: the state has settled
  3. CIt falls, to about 86%
  4. DIt falls all the way back to 1/321/32, where it started
Show the answer

Answer: C. It falls, to about 86%

sin⁡θ=1/32\sin\theta = 1/\sqrt{32} gives θ≈10.18∘\theta \approx 10.18^\circ. After 4 iterations the state is at 9θ≈91.6∘9\theta \approx 91.6^\circ from ∣bad⟩\ket{\text{bad}}, near 90∘90^\circ. One more iteration turns it to 11θ≈112.0∘11\theta \approx 112.0^\circ, well past 90∘90^\circ, 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, 2θ2\theta, 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 2θ2\theta, 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 2θ2\theta further, not all the way back. The chance falls to 86%, still well above 1/321/32.

Check yourself

Grover search over N=512N = 512 items with 2 marked items. How many iterations bring the chance of finding a marked item to its first peak?

Hints
  1. Find θ\theta from sin⁡θ=M/N\sin\theta = \sqrt{M/N}, in radians.
  2. You want (2k+1)θ(2k+1)\theta as close as possible to π/2\pi/2: k=π/(4θ)−12k = \pi/(4\theta) - \tfrac12, rounded to a whole number.
  3. Here θ≈0.0625\theta \approx 0.0625 radians, so π/(4θ)−12≈12.06\pi/(4\theta) - \tfrac12 \approx 12.06.
Show the answer

Answer: 1212

sin⁡θ=2/512=116\sin\theta = \sqrt{2/512} = \tfrac{1}{16} gives θ≈0.0625\theta \approx 0.0625 radians, about 3.58∘3.58^\circ. Then π/(4θ)−12≈12.06\pi/(4\theta) - \tfrac12 \approx 12.06, which rounds to 12. Check: 25θ≈89.6∘25\theta \approx 89.6^\circ, close to 90∘90^\circ, and the chance is 100%.

Common slips

  • 1818: That is the count for one marked item, π4N\tfrac{\pi}{4}\sqrt N. With 2 marked items the start angle is larger, sin⁡θ=2/512\sin\theta = \sqrt{2/512}, so fewer iterations reach 90∘90^\circ.
  • 1313: The shortcut π4N/M≈12.57\tfrac{\pi}{4}\sqrt{N/M} \approx 12.57 is a good estimate, but here it rounds to the wrong side. The exact rule rounds π/(4θ)−12≈12.06\pi/(4\theta) - \tfrac12 \approx 12.06, which gives 12: chance 100%, against 98.6% for 13.
  • 1616: N/M\sqrt{N/M} is how the count grows, but the constant matters. The state must turn through about 90∘90^\circ, in steps of 2θ2\theta, which takes about π4N/M\tfrac{\pi}{4}\sqrt{N/M} iterations. 16 iterations give only 77.6%.
  • 1111: One fewer stops short: after 11 iterations the state is at 82.4∘82.4^\circ, and the chance is 98.3%, against 100% after 12.

Check yourself

A secret key has 64 bits, so there are N=264N = 2^{64} possible keys, and an oracle recognises the right one. About how many oracle queries does Grover search need to find the key?

  1. AAbout 64, one query per bit of the key
  2. BAbout 2632^{63}, half the keys
  3. COne: a single query tests every key at once
  4. DAbout 2322^{32} (more exactly π4⋅232≈3.4×109\tfrac{\pi}{4}\cdot 2^{32} \approx 3.4\times10^{9})
Show the answer

Answer: D. About 2322^{32} (more exactly π4⋅232≈3.4×109\tfrac{\pi}{4}\cdot 2^{32} \approx 3.4\times10^{9})

With one marked item among N=264N = 2^{64}, Grover search needs about π4N=π4⋅232≈3.4×109\tfrac{\pi}{4}\sqrt N = \tfrac{\pi}{4}\cdot 2^{32} \approx 3.4\times10^{9} queries, against about 2632^{63} 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 2/N2/\sqrt N radians, so it takes about π4N=π4⋅232\tfrac{\pi}{4}\sqrt N = \tfrac{\pi}{4}\cdot 2^{32} queries: the square root of NN, 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 NN, which is 2322^{32}.
  • 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 π4N\tfrac{\pi}{4}\sqrt N rounds of oracle and diffusion.

Check yourself

A Qiskit program runs Grover search on 4 qubits with the marked item ∣1000⟩\ket{1000} (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?

  1. A1000
  2. B0001
  3. CNo string stands out: all 16 appear about equally often
  4. D0111
Show the answer

Answer: B. 0001

3 iterations put probability 96.1% on ∣1000⟩\ket{1000}. 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 ∣1000⟩\ket{1000} 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 XX gates in the oracle changed the answer. They are undone right after the multi-controlled ZZ; they only move the sign flip onto ∣1000⟩\ket{1000}. The result is ∣1000⟩\ket{1000}, 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.

  1. 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: a→2aˉ−aa \to 2\bar a - a
    • For four items with one marked, one iteration gives the marked item amplitude 1
  2. Draw one iteration in the plane of ∣good⟩\ket{\text{good}} and ∣bad⟩\ket{\text{bad}}. Where does the state start, and what do the two steps do?

    Show the key points
    • ∣s⟩\ket s starts at the angle θ\theta from ∣bad⟩\ket{\text{bad}}, with sin⁡θ=M/N\sin\theta = \sqrt{M/N}
    • The oracle mirrors the state in ∣bad⟩\ket{\text{bad}} and the diffusion step in ∣s⟩\ket s; together they turn it by 2θ2\theta
    • After kk iterations the chance of a marked item is sin⁡2((2k+1)θ)\sin^2((2k+1)\theta)
  3. How many iterations should you run, what goes wrong with more, and how big is the speedup?

    Show the key points
    • The kk that puts (2k+1)θ(2k+1)\theta nearest 90∘90^\circ, about π4N/M\tfrac{\pi}{4}\sqrt{N/M}
    • More iterations turn the state past ∣good⟩\ket{\text{good}}, and the chance falls
    • About N\sqrt N queries against about N/2N/2: a quadratic speedup, not an exponential one, and the best possible

Key ideas

  1. Unstructured search: with one marked item among NN, a classical search needs about N/2N/2 checks on average and N−1N - 1 in the worst case.

  2. The phase oracle O∣x⟩=(−1)f(x)∣x⟩O\ket x = (-1)^{f(x)}\ket x flips the sign of every marked item. On its own it changes no probability.

  3. The diffusion step D=2∣s⟩⟨s∣−ID = 2\ket s\bra s - I reflects every amplitude about the mean. The circuit HH, XX, multi-controlled ZZ, XX, HH gives −D-D, which is DD up to a global phase.

  4. In the plane of ∣good⟩\ket{\text{good}} and ∣bad⟩\ket{\text{bad}}, ∣s⟩\ket s sits at the angle θ\theta from ∣bad⟩\ket{\text{bad}}, with sin⁡θ=M/N\sin\theta = \sqrt{M/N}. The two steps are mirrors; one iteration turns the state by 2θ2\theta, and kk iterations succeed with chance sin⁡2((2k+1)θ)\sin^2((2k+1)\theta).

  5. Run about π4N/M\tfrac{\pi}{4}\sqrt{N/M} iterations and stop, since more turn the state past ∣good⟩\ket{\text{good}}. 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.

Start the interactive lesson