Training Quantum Physics Quantum Algorithms II: Grover's Search, QAOA, and Variational Quantum Algorithms
12 / 15

Quantum Algorithms II: Grover's Search, QAOA, and Variational Quantum Algorithms

40 min Quantum Physics

Quantum Algorithms II: Grover's Search, QAOA, and Variational Quantum Algorithms

Grover's search algorithm, introduced by Lov Grover in 1996, provides a quadratic speedup for the problem of searching an unstructured database of $N$ items for a marked element. Classically, this requires $O(N)$ queries on average; Grover's algorithm needs only $O(\sqrt{N})$ queries. While this is not exponential, it is provably optimal — no quantum algorithm can search an unstructured database in fewer than $\Omega(\sqrt{N})$ queries — and the quadratic advantage compounds across countless applications, from inverting cryptographic hash functions to solving NP-hard optimization problems heuristically.

The algorithm operates through a beautifully geometric mechanism called amplitude amplification. Starting from a uniform superposition over all $N$ items, each "Grover iteration" applies two reflections: first an oracle $U_f$ that flips the phase of the marked state, then a diffusion operator $D = 2|s\rangle\langle s| - I$ that reflects amplitudes about the average. Geometrically, in the 2D plane spanned by the marked state $|\omega\rangle$ and the uniform superposition $|s\rangle$, each iteration rotates the state by an angle $2\theta$ where $\sin\theta = 1/\sqrt{N}$. After $k = \lfloor \pi/(4\theta) \rfloor \approx \frac{\pi}{4}\sqrt{N}$ iterations, the state aligns almost perfectly with $|\omega\rangle$, giving success probability $\sin^2((2k+1)\theta) \approx 1$.

The optimality of Grover's algorithm is established by the BBBV theorem (Bennett, Bernstein, Brassard, Vazirani, 1997), which proves a lower bound of $\Omega(\sqrt{N})$ queries for quantum search using adversarial arguments on information-theoretic grounds. This is a rare example of a proven quantum lower bound and tells us something deep: quantum mechanics, despite its power, cannot miraculously solve unstructured problems exponentially faster. Structure in the problem — like the periodicity that Shor's algorithm exploits — is necessary for exponential quantum speedup.

Quantum amplitude amplification generalizes Grover's algorithm: given any quantum algorithm $\mathcal{A}$ that produces a "good" state with probability $p$, amplitude amplification finds the good state in $O(1/\sqrt{p})$ applications of $\mathcal{A}$, a quadratic improvement over the classical $O(1/p)$ repetitions. This is used to accelerate Monte Carlo integration (achieving $O(1/\epsilon)$ complexity vs classical $O(1/\epsilon^2)$), to speed up BPP algorithms, and as a subroutine within Shor's algorithm itself.

The Quantum Approximate Optimization Algorithm (QAOA), introduced by Farhi, Goldstone, and Gutmann in 2014, represents a different paradigm: heuristic near-term quantum algorithms for combinatorial optimization. QAOA targets NP-hard problems like Maximum Cut (MaxCut) by preparing a parameterized quantum state through alternating applications of a problem Hamiltonian $e^{-i\gamma H_C}$ and a mixing Hamiltonian $e^{-i\beta H_B}$, with $p$ layers of depth. The parameters $(\boldsymbol{\gamma}, \boldsymbol{\beta}) \in \mathbb{R}^{2p}$ are optimized classically. At $p = 1$ for MaxCut, QAOA achieves approximation ratio $0.6924$ — beating the naive $0.5$ but below the Goemans-Williamson SDP bound of $0.878$. As $p \to \infty$, QAOA converges to the exact solution, but the classical optimization over $2p$ parameters becomes harder.

The Variational Quantum Eigensolver (VQE) is the quantum chemistry flagship of the variational approach. Given a molecular Hamiltonian $H$ expressed as a sum of Pauli operators, VQE prepares a parameterized ansatz state $|\psi(\boldsymbol{\theta})\rangle$ on a quantum computer and measures $\langle\psi(\boldsymbol{\theta})|H|\psi(\boldsymbol{\theta})\rangle$ via repeated sampling. A classical optimizer adjusts $\boldsymbol{\theta}$ to minimize this expectation value, exploiting the variational principle: the ground state energy $E_0 \leq \langle H \rangle$ for any trial state. The quantum advantage is that the ansatz can explore a $2^n$-dimensional Hilbert space; the measurement collapses it to a classical real number, bridging quantum and classical computation.

