Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Lab 8 · Variational circuits and their gradients

Level 7 · Quantum Causal AI research · Prerequisite lessons: L7-01 (Define the learning task), L7-02 (Variational circuits and parameter-shift gradients), L7-04 (Barren plateaus and trainability).

What you’ll learn

  • How a small parametrized circuit defines a model fθ(x)f_{\boldsymbol\theta}(x). How to train it with a classical optimizer, as in hybrid quantum circuit learning Mitarai et al., 2018.

  • Why the parameter-shift rule gives exact gradients from two circuit runs Schuld et al., 2019. How it compares with finite differences when each run has shot noise.

  • What a barren plateau looks like in a toy simulation: gradient variance that shrinks exponentially with the number of qubits McClean et al., 2018.

Open in Colab

1 · The model and its exact gradient

The engine’s model prepares ∣ψ⟩=RY(θ1)RZ(x)RY(θ0)∣0⟩|\psi\rangle=R_Y(\theta_1)R_Z(x)R_Y(\theta_0)|0\rangle and returns fθ(x)=⟨ψ∣Z∣ψ⟩f_{\boldsymbol\theta}(x)=\langle\psi|Z|\psi\rangle. Follow the Bloch vector through the three rotations and you get the closed form

fθ(x)=cos⁡θ0cos⁡θ1−sin⁡θ0sin⁡θ1cos⁡x.f_{\boldsymbol\theta}(x)=\cos\theta_0\cos\theta_1-\sin\theta_0\sin\theta_1\cos x .

Take a gate e−iθP/2e^{-i\theta P/2} with a Pauli PP. Its derivative is an exact finite difference with a large shift Schuld et al., 2019:

∂f∂θk=12[f(θk+π2)−f(θk−π2)].\frac{\partial f}{\partial\theta_k}=\tfrac12\Big[f(\theta_k+\tfrac{\pi}{2})-f(\theta_k-\tfrac{\pi}{2})\Big].
closed form and exact parameter-shift gradient verified at 300 random points

Explain. The parameter-shift rule is exact. It agrees with the analytic derivative to machine precision. The reason: ff is a sinusoid of each θk\theta_k (period 2π2\pi), and for a sinusoid this two-point formula is an identity.

2 · A regression task and its loss surface

We generate a target from hidden parameters θ⋆=(1.1, 0.7)\boldsymbol\theta^\star=(1.1,\,0.7) on 15 inputs in [−π,π][-\pi,\pi]. We fit it with the mean squared error

L(θ)=1m∑j=1m(fθ(xj)−yj)2,∇L=2m∑j(fθ(xj)−yj)∇fθ(xj).L(\boldsymbol\theta)=\frac{1}{m}\sum_{j=1}^{m}\big(f_{\boldsymbol\theta}(x_j)-y_j\big)^2,\qquad \nabla L=\frac{2}{m}\sum_j\big(f_{\boldsymbol\theta}(x_j)-y_j\big)\nabla f_{\boldsymbol\theta}(x_j).

The target is realizable by construction, so the global minimum is L=0L=0. Hover over the heatmap to read the loss.

initial loss 1.2777 -> final loss 8.01e-10 after 80 steps; final theta = [-0.6999 -1.1   ]
Loading...

The loss surface has several equally good minima, because the model has symmetries. For example, (θ0,θ1)↦(θ1,θ0)(\theta_0,\theta_1)\mapsto(\theta_1,\theta_0) leaves ff unchanged. The optimizer may therefore converge to a point other than θ⋆\boldsymbol\theta^\star with the same loss. The figure below replays training. Drag the slider to step through the optimization.

Loading...

3 · Shot noise: parameter shift vs finite differences

On hardware, each expectation value is estimated from NN shots. With f^=2n0/N−1\hat f=2n_0/N-1 we have Var⁡f^=(1−f2)/N\operatorname{Var}\hat f=(1-f^2)/N. Here are two estimators of ∂f/∂θ0\partial f/\partial\theta_0. Each uses two circuit settings with NN shots each:

  • parameter shift: g^PS=12[f^(θ0+π2)−f^(θ0−π2)]\hat g_{\rm PS}=\tfrac12\big[\hat f(\theta_0+\tfrac\pi2)-\hat f(\theta_0-\tfrac\pi2)\big], unbiased, variance ≤1/(2N)\le 1/(2N);

  • central finite difference: g^FD=[f^(θ0+h)−f^(θ0−h)]/(2h)\hat g_{\rm FD}=\big[\hat f(\theta_0+h)-\hat f(\theta_0-h)\big]/(2h), variance ≈(1−f2)/(2h2N)\approx(1-f^2)/(2h^2N), plus a bias of order h2h^2.

