Level 3 · Algorithms

Grover’s Algorithm

About 5,357 words · roughly 24 minute read · sources checked December 2, 2026

Grover’s algorithm, published by Lov Grover in 1996, searches an unstructured collection of N items in about √N steps, where any classical algorithm needs about N. It is the second great quantum algorithm after Shor’s, and in some ways the more general one: any problem where you can check a candidate answer quickly, from scheduling to satisfiability to key search, can in principle be sped up by it.

The speed-up is quadratic, not exponential, and it is provably the best possible for this kind of problem. Those two facts together are the honest summary. This lesson shows why the algorithm works with a clean geometric picture, runs it by hand on four and eight items, shows the surprising way it fails if you run it too long, and then asks what the quadratic gain is actually worth once you count the cost of real quantum operations.

You need the Level 2 lessons on superposition and gates. The earlier superposition lesson already ran the four-item case; here we explain why it worked and generalise.

What you will learn

  • The search problem, the oracle and the role of marked items.
  • How the oracle and the inversion about the mean combine into a rotation by a fixed angle.
  • The formula for the success probability after j iterations, and the optimal number of iterations.
  • Worked traces for N = 4 and N = 8, including overshoot.
  • How the circuit is built: phase oracle, Hadamards and a multi-controlled phase.
  • What to do when the number of solutions is unknown, and the generalisation to amplitude amplification.
  • Why √N is optimal (the BBBV result) and what the speed-up costs in practice.

Helpful first: Superposition, Quantum Gates, Quantum Algorithms.

The problem: finding a needle without structure

Suppose there are N possible answers, labelled 0 to N − 1, and a function f that returns 1 for the correct answers and 0 for the rest. You can evaluate f on any candidate but you have no other information about where the solutions are. The task is to find an x with f(x) = 1. Computer scientists call this unstructured search, and the function f is accessed as an oracle.

Classically the only strategy is to try candidates. With one solution among N items, you need N/2 tries on average and N in the worst case. No cleverness helps, because without structure, each evaluation tells you only about the one candidate you tried.

Many practical problems have exactly this form, once you view the checking step as the oracle. Satisfiability asks for an assignment of variables that makes a formula true. A password or key search asks for an input whose hash or encryption matches. A scheduling problem asks for a timetable that violates no constraint. In each case checking a proposed answer is easy, and the difficulty is the enormous number of candidates.

What Grover gives

With the same oracle, a quantum computer finds a solution after about (π/4)√N calls when one solution exists. That is a quadratic improvement over the classical N/2. It is a speed-up on the number of oracle calls, and it applies to any problem phrased this way.

The idea: two reflections make a rotation

Grover’s algorithm repeats two simple operations on the uniform superposition |s⟩ = (1/√N) Σₓ |x⟩, which has equal amplitude on every candidate.

  1. The oracle flips the sign of the amplitude of every solution. In matrix form it is the reflection I − 2|w⟩⟨w| for a single solution |w⟩. It is implemented by phase kickback, with the oracle’s output qubit held in |−⟩.
  2. The diffusion operator, also called inversion about the mean, replaces each amplitude a by 2m − a, where m is the average of all the amplitudes. In matrix form it is 2|s⟩⟨s| − I, a reflection about the uniform state.

The oracle step does not change any probability, because signs vanish when squared. What it does is create a difference between the solution’s amplitude and all the others, a difference stored in a sign. The diffusion step converts the sign difference into a size difference: the mean is dragged slightly below the common value of the non-solutions by the one negative amplitude, so after reflecting around it every non-solution’s amplitude goes down by a little, and the solution’s amplitude goes up by a lot.

A two-dimensional picture

The entire process lives in a plane. Let |w⟩ be the solution and |w⊥⟩ the uniform superposition of all the non-solutions. The starting state |s⟩ lies in the plane spanned by these two, at a small angle θ to |w⊥⟩, where sin θ = 1/√N. Each Grover iteration, an oracle reflection followed by a diffusion reflection, is a product of two reflections, and a product of two reflections is a rotation. It rotates the state by 2θ towards |w⟩.

