Exercises: Variational Algorithms

These need pennylane, networkx, and qiskit. Solutions to starred exercises are in Answers to Selected Exercises.


VQE

24.1 ★ Diagonalize the H₂ Hamiltonian directly and report all four eigenvalues. Confirm the ground state matches the literature value of about −1.8572 Ha.

24.2 ★ Implement the four-parameter Hartree–Fock ansatz and run VQE on an exact simulator from six random starts. Report the best energy and its error.

24.3 ★ Verify the variational principle empirically: evaluate the cost at 100 random parameter settings and confirm none falls below the exact ground-state energy.

24.4 ★★ Replace the Hartree–Fock reference with $|00\rangle$ and re-run. Does the ansatz still reach chemical accuracy? How many iterations does it need compared with the HF start?

24.5 ★★ Compute the Hartree–Fock energy itself (the ansatz with all parameters zero). What fraction of the correlation energy does VQE recover?

24.6 ★★ Build the H₂ Hamiltonian at several bond lengths (0.5, 0.735, 1.0, 1.5, 2.0 Å) and plot the VQE dissociation curve against exact diagonalization. Where is the error largest, and why?

24.7 ★★★ Replace the hand-built ansatz with StronglyEntanglingLayers (Chapter 16) at 1, 2, and 3 layers. Compare the converged energy, the parameter count, and the gradient cost. Is the hardware-efficient ansatz better or worse here?


The shot budget

24.8 ★ Find the optimal parameters on an exact simulator, then evaluate the energy at 100, 1,000, 10,000, and 100,000 shots. Report the error at each.

24.9 ★ Repeat 24.8 twenty-five times per shot count and report the mean and standard deviation. Why is the mean the number to quote?

24.10 ★★ Confirm the mean error scales as $1/\sqrt N$ by fitting $\log(\text{error})$ against $\log(N)$. What slope do you get?

24.11 ★★ Implement shots_for_precision(sigma, epsilon) and verify against your measurement: at what shot count does the mean error first drop below 1.6 mHa?

24.12 ★★ Compute the total shot budget for 4, 20, 50, and 100 parameters at 200 iterations. Convert to QPU hours at 10,000 shots/second.

24.13 ★★ Run the full optimization at 1,000 shots per evaluation rather than exactly. Does it converge? How does the final energy compare to the exact-simulator result?

24.14 ★★★ Compare a gradient-based optimizer (Adam) against a gradient-free one (COBYLA or SPSA) at a fixed total shot budget. Which reaches a lower energy? Explain in terms of the $2n+1$ factor.

24.15 ★★★ The H₂ Hamiltonian has 5 terms. Group them into commuting sets that can share a measurement basis. How many distinct measurement settings do you need, and what does that save?


QAOA

24.16 ★ Brute-force the MaxCut optimum for the 4-node graph in §24.4 and report the full cut distribution.

24.17 ★ Implement QAOA at $p = 1, 2, 3, 4$ and reproduce the approximation-ratio table.

24.18 ★ Sample the optimized $p=3$ state and report the best cut and the fraction of shots achieving the optimum. Why is the best sample the deliverable rather than the mean?

24.19 ★★ Compare each QAOA ratio against the Goemans–Williamson guarantee of 0.87856. At which $p$ does QAOA first exceed it?

24.20 ★★ Count the two-qubit gates in a $p$-layer QAOA circuit for graphs with 5, 20, and 100 edges. Using Chapter 12's median gate error, estimate the survival probability at each depth.

24.21 ★★ Transpile a $p=2$ QAOA circuit for a 10-node random graph onto FakeSherbrooke and measure the routing overhead (Chapter 17). How much does the non-local interaction graph cost?

24.22 ★★★ Implement Goemans–Williamson using an SDP solver and run it on the same instance. Compare its actual ratio (not its worst-case bound) against QAOA at each $p$.

24.23 ★★★ Construct a graph family where QAOA at $p=1$ performs poorly. What structural property causes it?


The stack

24.24 ★★ For a $p=2$ QAOA circuit on a 6-node graph, compute: the layout score (Chapter 12), the connectivity overhead (Chapter 17), the gradient cost (Chapter 16), and the recommended mitigation (Chapter 13). Present as one report.

24.25 ★★ Show that mitigation can make things worse at a fixed total shot budget: run VQE with and without readout mitigation at a fixed total number of shots, and compare the final errors.

24.26 ★★★ Measure the gradient variance of your VQE ansatz at 2, 4, 6, and 8 qubits (extending the Hamiltonian with identity terms). Does the Hartree–Fock start avoid the barren plateau that Chapter 16 measured for random initialization?


Project

24.27 ★★ (Project Checkpoint) Build vqelab/variational_algorithms.py with shots_for_precision, shot_budget, h2_exact_ground_state, vqe() returning a VQEResult, and qaoa_maxcut() returning a QAOAResult. Write tests asserting:

  1. The H₂ reference matches −1.857275 to $10^{-5}$.
  2. VQE reaches chemical accuracy on an exact simulator.
  3. VQE respects the variational principle (energy ≥ reference).
  4. Every start converges to the same value.
  5. shots_for_precision(0.5, 0.0016) ≈ 97,657, and halving $\epsilon$ quadruples it.
  6. The budget for 100 parameters exceeds $10^9$ shots and 24 QPU hours.
  7. The budget scales linearly in parameter count.
  8. VQEResult.total_shots equals $(2n+1) \times$ iterations $\times$ shots.
  9. An exact run reports zero shots.
  10. Brute force finds the MaxCut optimum.
  11. The QAOA approximation ratio increases with $p$.
  12. QAOA at $p=1$ is BELOW the Goemans–Williamson guarantee, and the summary says so.
  13. QAOA at $p=3$ beats it.
  14. The best sample hits the optimum.

Tests 6 and 12 are the ones this chapter exists to encode.

24.28 ★★★ Extend the module with three_level_test(ansatz, hamiltonian) running the same circuit at (a) exact, (b) noiseless with finite shots, and (c) noisy with finite shots, and reporting which level first fails chemical accuracy. Apply it to your ansatz from 24.7.


Going further

24.29 ★★★ ADAPT-VQE grows the ansatz adaptively, adding the operator with the largest gradient at each step. Read the method and implement it for H₂. How many parameters does it need compared with the fixed ansatz?

24.30 ★★★ §24.7 estimates the classical crossover for quantum chemistry at around 50 correlated orbitals. Look up the resource requirements for a VQE calculation at that size — qubits, Hamiltonian terms, and shots — and compare with Chapter 15's fault-tolerant estimate for the same system via phase estimation. Which route looks more plausible, and why?