|q⟩ Bad Qubits

intermediate · Programming · Quantum Machine Learning (Intro)

The Parameter-Shift Rule

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(θ)U(\boldsymbol{\theta}) acting on ∣0⟩⊗n|0\rangle^{\otimes n} produces the state

∣ψ(θ)⟩=U(θ)∣0⟩⊗n.|\psi(\boldsymbol{\theta})\rangle = U(\boldsymbol{\theta})|0\rangle^{\otimes n}.

The training signal is an expectation value of some observable OO (often a Pauli operator):

E(θ)=⟨ψ(θ)∣O∣ψ(θ)⟩.E(\boldsymbol{\theta}) = \langle \psi(\boldsymbol{\theta}) | O | \psi(\boldsymbol{\theta}) \rangle.

For the simplest case — a single Ry(θ)R_y(\theta) gate followed by a ZZ measurement — the expectation is

E(θ)=⟨0∣Ry(θ)† Z Ry(θ)∣0⟩=cos⁡θ.E(\theta) = \langle 0 | R_y(\theta)^\dagger \, Z \, R_y(\theta) | 0 \rangle = \cos\theta.

You can verify this directly: Ry(θ)∣0⟩=cos⁡(θ/2)∣0⟩+sin⁡(θ/2)∣1⟩R_y(\theta)|0\rangle = \cos(\theta/2)|0\rangle + \sin(\theta/2)|1\rangle, so P(0)=cos⁡2(θ/2)P(0) = \cos^2(\theta/2) and P(1)=sin⁡2(θ/2)P(1) = \sin^2(\theta/2), giving E=P(0)−P(1)=cos⁡2(θ/2)−sin⁡2(θ/2)=cos⁡θE = P(0) - P(1) = \cos^2(\theta/2) - \sin^2(\theta/2) = \cos\theta.

The shift formula

Any generator that appears in a rotation gate of the form e−iθGe^{-i\theta G} with G2=I/4G^2 = I/4 (for example G=Y/2G = Y/2 in RyR_y) satisfies the following identity (Mitarai et al., 2018; Schuld et al., 2019):

∂E∂θ=12 ⁣[E ⁣(θ+π2)−E ⁣(θ−π2)].\frac{\partial E}{\partial \theta} = \frac{1}{2}\!\left[E\!\left(\theta + \tfrac{\pi}{2}\right) - E\!\left(\theta - \tfrac{\pi}{2}\right)\right].

This is not a finite-difference approximation — it is exact. The shift ±π/2\pm\pi/2 is not a small ε\varepsilon chosen for numerical stability; it comes from the eigenvalues of the generator.

Why it works

Write E(θ)=⟨ψ∣eiθGOe−iθG∣ψ⟩E(\theta) = \langle \psi | e^{i\theta G} O e^{-i\theta G} | \psi \rangle. Differentiating gives i⟨[G,O]⟩i\langle [G, O] \rangle. For G=Y/2G = Y/2 the commutator identity and G2=I/4G^2 = I/4 lead to

ddθE(θ)=E(θ+π/2)−E(θ−π/2)2,\frac{d}{d\theta} E(\theta) = \frac{E(\theta+\pi/2) - E(\theta-\pi/2)}{2},

which can be evaluated by running the same circuit at two shifted angles and subtracting.

Applying it to Ry(θ)R_y(\theta) at θ=π/4\theta = \pi/4

With E(θ)=cos⁡θE(\theta) = \cos\theta the analytic derivative is dE/dθ=−sin⁡θdE/d\theta = -\sin\theta. At θ=π/4\theta = \pi/4:

dEdθ∣θ=π/4=−sin⁡ ⁣(π4)=−12≈−0.7071.\frac{dE}{d\theta}\bigg|_{\theta=\pi/4} = -\sin\!\left(\tfrac{\pi}{4}\right) = -\tfrac{1}{\sqrt{2}} \approx -0.7071.

The parameter-shift formula gives the same result using only the shifted evaluations:

12[cos⁡ ⁣(π4+π2)−cos⁡ ⁣(π4−π2)]=12[cos⁡3π4−cos⁡ ⁣(−π4)]=12 ⁣[−12−12]=−12.\frac{1}{2}\left[\cos\!\left(\frac{\pi}{4}+\frac{\pi}{2}\right) - \cos\!\left(\frac{\pi}{4}-\frac{\pi}{2}\right)\right] = \frac{1}{2}\left[\cos\frac{3\pi}{4} - \cos\!\left(-\frac{\pi}{4}\right)\right] = \frac{1}{2}\!\left[-\frac{1}{\sqrt{2}} - \frac{1}{\sqrt{2}}\right] = -\frac{1}{\sqrt{2}}.

The two methods agree exactly, confirming the rule.

Try it

Use the parameter-shift rule to compute the gradient of E(θ)=cos⁡θE(\theta) = \cos\theta at θ=π/4\theta = \pi/4. Evaluate EE at the two shifted angles and apply the formula. The grader checks that your returned value equals −1/2-1/\sqrt{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.