|w⊥⟩ (non-solutions)|w⟩ (solution)|s⟩ startafter oracleafter 1 iterationθ2θθ drawn large; in practice sin θ = 1/√N is tiny.
The state vector lives in a plane. The oracle reflects it across |w⊥⟩, and the diffusion reflects it across |s⟩. Together they rotate the state by 2θ towards the solution |w⟩.
P(success after j iterations) = sin²((2j + 1)θ),   sin θ = √(k/N)
For k solutions among N items. For one solution, sin θ = 1/√N.

This is the central formula of the lesson. After j iterations the state makes an angle (2j + 1)θ with |w⊥⟩, so its component along the solution is sin((2j + 1)θ), and the probability of measuring a solution is the square. The probability rises as the state rotates, reaches 1 when (2j + 1)θ = π/2, and then falls again if rotation continues.

Worked examples: N = 4 and N = 8

Worked example

N = 4, one solution

  1. sin θ = 1/√4 = 1/2, so θ = 30°.
  2. After j = 1 iteration the angle is (2·1 + 1)θ = 90°, so P = sin²(90°) = 1. One iteration gives certainty, as the amplitude trace in the superposition lesson showed: (½, ½, ½, ½) → (½, ½, ½, −½) → (0, 0, 0, 1).
  3. This case is special. Because 90° is exactly three times 30°, it is the only single-solution case where one iteration is exactly perfect. (Whenever the solutions make up exactly a quarter of the items, one iteration is also perfect.)

Worked example

N = 8, one solution: the whole trace

  1. sin θ = 1/√8 ≈ 0.3536, so θ ≈ 20.705°. Mark item 101 (number 5) as the solution.
  2. Start: all eight amplitudes equal 1/√8 ≈ 0.354.
  3. Iteration 1: the oracle makes the amplitude of item 5 equal to −0.354. The mean is (7 × 0.354 − 0.354)/8 = 0.265. Inversion gives 2 × 0.265 − 0.354 = 0.177 for each of the seven others, and 2 × 0.265 + 0.354 = 0.884 for item 5. Probability of item 5: 0.884² = 0.781.
  4. Iteration 2: the amplitudes become −0.088 for each non-solution and 0.972 for item 5. Probability: 0.972² = 0.945.
  5. Iteration 3: the amplitudes become −0.309 for each non-solution and 0.575 for item 5. Probability: 0.575² = 0.330. The success probability has fallen.
  6. These match sin²(62.1°) = 0.781, sin²(103.5°) = 0.945 and sin²(144.9°) = 0.330, using (2j + 1) × 20.705°. I confirmed the amplitudes by matrix multiplication.
  7. The best choice is j = 2 with success probability about 94.5%. The residual 5.5% is not ‘noise’: for N = 8, no integer number of iterations hits exactly 90°.

The N = 8 trace shows the feature that makes Grover’s algorithm different from classical search. Doing more work can make the answer worse. The rotation overshoots the solution and carries the state back towards |w⊥⟩. You must stop at the right moment, and the right moment depends on N and on the number of solutions.

Proving the rotation: the algebra behind sin((2j + 1)θ)

The geometric claim, that one iteration rotates the state by 2θ, can be checked with two trigonometric identities. This section does it so that the formula is not taken on trust.

Write the uniform state in the basis {|w⟩, |w⊥⟩}, where |w⟩ is the solution and |w⊥⟩ is the uniform superposition of the N − 1 non-solutions. Then

|s⟩ = sin θ |w⟩ + cos θ |w⊥⟩,   sin θ = 1/√N

because the component along |w⟩ is the single amplitude 1/√N, and the rest has the remaining weight. The two operations act in this plane as follows. The oracle flips the sign of the |w⟩ component, O = I − 2|w⟩⟨w|. The diffusion operator is D = 2|s⟩⟨s| − I.

Worked example

