Training a variational quantum circuit means adjusting its gate angles to minimize a cost function.
To do that efficiently we need gradients — but how do you differentiate with respect to an angle
that lives inside a quantum gate? The answer is the parameter-shift rule, an exact, hardware-
compatible recipe that requires only two circuit evaluations per parameter.
Expectation values as the loss
A variational circuit U(θ) acting on ∣0⟩⊗n produces the state
∣ψ(θ)⟩=U(θ)∣0⟩⊗n.
The training signal is an expectation value of some observable O (often a Pauli operator):
E(θ)=⟨ψ(θ)∣O∣ψ(θ)⟩.
For the simplest case — a single Ry(θ) gate followed by a Z measurement — the
expectation is
E(θ)=⟨0∣Ry(θ)†ZRy(θ)∣0⟩=cosθ.
You can verify this directly: Ry(θ)∣0⟩=cos(θ/2)∣0⟩+sin(θ/2)∣1⟩,
so P(0)=cos2(θ/2) and P(1)=sin2(θ/2), giving
E=P(0)−P(1)=cos2(θ/2)−sin2(θ/2)=cosθ.
The shift formula
Any generator that appears in a rotation gate of the form e−iθG with G2=I/4 (for
example G=Y/2 in Ry) satisfies the following identity (Mitarai et al., 2018; Schuld et al., 2019):
∂θ∂E=21[E(θ+2π)−E(θ−2π)].
This is not a finite-difference approximation — it is exact. The shift ±π/2 is not a small
ε chosen for numerical stability; it comes from the eigenvalues of the generator.
Why it works
Write E(θ)=⟨ψ∣eiθGOe−iθG∣ψ⟩. Differentiating
gives i⟨[G,O]⟩. For G=Y/2 the commutator identity and G2=I/4 lead to
dθdE(θ)=2E(θ+π/2)−E(θ−π/2),
which can be evaluated by running the same circuit at two shifted angles and subtracting.
Applying it to Ry(θ) at θ=π/4
With E(θ)=cosθ the analytic derivative is dE/dθ=−sinθ.
At θ=π/4:
dθdEθ=π/4=−sin(4π)=−21≈−0.7071.
The parameter-shift formula gives the same result using only the shifted evaluations:
The two methods agree exactly, confirming the rule.
Try it
Use the parameter-shift rule to compute the gradient of E(θ)=cosθ at
θ=π/4. Evaluate E at the two shifted angles and apply the formula.
The grader checks that your returned value equals −1/2 to machine precision.
Run your code to see the quantum state.
Sign in on the full site to ask questions and join the discussion.