The barren plateau problem threatens the scalability of variational algorithms. As the number of qubits $n$ grows, the gradient of the cost function with respect to parameters typically vanishes exponentially: $\text{Var}[\partial \mathcal{L}/\partial \theta_k] = O(2^{-n})$ for random parameterized circuits. This means the loss landscape is exponentially flat, making gradient-based optimization exponentially slow. Proposed mitigations include structured ansatze (UCCSD for chemistry, hardware-efficient ansatze), layerwise training, and quantum natural gradient methods that use the quantum Fisher information metric.

The HHL algorithm (Harrow, Hassidim, Lloyd, 2009) solves the linear system $Ax = b$ in $O(\text{poly}(\log N, \kappa, 1/\epsilon))$ time, where $\kappa$ is the condition number and $\epsilon$ is the precision — an exponential speedup over classical Gaussian elimination at $O(N^3)$. HHL uses phase estimation to encode the eigenvalues of $A$ in ancilla qubits, then applies a controlled rotation $R_k: |\lambda_k\rangle|0\rangle \mapsto |\lambda_k\rangle(\sqrt{1-C^2/\lambda_k^2}|0\rangle + C/\lambda_k|1\rangle)$, effectively computing $1/\lambda_k$. The exponential speedup is genuine but heavily caveated: the input must be provided as a quantum state (which may be hard to prepare), the output is a quantum state (measuring all $N$ components requires $O(N)$ measurements), and $A$ must be sparse and well-conditioned.

The NISQ (Noisy Intermediate-Scale Quantum) era, a term coined by John Preskill in 2018, describes present-day quantum devices with 50–1000 qubits, no error correction, and gate fidelities of 99–99.9%. NISQ algorithms like QAOA and VQE are designed to work within these constraints, using shallow circuits (depth $O(1)$ to $O(n)$) that decohere before errors accumulate catastrophically. Whether NISQ devices can demonstrate practical quantum advantage on real-world problems — not just contrived benchmarks — remains an open and hotly debated question as of 2026.

Grover's Oracle

The oracle $U_f$ for a marked element $\omega$ acts as:

$$U_f |x\rangle = \begin{cases} -|x\rangle & \text{if } x = \omega \\ |x\rangle & \text{otherwise} \end{cases}$$

In matrix form relative to $\{|\omega\rangle, \text{span}\{|\omega\rangle\}^\perp\}$: $U_f = I - 2|\omega\rangle\langle\omega|$. This flips the phase of the target state without collapsing the superposition.

Grover's Diffusion Operator

The diffusion (inversion about average) operator is:

$$D = 2|s\rangle\langle s| - I, \quad |s\rangle = \frac{1}{\sqrt{N}} \sum_{x=0}^{N-1} |x\rangle$$

