Skip to the content
Kets & Gates homeOpen the app

Lesson 17: Amplitude amplification

Boosting any algorithm’s success chance, even when you don’t know the odds

about 40 min13 partsbest after lesson 16

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 / 13 · Learn

Before you start

Lesson 16 found a marked item among NN by turning a state step by step toward it. This lesson keeps the turning and drops the search. Take any quantum algorithm whose result is good with probability pp: the same kind of step raises that chance close to 1 in about 1/p1/\sqrt p rounds, where rerunning the algorithm takes about 1/p1/p tries.

Then: what to do when you don’t know pp, and what the speed-up does not give you.

By the end of this lesson you can:

  • write the iterate Q=−AS0A−1SgoodQ = -AS_0A^{-1}S_{\text{good}} for any algorithm AA, and build it in code;
  • explain why QQ turns the state by 2θ2\theta, where sin⁡θ=p\sin\theta = \sqrt p, and compute the success chance sin⁡2((2k+1)θ)\sin^2((2k+1)\theta) after kk rounds;
  • choose the number of rounds, and compare the cost with rerunning;
  • succeed without knowing pp, by choosing the number of rounds at random;
  • say what Grover search does not do: it is no faster database lookup, and its square-root gain cannot be beaten.

Check yourself

A warm-up: make a guess before reading on. The lesson comes back to it.

An algorithm AA gives a good answer with probability pp, and you can check any answer. Rerunning AA until it succeeds takes about 1/p1/p runs, so if pp becomes 100 times smaller, rerunning costs 100 times more. This lesson’s method replaces rerunning. If pp becomes 100 times smaller, how does the cost of that method change?

  1. AIt grows 100 times too: nothing beats rerunning
  2. BIt grows about 10 times
  3. CIt grows by a fixed amount: about 7 more rounds
  4. DIt doesn’t change: one quantum run checks every answer at once
Show the answer

Answer: B. It grows about 10 times

Each round turns the state by 2θ2\theta, where sin⁡θ=p\sin\theta = \sqrt p, and it has to turn through about 90∘90^\circ. For small pp, θ≈p\theta \approx \sqrt p radians, so it takes about π4/p\tfrac{\pi}{4}/\sqrt p rounds: 100 times smaller pp means 10 times more rounds.

Why not the others

  • A. Rerunning grows like 1/p1/p, but it is not the only option. Amplitude amplification turns the state by a fixed angle per round, and the number of rounds grows like 1/p1/\sqrt p: 100 times smaller pp costs only 10 times more.
  • C. That would be a logarithmic cost, like halving an interval 7 times. Amplitude amplification gains a square root, not more: 100 times smaller pp needs about 10 times as many rounds, and no method does better in general.
  • D. A run on a superposition still ends in one measurement with one random result. The good results have to be made likely first, and that takes rounds: about π4/p\tfrac{\pi}{4}/\sqrt p of them.

Check yourself

A warm-up: make a guess before reading on. The lesson comes back to it.

A Grover search over N=64N = 64 items is tuned for one marked item: it makes 6 iterations, which find that item with probability 99.7%. Unknown to you, 4 items are marked. With the same 6 iterations, what is the chance of finding a marked item?

  1. AOver 99.7%: more marked items are easier to find
  2. BAbout 99.7%: the number of marked items doesn’t matter
  3. CAbout 6.3%, the chance of a random guess
  4. DAbout 2.0%, worse than a random guess
Show the answer

Answer: D. About 2.0%, worse than a random guess

With one marked item sin⁡θ=1/64\sin\theta = \sqrt{1/64}, so θ≈7.18∘\theta \approx 7.18^\circ, and 6 iterations reach 13θ≈93.3∘13\theta \approx 93.3^\circ. With 4 marked items sin⁡θ\sin\theta doubles: θ≈14.48∘\theta \approx 14.48^\circ, and the same 6 iterations reach about 188.2∘188.2^\circ, past ∣good⟩\ket{\text{good}} at 90∘90^\circ and nearly round to the unmarked side. The chance is sin⁡2(188.2∘)≈0.02\sin^2(188.2^\circ) \approx 0.02, while a random guess gets 4/64≈0.0634/64 \approx 0.063.

Why not the others

  • A. More marked items make each iteration turn the state further: sin⁡θ=4/64\sin\theta = \sqrt{4/64} is twice 1/64\sqrt{1/64}. 6 iterations then carry the state past the marked items, so more turning is not better here.
  • B. The angle of each iteration depends on how many items are marked. With 4 of them θ\theta is about twice as large, so 6 iterations turn the state about twice as far as planned.
  • C. The iterations are not wasted: they move the state, just too far. It ends close to the unmarked states, and the chance, 2.0%, is below even a random guess.

2 / 13 · Learn

From search to any algorithm

Grover search starts from H⊗n∣0…0⟩H^{\otimes n}\ket{0\ldots0}, which gives every item the same small amplitude. Many problems allow a better start: some circuit AA that is already right with probability pp.

So let AA be any circuit with no measurements in it, and call some outcomes good. You need a way to recognize them, nothing more. Measuring A∣0…0⟩A\ket{0\ldots0} gives a good outcome with probability pp, and rerunning AA until one appears takes 1/p1/p runs on average. Amplitude amplification does better with one iterate:

Q=−A S0 A−1 SgoodQ = -A\,S_0\,A^{-1}\,S_{\text{good}}
  • SgoodS_{\text{good}} flips the sign of every good basis state;
  • A−1A^{-1} undoes AA (possible because AA is unitary);
  • S0S_0 flips the sign of ∣0…0⟩\ket{0\ldots0} alone;
  • AA runs again.

Read the product right to left: SgoodS_{\text{good}} acts first. The minus sign in front is a global phase.

First AA, then kk rounds of QQ, then a measurement in the 0/1 basis. Inside each round the four pieces act in time order, which is the reverse of their order in the product Q=−AS0A−1SgoodQ = -AS_0A^{-1}S_{\text{good}}.

Check yourself

Which piece of QQ needs to know what a good outcome looks like?

  1. ANone: QQ finds the good outcomes by trying them all
  2. BAA and A−1A^{-1}: the algorithm has to aim at the good outcomes
  3. CSgoodS_{\text{good}}
  4. DS0S_0
Show the answer

Answer: C. SgoodS_{\text{good}}

Only SgoodS_{\text{good}} depends on which outcomes are good. S0S_0 depends only on the number of qubits, and AA is the given algorithm. In search, SgoodS_{\text{good}} is the oracle; in general, recognizing a good outcome is often far easier than producing one.

Why not the others

  • A. QQ never reads out every outcome. Its only contact with the problem is SgoodS_{\text{good}}, which flips the sign of the good states. Without it, QQ would have nothing to steer toward.
  • B. AA is the algorithm you already had. It doesn’t need to know which of its outcomes are good, only to produce them with some probability pp. Recognizing them is the job of SgoodS_{\text{good}}.
  • D. S0S_0 only singles out the all-zeros state ∣0…0⟩\ket{0\ldots0}, the input of AA. It is the same circuit for every problem on the same number of qubits.

Grover search is the special case A=H⊗nA = H^{\otimes n}, with “good” meaning marked. Then p=M/Np = M/N for MM marked items among N=2nN = 2^n, SgoodS_{\text{good}} is the oracle, and −H⊗nS0H⊗n=2∣s⟩⟨s∣−I-H^{\otimes n}S_0H^{\otimes n} = 2\ket s\bra s - I is the diffusion step, the reflection about the uniform superposition ∣s⟩\ket s.

For any AA, the same holds with ∣ψ⟩=A∣0…0⟩\ket\psi = A\ket{0\ldots0} in place of ∣s⟩\ket s: −AS0A−1=2∣ψ⟩⟨ψ∣−I-AS_0A^{-1} = 2\ket\psi\bra\psi - I.

