Information Technology is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Grover's search algorithm

Grover's search algorithm: a quantum amplitude-amplification method giving a quadratic speedup (Θ(√(N/M))) for finding marked items in an unstructured database of size N with M marked answers.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Grover's algorithm is used by engineers when they need to search an unstructured list, database or solution space faster than classical linear search. In hardware-aware quantum software and algorithm design, it gives a guaranteed quadratic speedup (useful when N is large and no structure helps classical methods).

Key ideas

Start from first principles and simple definitions.

  • Qubit: a two-level quantum system with state |ψ> = α|0> + β|1>, where α, β are complex amplitudes and |α|^2 + |β|^2 = 1. (You have already seen qubits earlier in the course.)

  • Marked item: a computational basis state that is a valid solution; call one marked state |w> or a set of M marked states (their span is the marked subspace). If we index the database from 0..N-1, a marked item corresponds to one index or a set of indices.

  • Uniform superposition: the state |s> = (1/√N) Σ_{x=0}^{N-1} |x>. This is prepared by applying Hadamard gates to all qubits starting from |0...0>.

  • Oracle (phase oracle): a unitary O that flips the sign (applies phase −1) to marked states and leaves others unchanged. For a single marked |w>: O|x> = −|x> if x = w, and O|x> = |x> otherwise. In operator form for single |w>: O = I − 2|w><w|. For M > 1 marked states with projector onto the marked subspace Π_marked = Σ_{x∈marked} |x><x|, the oracle is O = I − 2Π_marked.

  • Diffusion operator (inversion about the mean): D = 2|s><s| − I. It reflects amplitudes about the average amplitude (equivalently a reflection through |s>).

  • Grover iterate (G): the product G = D·O. Each application of G increases the amplitude of marked states (until it overshoots) via a rotation in a two-dimensional subspace.

Key mathematical reduction (the usual exam derivation):

  1. Let M be the number of marked states. Define normalized states:
    • |w> as the (normalized) equal superposition over marked states (if M=1, |w> is the marked basis state).
    • |w_perp> as the normalized equal superposition over unmarked states, chosen so that <w|w_perp> = 0. The initial state |s> can be written as |s> = sin(θ)|w> + cos(θ)|w_perp> where sin(θ) = sqrt(M/N). This decomposition is exact.
  2. Oracle O acts as a reflection in the |w> direction: O|w> = −|w>, O|w_perp> = |w_perp>.
  3. Diffusion D is a reflection in the |s> direction: D|s> = |s>, and it reverses the component orthogonal to |s>.
  4. The product G = D·O is therefore the composition of two reflections in two lines (subspaces) in the 2D space spanned by |w>, |w_perp>. Composition of two reflections is a rotation. The rotation angle is 2θ.
  5. After k applications of G starting from |s> the state becomes: |ψ_k> = sin((2k+1)θ)|w> + cos((2k+1)θ)|w_perp>. Thus the amplitude on the marked subspace is sin((2k+1)θ).
  6. To maximize the success probability (square of amplitude), choose k so (2k+1)θ ≈ π/2, i.e. k ≈ (π/(4θ)) − 1/2. Using sin θ = sqrt(M/N), we get the famous scaling: k = O(√(N/M)). For a single marked item (M=1) this is k ≈ π/4 · √N.

What to draw (diagram students should know to draw):

  • Draw a 2D plane with orthonormal axes labelled |w_perp> (horizontal) and |w> (vertical). Mark the vector |s> making angle θ with |w_perp> (so its vertical component = sin θ). Show a small arc representing rotation by 2θ produced by one Grover iterate taking |s> closer to |w>. Label the angle θ and rotation 2θ; draw arrow showing iteration steps rotating the vector toward |w>.

Assumptions behind derivation: pure states, ideal oracle and diffusion operations (no noise), and the ability to implement O and D exactly or with known gate decompositions.

Where universities ask for derivations: typically they ask (a) show decomposition of |s> into |w> and orthogonal subspace, (b) show O and D are reflections, (c) show G is rotation by 2θ and derive the expression for amplitude after k iterations, and (d) solve for number of iterations to maximize success.

Formulas

|s> = (1/√N) Σ_{x=0}^{N-1} |x> — |s> is the uniform superposition, N is the number of items, dimension unitless.

O = I − 2|w><w| — oracle for a single marked state |w>; I is identity (dimension N × N).

D = 2|s><s| − I — diffusion operator (inversion about the mean).

G = D·O — Grover iterate (one step of the algorithm).

sin(θ) = sqrt(M/N) — definition of angle θ when M marked states exist; unitless; applies when M ≤ N.

