Part IV: Implementing Quantum Algorithms

"The hard part of implementing a quantum algorithm is almost never the algorithm."

Here is the thing nobody tells you before you implement your first quantum algorithm.

Grover's algorithm is four lines of pseudocode. Prepare a uniform superposition, apply the oracle, apply the diffusion operator, repeat about $\frac{\pi}{4}\sqrt{N}$ times, measure. You can hold the entire algorithm in your head. It took Lov Grover a paper of a few pages to state.

Then you sit down to implement it, and you discover that the oracle — the "black box" that the algorithm treats as a given — is where all the work lives. Building an oracle that marks the states satisfying an arbitrary Boolean condition means building a reversible circuit for that condition, which means multi-controlled gates, which means ancilla qubits, which means uncomputing those ancillas so they do not stay entangled with your answer and destroy the interference the algorithm depends on. The four-line algorithm sits on top of a hundred lines of oracle, and the hundred lines are where the bugs are.

That is why this part opens with a chapter on oracles rather than a chapter on Grover.

The arc

Part IV implements, in code, on simulators and on hardware, essentially every quantum algorithm you have heard of. The order is deliberate:

Foundations first (Chapter 19), because oracles are the substrate.

Then the historical sequence (Chapter 20): Deutsch, Deutsch–Jozsa, Bernstein–Vazirani, and Simon. These are toy problems and they are not toys. Each one isolates exactly one quantum phenomenon — phase kickback, interference across an exponential superposition, a hidden linear structure, a hidden subgroup — and each one is small enough to run on real hardware today and get a clean answer. They are also the best debugging targets in quantum programming, because you know the right answer in advance.

Then the two famous ones (Chapters 21–23): Grover, the QFT and phase estimation, and Shor. Grover you can run. The QFT you can run. Shor you can run for the number 15, and Chapter 23 is uncompromising about why that is not a step toward factoring RSA keys, with the resource estimate to prove it.

Then the algorithms that actually run today (Chapter 24): the variational family. VQE and QAOA are not elegant. They are hybrid, iterative, noise-tolerant, and heuristic, and they are the only algorithms in this part with a plausible claim to near-term usefulness. The book's running project is assembled here.

Then the thing that would change everything (Chapter 25): error correction, implemented as actual circuits. The 3-qubit codes, Shor's 9-qubit code, syndrome extraction, and decoding. And a clear-eyed look at the overhead that stands between here and a fault-tolerant machine.

What You Will Learn

Chapter 19 — Quantum Oracles. Phase oracles versus Boolean oracles, phase kickback, building oracles from truth tables, multi-controlled gates, ancillas, and uncomputation.

Chapter 20 — The First Quantum Algorithms. Deutsch, Deutsch–Jozsa, Bernstein–Vazirani, and Simon — implemented, run on hardware, and analyzed for what each one actually proves.

Chapter 21 — Grover's Algorithm. Oracle plus diffuser, the iteration count and why exceeding it makes things worse, 2- through 4-qubit implementations, amplitude growth plotted per iteration, and the point on real hardware where noise overwhelms the signal.

Chapter 22 — The Quantum Fourier Transform. The circuit and its recursive structure, the inverse, the approximate QFT, and the three things built on it: phase estimation, counting, and order finding.

Chapter 23 — Shor's Algorithm. The complete algorithm, including the modular exponentiation circuit that is the most complex construction in this book. Factoring 15 and 21. Continued fractions. And the honest resource estimate for RSA-2048.

Chapter 24 — Variational Algorithms. The hybrid loop, ansatz design, Hamiltonians as Pauli sums, cost evaluation with the Estimator primitive, classical optimizers, and convergence diagnostics. VQE is assembled here and QAOA is introduced.

Chapter 25 — Quantum Error Correction in Code. Bit-flip, phase-flip, and Shor's 9-qubit code as circuits. Syndrome measurement with mid-circuit measurement. Classical decoding. Injecting errors and watching correction work. The threshold theorem, and the surface code's price.

How This Part Fits

Part IV depends on Parts I and II heavily and on Part III not at all — the implementations are in Qiskit, with 🔀 In Another Framework callouts pointing at the alternatives. Chapters 24 and 25 are prerequisites for Parts VI and VII respectively.

One deliberate structural choice: Chapter 21 shows a Grover implementation that fails, returning a flat uniform distribution instead of the marked state. Chapter 21 diagnoses the symptom and stops there. The full diagnosis waits for Chapter 26, because the point of that bug is the method used to find it, and the method is what Part V is about. If you cannot stand the suspense, the answer is in Chapter 26 §26.5 and you will spoil a good exercise.

Time Investment

Chapter Title Difficulty Est. hours
19 Quantum Oracles Intermediate 7
20 The First Quantum Algorithms Intermediate 7
21 Grover's Algorithm Intermediate 8
22 The Quantum Fourier Transform Advanced 8
23 Shor's Algorithm Advanced 9
24 Variational Algorithms Advanced 9
25 Quantum Error Correction in Code Advanced 8
Part IV total ~56 hours

The Project in This Part

This is where vqelab becomes a real program. Chapter 19 turns Hamiltonians into first-class data. Chapter 22 contributes a reusable QFT. And Chapter 24 assembles the optimization loop and produces the project's first end-to-end energy — a genuine variational minimization, converging, on a two-qubit test Hamiltonian.

The molecule itself waits for Chapter 36. But after Chapter 24 the machine is built, and running it on hydrogen is a matter of supplying the right Hamiltonian.

Turn to Chapter 19.

Chapters in This Part