A map that keeps one direction and flips the sign of every direction at right angles to it is a reflection, like a mirror whose line is that direction. The formula says that half of QQ is a reflection in the line of ∣ψ⟩\ket\psi. Check the claim on ∣ψ⟩\ket\psi itself, one piece at a time, and watch the signs.

Check yourself

Let ∣ψ⟩=A∣0…0⟩\ket\psi = A\ket{0\ldots0}. Follow ∣ψ⟩\ket\psi through −AS0A−1-AS_0A^{-1}, one piece at a time. What comes out?

  1. A∣0…0⟩\ket{0\ldots0}
  2. BA state closer to the good outcomes
  3. C−∣ψ⟩-\ket\psi
  4. D∣ψ⟩\ket\psi, unchanged
Hints
  1. Start with A−1∣ψ⟩A^{-1}\ket\psi: what is it?
  2. Then S0S_0 flips the sign of ∣0…0⟩\ket{0\ldots0}, AA runs, and the leading minus sign acts last.
Show the answer

Answer: D. ∣ψ⟩\ket\psi, unchanged

A−1∣ψ⟩=∣0…0⟩A^{-1}\ket\psi = \ket{0\ldots0}, then S0S_0 flips it to −∣0…0⟩-\ket{0\ldots0}, AA gives −∣ψ⟩-\ket\psi, and the minus sign in front restores +∣ψ⟩+\ket\psi. A state orthogonal to ∣ψ⟩\ket\psi instead ends with a minus sign, so −AS0A−1-AS_0A^{-1} is a mirror whose line is ∣ψ⟩\ket\psi.

Why not the others

  • A. A−1A^{-1} alone would do that, but AA runs again at the end and brings it back: the net effect on ∣ψ⟩\ket\psi is no change at all.
  • B. This half of QQ only reflects: it keeps the line of ∣ψ⟩\ket\psi and flips the sign of everything orthogonal to it. The turn toward the good states comes from combining both halves, as the next segments show.
  • C. Through the pieces: A−1∣ψ⟩=∣0…0⟩A^{-1}\ket\psi = \ket{0\ldots0}, S0S_0 gives −∣0…0⟩-\ket{0\ldots0}, AA gives −∣ψ⟩-\ket\psi. You still need the minus sign in front of −AS0A−1-AS_0A^{-1}, which makes it +∣ψ⟩+\ket\psi.

3 / 13 · Explore

One round on one qubit

The smallest example has one qubit. Ry(ϕ)R_y(\phi), Qiskit’s ry, turns ∣0⟩\ket0 into cos⁡ϕ2∣0⟩+sin⁡ϕ2∣1⟩\cos\tfrac\phi2\ket0 + \sin\tfrac\phi2\ket1, and its inverse is Ry(−ϕ)R_y(-\phi). Take A=Ry(π/3)A = R_y(\pi/3):

A∣0⟩=32∣0⟩+12∣1⟩.A\ket0 = \tfrac{\sqrt3}{2}\ket0 + \tfrac12\ket1.

Call ∣1⟩\ket1 good, so p=14p = \tfrac14. Then Sgood=ZS_{\text{good}} = Z, which flips the sign of ∣1⟩\ket1, and S0=XZXS_0 = XZX, which flips the sign of ∣0⟩\ket0.

Predict, then see

A=Ry(π/3)A = R_y(\pi/3) succeeds with p=14p = \tfrac14. After AA and one round of QQ, what is the chance of measuring ∣1⟩\ket1?

  1. A34\tfrac34: the chance triples
  2. B1: the result is ∣1⟩\ket1 every time
  3. C14\tfrac14: every piece only flips signs or undoes AA, so the chance can’t change
  4. D12\tfrac12: each round adds another pp

Where did the extra probability come from? In a sentence or two: how can steps that only flip signs or undo AA change a probability?

Show what happens

What happens: B. 1: the result is ∣1⟩\ket1 every time

Write A∣0⟩=cos⁡θ∣0⟩+sin⁡θ∣1⟩A\ket0 = \cos\theta\ket0 + \sin\theta\ket1 with θ=30∘\theta = 30^\circ, since sin⁡θ=12=p\sin\theta = \tfrac12 = \sqrt p. Step by step: ZZ gives (32,−12)(\tfrac{\sqrt3}{2}, -\tfrac12), at −30∘-30^\circ; A−1A^{-1} turns that back by 30∘30^\circ to (12,−32)(\tfrac12, -\tfrac{\sqrt3}{2}); XZXXZX flips the first entry, (−12,−32)(-\tfrac12, -\tfrac{\sqrt3}{2}); AA turns forward by 30∘30^\circ to (0,−1)(0, -1). That is −∣1⟩-\ket1, and with the minus sign in front of QQ, exactly ∣1⟩\ket1. The good amplitude went from sin⁡30∘\sin 30^\circ to sin⁡90∘\sin 90^\circ: one round turned the state by 60∘=2θ60^\circ = 2\theta.

Why not the others

  • A. The program gives 1. For small pp the good amplitude roughly triples in the first round, so the chance grows about ninefold, not threefold. Here pp is not small, and the exact rule is a turn from 30∘30^\circ to 90∘90^\circ.
  • C. Each sign flip alone leaves the chances alone, but A−1A^{-1} and AA sit between the flips and mix the amplitudes. A sign flipped before the mixing changes what comes out after it: the program shows P(1)P(1) going from 25% to 100%.
  • D. Adding about pp per try is how rerunning behaves. QQ acts on amplitudes instead: it turns the state by a fixed angle, and here one turn lands exactly on ∣1⟩\ket1.

4 / 13 · Learn

Two reflections make a rotation

The example is no accident. Split ∣ψ⟩=A∣0…0⟩\ket\psi = A\ket{0\ldots0} into its good and bad parts:

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

where sin⁡θ=p\sin\theta = \sqrt p, ∣good⟩\ket{\text{good}} is the normalized good part and ∣bad⟩\ket{\text{bad}} the normalized rest. Both halves of QQ are mirrors in the plane of these two states:

  • SgoodS_{\text{good}} flips the ∣good⟩\ket{\text{good}} part: a mirror along ∣bad⟩\ket{\text{bad}};
  • −AS0A−1-AS_0A^{-1} keeps ∣ψ⟩\ket\psi and flips what is orthogonal to it: a mirror along ∣ψ⟩\ket\psi.

Two mirrors whose lines meet at angle θ\theta make a rotation by 2θ2\theta. So each round turns the state 2θ2\theta further toward ∣good⟩\ket{\text{good}}, and after kk rounds

Qk∣ψ⟩=sin⁡((2k+1)θ)∣good⟩+cos⁡((2k+1)θ)∣bad⟩.\begin{aligned}Q^k\ket\psi = {}&\sin((2k+1)\theta)\ket{\text{good}} \\ &+ \cos((2k+1)\theta)\ket{\text{bad}}.\end{aligned}

The chance of a good outcome is sin⁡2((2k+1)θ){\sin^2((2k+1)\theta)}.

The example from above, p=14p = \tfrac14, so θ=30∘\theta = 30^\circ. Step 1 mirrors ∣ψ⟩\ket\psi in the ∣bad⟩\ket{\text{bad}} axis, to −30∘-30^\circ. Step 2 mirrors that in the line of ∣ψ⟩\ket\psi, to 90∘90^\circ, which is ∣good⟩\ket{\text{good}}. The net effect is a turn by 2θ=60∘2\theta = 60^\circ.

Check yourself

An algorithm already succeeds with p=34p = \tfrac34. You apply one round of QQ anyway. What is the chance of a good outcome now?