One iteration, step by step

  1. Start from |ψ⟩ = sin α |w⟩ + cos α |w⊥⟩, where α is the current angle from |w⊥⟩. Initially α = θ.
  2. Apply the oracle: O|ψ⟩ = −sin α |w⟩ + cos α |w⊥⟩. This is the reflection of the state across the |w⊥⟩ axis, so the angle becomes −α.
  3. Now apply D = 2|s⟩⟨s| − I. This reflects the vector across the line through |s⟩, which makes angle θ with the |w⊥⟩ axis. A reflection across a line at angle θ sends a vector at angle β to angle 2θ − β.
  4. The vector after the oracle has angle β = −α, so the new angle is 2θ − (−α) = α + 2θ.
  5. Each iteration therefore increases the angle by 2θ. Starting at α = θ, after j iterations the angle is θ + 2jθ = (2j + 1)θ, and the probability of the solution is sin²((2j + 1)θ). ∎
  6. As a numerical check with θ = 0.3 radians, the matrix product D·O applied to (sin θ, cos θ) gives (0.7833, 0.6216), which equals (sin 3θ, cos 3θ). So the rotation really is by 2θ.

Two things follow immediately. First, the same argument works for k solutions if |w⟩ is replaced by the uniform superposition over all solutions, with sin θ = √(k/N). Second, the argument is independent of how the starting state was prepared, which is the observation behind amplitude amplification: any state whose good component has size sin θ can be rotated towards the good subspace by the same pair of reflections.

How many iterations? The count and the overshoot

The success probability is largest when (2j + 1)θ is closest to π/2. Solving for j gives j ≈ π/(4θ) − 1/2. For a single solution and large N, θ ≈ 1/√N, so the best number of iterations is about (π/4)√N. With k solutions, sin θ = √(k/N) and the count becomes about (π/4)√(N/k).

j = 6: 0.997036912number of Grover iterations j10N = 64, one solution. Too many iterations rotate past the answer.
Success probability against the number of iterations for N = 64 and one solution. It peaks at j = 6 and then falls, reaching almost 0 again at about j = 12.
jAngle (2j + 1)θP(success), N = 64
07.2°0.016
121.5°0.135
235.9°0.344
350.3°0.591
464.6°0.816
579.0°0.964
693.3°0.997
7107.7°0.907
8122.1°0.718
9136.4°0.475

For N = 64, sin θ = 1/8 so θ = 7.18°. The peak is at j = 6, close to (π/4) × 8 = 6.28.

Worked example

A million items

  1. N = 2²⁰ = 1,048,576, one solution. θ ≈ 1/1,024 radians ≈ 0.0559°.
  2. The optimal count is about (π/4) × 1,024 ≈ 804 iterations. At j = 804 the success probability is 0.9999998.
  3. At j = 805 it is 0.999994, a negligible drop. So near the peak the curve is flat, and the exact count is not critical to within a few iterations.
  4. Far from the peak it matters a lot. At j = 1,608 (twice as many, a full 360° turn) the probability is about 10⁻¹⁰: the state has rotated all the way past the solution and back to where it started, almost orthogonal to it.
  5. Compare the classical cost: about 524,000 evaluations on average. The quantum cost is about 804 oracle calls, roughly 650 times fewer.

Many solutions

If there are k solutions, each iteration still rotates by 2θ, with sin θ = √(k/N). More solutions means a bigger angle and so fewer iterations. When k = N/4 one iteration suffices, as in the N = 4 case, and I verified numerically that one iteration gives certainty for k = N/4 with N = 4, 8 and 16.

The practical difficulty

To stop at the right time you need to know roughly how many solutions there are. If k is unknown, you can estimate it with quantum counting, which is built from phase estimation applied to the Grover iteration, or you can use a randomised schedule of iteration counts that finds a solution in about √(N/k) oracle calls on average without knowing k. These refinements are standard in the literature, but I have not opened the original papers for this lesson.

Building the circuit

A Grover iteration has two parts, and both are built from gates you already know.

|0⟩H|0⟩H|0⟩HH^⊗n|−⟩repeat about (π/4)√N timesU_f oracleDiffusionH^⊗n · (2|0⟩⟨0| − I) · H^⊗n
One Grover iteration on n qubits: the phase oracle U_f, then the diffusion operator H^⊗n (2|0⟩⟨0| − I) H^⊗n. The iteration is repeated about (π/4)√N times before measuring.

The oracle