true gradient -0.7225
N      PS-RMSE   FD(h=0.01)  FD(h=0.1)  FD(h=0.5)  FD(h=1.0)
30     0.0887    12.8073    1.3290    0.2415    0.1547  
100    0.0480    6.9377    0.7003    0.1363    0.1271  
300    0.0278    4.1133    0.4048    0.0814    0.1191  
1000   0.0154    2.2600    0.2223    0.0514    0.1160  
3000   0.0091    1.2775    0.1285    0.0385    0.1149  
10000  0.0050    0.6847    0.0695    0.0322    0.1146  
exact bias of FD at h=1: +0.1145 (equals (sin(1) - 1) * gradient = +0.1145)
Loading...

Explain. Shrinking hh removes bias but amplifies shot noise like 1/h1/h. Tiny steps are useless on noisy hardware. A large step reduces noise but adds bias. The code computes the exact bias at h=1h=1 as (sin⁡1−1) ∂θ0f(\sin 1-1)\,\partial_{\theta_0}f. Parameter shift gets both: it is unbiased, and its shift is large (π/2\pi/2). In this example its RMSE was below the finite-difference RMSE for h∈{0.01,0.1,0.5}h\in\{0.01,0.1,0.5\} at every NN tested. That comparison is specific to this circuit and point. The code reports the h=1h=1 column without a claim.

4 · Barren plateaus in a toy simulation

McClean et al. studied wide classes of randomly initialized parametrized circuits, those that behave like unitary 2-designs McClean et al., 2018. They showed that, for these circuits, the probability that a gradient component is non-negligible is exponentially small in the number of qubits. We reproduce the trend with a small statevector simulator:

  • nn qubits start in RY(π/4)⊗n∣0⟩R_Y(\pi/4)^{\otimes n}|0\rangle;

  • each of L=5nL=5n layers applies a random Pauli rotation (RXR_X, RYR_Y, or RZR_Z, uniformly) with a uniformly random angle to each qubit, then a ladder of controlled-Z gates;

  • the cost is the global projector C=∣⟨0n∣ψ⟩∣2C=|\langle 0^n|\psi\rangle|^2;

  • we estimate Var⁡[∂C/∂θ1]\operatorname{Var}[\partial C/\partial\theta_1] over 300 random circuits for n=2,…,8n=2,\dots,8, using the exact parameter-shift rule.

First, we check the simulator against a dense matrix construction and against a finite difference.

statevector simulator and parameter-shift gradient validated
n = 2: Var[dC/dtheta_1] = 1.326e-02   mean = +3.0e-03
n = 3: Var[dC/dtheta_1] = 5.710e-03   mean = +6.3e-03
n = 4: Var[dC/dtheta_1] = 1.560e-03   mean = +3.4e-04
n = 5: Var[dC/dtheta_1] = 3.301e-04   mean = +3.0e-04
n = 6: Var[dC/dtheta_1] = 7.586e-05   mean = -3.0e-04
n = 7: Var[dC/dtheta_1] = 2.549e-05   mean = -1.9e-04
n = 8: Var[dC/dtheta_1] = 5.866e-06   mean = -1.2e-04
fitted log2-variance slope: -1.91 per qubit  (variance shrinks by about x3.8 per added qubit)
<Figure size 700x420 with 1 Axes>

Explain. The gradient mean is essentially zero. Its variance falls exponentially with nn in this toy; the code asserts a fitted slope steeper than 2−n2^{-n}. A gradient that small needs many shots to resolve. From Section 3, a parameter-shift estimate with NN shots has standard deviation up to 1/2N1/\sqrt{2N}. To resolve a gradient of typical size Var⁡\sqrt{\operatorname{Var}} you need N≳1/(2Var⁡)N\gtrsim 1/(2\operatorname{Var}) shots, and that number grows exponentially in nn.

What this simulation does not establish. We did not check that these circuits form a 2-design. We did not separate the effect of depth from the effect of the global cost. And seven system sizes cannot pin down an asymptotic rate.