Hints
  1. Find θ\theta from sin⁡θ=p\sin\theta = \sqrt p.
  2. 3/4=3/2=sin⁡60∘\sqrt{3/4} = \sqrt3/2 = \sin 60^\circ. After one round the angle is 3θ3\theta.
Show the answer

Answer: 00

sin⁡θ=3/2\sin\theta = \sqrt3/2, so θ=60∘\theta = 60^\circ. One round turns the state to 3θ=180∘3\theta = 180^\circ, which is −∣bad⟩-\ket{\text{bad}}, and sin⁡2180∘=0\sin^2 180^\circ = 0. Above p=12p = \tfrac12 one round already makes things worse; there you would just run AA and check.

Common slips

  • 11: One round lands on ∣good⟩\ket{\text{good}} only when 3θ=90∘3\theta = 90^\circ, that is for p=14p = \tfrac14. Here sin⁡θ=3/2\sin\theta = \sqrt3/2, so θ=60∘\theta = 60^\circ and 3θ=180∘3\theta = 180^\circ: the state has turned past ∣good⟩\ket{\text{good}} all the way to −∣bad⟩-\ket{\text{bad}}.
  • 34\frac{3}{4}: The round does change the state: it turns it by 2θ=120∘2\theta = 120^\circ, from 60∘60^\circ to 180∘180^\circ, and sin⁡2180∘=0\sin^2 180^\circ = 0.
  • 14\frac{1}{4}: That is 1−p1 - p, the bad part before the round. After the round the angle is 3θ=180∘3\theta = 180^\circ, and the good chance is sin⁡2180∘=0\sin^2 180^\circ = 0.

5 / 13 · Worked example

Worked example: how many rounds?

The best number of rounds makes (2k+1)θ(2k+1)\theta as close to 90∘90^\circ as possible: kk is the whole number nearest to 90∘2θ−12\dfrac{90^\circ}{2\theta} - \dfrac12. Keep a calculator at hand for θ=arcsin⁡p\theta = \arcsin\sqrt p.

Worked example

An algorithm that succeeds once in a hundred runs

First p=1100p = \tfrac{1}{100}, worked for you; then p=125p = \tfrac{1}{25}, which you finish.

  1. Angle

    sin⁡θ=1/100=110\sin\theta = \sqrt{1/100} = \tfrac{1}{10}, so θ=arcsin⁡0.1≈5.74∘\theta = \arcsin 0.1 \approx 5.74^\circ.

  2. Rounds

    90∘2θ−12≈7.84−0.5=7.34\dfrac{90^\circ}{2\theta} - \dfrac12 \approx 7.84 - 0.5 = 7.34, so k=7k = 7. The state ends at 15θ≈86.1∘15\theta \approx 86.1^\circ.

  3. Chance

    sin⁡286.1∘≈0.995\sin^2 86.1^\circ \approx 0.995. With k=8k = 8 the state would pass 90∘90^\circ, to 17θ≈97.6∘17\theta \approx 97.6^\circ, and the chance would drop to 0.9830.983.

  4. Cost

    In all, AA and A−1A^{-1} run 2k+1=152k + 1 = 15 times, and SgoodS_{\text{good}} runs 7 times. Rerunning AA needs 100 runs on average, and 15 reruns succeed with chance only 1−0.9915≈0.141 - 0.99^{15} \approx 0.14.

  5. Your turn: rounds

    Now p=125p = \tfrac{1}{25}: sin⁡θ=15\sin\theta = \tfrac15, so θ≈11.54∘\theta \approx 11.54^\circ.

    Your turn

    With θ≈11.54∘\theta \approx 11.54^\circ, how many rounds of QQ make a good outcome most likely?

    Hints
    1. Which kk puts (2k+1)θ(2k+1)\theta closest to 90∘90^\circ?
    2. 90∘2θ−12=9023.07−0.5\dfrac{90^\circ}{2\theta} - \dfrac12 = \dfrac{90}{23.07} - 0.5.
    Show the answer

    Answer: 33

    90∘2θ−12≈3.90−0.5=3.40\dfrac{90^\circ}{2\theta} - \dfrac12 \approx 3.90 - 0.5 = 3.40, so k=3k = 3: the state ends at 7θ≈80.8∘7\theta \approx 80.8^\circ.

    Common slips

    • 44: Four rounds take the state to 9θ≈103.8∘9\theta \approx 103.8^\circ, past 90∘90^\circ, where the chance is 0.9430.943. Remember the −12-\tfrac12: the state already starts at θ\theta, so three rounds reach 7θ≈80.8∘7\theta \approx 80.8^\circ.
    • 2525: 25 is 1/p1/p, the average number of reruns of AA. Rounds of QQ grow like 1/p1/\sqrt p: the state needs to turn only about 90∘90^\circ, in steps of 2θ≈23∘2\theta \approx 23^\circ.
    • 22: Two rounds reach 5θ≈57.7∘5\theta \approx 57.7^\circ, with chance 0.7140.714. One more round gets much closer to 90∘90^\circ.
  6. Your turn: chance

    Your turn

    With k=3k = 3 rounds and θ≈11.54∘\theta \approx 11.54^\circ, what is the chance of a good outcome? (Two or three decimal places.)

    Hints
    1. The angle after kk rounds is (2k+1)θ(2k+1)\theta.
    2. 7×11.54∘≈80.76∘7 \times 11.54^\circ \approx 80.76^\circ; square its sine.
    Show the answer

    Answer: 0.9740.974

    7θ≈80.76∘7\theta \approx 80.76^\circ, and sin⁡280.76∘≈0.974\sin^2 80.76^\circ \approx 0.974. Rerunning AA the same 2k+1=72k+1 = 7 times would succeed with chance 1−0.967≈0.251 - 0.96^7 \approx 0.25.

    Common slips

    • 125\frac{1}{25}: That is pp, the chance before any round. After three rounds the angle is 7θ≈80.8∘7\theta \approx 80.8^\circ.
    • 0.7140.714: That is the chance after two rounds, at 5θ5\theta. Three rounds reach 7θ7\theta.
    • 0.9430.943: That is the chance after four rounds, at 9θ≈103.8∘9\theta \approx 103.8^\circ. Three rounds reach 7θ≈80.8∘7\theta \approx 80.8^\circ.

One more, fully worked, with p=19p = \tfrac19.

Another worked example

Another example: p = 1/9

  1. Angle

    sin⁡θ=1/9=13\sin\theta = \sqrt{1/9} = \tfrac13, so θ≈19.47∘\theta \approx 19.47^\circ.

  2. Rounds

    90∘2θ−12≈2.31−0.5=1.81\dfrac{90^\circ}{2\theta} - \dfrac12 \approx 2.31 - 0.5 = 1.81, so k=2k = 2. The state ends at 5θ≈97.4∘5\theta \approx 97.4^\circ: a little past 90∘90^\circ, but closer than 3θ≈58.4∘3\theta \approx 58.4^\circ.

  3. Chance

    sin⁡297.4∘≈0.984\sin^2 97.4^\circ \approx 0.984, against sin⁡258.4∘≈0.726\sin^2 58.4^\circ \approx 0.726 for k=1k = 1. Rerunning AA five times would succeed with chance 1−(89)5≈0.451 - (\tfrac89)^5 \approx 0.45.

6 / 13 · Practice

Build Q in code

In code, A−1A^{-1} of a rotation is the same rotation with the opposite angle, and the minus sign in front of QQ needs no gate. So one round of QQ is the six gate calls from the prediction program. To apply several rounds, write the round once as a function and call it in a loop.

The same recipe works on any number of qubits. For a circuit a, Qiskit’s a.inverse() builds A−1A^{-1}: the gates in reverse order, each one inverted. S0S_0 is an XX on every qubit, a ZZ controlled by all the others, and the XX gates again. The example under the exercise does this on three qubits.