The oracle is a reversible circuit that computes f(x) into an extra qubit. With the extra qubit in |−⟩, phase kickback turns this into a sign: |x⟩ → (−1)^f(x) |x⟩. This is the same trick used in the Deutsch algorithm. Building the oracle means turning the checking procedure, such as ‘does this assignment satisfy the formula?’, into a reversible quantum circuit, using Toffoli gates and helper qubits. Its size is the cost of one classical evaluation of f, up to a modest factor.

The diffusion operator

The operator 2|s⟩⟨s| − I is a reflection about the uniform state. Because |s⟩ = H^⊗n|0…0⟩, it equals H^⊗n (2|0⟩⟨0| − I) H^⊗n. The middle factor flips the sign of every basis state except |0…0⟩, which is a multi-controlled Z gate sandwiched between layers of X gates (up to a global sign). So the diffusion operator is a layer of Hadamards, a layer of X gates, an n-qubit controlled-Z, another layer of X, and a final layer of Hadamards. The n-qubit controlled-Z can be built from O(n) Toffoli gates with helper qubits.

Worked example

Checking the diffusion formula on four items

  1. For N = 4, the uniform state is |s⟩ = ½(1, 1, 1, 1). The operator 2|s⟩⟨s| − I has every entry equal to ½ off the diagonal and −½ on the diagonal: its first row is (−½, ½, ½, ½).
  2. Apply it to the state after the oracle, (½, ½, ½, −½). The first component is −¼ + ¼ + ¼ − ¼ = 0. The last component is ¼ + ¼ + ¼ + ¼ = 1 (the diagonal entry −½ times −½ is +¼).
  3. The result is (0, 0, 0, 1). This agrees with the ‘2m − a’ rule used earlier.
  4. The cost of the diffusion operator is independent of the oracle and grows only linearly with the number of qubits, so for large N the oracle dominates.

The total cost of the algorithm is therefore about (π/4)√N times (oracle size + O(n) gates), plus the cost of preparation and measurement. For an N = 2ⁿ search this is about 2^(n/2) iterations. The number of qubits needed is n for the register plus whatever the oracle needs as workspace.

Amplitude amplification: the general principle

The two-reflection argument does not need the starting state to be uniform. Suppose any quantum algorithm A prepares a state in which the ‘good’ outcomes have total probability p. Replace the uniform state by A|0⟩ and the diffusion step by a reflection about it. The same rotation argument shows that about 1/√p repetitions of the combined operation raise the success probability to nearly 1. Compare the classical approach of repeating A and checking, which needs about 1/p tries. This is called amplitude amplification, and it is a quadratic speed-up for any randomised search procedure.

This is why Montanaro’s survey lists, among quadratic speed-ups, the following: Grover search and amplitude amplification (from O(N) to O(√N) evaluations), search for NP certificates and 3-SAT with a quadratic gain, finding the minimum of a list (a method due to Dürr and Høyer), graph connectivity in O(N^(3/2)) against classical O(N²), and speed-ups for Markov chain algorithms. In each case, it is amplitude amplification, or a close relative, doing the work.

Worked example

Applying it to satisfiability

  1. A formula has m = 80 Boolean variables, so there are N = 2⁸⁰ assignments. A classical brute-force search takes up to 2⁸⁰ ≈ 1.2 × 10²⁴ checks.
  2. Grover needs about (π/4) × 2⁴⁰ ≈ 8.6 × 10¹¹ oracle calls, where each call checks one assignment in superposition.
  3. That reduces a hopeless 10²⁴ to a large but conceivable 10¹². But classical SAT solvers do not use brute force, and for most practical formulas they find solutions or prove none exist using clever search, nowhere near 2⁸⁰ steps.
  4. So the fair statement is that Grover gives a quadratic gain over brute force, and that a competitive quantum SAT solver would have to beat the best classical heuristics, which the quadratic gain alone does not guarantee.

A concrete oracle and a complete small search

Marking one item with a controlled-Z

The simplest oracle marks a single known bit string. To flip the sign of |101⟩ and no other basis state on three qubits, apply X gates to the qubits where the target string has a 0 (here the middle qubit), then a controlled-controlled-Z (which flips the sign only of |111⟩), then undo the X gates. The X gates turn |101⟩ into |111⟩, so the sign flip lands on the right state, and the second set of X gates restores the labelling. The result is the diagonal matrix with −1 in the position of |101⟩ and +1 elsewhere.

