To train a VQE you need the gradient of the energy with respect to every circuit parameter. On a
classical simulator you could differentiate the state vector directly, but on real hardware you
only ever get expectation values from measurements. The question this lesson answers is: how do
you obtain an exact analytic gradient using only circuit evaluations?
Why finite differences fail
The naive approach is the finite-difference estimate
∂θk∂E≈2εE(θk+ε)−E(θk−ε).
This has two problems on a quantum device. A small ε makes the numerator a difference
of two nearly equal noisy numbers — the shot noise blows up as 1/ε. A large
ε introduces O(ε2) truncation bias. There is no good middle ground.
The parameter-shift rule
For gates of the form U(θ)=e−iθP/2 with a Pauli generator P (so P2=I,
which covers RX,RY,RZ), the gradient is given exactly by the same circuit evaluated at
two macroscopically shifted points:
∂θk∂E=21[E(θk+2π)−E(θk−2π)].
This is not an approximation. Because the eigenvalues of P/2 are ±21, the energy as
a function of θk is a pure sinusoid acosθk+bsinθk+c, and a sinusoid's
derivative is recovered exactly from samples a quarter-period apart. The shift is a fixed π/2,
so each evaluation is a normal-magnitude expectation value — no division by a tiny ε,
no truncation bias.
A concrete check
Take the simplest case: a single qubit in ∣0⟩, apply RY(θ), and measure Z. Since
RY(θ)∣0⟩=cos(θ/2)∣0⟩+sin(θ/2)∣1⟩,
E(θ)=⟨Z⟩=cos22θ−sin22θ=cosθ.
The true derivative is −sinθ. Apply the shift rule:
The two-evaluation recipe reproduces the analytic gradient exactly.
Try it
Implement E(θ)=cosθ for the RY-then-Z circuit, then return the parameter-shift
estimate of the gradient at θ=0.8 radians. The grader checks your returned number against
the exact value −sin(0.8).
Run your code to see the quantum state.
Sign in on the full site to ask questions and join the discussion.