Check yourself

Now take A=Ry(π/9)A = R_y(\pi/9), so A∣0⟩=cos⁡θ∣0⟩+sin⁡θ∣1⟩A\ket0 = \cos\theta\ket0 + \sin\theta\ket1 with θ=10∘\theta = 10^\circ, and p=sin⁡2θ≈0.03p = \sin^2\theta \approx 0.03. Call ∣1⟩\ket1 good. Then 4 rounds of QQ reach (2⋅4+1)θ=90∘(2\cdot 4 + 1)\theta = 90^\circ. Complete the program: write one round of QQ in q_round, then apply it 4 times, so that the qubit ends in ∣1⟩\ket1 (up to a global phase).

from qiskit import QuantumCircuit
from math import pi

qc = QuantumCircuit(1)
qc.ry(pi / 9, 0)     # A

def q_round(qc):
    # one round of Q: S_good, then A^-1, then S0, then A
    pass

# apply q_round 4 times here

Write the program in the app’s code editor (Python with Qiskit’s circuit methods), run it, and check it.

Hints
  1. Copy the six gate calls of one round from the prediction program, with the angle changed to pi / 9.
  2. Put them inside q_round (indented by four spaces, replacing pass), then call q_round(qc) 4 times, for example in for i in range(4):.
Show the answer

One program that works:

from qiskit import QuantumCircuit
from math import pi

qc = QuantumCircuit(1)
qc.ry(pi / 9, 0)     # A

def q_round(qc):
    qc.z(0)              # S_good: flip the sign of |1>
    qc.ry(-pi / 9, 0)    # A^-1
    qc.x(0)              # S0 = X Z X: flip the sign of |0>
    qc.z(0)
    qc.x(0)
    qc.ry(pi / 9, 0)     # A

for i in range(4):
    q_round(qc)

Each round turns the state by 2θ2\theta, so after 4 rounds it sits at (2⋅4+1)θ=90∘(2\cdot 4+1)\theta = 90^\circ from ∣0⟩\ket0: the state is ±∣1⟩\pm\ket1, and the sign is a global phase. (On one qubit, plain ZZ in place of XZXXZX also passes, since XZX=−ZXZX = -Z differs only by a global sign. With more qubits, S0S_0 and SgoodS_{\text{good}} are different mirrors.)

One round is Sgood=ZS_{\text{good}} = Z, then A−1=Ry(−π/9)A^{-1} = R_y(-\pi/9), then S0=XZXS_0 = XZX, then A=Ry(π/9)A = R_y(\pi/9); the minus sign in front of QQ is a global phase and needs no gate. Apply that round 4 times after the first AA. The angle goes θ→3θ→⋯→(2⋅4+1)θ=90∘\theta \to 3\theta \to \cdots \to (2\cdot4+1)\theta = 90^\circ, so the final state is ±∣1⟩\pm\ket1.

7 / 13 · Learn

What it costs

For small pp, θ≈p\theta \approx \sqrt p in radians, and the best number of rounds is close to

k≈π4θ≈π4⋅1p.k \approx \frac{\pi}{4\theta} \approx \frac{\pi}{4}\cdot\frac{1}{\sqrt p}.

Each round runs AA once, A−1A^{-1} once and SgoodS_{\text{good}} once. Rerunning AA needs 1/p1/p runs on average. For p=10−6p = 10^{-6} that is about 785 rounds against a million runs: a square-root saving, the same one Grover search gets. With A=H⊗nA = H^{\otimes n} and p=M/Np = M/N it reads k≈π4N/Mk \approx \tfrac{\pi}{4}\sqrt{N/M}.

The chance is not monotonic in kk. Past the best kk the state turns beyond ∣good⟩\ket{\text{good}}, and the chance falls again.

For p=1100p = \tfrac{1}{100}: the bars show sin⁡2((2k+1)θ)\sin^2((2k+1)\theta) after kk rounds, best at k=7k = 7 with 99.5%. The dots show rerunning AA as often as kk rounds use AA and A−1A^{-1}, that is 2k+12k+1 times: 15 runs give only 14%. Past k=7k = 7 the bars shrink, because the state has turned past ∣good⟩\ket{\text{good}}.

Check yourself

An algorithm succeeds with probability p=10−4p = 10^{-4}. About how many rounds of QQ make a good outcome nearly certain?

Hints
  1. For small pp, θ≈p\theta \approx \sqrt p in radians.
  2. Rounds ≈π4θ≈π4⋅1p\approx \dfrac{\pi}{4\theta} \approx \dfrac{\pi}{4}\cdot\dfrac{1}{\sqrt p}.
Show the answer

Answer: 7878

p=10−2\sqrt p = 10^{-2}, so θ≈10−2\theta \approx 10^{-2} radians and about π4⋅100≈78.5\tfrac{\pi}{4}\cdot 100 \approx 78.5 rounds; the exact rule gives k=78k = 78, with success over 99.9%. Rerunning would need about 10,000 runs on average.

Common slips

  • 1000010000: 10,000 is 1/p1/p, the average number of reruns of AA. Rounds of QQ grow like 1/p1/\sqrt p instead.
  • 100100: 1/p=1001/\sqrt p = 100 is the right scale, but the state only has to turn about 90∘90^\circ in steps of 2θ≈2p2\theta \approx 2\sqrt p: that is about π4⋅100≈78.5\tfrac{\pi}{4}\cdot 100 \approx 78.5 rounds.
  • 157157: 157 counts every use of AA and A−1A^{-1}, 2k+12k+1 in all. The question asks for rounds of QQ, each of which uses both.

8 / 13 · Learn

When you don’t know p

Everything so far needed pp to choose kk. Often you don’t know it: you don’t know how many items are marked, or how often your algorithm succeeds. A wrong guess is costly, because the chance oscillates: too many rounds can be worse than none. The search in the second prequestion shows how badly it can go.

The fix, from Boyer, Brassard, Høyer and Tapp (1998), is to choose kk at random. Fix a bound mm and pick kk uniformly from 0,1,…,m−10, 1, \ldots, m-1. Averaged over kk, the chance of success is

1m∑k=0m−1sin⁡2((2k+1)θ)=12−sin⁡(4mθ)4msin⁡2θ.\frac1m\sum_{k=0}^{m-1}\sin^2((2k+1)\theta) = \frac12 - \frac{\sin(4m\theta)}{4m\sin 2\theta}.

Why about 12\tfrac12: the angles (2k+1)θ(2k+1)\theta spread around the circle, and sin⁡2\sin^2 averages 12\tfrac12 over a full turn. The second term shrinks as mm grows. Since ∣sin⁡(4mθ)∣≤1\abs{\sin(4m\theta)} \le 1, as soon as m≥1/sin⁡2θm \ge 1/\sin 2\theta that term is at most 14\tfrac14, so the average is at least 14\tfrac14.

For p=1100p = \tfrac{1}{100}, the average success when kk is picked at random below mm. Once mm passes 1/sin⁡2θ≈51/\sin 2\theta \approx 5, the average is at least 14\tfrac14, and it settles near 12\tfrac12. The dashed curve, 12−1/(4msin⁡2θ)\tfrac12 - 1/(4m\sin 2\theta), is the floor the formula guarantees.

Check yourself

Take p=14p = \tfrac14, so θ=30∘\theta = 30^\circ. You pick kk at random from 0,1,2,30, 1, 2, 3 (so m=4m = 4), run AA and kk rounds of QQ, and measure. What is the chance of a good outcome?

Hints
  1. List the angles (2k+1)θ(2k+1)\theta for k=0,1,2,3k = 0, 1, 2, 3.
  2. 30∘,90∘,150∘,210∘30^\circ, 90^\circ, 150^\circ, 210^\circ: square each sine, then average.
Show the answer