A real oracle does not know the answer in advance, of course. It computes f(x) by a reversible circuit and then uses phase kickback, but the effect on the amplitudes is the same sign pattern.

A small satisfiability instance

Consider three Boolean variables x₁, x₂, x₃ and the formula (x₁ OR x₂) AND (NOT x₁ OR x₃) AND (NOT x₂ OR NOT x₃). The oracle is a circuit that evaluates this formula on the candidate assignment and flips the sign if the formula is true.

Worked example

Solving the formula with Grover

  1. Check all 8 assignments classically to find the solutions: (x₁, x₂, x₃) = (0, 1, 0) and (1, 0, 1). So k = 2 out of N = 8.
  2. sin θ = √(2/8) = 1/2, so θ = 30°. After one iteration the angle is 3θ = 90°, and the success probability is sin²(90°) = 1.
  3. Simulating the circuit confirms this: after the oracle and the diffusion step, the amplitudes sit entirely on the two solutions (probability ½ each), and a measurement returns 010 or 101 with certainty.
  4. A second iteration would be a mistake: the angle becomes 150°, giving probability sin²(150°) = 0.25, which I also confirmed. Stopping at the right time matters.
  5. Here the classical search is trivial, with eight cases. The example is useful because it shows all the parts: oracle from a formula, count of solutions determines the iteration number, and one well-chosen iteration is enough.

When the number of solutions is unknown: quantum counting

The iteration count depends on k, which is usually unknown. One way out is to measure k. The Grover iteration G, one oracle reflection followed by one diffusion reflection, is a rotation by 2θ in the plane, so its eigenvalues are e^(±2iθ). Phase estimation applied to G therefore returns an estimate of 2θ, and since sin²θ = k/N, an estimate of k. This is called quantum counting.

Worked example

Counting 4 solutions among 64

  1. Here sin θ = √(4/64) = 1/4, so θ ≈ 14.48° and 2θ ≈ 28.96°. As a fraction of a full turn, the phase is φ = 2θ/360° ≈ 0.0804.
  2. Use t = 6 counting qubits, so 2ᵗφ ≈ 5.15. The measurement outcome is the integer y = 5 with probability about 0.93, according to my simulation of the phase-estimation distribution.
  3. Convert the outcome to an estimate of k: k ≈ N·sin²(π·y/2ᵗ) = 64 × sin²(5π/64) ≈ 3.78. Rounding to the nearest integer gives 4. ✓
  4. The adjacent outcomes y = 4 and y = 6 would give 2.4 and 5.4, so with only 6 counting qubits the estimate is accurate to about one unit here. More counting qubits give a finer estimate, at the cost of more controlled-G operations (the cost grows as √(N/k) to resolve k to a few units).

Counting adds its own cost, but only about as much as a single Grover search, so the total remains of order √N. Another approach, which avoids counting entirely, runs the algorithm with a randomly chosen number of iterations from a growing range, and finds a solution after about √(N/k) oracle calls on average without ever knowing k. I have not opened the original papers for either technique, so treat the cost statements as the standard ones.

Summary of the three situations

If you know k, use (π/4)√(N/k) iterations. If you can afford it, estimate k first by quantum counting. If neither, use the randomised schedule. In all three the cost stays proportional to √(N/k).

What Grover means for encryption and for real hardware

The best-known application is brute-force key search. A symmetric key of n bits has N = 2ⁿ possibilities, and Grover needs about (π/4)·2^(n/2) iterations, so the effective security halves: a 128-bit key behaves like a 64-bit one against this attack, and a 256-bit key like a 128-bit one. This is why the usual advice is to use 256-bit keys for long-term symmetric security, not to abandon symmetric ciphers.

Worked example