Circuit implementation: $D = H^{\otimes n}(2|0\rangle\langle 0| - I)H^{\otimes n}$. One full Grover iteration is $G = D \cdot U_f$, which is a rotation by angle $2\theta$ in the plane $\{|\omega\rangle, |s'\rangle\}$ where $|s'\rangle$ is the uniform superposition over non-marked states.

Grover's Success Probability

After $k$ iterations, starting from $|s\rangle$, the probability of measuring $|\omega\rangle$ is:

$$P_k = \sin^2\left((2k+1)\theta\right), \quad \sin\theta = \frac{1}{\sqrt{N}}$$

Optimal number of iterations: $k^* = \left\lfloor \frac{\pi}{4\theta} \right\rfloor \approx \frac{\pi}{4}\sqrt{N}$, giving $P_{k^*} \geq 1 - O(1/N)$.

Proof: Decompose $|s\rangle = \sin\theta|\omega\rangle + \cos\theta|s'\rangle$. Each $G$ rotates by $2\theta$ toward $|\omega\rangle$. After $k$ steps: $G^k|s\rangle = \sin((2k+1)\theta)|\omega\rangle + \cos((2k+1)\theta)|s'\rangle$.

QAOA Circuit

For a combinatorial problem with cost Hamiltonian $H_C$ (diagonal in computational basis) and mixer $H_B = \sum_i X_i$, the depth-$p$ QAOA state is:

$$|\boldsymbol{\gamma},\boldsymbol{\beta}\rangle = e^{-i\beta_p H_B} e^{-i\gamma_p H_C} \cdots e^{-i\beta_1 H_B} e^{-i\gamma_1 H_C} |s\rangle$$

The objective is to maximize $F_p(\boldsymbol{\gamma},\boldsymbol{\beta}) = \langle\boldsymbol{\gamma},\boldsymbol{\beta}|H_C|\boldsymbol{\gamma},\boldsymbol{\beta}\rangle$, optimized classically. Each $e^{-i\gamma H_C}$ applies controlled phase shifts; each $e^{-i\beta H_B}$ is a product of single-qubit $R_x(2\beta)$ rotations.

Example 1

Apply 1 Grover iteration to a 2-qubit system ($N=4$) searching for $|11\rangle = |3\rangle$. Compute the amplitude of $|3\rangle$ before and after the iteration.

Initial state: $|s\rangle = \frac{1}{2}(|00\rangle + |01\rangle + |10\rangle + |11\rangle)$. All amplitudes $= 1/2$.

After oracle $U_f$: $|\psi_1\rangle = \frac{1}{2}(|00\rangle + |01\rangle + |10\rangle - |11\rangle)$. Phase of $|11\rangle$ flipped.

Average amplitude: $\bar{a} = \frac{1}{4}\left(\frac{1}{2}+\frac{1}{2}+\frac{1}{2}-\frac{1}{2}\right) = \frac{1}{4}$.

Inversion about average: New amplitude of $|x\rangle$ is $2\bar{a} - a_x$:

$|00\rangle$: $2(1/4)-1/2 = 0$.   $|01\rangle$: $0$.   $|10\rangle$: $0$.   $|11\rangle$: $2(1/4)-(-1/2) = 1$.

$$|\psi_2\rangle = |11\rangle$$

For $N=4$: $\sin\theta = 1/2$, $\theta = \pi/6$. Optimal iterations $= \lfloor\pi/(4 \times \pi/6)\rfloor = \lfloor 3/2 \rfloor = 1$. One iteration gives success probability $\sin^2(3\theta) = \sin^2(\pi/2) = 1$. ✓

Example 2

For $N = 100$ items, compare Grover's quantum query complexity to classical search. How many iterations does Grover's need?

Classical: Average $N/2 = 50$ queries; worst case $100$ queries.

Grover's: $\sin\theta = 1/\sqrt{100} = 0.1$, so $\theta = \arcsin(0.1) \approx 0.1002$ rad.

$$k^* = \left\lfloor \frac{\pi}{4 \times 0.1002} \right\rfloor = \left\lfloor 7.85 \right\rfloor = 7 \text{ iterations}$$

Success probability after 7 iterations: $\sin^2(15\theta) = \sin^2(15 \times 0.1002) = \sin^2(1.503) \approx \sin^2(86.1°) \approx 0.997$.

Speedup: $50/7 \approx 7.1 \approx \sqrt{N}/\pi \times 4 \approx \sqrt{100}/4 \times \pi \approx$ quadratic speedup factor $\approx \sqrt{N}$.

Example 3

Describe how QAOA solves MaxCut on a 3-node triangle graph at depth $p=1$. Write the cost Hamiltonian $H_C$.

MaxCut on triangle $G=\{1,2,3\}$, edges $\{(1,2),(2,3),(1,3)\}$. MaxCut assigns spins $z_i \in \{+1,-1\}$ to maximize $\sum_{(i,j)\in E} \frac{1-z_iz_j}{2}$.

Cost Hamiltonian (using $Z_i$ with eigenvalues $\pm 1$):

$$H_C = \frac{1}{2}(I - Z_1Z_2) + \frac{1}{2}(I - Z_2Z_3) + \frac{1}{2}(I - Z_1Z_3)$$

$$= \frac{3}{2}I - \frac{1}{2}(Z_1Z_2 + Z_2Z_3 + Z_1Z_3)$$

Maximum cut is 2 (in any triangle, you can cut at most 2 edges). Ground state of $-H_C$ corresponds to max cut.

QAOA $p=1$ state: $|\gamma,\beta\rangle = e^{-i\beta(X_1+X_2+X_3)}e^{-i\gamma H_C}|{+}{+}{+}\rangle$.

Optimizing over $\gamma,\beta$ classically: the QAOA-1 expectation achieves $\langle H_C\rangle \geq 2 \times 0.6924 \approx 1.38$ (at least the $p=1$ approximation ratio guarantee).

Example 4

The HHL algorithm solves $Ax=b$ for $A=\begin{pmatrix}3&1\\1&3\end{pmatrix}$, $b=\begin{pmatrix}1\\0\end{pmatrix}$. Find eigenvalues and describe the quantum phase kickback step.

Eigenvalues: $\det(A-\lambda I)=(3-\lambda)^2-1=0 \Rightarrow \lambda = 2, 4$.

Eigenvectors: $|u_1\rangle = \frac{1}{\sqrt{2}}\begin{pmatrix}1\\-1\end{pmatrix}$ ($\lambda_1=2$), $|u_2\rangle = \frac{1}{\sqrt{2}}\begin{pmatrix}1\\1\end{pmatrix}$ ($\lambda_2=4$).

Decompose: $|b\rangle = \frac{1}{\sqrt{2}}|u_1\rangle + \frac{1}{\sqrt{2}}|u_2\rangle$.

HHL steps: Phase estimation encodes $\lambda_1=2$ and $\lambda_2=4$ into ancilla qubits. Ancilla rotation: apply $R_y(2\arcsin(C/\lambda))$ conditioned on ancilla, adding amplitude $C/\lambda_k$ in the $|1\rangle$ branch. Post-select on ancilla $= |1\rangle$:

$$|x\rangle \propto \frac{1}{\lambda_1}|u_1\rangle + \frac{1}{\lambda_2}|u_2\rangle = \frac{1}{2}|u_1\rangle + \frac{1}{4}|u_2\rangle$$

which is proportional to $A^{-1}|b\rangle$. Classical solution: $x = A^{-1}b = \begin{pmatrix}3/8\\-1/8\end{pmatrix}$, and $|x\rangle \propto \begin{pmatrix}3\\-1\end{pmatrix}$, correctly captured by the quantum state.

Interactive Explorer: Grover's Amplitude Amplification
(slider: power-of-2 exponent, so $N=2^k$)
Target amplitude $\sin((2k+1)\theta)$:
Success probability $\sin^2((2k+1)\theta)$:
Optimal iterations $k^*$:
Quantum speedup vs classical:

Practice Problems

1. For $N = 64$, compute $\theta$, the optimal number of Grover iterations $k^*$, and the success probability $P_{k^*}$.
2. Apply the diffusion operator formula to show that inversion about the mean maps amplitude $a_x$ to $2\bar{a} - a_x$ where $\bar{a}$ is the mean amplitude.
3. Why does Grover's algorithm fail if you apply too many iterations? What happens at $k = 2k^*$?
4. State the BBBV theorem. Why does it imply no quantum algorithm can solve unstructured search in $o(\sqrt{N})$ queries?
5. In quantum amplitude amplification, a circuit $\mathcal{A}$ finds a solution with probability $p = 0.01$. How many applications of $\mathcal{A}$ does amplitude amplification need? Compare to classical repetition.
6. Write the MaxCut cost Hamiltonian for a 4-node path graph $1-2-3-4$ using $Z_i$ operators.
7. What is the barren plateau problem in VQE? Give a mathematical statement of how gradients scale with system size.
8. HHL's complexity is $O(\text{poly}(\log N, \kappa, 1/\epsilon))$. What are the three main caveats that limit its practical quantum advantage?
9. Grover's algorithm can be used to speed up brute-force search of AES-128 keys. If there are $N = 2^{128}$ keys, how many Grover iterations are needed?
10. Compare VQE and QAOA: what problems are they designed for, what is the role of the classical optimizer, and what is the key hardware requirement for each?
11. The unitary coupled cluster singles and doubles (UCCSD) ansatz for VQE has $O(n^4)$ parameters for $n$ orbitals. For a 50-orbital molecule, how many parameters must the classical optimizer handle?
12. Prove that the Grover iteration $G = (2|s\rangle\langle s| - I)(I - 2|\omega\rangle\langle\omega|)$ is a rotation by $2\theta$ in the plane $\{|\omega\rangle, |s'\rangle\}$.
Show Answer Key

1. $\sin\theta = 1/\sqrt{64} = 1/8$. $\theta = \arcsin(1/8) \approx 0.1253$ rad. $k^* = \lfloor\pi/(4\times0.1253)\rfloor = \lfloor6.28\rfloor = 6$ iterations. $P_6 = \sin^2(13\theta) = \sin^2(1.629) \approx 0.998$.

2. $D = 2|s\rangle\langle s| - I$. On amplitude vector: $D|\psi\rangle = 2|s\rangle\langle s|\psi\rangle - |\psi\rangle$. $\langle s|\psi\rangle = \frac{1}{\sqrt{N}}\sum_x a_x = \sqrt{N}\bar{a}$. So $D|\psi\rangle$ has $x$-component: $2\frac{1}{\sqrt{N}}(\sqrt{N}\bar{a}) - a_x = 2\bar{a}-a_x$. ✓

3. The state oscillates; at $k=2k^*$ it has rotated $4k^*\theta \approx \pi$ and is near $-|\omega\rangle$'s orthogonal complement, giving $P_{2k^*} \approx 0$. The algorithm "overshoots" the target.

4. BBBV (1997): any quantum algorithm making $T$ queries to a black-box function $f:\{0,1\}^n\to\{0,1\}$ that finds the unique input $x$ with $f(x)=1$ must satisfy $T = \Omega(\sqrt{N})$. Proof via adversarial progress measure tracking how much quantum weight is on the marked input after each query. Since weight starts at $1/N$ and must reach $\Omega(1)$, each query can increase it by at most $O(T/N)$, requiring $T=\Omega(\sqrt{N})$.

5. Amplitude amplification: $O(1/\sqrt{p}) = O(1/\sqrt{0.01}) = O(10)$ applications. Classical repetition: $O(1/p) = O(100)$ applications. Quadratic speedup: $100/10 = 10\times$.

6. Path $1-2-3-4$, edges $(1,2),(2,3),(3,4)$: $H_C = \frac{1}{2}(I-Z_1Z_2) + \frac{1}{2}(I-Z_2Z_3) + \frac{1}{2}(I-Z_3Z_4)$.

7. For a random $n$-qubit parameterized circuit with $L$ layers, $\text{Var}[\partial\mathcal{L}/\partial\theta_k] = O(b^{-n})$ for some $b>1$. Gradients vanish exponentially in $n$, making gradient-based optimization require exponentially many circuit evaluations to estimate even one gradient component.

8. (1) Input loading: preparing $|b\rangle$ from classical data may itself require $O(N)$ operations (no quantum RAM). (2) Output readout: $|x\rangle$ is a quantum state; reading all $N$ components requires $O(N)$ measurements, eliminating exponential speedup. (3) Condition number: complexity scales as $O(\kappa)$, so ill-conditioned systems ($\kappa \gg 1$) may still be slow.

9. $k^* \approx \frac{\pi}{4}\sqrt{2^{128}} = \frac{\pi}{4} \times 2^{64} \approx 1.5 \times 10^{19}$ iterations. This effectively halves the key length from 128 to 64 bits, which is why post-quantum standards often recommend AES-256.

10. VQE: ground-state energy of molecular Hamiltonians; classical optimizer adjusts continuous parameters $\boldsymbol{\theta}$; requires mid-circuit measurements and many shots for expectation values. QAOA: combinatorial optimization (MaxCut, TSP); classical optimizer adjusts $\gamma,\beta$; requires alternating cost/mixer layers. Both use a classical-quantum feedback loop; VQE typically needs more circuit depth.

11. $O(n^4) = 50^4 = 6{,}250{,}000$ parameters. Classical optimization in this $6.25$M-dimensional space is itself computationally challenging.

12. In the basis $\{|\omega\rangle, |s'\rangle\}$: $U_f = I - 2|\omega\rangle\langle\omega|$ has matrix $\begin{pmatrix}-1&0\\0&1\end{pmatrix}$ (reflection about $|s'\rangle$-axis). $D = 2|s\rangle\langle s|-I$; since $|s\rangle = \sin\theta|\omega\rangle+\cos\theta|s'\rangle$, its matrix is $\begin{pmatrix}\cos2\theta&\sin2\theta\\\sin2\theta&-\cos2\theta\end{pmatrix}$ (reflection about the $|s\rangle$ direction). Product of two reflections = rotation by twice the angle between the reflection axes. Axis of $U_f$ is $|s'\rangle$; axis of $D$ is $|s\rangle$; angle between them is $\theta$. So $G = D \cdot U_f$ is a rotation by $2\theta$. ✓