Going deeper: why the shift rule is exact

Let f(θ)=⟨ψ∣U†(θ) O U(θ)∣ψ⟩f(\theta)=\langle\psi|U^\dagger(\theta)\,O\,U(\theta)|\psi\rangle with U(θ)=e−iθP/2U(\theta)=e^{-i\theta P/2} and P2=IP^2=I. Then U(θ)=cos⁡θ2 I−isin⁡θ2 PU(\theta)=\cos\tfrac\theta2\,I-i\sin\tfrac\theta2\,P, so f(θ)=a+bcos⁡θ+csin⁡θf(\theta)=a+b\cos\theta+c\sin\theta for constants a,b,ca,b,c. For any such function,

12[f(θ+π2)−f(θ−π2)]=−bsin⁡θ+ccos⁡θ=f′(θ).\tfrac12\big[f(\theta+\tfrac\pi2)-f(\theta-\tfrac\pi2)\big]=-b\sin\theta+c\cos\theta=f'(\theta).

The same argument fails for generators with more than two distinct eigenvalues. Schuld et al. handle such gates by conditioning a single gate on an ancilla Schuld et al., 2019. Later work derives rules with more shifts.

f(theta_0) = -0.0000 + 0.6216 cos + -0.7215 sin  (exact fit)

Exercises

Solution

f=cos⁡θ0cos⁡θ1−(sin⁡θ0sin⁡θ1)cos⁡xf=\cos\theta_0\cos\theta_1-(\sin\theta_0\sin\theta_1)\cos x: so α=cos⁡θ0cos⁡θ1\alpha=\cos\theta_0\cos\theta_1 and β=−sin⁡θ0sin⁡θ1\beta=-\sin\theta_0\sin\theta_1. It can never fit sin⁡x\sin x, cos⁡2x\cos 2x, or any function that is not even in xx. More expressive models re-upload xx several times.

Solution

Var⁡≤1/(2N)=5×10−4\operatorname{Var}\le 1/(2N)=5\times10^{-4}, so the standard deviation is at most 5×10−4≈0.022\sqrt{5\times10^{-4}}\approx0.022.

Solution

Require 1/2N≤Var⁡1/\sqrt{2N}\le\sqrt{\operatorname{Var}}, i.e. N≥1/(2Var⁡)N\ge 1/(2\operatorname{Var}). The check cell computes this number from the simulated variance. It is in the tens of thousands, compared with a few tens at n=2n=2.

Check yourself

shots per circuit needed so PS noise <= typical gradient size: {2: 38, 3: 88, 4: 321, 5: 1515, 6: 6592, 7: 19612, 8: 85245}
All checks passed.

Limits of this model

The sizes are toys: one qubit for training, at most eight for the plateau study. Asymptotic statements belong to the cited proofs, not to these plots.

The target is realizable. The model itself generated the data. Real learning tasks rarely work that way, and a good fit says nothing about generalization.

The noise is idealized. We simulate only binomial shot noise. Gate errors, readout errors, and drift are absent.

We used one random ensemble. The plateau result depends on the circuit family, depth, cost function, and initialization. Other choices (local costs, shallow or structured circuits) can behave differently.

We make no advantage claim. Every number here was computed classically in seconds. Nothing shows that a quantum model learns better than a classical one.

References
  1. Mitarai, K., Negoro, M., Kitagawa, M., & Fujii, K. (2018). Quantum Circuit Learning. Physical Review A, 98(3). 10.1103/PhysRevA.98.032309
  2. Schuld, M., Bergholm, V., Gogolin, C., Izaac, J., & Killoran, N. (2019). Evaluating analytic gradients on quantum hardware. Physical Review A, 99(3). 10.1103/PhysRevA.99.032331
  3. McClean, J. R., Boixo, S., Smelyanskiy, V. N., Babbush, R., & Neven, H. (2018). Barren plateaus in quantum neural network training landscapes. Nature Communications, 9(1). 10.1038/s41467-018-07090-4
  4. Wang, S., Fontana, E., Cerezo, M., Sharma, K., Sone, A., Cincio, L., & Coles, P. J. (2021). Noise-Induced Barren Plateaus in Variational Quantum Algorithms. Nature Communications, 12(1). 10.1038/s41467-021-27045-6