How long would 2⁶⁴ iterations take?

  1. Searching a 128-bit key needs about 2⁶⁴ ≈ 1.8 × 10¹⁹ Grover iterations (ignoring the π/4 factor).
  2. Each iteration contains a full evaluation of the cipher inside the oracle, as a fault-tolerant circuit. Suppose, optimistically, one iteration takes 1 µs.
  3. Total time: 1.8 × 10¹⁹ × 10⁻⁶ s ≈ 1.8 × 10¹³ s ≈ 585,000 years on one machine.
  4. The iterations are inherently sequential, because each one uses the output of the last. To finish in a year, you would need the work split across hundreds of thousands of machines.
  5. Splitting does not help as much as it would classically. The known result, as I recall from Zalka’s work (I have not reopened it), is that S parallel quantum machines speed Grover up by only a factor of √S, not S. So 585,000 machines would cut the time by about 765, not 585,000, leaving roughly 765 years.

The 1 µs per iteration is also generous: a cipher oracle needs many logical gates, and fault-tolerant gates are slower than one microsecond. So the practical picture is that Grover weakens symmetric keys in principle, but a 128-bit key is not in realistic danger from it, and 256-bit keys leave a very large margin. The quadratic speed-up is real, but it is a square root of a very large number, which is still large.

Why you cannot do better: the BBBV theorem

A natural hope is that a cleverer algorithm could search in time polynomial in log N, which would solve NP-complete problems efficiently. In 1997 Bennett, Bernstein, Brassard and Vazirani showed this cannot be done for black-box search. Their paper’s abstract states that relative to an oracle chosen uniformly at random, NP cannot be solved on a quantum computer in time o(2^(n/2)) with probability 1. Since the search space has size N = 2ⁿ, this means that any quantum algorithm needs on the order of √N queries, and Grover’s algorithm, which accepts NP relative to any oracle in O(2^(n/2)) time, shows that the bound is tight.

The intuition for why √N is a floor comes from the geometry above. Each oracle query can rotate the state by only a limited angle towards the solution, because the oracle’s effect on the amplitude of a single unknown item is bounded. To rotate from the uniform state, which has overlap 1/√N with the solution, to the solution itself, you need roughly √N such rotations. The formal proof makes this precise with a hybrid argument that tracks how much any algorithm’s state can change when the oracle is altered at one position.

What this does and does not prove

It proves that any algorithm that treats the oracle as a black box cannot beat a quadratic speed-up. It does not prove that all NP-complete problems lack a faster quantum algorithm that uses the internal structure of the problem. It is strong evidence, and the standard belief is that quantum computers will not solve NP-complete problems in polynomial time.

What a quadratic speed-up is worth in practice

A speed-up from N to √N sounds enormous when N is 2¹²⁸. The honest accounting has to include the speed of the machine and the cost of the oracle.

Worked example

Searching a 128-bit key

  1. A brute-force search of a 128-bit key space has N = 2¹²⁸ ≈ 3.4 × 10³⁸ candidates. Grover reduces the oracle calls to about (π/4) × 2⁶⁴ ≈ 1.4 × 10¹⁹.
  2. Suppose, generously, that each Grover iteration took one microsecond (for a fault-tolerant machine, with the cipher’s encryption circuit in the oracle, this is very optimistic). The iterations are sequential, since each depends on the previous. The total is 1.4 × 10¹⁹ µs ≈ 1.4 × 10¹³ s, which is about 460,000 years.
  3. Running k machines does not help in proportion. A known analysis finds that splitting a Grover search across k independent machines speeds it up by only about √k, not k, so reducing 460,000 years to one year would take on the order of 10¹¹ machines. I have not opened the source for this result, so treat it as a standard but unverified-here statement.
  4. Against this, an attacker with classical parallel hardware can divide a classical search by k directly. The comparison is therefore far less favourable to the quantum attacker than the headline suggests.

For these reasons, the widely used recommendation is to treat Grover’s algorithm as roughly halving the effective bit-length of a symmetric key, and to answer it by using 256-bit keys where long-term security is needed, which leaves a margin of about 128 bits. The practical threat to symmetric cryptography is much smaller than the threat Shor’s algorithm poses to public-key cryptography. The cost of implementing the encryption function as a reversible quantum circuit is itself large, and published estimates of the resources for attacking AES exist, but I did not open them, so I do not quote figures.

Noise makes it worse

Grover’s algorithm needs a long sequence of coherent iterations. Errors in the oracle or diffusion steps push the state away from the ideal rotation, and the success probability decays. A rough estimate treats each iteration as failing independently with probability δ.