|ψ_k> = sin((2k+1)θ)|w> + cos((2k+1)θ)|w_perp> — state after k Grover iterations; amplitudes are unitless complex numbers (here real and positive by convention of decomposition).

k_opt ≈ floor( (π / (4θ)) − 1/2 ) or often k ≈ round(π/(4θ) − 1/2) — number of iterations to maximize probability; for M=1, k ≈ π/4 · √N.

Probability(success after k) = sin^2((2k+1)θ) — probability to measure a marked state after k iterations.

Notes: units are unitless; these formulas assume ideal gates and that θ is small enough that integer rounding matters — choose the nearest integer to (π/(4θ)) − 1/2 for best probability.

Worked examples

Example 1 — small exact case N=4, one marked state (M=1). Show matrix action and exact result.

  1. Basis order: |00>, |01>, |10>, |11>. Let the marked state be |w> = |11>.
  2. Uniform state: |s> = (1/2)[1,1,1,1]^T.
  3. Oracle O = I − 2|11><11|. Applying O to |s> flips sign of last component: O|s> = [1/2, 1/2, 1/2, −1/2]^T.
  4. Diffusion: |s><s| = (1/4)J where J is 4×4 all-ones matrix. So D = 2*(1/4)J − I = (1/2)J − I.
  5. Compute (1/2)J·O|s>: sum of entries of O|s> = 1/2+1/2+1/2−1/2 = 1. So (1/2)J·O|s> = [1/2,1/2,1/2,1/2]^T. Then D·(O|s>) = (1/2)J·O|s> − O|s> = [0,0,0,1]^T = |11>.
  6. After one Grover iterate we are exactly in the marked state; measurement gives the marked index with probability 1. Final answer: one iteration produces success with probability 1 for N=4, M=1.

Example 2 — numeric approximate case N=8, M=1.

  1. sin θ = 1/√8 = 0.353553; θ ≈ 0.361367 rad.
  2. k ≈ π/(4θ) ≈ 3.141593 / (4·0.361367) ≈ 3.141593 / 1.445468 ≈ 2.174. Choose integer k = 2.
  3. Amplitude on |w> after k=2: sin((2·2+1)θ) = sin(5θ) = sin(5·0.361367) = sin(1.806835) ≈ 0.9710.
  4. Probability ≈ 0.9710^2 ≈ 0.9430. Final answer: after 2 iterations success probability ≈ 94.3% (much better than classical 1/8 = 12.5% in a single random guess).

(You can check other k values: k=1 gives sin(3θ) ≈ sin(1.0841) ≈ 0.884, prob ≈ 0.781; k=3 gives sin(7θ) ≈ sin(2.5296) ≈ 0.574, prob ≈ 0.329 — shows overshoot if you apply too many iterations.)

Common mistakes

  • Using k ≈ π/(4√(N/M)) but forgetting the √(M) in denominator when M > 1.
  • Rounding k incorrectly; nearest integer to π/(4θ) gives best practical chance (and include ±1/2 in exact formula if asked).
  • Forgetting that measurement collapses the state and that repeated measurements require repeated runs.
  • Forgetting oracle and diffusion must be implementable as unitary circuits (not just equations).

For your exam

Kinds of questions:

  • Definitions: explain oracle, diffusion operator, Grover iterate.
  • Short notes: amplitude amplification, geometric picture (rotation), complexity comparison with classical search.
  • Derivations: algebraic derivation that G is a rotation by 2θ and calculation of |ψ_k> = sin((2k+1)θ)|w> + cos((2k+1)θ)|w_perp>.
  • Numericals: compute k and success probability for given N and M (like N=8, N=4). Show matrices for small N (N=4) and apply operators to |s>.
  • Diagrams: draw 2D rotation picture with labels (|w>, |w_perp>, |s>, θ, rotation 2θ).

What full-marks answers include:

  • Clear definitions with operator form (O = I − 2|w><w| and D = 2|s><s| − I).
  • Correct decomposition |s> = sin θ |w> + cos θ |w_perp> with sin θ = √(M/N).
  • Step-by-step proof that G is a rotation and expression for state after k iterations.
  • Correct rounding and numerical computation for k and final probability. If a matrix example is requested, include basis order and show matrix multiplication.

Quick check

  1. For M marked items in N total, what is sin θ?
  2. What is the operator form of the diffusion step?
  3. After k Grover iterations the amplitude on |w> equals what function of θ and k?

Answers: sin θ = sqrt(M/N); D = 2|s><s| − I; amplitude = sin((2k+1)θ).

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?