|q⟩ Bad Qubits

intermediate · Programming · Variational Quantum Eigensolver (VQE)

Parameter Optimization

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(θ)=eiθP/2G(\theta) = e^{-i\theta P / 2} where PP is a Pauli operator, the derivative of the energy expectation E(θ)=ψ(θ)Hψ(θ)E(\theta) = \langle \psi(\theta) | H | \psi(\theta) \rangle with respect to θ\theta is

dEdθ=E ⁣(θ+π2)E ⁣(θπ2)2.\frac{dE}{d\theta} = \frac{E\!\left(\theta + \tfrac{\pi}{2}\right) - E\!\left(\theta - \tfrac{\pi}{2}\right)}{2}.

This is not a finite-difference approximation — it is an exact identity, because sin\sin and cos\cos satisfy this shift relation. In practice you run the circuit twice (with θ\theta shifted by +π/2+\pi/2 and π/2-\pi/2) and take the scaled difference. Each call to the quantum device gives you one sample of the energy, so a full gradient over pp parameters costs 2p2p circuit evaluations.

Gradient descent

Once you have the gradient θE\nabla_{\boldsymbol{\theta}} E, the simplest update rule is gradient descent:

θ(t+1)=θ(t)ηθE ⁣(θ(t)),\boldsymbol{\theta}^{(t+1)} = \boldsymbol{\theta}^{(t)} - \eta \, \nabla_{\boldsymbol{\theta}} E\!\left(\boldsymbol{\theta}^{(t)}\right),

where η\eta is the learning rate (a small positive number). Because EE0E \geq E_0 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:

  1. Run the ansatz at θ\boldsymbol{\theta}.
  2. For each angle θk\theta_k, run the ansatz at θk±π/2\theta_k \pm \pi/2 and compute E/θk=[E(θk+π/2)E(θkπ/2)]/2\partial E / \partial \theta_k = \bigl[E(\theta_k + \pi/2) - E(\theta_k - \pi/2)\bigr] / 2.
  3. Update θkθkηE/θk\theta_k \leftarrow \theta_k - \eta \cdot \partial E / \partial \theta_k.
  4. Repeat until convergence.

For a single-angle ansatz (p=1p = 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\theta + \pi/2 and θπ/2\theta - \pi/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.