Worked example

How noise limits the number of iterations

  1. Take the N = 2²⁰ search with 804 iterations, and suppose each iteration (oracle plus diffusion) has an overall error of δ = 0.001.
  2. The chance that no error occurs in any iteration is about (1 − 0.001)⁸⁰⁴ ≈ e^(−0.804) ≈ 0.45.
  3. With δ = 0.01, the same estimate gives e^(−8.04) ≈ 3 × 10⁻⁴, so the algorithm essentially never succeeds without error correction.
  4. This crude model treats every error as fatal, which overstates the damage, but it shows the scale of the requirement: the error per iteration must be well below 1/√N. A fault-tolerant machine is therefore needed for large searches, and this is where the cost of error correction enters the speed-up.

The summary is that Grover’s quadratic advantage is real and provably optimal. Whether it becomes a practical advantage depends on the ratio of quantum to classical operation costs, and for many problems that ratio is large enough to erase the gain until quantum hardware becomes much faster and cheaper per operation.

Run the search in your browser

Here are the searches from this lesson, ready to run. The marked item is 101 on three qubits and 11 on two. Predict the probability of the marked item before pressing Run, then compare with the formula sin²((2k + 1)θ) from the proof section. Results follow Qiskit's bit order.

Two qubits, four items: exact after one iteration

Run this circuit

Grover on two qubits: one iteration is exact

q0q1HHHHXXXXHH

Bit order: q0 is the rightmost digit of each result, as in Qiskit. All qubits start in |0⟩ unless marked. This is an ideal simulator with no hardware noise.

Predict first: Four items, marked item 11. A random guess succeeds 25% of the time. After one Grover iteration, how likely is 11?

Press Run to simulate. Results appear here with a short explanation.

Three qubits, eight items: one iteration, two, three

Run this circuit

Grover, 3 qubits, one iteration

q0q1q2HHHXHHXHHHXXXHHXXXHHH

Bit order: q0 is the rightmost digit of each result, as in Qiskit. All qubits start in |0⟩ unless marked. This is an ideal simulator with no hardware noise.

Predict first: Eight items, one marked (101). A random guess succeeds 12.5% of the time. After one Grover iteration, what is P(101)?

Press Run to simulate. Results appear here with a short explanation.

Run this circuit

Grover, 3 qubits, two iterations

q0q1q2HHHXHHXHHHXXXHHXXXHHHXHHXHHHXXXHHXXXHHH

Bit order: q0 is the rightmost digit of each result, as in Qiskit. All qubits start in |0⟩ unless marked. This is an ideal simulator with no hardware noise.

Predict first: One more iteration. Does the success probability rise again?

Press Run to simulate. Results appear here with a short explanation.

Run this circuit

Grover, 3 qubits, three iterations (overshoot)

q0q1q2HHHXHHXHHHXXXHHXXXHHHXHHXHHHXXXHHXXXHHHXHHXHHHXXXHHXXXHHH

Bit order: q0 is the rightmost digit of each result, as in Qiskit. All qubits start in |0⟩ unless marked. This is an ideal simulator with no hardware noise.

Predict first: If two iterations were good, three should be better. Right?

Press Run to simulate. Results appear here with a short explanation.

The three-qubit probabilities are 0.78125, 0.9453 and 0.3301, and they match the formula. The third circuit overshoots, which is not a bug. It is what happens if you keep rotating past the target. The number of iterations to use is the floor of (π/4)√N minus a half, rounded, and for eight items that is two.

NOptimal kSuccess probability at kClassical average checks (N/2)
411.00002
820.94534
1630.96138
6460.996632
1,024250.9995512
1,048,5768041.0000524,288

Optimal iteration count and success probability for one marked item, from sin²((2k + 1)θ). My own calculation. The classical column is the average number of checks for a random search without repeats.

The last row is the quadratic speed-up in one line: about 804 quantum iterations against roughly half a million classical checks. But remember from the cost section that an iteration is not a check, and on error-corrected hardware it may be thousands of times slower.

Common misunderstandings