Answer: 716\frac{7}{16}

The angles are 30∘,90∘,150∘,210∘30^\circ, 90^\circ, 150^\circ, 210^\circ, with chances 14,1,14,14\tfrac14, 1, \tfrac14, \tfrac14. Their average is 7/44=716≈0.44\tfrac{7/4}{4} = \tfrac{7}{16} \approx 0.44. The formula agrees: 12−sin⁡480∘16sin⁡60∘=12−116\tfrac12 - \tfrac{\sin 480^\circ}{16\sin 60^\circ} = \tfrac12 - \tfrac{1}{16}.

Common slips

  • 11: One round, k=1k = 1, gives certainty, but you pick kk at random: three of the four choices end at 30∘30^\circ, 150∘150^\circ or 210∘210^\circ, each with chance 14\tfrac14.
  • 14\frac{1}{4}: Three of the four choices give 14\tfrac14, but k=1k = 1 gives 1. The chance is the average, 14(14+1+14+14)\tfrac14(\tfrac14 + 1 + \tfrac14 + \tfrac14).
  • 12\frac{1}{2}: 12\tfrac12 is where the average settles for large mm. For m=4m = 4, average the four chances: 14(14+1+14+14)\tfrac14(\tfrac14 + 1 + \tfrac14 + \tfrac14).

The full method grows mm as it goes, so it never needs θ\theta:

  1. Start with m=1m = 1.
  2. Pick kk at random from 0,…,m−10, \ldots, m-1. Run AA, apply QQ kk times, measure, and check the result.
  3. If it is good, stop. If not, multiply mm by a fixed factor (the paper uses 65\tfrac65) and go back to step 2.

While mm is small the tries are cheap. Once mm passes 1/sin⁡2θ1/\sin 2\theta, each try succeeds with chance at least 14\tfrac14. Added up, the expected number of rounds is of order 1/p1/\sqrt p, which for search is order N/M\sqrt{N/M}: the same scaling as knowing pp, with a larger constant. If nothing is marked, nothing can succeed, so you stop once the total passes a fixed multiple of N\sqrt N and report “none”. Had there been a marked item, it would very likely have turned up by then.

Check yourself

Why does the method start with m=1m = 1 and grow it by 65\tfrac65 after each failure, instead of choosing a huge mm from the start?

  1. AA huge mm makes the average success drop toward 0
  2. BA huge mm makes kk huge too, which wastes rounds whenever pp turns out to be large
  3. CBecause kk has to hit the best number of rounds exactly
  4. Dmm must stay below 1/sin⁡2θ1/\sin 2\theta
Show the answer

Answer: B. A huge mm makes kk huge too, which wastes rounds whenever pp turns out to be large

Each try costs about kk rounds, and kk is typically about m/2m/2. Growing mm by a constant factor from 1 means mm reaches 1/sin⁡2θ1/\sin 2\theta having spent only a constant multiple of that many rounds, whatever θ\theta is. A huge starting mm would spend order mm rounds per try even when pp is large and a round or two would do.

Why not the others

  • A. For large mm the average settles near 12\tfrac12, not 0: the second term of the formula shrinks like 1/m1/m. The trouble is cost: a random kk below a huge mm is usually huge, and each round costs a run of AA and A−1A^{-1}.
  • C. No exact kk is needed. Once mm is large enough, a random kk below it succeeds with chance about 12\tfrac12 on average.
  • D. It is the other way round: the guarantee of at least 14\tfrac14 starts once mm passes 1/sin⁡2θ1/\sin 2\theta. Growing mm step by step reaches that point without overshooting it by more than a constant factor.

9 / 13 · Learn

The fine print

Grover search is often described as searching a database in N\sqrt N steps. Three details limit what that means.

  • The data has to get in. The oracle must answer “is item xx marked?” for a superposition of positions xx, so the whole list must sit in a memory that a circuit can query in superposition. Writing NN items into it takes order NN steps, as many as a classical computer needs to scan the list once.
  • The oracle is a circuit. Every query runs a circuit that computes the check, and it costs gates, often many.
  • The gain is quadratic. N\sqrt N against NN is a large saving for large NN, but nothing like the exponential gap of Simon’s algorithm in Lesson 15, and the slower, error-corrected gates of a real machine eat into it.

The speed-up pays off when there is no list to load: the items are all nn⁠-bit strings, and being good is a property a circuit can check. Lesson 18 builds such an oracle.

Check yourself

A company keeps a billion (10910^{9}) customer records, unsorted, on ordinary disks. It wants to find the one record with a given ID, once. Which statement is accurate?

  1. AA quantum computer finds it in about 30 queries, one per bit of the record’s position
  2. BA quantum search would not help: putting the records into a quantum memory takes about 10910^{9} steps, as long as one classical scan
  3. CGrover search finds it with about π4109≈25,000\tfrac{\pi}{4}\sqrt{10^{9}} \approx 25{,}000 queries, so it is far faster than a scan
  4. DGrover search needs the records sorted by ID first; after that it is fast
Show the answer

Answer: B. A quantum search would not help: putting the records into a quantum memory takes about 10910^{9} steps, as long as one classical scan

The count N\sqrt N assumes an oracle that answers for a superposition of positions. For stored data that means a quantum memory holding all 10910^{9} records, and filling it costs order NN steps. For a one-off lookup a classical scan costs no more than that.

Why not the others

  • A. That would be an exponential speed-up, and unstructured search has none: every quantum method needs on the order of N\sqrt N queries. And the cost of loading the data comes on top.
  • C. The query count is right, but each query needs the records in a memory that a circuit can address in superposition. Writing 10910^{9} records into it takes about 10910^{9} steps, the cost of the scan you hoped to avoid.
  • D. Grover search needs no order at all; that is its point. And once the records are sorted, a classical binary search takes about log⁡2N\log_2 N steps. The obstacle is getting the data into a quantum memory, which costs order NN.

Could a cleverer quantum algorithm beat N\sqrt N? No. Bennett, Bernstein, Brassard and Vazirani proved in 1997 that any quantum algorithm needs on the order of N\sqrt N queries to find a marked item among NN when nothing else is known about where it is; their paper is listed under Go further. So Grover search, and amplitude amplification with its 1/p1/\sqrt p, are optimal up to a constant factor: a square root is all that interference can buy for unstructured search.

Check yourself

Someone claims a quantum algorithm that finds the one marked item among N=250N = 2^{50} items, with nothing known about where it is, using 1,000 queries. What should you think?

  1. APlausible, if it picks the number of rounds at random
  2. BPlausible, if it applies QQ about 1,000 times
  3. CIt can’t be right as stated: every quantum method needs on the order of N=225\sqrt N = 2^{25}, about 34 million, queries
  4. DPlausible: quantum algorithms are exponentially faster than classical ones
Show the answer

Answer: C. It can’t be right as stated: every quantum method needs on the order of N=225\sqrt N = 2^{25}, about 34 million, queries

For unstructured search every quantum algorithm needs order N\sqrt N queries, and 250=225\sqrt{2^{50}} = 2^{25}. Grover’s π4N≈26,000,000\tfrac{\pi}{4}\sqrt N \approx 26{,}000{,}000 is within a constant factor of that limit. A method using 1,000 queries must be exploiting structure that the problem, as stated, doesn’t have.

Why not the others

  • A. Picking kk at random handles an unknown number of marked items. It costs a constant factor more than knowing that number, never less.
  • B. Every round of QQ is a query: it uses SgoodS_{\text{good}} once. With one marked item among 2502^{50}, about π4⋅225≈26,000,000\tfrac{\pi}{4}\cdot 2^{25} \approx 26{,}000{,}000 rounds are needed.
  • D. Some problems have exponential quantum speed-ups, but unstructured search is proven not to: any quantum algorithm needs order N\sqrt N queries, here about 2252^{25}.

