The VQE loop updates circuit parameters until the energy expectation converges. Two ideas make this
possible: the parameter-shift rule gives an exact quantum gradient, and a classical optimizer
translates that gradient into a new set of angles.
The parameter-shift rule
For any gate of the form G(θ)=e−iθP/2 where P is a Pauli operator, the
derivative of the energy expectation E(θ)=⟨ψ(θ)∣H∣ψ(θ)⟩
with respect to θ is
dθdE=2E(θ+2π)−E(θ−2π).
This is not a finite-difference approximation — it is an exact identity, because sin and
cos satisfy this shift relation. In practice you run the circuit twice (with θ shifted by
+π/2 and −π/2) and take the scaled difference. Each call to the quantum device gives you
one sample of the energy, so a full gradient over p parameters costs 2p circuit evaluations.
Gradient descent
Once you have the gradient ∇θE, the simplest update rule is
gradient descent:
θ(t+1)=θ(t)−η∇θE(θ(t)),
where η is the learning rate (a small positive number). Because E≥E0 always holds,
the energy can only decrease toward the ground-state energy as long as the step size is small
enough to avoid overshooting.
More sophisticated optimizers adapt the step size or use curvature estimates. Gradient-based choices
include ADAM and L-BFGS. For shallow VQE circuits, COBYLA (a derivative-free method) and SPSA (a
stochastic gradient estimator) are also common choices because they are robust to shot noise.
One full update step
Putting it together:
Run the ansatz at θ.
For each angle θk, run the ansatz at θk±π/2 and compute
∂E/∂θk=[E(θk+π/2)−E(θk−π/2)]/2.
Update θk←θk−η⋅∂E/∂θk.
Repeat until convergence.
For a single-angle ansatz (p=1), step 2 costs only two extra circuit runs and the update
reduces to a scalar subtraction.
Try it
In the exercise the quantum device has already been called: E_plus and E_minus are the measured
energies at θ+π/2 and θ−π/2. Your task is to apply the parameter-shift rule
and return the angle after one gradient-descent step.
Run your code to see the quantum state.
Sign in on the full site to ask questions and join the discussion.