MisunderstandingCorrection
Grover checks all items at once and returns the right oneThe oracle marks the solution with a sign, and repeated reflections rotate the state towards it. Measuring early returns a random item.
More iterations always means a better answerThe success probability oscillates. Past the optimum it falls, and after a full turn it returns almost to zero.
Grover gives an exponential speed-upIt gives a quadratic one, provably the best possible for unstructured search.
Grover breaks AESIt reduces the effective key length by about half in an idealised count. AES-256 keeps a large margin, and the practical overheads are heavy.
You can solve NP-complete problems quickly with itOnly a quadratic improvement over brute force. Better classical heuristics often do far better in practice.
The oracle is freeBuilding the oracle costs about one classical evaluation of f, and for a cipher that is a large circuit.

Practice problems

Check yourself

For N = 16 and one solution, find θ and the best number of iterations.

sin θ = 1/4, so θ ≈ 14.48°. The best j satisfies (2j + 1)θ ≈ 90°, so j ≈ 2.6, which rounds to 3. P(3) = sin²(7 × 14.48°) = sin²(101.3°) ≈ 0.961.

For N = 64, what is the success probability after 6 iterations and after 7?

About 0.997 after 6 and 0.907 after 7, since (2j + 1)θ = 93.6° and 108°.

For N = 8, why does j = 3 give a lower success probability than j = 2?

(2·3 + 1)θ = 144.9°, past 90°, so sin² has fallen to 0.33. The state has rotated past the solution.

How many iterations for N = 10⁶ and one solution?

(π/4) × 1000 ≈ 785.

If there are k = 4 solutions among N = 256 items, about how many iterations?

sin θ = √(4/256) = 1/8, θ ≈ 7.18°, so j ≈ 90°/(2 × 7.18°) − 0.5 ≈ 5.8, i.e. about 6. This equals (π/4)√(N/k) = (π/4) × 8 ≈ 6.3.

Why is the diffusion operator called inversion about the mean?

It maps each amplitude a to 2m − a, where m is the average, which is a reflection of every amplitude about the mean.

A 128-bit key search by Grover needs about how many oracle calls?

(π/4) × 2⁶⁴ ≈ 1.4 × 10¹⁹.

What does the BBBV theorem imply for the number of queries needed in unstructured search?

On the order of √N queries are necessary, so Grover’s algorithm is optimal for black-box search.

Per-iteration error is 0.002 and the algorithm needs 500 iterations. Estimate the chance of no error.

e^(−0.002 × 500) = e^(−1) ≈ 0.37.

References and further reading

The explanations and every numerical example (success probabilities, amplitude traces, iteration counts, run-time estimates) are written for this site and were computed directly. I opened the arXiv abstracts of Bennett–Bernstein–Brassard–Vazirani (optimality) and Montanaro’s survey (stated speed-ups). Grover’s paper was checked for title and author only. The statements about quantum counting, amplitude amplification, parallel Grover and the cost of an AES oracle come from the standard literature and were not opened here.

  1. A fast quantum mechanical algorithm for database search (arXiv quant-ph/9605043, L. K. Grover) The original algorithm. Title and author checked; full text not re-read.
  2. Strengths and Weaknesses of Quantum Computing (arXiv quant-ph/9701001, C. H. Bennett, E. Bernstein, G. Brassard, U. Vazirani) Opened. Relative to a random oracle NP needs time at least about 2^(n/2); Grover is tight.
  3. Quantum algorithms: an overview (npj Quantum Information, A. Montanaro, 2016) Opened. The list of quadratic speed-ups from amplitude amplification.
  4. What Makes Quantum Computing So Hard to Explain? (Quanta Magazine, Scott Aaronson, June 8, 2021) Why interference, not parallelism, is the mechanism.
  5. Meet Willow, our state-of-the-art quantum chip (Google, December 2024) Context on the error rates and scale of current hardware.
  6. Announcing Approval of Three Federal Information Processing Standards (FIPS) for Post-Quantum Cryptography (NIST, August 13, 2024) Opened. Context for cryptographic migration; the page does not discuss Grover or key sizes.
  7. Physics 219 Quantum Computation: lecture notes (John Preskill, Caltech) Free notes on Grover search and amplitude amplification.