10 / 13 · Practice

Practice

Three items, each with new numbers when it comes back. Hints are there if you need them.

Check yourself

An algorithm succeeds with probability p=125p = \tfrac{1}{25}. What is the chance of a good outcome after AA and 2 rounds of QQ? (Two or three decimal places.)

Hints
  1. Find θ\theta from sin⁡θ=p\sin\theta = \sqrt p.
  2. After kk rounds the angle is (2k+1)θ(2k+1)\theta; the chance is the square of its sine.
  3. Here θ≈11.54∘\theta \approx 11.54^\circ and 5θ≈57.68∘5\theta \approx 57.68^\circ.
Show the answer

Answer: 0.7140.714

θ=arcsin⁡125≈11.54∘\theta = \arcsin\sqrt{\tfrac{1}{25}} \approx 11.54^\circ, so 2 rounds reach 5θ≈57.68∘5\theta \approx 57.68^\circ, and sin⁡2(57.68∘)≈0.714\sin^2(57.68^\circ) \approx 0.714.

sin⁡θ=125\sin\theta = \sqrt{\tfrac{1}{25}} gives θ≈11.54∘\theta \approx 11.54^\circ. Each round adds 2θ2\theta to the angle, so after 2 rounds it is (2⋅2+1)θ≈57.68∘(2\cdot2+1)\theta \approx 57.68^\circ. The chance of a good outcome is sin⁡2(57.68∘)≈0.714\sin^2(57.68^\circ) \approx 0.714.

Common slips

  • 1325\frac{13}{25}: That uses the angle 2kθ2k\theta. The state starts at θ\theta, so after 2 rounds it is at (2k+1)θ=5θ(2k+1)\theta = 5\theta.
  • 0.3230.323: That turns by θ\theta per round. Two reflections whose mirrors meet at θ\theta turn the state by 2θ2\theta.
  • 11: That grows the amplitude in a straight line, (2k+1)p(2k+1)\sqrt p, which only holds while the angle is small. Use sin⁡((2k+1)θ)\sin((2k+1)\theta) itself.
  • 125\frac{1}{25}: That is the chance before any round.

Check yourself

Grover search over N=216N = 2^{16} items, of which M=16M = 16 are marked. About how many iterations make finding a marked item most likely?

Hints
  1. Grover is amplitude amplification with p=M/Np = M/N. For small pp, θ≈p\theta \approx \sqrt p in radians.
  2. Iterations ≈π4θ≈π4N/M\approx \dfrac{\pi}{4\theta} \approx \dfrac{\pi}{4}\sqrt{N/M}.
Show the answer

Answer: 5050

N/M=64\sqrt{N/M} = 64, so about π4⋅64≈50.3\tfrac{\pi}{4}\cdot 64 \approx 50.3 iterations. The exact rule, kk nearest to π4θ−12\tfrac{\pi}{4\theta} - \tfrac12, gives k=50k = 50, with success chance over 99.9%.

Here p=M/N=16/216p = M/N = 16/2^{16}, so sin⁡θ=p\sin\theta = \sqrt p and θ≈0.0156\theta \approx 0.0156 radians. The best kk makes (2k+1)θ(2k+1)\theta closest to π2\tfrac{\pi}{2}: k≈π4θ−12k \approx \tfrac{\pi}{4\theta} - \tfrac12, which rounds to 50. The quick estimate π4N/M≈50.3\tfrac{\pi}{4}\sqrt{N/M} \approx 50.3 is within one of it.

Common slips

  • 6464: N/M=64\sqrt{N/M} = 64 sets the scale, but it is not the count. Each iteration turns the state by 2θ≈2M/N2\theta \approx 2\sqrt{M/N} radians, and it only needs to turn about π2\tfrac{\pi}{2}: that takes about π4⋅64≈50\tfrac{\pi}{4}\cdot 64 \approx 50 iterations.
  • 40964096: 4,096 is 1/p=N/M1/p = N/M, the average number of random guesses. Iterations grow like the square root of that.
  • 201201: 201 is the count for one marked item. With M=16M = 16 marked items sin⁡θ=M/N\sin\theta = \sqrt{M/N} is 16=4\sqrt{16} = 4 times larger, so 4 times fewer iterations are needed.

Check yourself

You don’t know pp; in fact p=14p = \tfrac14, so θ=30∘\theta = 30^\circ. You pick kk at random from 00 to 11 (so m=2m = 2), run AA and kk rounds of QQ, and measure. What is the chance of a good outcome?

Hints
  1. List the angles (2k+1)θ(2k+1)\theta for each allowed kk.
  2. The angles are 30∘,90∘30^\circ, 90^\circ. Square each sine, then average the 2 values.
Show the answer

Answer: 58\frac{5}{8}

The angles 30∘,90∘30^\circ, 90^\circ give chances 14,1\tfrac14, 1. Each kk is equally likely, so the chance is their average, 58\tfrac{5}{8}.

With θ=30∘\theta = 30^\circ, the allowed k=0,…,1k = 0, \ldots, 1 end at the angles (2k+1)θ=30∘,90∘(2k+1)\theta = 30^\circ, 90^\circ. Their chances sin⁡2((2k+1)θ)\sin^2((2k+1)\theta) are 14,1\tfrac14, 1. Picking kk uniformly at random, the chance of a good outcome is the average of these 2 numbers: 58≈0.625\tfrac{5}{8} \approx 0.625.

Common slips

  • 12\frac{1}{2}: 12\tfrac12 is where the average settles when mm is large. For m=2m = 2, average the 2 chances themselves: 14,1\tfrac14, 1.
  • 11: That is the best single kk. Here kk is random, so every choice from 0 to 1 counts equally: average the 2 chances.
  • 14\frac{1}{4}: That is k=0k = 0 alone: no rounds at all. Average over all the allowed kk.

11 / 13 · Explain

Explain it

Explain it

Each piece of QQ only flips signs or undoes AA. Explain why applying QQ to A∣0…0⟩A\ket{0\ldots0} still raises the chance of a good outcome, and why too many rounds lower it again.

Show a model answer

Write A∣0…0⟩=sin⁡θ∣good⟩+cos⁡θ∣bad⟩A\ket{0\ldots0} = \sin\theta\ket{\text{good}} + \cos\theta\ket{\text{bad}} with sin⁡θ=p\sin\theta = \sqrt p. In the plane of these two states, SgoodS_{\text{good}} flips the good part, which is a reflection in the ∣bad⟩\ket{\text{bad}} line, and −AS0A−1-AS_0A^{-1} is a reflection in the line of A∣0…0⟩A\ket{0\ldots0}. The two lines meet at angle θ\theta, and two reflections make a rotation by twice that angle: each round turns the state 2θ2\theta toward ∣good⟩\ket{\text{good}}. After kk rounds the angle is (2k+1)θ(2k+1)\theta and the chance is sin⁡2((2k+1)θ)\sin^2((2k+1)\theta), which grows until the angle reaches 90∘90^\circ. More rounds carry the state past ∣good⟩\ket{\text{good}}, and the chance falls again.

A good answer covers

  • SgoodS_{\text{good}} is a reflection that flips the good part, and −AS0A−1-AS_0A^{-1} is a reflection in the line of A∣0…0⟩A\ket{0\ldots0}
  • Two reflections make a rotation by 2θ2\theta toward ∣good⟩\ket{\text{good}}, where sin⁡θ=p\sin\theta = \sqrt p
  • After kk rounds the chance is sin⁡2((2k+1)θ)\sin^2((2k+1)\theta)
  • Past 90∘90^\circ the state turns away from ∣good⟩\ket{\text{good}}, so the chance falls

12 / 13 · Mastery check

Mastery check

Check yourself

An algorithm AA gives a good answer with probability pp, and you can check any answer. Rerunning AA until it succeeds takes about 1/p1/p runs, so if pp becomes a million times smaller, rerunning costs a million times more. This lesson’s method replaces rerunning. If pp becomes a million times smaller, how does the cost of that method change?

  1. AIt grows a million times too: nothing beats rerunning
  2. BIt grows about 1,000 times
  3. CIt grows by a fixed amount: about 20 more rounds
  4. DIt doesn’t change: one quantum run checks every answer at once
Show the answer

Answer: B. It grows about 1,000 times

Each round turns the state by 2θ2\theta, where sin⁡θ=p\sin\theta = \sqrt p, and it has to turn through about 90∘90^\circ. For small pp, θ≈p\theta \approx \sqrt p radians, so it takes about π4/p\tfrac{\pi}{4}/\sqrt p rounds: a million times smaller pp means 1,000 times more rounds.

Why not the others

  • A. Rerunning grows like 1/p1/p, but it is not the only option. Amplitude amplification turns the state by a fixed angle per round, and the number of rounds grows like 1/p1/\sqrt p: a million times smaller pp costs only 1,000 times more.
  • C. That would be a logarithmic cost, like halving an interval 20 times. Amplitude amplification gains a square root, not more: a million times smaller pp needs about 1,000 times as many rounds, and no method does better in general.
  • D. A run on a superposition still ends in one measurement with one random result. The good results have to be made likely first, and that takes rounds: about π4/p\tfrac{\pi}{4}/\sqrt p of them.

Check yourself

Let ∣ψ⟩=A∣0…0⟩\ket\psi = A\ket{0\ldots0}, and let ∣ϕ⟩\ket\phi be any state orthogonal to ∣ψ⟩\ket\psi. What does −AS0A−1-AS_0A^{-1} do to ∣ϕ⟩\ket\phi?

  1. ATurns it toward the good states
  2. BLeaves it unchanged
  3. CMultiplies it by −1-1
  4. DSends it to ∣0…0⟩\ket{0\ldots0}
Show the answer

Answer: C. Multiplies it by −1-1

A−1∣ϕ⟩A^{-1}\ket\phi is orthogonal to ∣0…0⟩\ket{0\ldots0}, so S0S_0 leaves it alone, AA returns ∣ϕ⟩\ket\phi, and the leading minus sign gives −∣ϕ⟩-\ket\phi. With ∣ψ⟩↦∣ψ⟩\ket\psi \mapsto \ket\psi, that makes −AS0A−1=2∣ψ⟩⟨ψ∣−I-AS_0A^{-1} = 2\ket\psi\bra\psi - I, the mirror in the line of ∣ψ⟩\ket\psi.

Why not the others

  • A. Half of QQ only reflects: it fixes the line of ∣ψ⟩\ket\psi and flips everything orthogonal to it. A turn toward the good states needs both halves of QQ.
  • B. A−1∣ϕ⟩A^{-1}\ket\phi is orthogonal to A−1∣ψ⟩=∣0…0⟩A^{-1}\ket\psi = \ket{0\ldots0}, so S0S_0 leaves it alone, and AA brings back ∣ϕ⟩\ket\phi. The minus sign in front then flips it. It is ∣ψ⟩\ket\psi that stays unchanged.
  • D. A−1A^{-1} alone sends ∣ψ⟩\ket\psi, not ∣ϕ⟩\ket\phi, to ∣0…0⟩\ket{0\ldots0}, and AA undoes that at the end. The net effect on ∣ϕ⟩\ket\phi is a sign: −∣ϕ⟩-\ket\phi.

Check yourself

An algorithm succeeds with probability p=18p = \tfrac18. You run AA and then one round of QQ. What is the chance of a good outcome now? (A fraction, or three decimal places.)

Hints
  1. Find θ\theta from sin⁡θ=p\sin\theta = \sqrt p.
  2. One round takes the angle from θ\theta to 3θ3\theta; the chance is sin⁡23θ\sin^2 3\theta.
Show the answer

Answer: 2532\frac{25}{32}

θ=arcsin⁡18≈20.70∘\theta = \arcsin\sqrt{\tfrac18} \approx 20.70^\circ, so 3θ≈62.11∘3\theta \approx 62.11^\circ and sin⁡23θ=2532≈0.781\sin^2 3\theta = \tfrac{25}{32} \approx 0.781. In general one round gives p(3−4p)2p(3 - 4p)^2, from sin⁡3θ=3sin⁡θ−4sin⁡3θ\sin 3\theta = 3\sin\theta - 4\sin^3\theta.

From sin⁡θ=p=18\sin\theta = \sqrt p = \sqrt{\tfrac18}, θ≈20.70∘\theta \approx 20.70^\circ. One round of QQ turns the state by 2θ2\theta, from θ\theta to 3θ≈62.11∘3\theta \approx 62.11^\circ. The chance of a good outcome is sin⁡2(62.11∘)≈0.781\sin^2(62.11^\circ) \approx 0.781.

Common slips

  • 18\frac{1}{8}: That is pp, the chance before the round. One round turns the state from θ≈20.70∘\theta \approx 20.70^\circ to 3θ≈62.11∘3\theta \approx 62.11^\circ.
  • 38\frac{3}{8}: One round does not add pp twice more. QQ acts on amplitudes: it turns the state by 2θ2\theta, and the chance is sin⁡23θ\sin^2 3\theta.
  • 0.4380.438: That is sin⁡22θ\sin^2 2\theta. The state starts at angle θ\theta, so one turn of 2θ2\theta takes it to 3θ3\theta.

Check yourself

A Grover search over N=512N = 512 items is tuned for one marked item: it makes 17 iterations, which find that item with probability 99.9%. Unknown to you, 4 items are marked. With the same 17 iterations, what is the chance of finding a marked item?

  1. AOver 99.9%: more marked items are easier to find
  2. BAbout 99.9%: the number of marked items doesn’t matter
  3. CAbout 0.8%, the chance of a random guess
  4. DAbout 0.2%, worse than a random guess
Show the answer

Answer: D. About 0.2%, worse than a random guess

With one marked item sin⁡θ=1/512\sin\theta = \sqrt{1/512}, so θ≈2.53∘\theta \approx 2.53^\circ, and 17 iterations reach 35θ≈88.7∘35\theta \approx 88.7^\circ. With 4 marked items sin⁡θ\sin\theta doubles: θ≈5.07∘\theta \approx 5.07^\circ, and the same 17 iterations reach about 177.5∘177.5^\circ, past ∣good⟩\ket{\text{good}} at 90∘90^\circ and nearly round to the unmarked side. The chance is sin⁡2(177.5∘)≈0.002\sin^2(177.5^\circ) \approx 0.002, while a random guess gets 4/512≈0.0084/512 \approx 0.008.

Why not the others

  • A. More marked items make each iteration turn the state further: sin⁡θ=4/512\sin\theta = \sqrt{4/512} is twice 1/512\sqrt{1/512}. 17 iterations then carry the state past the marked items, so more turning is not better here.
  • B. The angle of each iteration depends on how many items are marked. With 4 of them θ\theta is about twice as large, so 17 iterations turn the state about twice as far as planned.
  • C. The iterations are not wasted: they move the state, just too far. It ends close to the unmarked states, and the chance, 0.2%, is below even a random guess.

Check yourself

Grover search over N=216N = 2^{16} items, of which M=1M = 1 is marked. About how many iterations make finding a marked item most likely?

Hints
  1. Grover is amplitude amplification with p=M/Np = M/N. For small pp, θ≈p\theta \approx \sqrt p in radians.
  2. Iterations ≈π4θ≈π4N/M\approx \dfrac{\pi}{4\theta} \approx \dfrac{\pi}{4}\sqrt{N/M}.
Show the answer

Answer: 201201

N/M=256\sqrt{N/M} = 256, so about π4⋅256≈201.1\tfrac{\pi}{4}\cdot 256 \approx 201.1 iterations. The exact rule, kk nearest to π4θ−12\tfrac{\pi}{4\theta} - \tfrac12, gives k=201k = 201, with success chance over 99.9%.

Here p=M/N=1/216p = M/N = 1/2^{16}, so sin⁡θ=p\sin\theta = \sqrt p and θ≈0.00391\theta \approx 0.00391 radians. The best kk makes (2k+1)θ(2k+1)\theta closest to π2\tfrac{\pi}{2}: k≈π4θ−12k \approx \tfrac{\pi}{4\theta} - \tfrac12, which rounds to 201. The quick estimate π4N/M≈201.1\tfrac{\pi}{4}\sqrt{N/M} \approx 201.1 is within one of it.

Common slips

  • 256256: N/M=256\sqrt{N/M} = 256 sets the scale, but it is not the count. Each iteration turns the state by 2θ≈2M/N2\theta \approx 2\sqrt{M/N} radians, and it only needs to turn about π2\tfrac{\pi}{2}: that takes about π4⋅256≈201\tfrac{\pi}{4}\cdot 256 \approx 201 iterations.
  • 6553665536: 65,536 is 1/p=N/M1/p = N/M, the average number of random guesses. Iterations grow like the square root of that.
  • 3276832768: 32,768 is N/2N/2, about how many items a classical search checks on average. Grover needs about π4N\tfrac{\pi}{4}\sqrt{N} iterations.

Check yourself

You don’t know pp; in fact p=14p = \tfrac14, so θ=30∘\theta = 30^\circ. You pick kk at random from 00 to 33 (so m=4m = 4), run AA and kk rounds of QQ, and measure. What is the chance of a good outcome?

Hints
  1. List the angles (2k+1)θ(2k+1)\theta for each allowed kk.
  2. The angles are 30∘,90∘,150∘,210∘30^\circ, 90^\circ, 150^\circ, 210^\circ. Square each sine, then average the 4 values.
Show the answer

Answer: 716\frac{7}{16}

The angles 30∘,90∘,150∘,210∘30^\circ, 90^\circ, 150^\circ, 210^\circ give chances 14,1,14,14\tfrac14, 1, \tfrac14, \tfrac14. Each kk is equally likely, so the chance is their average, 716\tfrac{7}{16}.

With θ=30∘\theta = 30^\circ, the allowed k=0,…,3k = 0, \ldots, 3 end at the angles (2k+1)θ=30∘,90∘,150∘,210∘(2k+1)\theta = 30^\circ, 90^\circ, 150^\circ, 210^\circ. Their chances sin⁡2((2k+1)θ)\sin^2((2k+1)\theta) are 14,1,14,14\tfrac14, 1, \tfrac14, \tfrac14. Picking kk uniformly at random, the chance of a good outcome is the average of these 4 numbers: 716≈0.437\tfrac{7}{16} \approx 0.437.

Common slips

  • 12\frac{1}{2}: 12\tfrac12 is where the average settles when mm is large. For m=4m = 4, average the 4 chances themselves: 14,1,14,14\tfrac14, 1, \tfrac14, \tfrac14.
  • 11: That is the best single kk. Here kk is random, so every choice from 0 to 3 counts equally: average the 4 chances.
  • 14\frac{1}{4}: That is k=0k = 0 alone: no rounds at all. Average over all the allowed kk.

Check yourself

Someone claims a quantum algorithm that finds the one marked item among N=240N = 2^{40} items, with nothing known about where it is, using 1,000 queries. What should you think?

  1. APlausible, if it applies QQ about 1,000 times
  2. BIt can’t be right as stated: every quantum method needs on the order of N=220\sqrt N = 2^{20}, about a million, queries
  3. CPlausible, if it picks the number of rounds at random
  4. DPlausible: quantum algorithms are exponentially faster than classical ones
Show the answer

Answer: B. It can’t be right as stated: every quantum method needs on the order of N=220\sqrt N = 2^{20}, about a million, queries

For unstructured search every quantum algorithm needs order N\sqrt N queries, and 240=220\sqrt{2^{40}} = 2^{20}. Grover’s π4N≈820,000\tfrac{\pi}{4}\sqrt N \approx 820{,}000 is within a constant factor of that limit. A method using 1,000 queries must be exploiting structure that the problem, as stated, doesn’t have.

Why not the others

  • A. Every round of QQ is a query: it uses SgoodS_{\text{good}} once. With one marked item among 2402^{40}, about π4⋅220≈820,000\tfrac{\pi}{4}\cdot 2^{20} \approx 820{,}000 rounds are needed.
  • C. Picking kk at random handles an unknown number of marked items. It costs a constant factor more than knowing that number, never less.
  • D. Some problems have exponential quantum speed-ups, but unstructured search is proven not to: any quantum algorithm needs order N\sqrt N queries, here about 2202^{20}.

13 / 13 · Recap

Recap

From memory

Before reading the summary, write down what you remember. Pulling it back from memory is what makes it last.

  1. Write the iterate QQ and say what each piece does.

    Show the key points
    • Q=−AS0A−1SgoodQ = -AS_0A^{-1}S_{\text{good}}; read right to left, so SgoodS_{\text{good}} acts first
    • SgoodS_{\text{good}} flips the sign of the good states; S0S_0 flips the sign of ∣0…0⟩\ket{0\ldots0}
    • A−1A^{-1} undoes AA; the minus sign is a global phase
    • Grover search is the case A=H⊗nA = H^{\otimes n}
  2. How many rounds should you run, what is the success chance, and how does that compare with rerunning AA?

    Show the key points
    • sin⁡θ=p\sin\theta = \sqrt p; after kk rounds the chance is sin⁡2((2k+1)θ)\sin^2((2k+1)\theta)
    • kk nearest 90∘2θ−12\tfrac{90^\circ}{2\theta} - \tfrac12, about π4/p\tfrac{\pi}{4}/\sqrt p; more rounds overshoot
    • Rerunning needs about 1/p1/p runs
  3. What do you do when pp is unknown, and what does Grover search not give you?

    Show the key points
    • Pick kk at random below mm, and grow mm by a factor such as 65\tfrac65 after each failure
    • Once m≥1/sin⁡2θm \ge 1/\sin 2\theta a try succeeds with chance at least 14\tfrac14; the expected cost stays of order 1/p1/\sqrt p
    • Stored data must be loaded (order NN), the oracle is a circuit, and order N\sqrt N queries is the proven minimum

Key ideas

  1. Amplitude amplification: for any algorithm AA without measurements and any way to recognize good outcomes, Q=−AS0A−1SgoodQ = -AS_0A^{-1}S_{\text{good}} turns A∣0…0⟩A\ket{0\ldots0} toward the good states.

  2. Two reflections make one rotation: with sin⁡θ=p\sin\theta = \sqrt p, kk rounds succeed with chance sin⁡2((2k+1)θ)\sin^2((2k+1)\theta). For p=14p = \tfrac14, one round gives certainty.

  3. About π4/p\tfrac{\pi}{4}/\sqrt p rounds instead of about 1/p1/p reruns. Grover search is the case A=H⊗nA = H^{\otimes n}, p=M/Np = M/N.

  4. Unknown pp: pick kk at random below a bound mm that grows by a constant factor. The expected cost stays of order 1/p1/\sqrt p, and if order N\sqrt N queries find nothing, report “none”.

  5. Grover search is no fast database lookup: loading costs order NN, the oracle is a circuit, and order N\sqrt N queries is the proven minimum for unstructured search.

Go further

Free resources for when you want more than this lesson covers.

Start the interactive lesson