QuantumCore

Lesson 55 · 150 min · advanced

Capstone: Data Re-uploading Hybrid Classifier

Loading progress…

The capstone puts the Stage 4 toolkit into one workflow: design, train and evaluate a two-qubit data re-uploading classifier for points inside or outside a circle. The lesson proves that LL uploads of RY(πxj)R_Y(\pi x_j) give a Fourier series with frequencies in {−L,…,L}2\{-L,\dots,L\}^2. It derives the mean-squared-error gradient through the exact parameter-shift rule and the cost of M(2P+1)M(2P+1) circuit evaluations per epoch, writes out Adam, and states the generalisation bound of Caro et al. (2022). It then measures how accuracy depends on depth and initialisation, and how the gradient variance behaves on two qubits.

Prerequisites: The Parameter-Shift Rule, Barren Plateaus, End-to-End Quantum-Kernel SVM

Objectives and key results
  • Define the two-qubit re-uploading classifier, show with the Heisenberg picture which weights and which uploads actually reach the observable Z0Z1Z_0Z_1, and compute its output for one layer.

  • Prove that LL re-uploads of RY(πxj)R_Y(\pi x_j) give f(x)=∑ω∈{−L..L}2cωeiπω⋅xf(x)=\sum_{\omega\in\{-L..L\}^2}c_\omega e^{i\pi\omega\cdot x} with c−ω=cω‾c_{-\omega}=\overline{c_\omega}, and verify the bound numerically with a 2-D discrete Fourier transform.

  • Derive the gradient of the mean squared error through the two-term parameter-shift rule for RYR_Y and RZR_Z weights, count M(2P+1)M(2P+1) circuit evaluations per epoch, and write the Adam update with bias correction.

  • State the generalisation bound O~(T/M)\tilde O(\sqrt{T/M}) of Caro et al., evaluate it for the lab, and compare it with the measured train/test gap for several depths and initialisations.

  • Derive the 2-design value 2/152/15 of the gradient variance on two qubits, compare it with Monte-Carlo estimates for L=1,…,12L=1,\dots,12, and explain why this model has no barren plateau.

formula · Spectrum of the re-uploading classifier
fw(x)=∑ω∈{−L,…,L}2cω(w) eiπ ω⋅x,c−ω=cω‾,∣ΩL∣=(2L+1)2f_w(x)=\sum_{\omega\in\{-L,\dots,L\}^2}c_\omega(w)\,e^{i\pi\,\omega\cdot x},\qquad c_{-\omega}=\overline{c_\omega},\qquad\lvert\Omega_L\rvert=(2L+1)^2
formula · MSE gradient by the parameter-shift rule
∂L∂wk=2M∑m=1M(fw(xm)−ym) 12[fw+π2ek(xm)−fw−π2ek(xm)],cost M(2P+1)\frac{\partial L}{\partial w_k}=\frac2M\sum_{m=1}^M\big(f_w(x_m)-y_m\big)\,\frac12\Big[f_{w+\frac\pi2e_k}(x_m)-f_{w-\frac\pi2e_k}(x_m)\Big],\qquad\text{cost }M(2P+1)
algorithm · Adam
mt=β1mt−1+(1−β1)gt,vt=β2vt−1+(1−β2)gt2,wt=wt−1−η mt/(1−β1t)vt/(1−β2t)+ϵm_t=\beta_1m_{t-1}+(1-\beta_1)g_t,\quad v_t=\beta_2v_{t-1}+(1-\beta_2)g_t^2,\quad w_t=w_{t-1}-\eta\,\frac{m_t/(1-\beta_1^t)}{\sqrt{v_t/(1-\beta_2^t)}+\epsilon}
theorem · Generalisation (Caro et al. 2022)
gen(w⋆)∈O(Tlog⁡TM+log⁡(1/δ)M)=O~(T/M)  with probability≥1−δ\mathrm{gen}(w^\star)\in O\Big(\sqrt{\tfrac{T\log T}{M}}+\sqrt{\tfrac{\log(1/\delta)}{M}}\Big)=\tilde O\big(\sqrt{T/M}\big)\ \text{ with probability}\ge1-\delta
formula · Gradient variance under a 2-design
Var[∂θf]=Tr(K2) Tr(H2)d2−1=12⋅415=215(d=4, H=Z0Z1)\mathrm{Var}\big[\partial_\theta f\big]=\frac{\mathrm{Tr}(K^2)\,\mathrm{Tr}(H^2)}{d^2-1}=\frac12\cdot\frac{4}{15}=\frac2{15}\quad(d=4,\ H=Z_0Z_1)

The capstone problem#

Stage 4 built a variational classifier piece by piece: a feature map followed by a trainable ansatz and a measured observable (VQC architecture), exact gradients from the parameter-shift rule, an understanding of when gradients vanish (barren plateaus), and the train/test protocol of the quantum-kernel pipeline. The capstone combines these on a task that a linear model cannot solve: classifying points of the unit square by whether they lie inside a circle. The model is the data re-uploading classifier of Pérez-Salinas et al. (2020), which feeds the same input into the circuit again in every layer.

Definition

Circle dataset

Draw 200200 points x=(x0,x1)x=(x_0,x_1) with coordinates from mulberry32(7), uniform on [0,1)2[0,1)^2, and label them y=+1y=+1 if (x0−12)2+(x1−12)2<0.15(x_0-\tfrac12)^2+(x_1-\tfrac12)^2<0.15 and y=−1y=-1 otherwise. The disc has area 0.15π≈0.4710.15\pi\approx0.471; 9999 of the 200200 points lie inside. The first 100100 points (5050 inside) form the training set, the last 100100 (4949 inside) the held-out test set. This is circleDataset(7, 200) in lib/qml.

Definition

Two-qubit data re-uploading classifier

For a depth L≥1L\ge1 and weights w=(wℓ,q,k)w=(w_{\ell,q,k}), ℓ<L\ell<L, q,k∈{0,1}q,k\in\{0,1\}, layer ℓ\ell applies in time order S(x)=RY(0)(πx0)⊗RY(1)(πx1),Wℓ=(RZ(wℓ,0,1)RY(wℓ,0,0))⊗(RZ(wℓ,1,1)RY(wℓ,1,0)),CNOT(0,1).S(x)=R_Y^{(0)}(\pi x_0)\otimes R_Y^{(1)}(\pi x_1),\qquad W_\ell=\big(R_Z(w_{\ell,0,1})R_Y(w_{\ell,0,0})\big)\otimes\big(R_Z(w_{\ell,1,1})R_Y(w_{\ell,1,0})\big),\qquad\mathrm{CNOT}(0,1). The model output and prediction are fw(x)=⟨00∣U†(x) (Z⊗Z) U(x)∣00⟩,U(x)=∏ℓ=L−10CNOT Wℓ S(x),y^=sign fw(x),f_w(x)=\langle00\rvert U^\dagger(x)\,(Z\otimes Z)\,U(x)\lvert00\rangle,\quad U(x)=\prod_{\ell=L-1}^{0}\mathrm{CNOT}\,W_\ell\,S(x),\qquad\hat y=\mathrm{sign}\,f_w(x), with f=0f=0 counted as wrong. There are P=4LP=4L weights and 7L7L gates. This is dataReuploadingClassifier(L); the lab grades templates L∈{3,4,5,6}L\in\{3,4,5,6\}.

Circuit · 2 qubits

Quantum circuit on 2 qubitsRY(1.00) on q0; RY(0.82) on q1; RY(2.97) on q0; RZ(-0.32) on q0; RY(4.94) on q1; RZ(4.87) on q1; X on q1 controlled by q0; RY(1.00) on q0; RY(0.82) on q1; RY(6.20) on q0; RZ(0.88) on q0; RY(4.21) on q1; RZ(5.91) on q1; X on q1 controlled by q0; RY(1.00) on q0; RY(0.82) on q1; RY(2.76) on q0; RZ(6.58) on q0; RY(1.36) on q1; RZ(3.60) on q1; X on q1 controlled by q0; RY(1.00) on q0; RY(0.82) on q1; RY(1.10) on q0; RZ(2.54) on q0; RY(2.84) on q1; RZ(1.30) on q1; X on q1 controlled by q0; RY(1.00) on q0; RY(0.82) on q1; RY(3.07) on q0; RZ(0.42) on q0; RY(1.97) on q1; RZ(4.82) on q1; X on q1 controlled by q0q0q1RY(1.00)RY(0.82)RY(2.97)RZ(-0.32)RY(4.94)RZ(4.87)RY(1.00)RY(0.82)RY(6.20)RZ(0.88)RY(4.21)RZ(5.91)RY(1.00)RY(0.82)RY(2.76)RZ(6.58)RY(1.36)RZ(3.60)RY(1.00)RY(0.82)RY(1.10)RZ(2.54)RY(2.84)RZ(1.30)RY(1.00)RY(0.82)RY(3.07)RZ(0.42)RY(1.97)RZ(4.82)

The L=5L=5 classifier with the first held-out point x≈(0.3174,0.2596)x\approx(0.3174,0.2596) (label +1+1) encoded and the lab's trained weights (rounded to four decimals) bound to numbers. Here f=⟨Z0Z1⟩≈0.3108f=\langle Z_0Z_1\rangle\approx0.3108, so the probability of an even-parity outcome (0000 or 1111) is 12(1+f)≈0.655\frac12(1+f)\approx0.655 and the point is classified as inside.

  • |00⟩0.2977
  • |01⟩0.1885
  • |10⟩0.1561
  • |11⟩0.3577
Proposition

Which weights reach the observable

(a) For every LL, fwf_w does not depend on wL−1,0,0w_{L-1,0,0}, wL−1,0,1w_{L-1,0,1}, wL−1,1,1w_{L-1,1,1} or on the last upload RY(0)(πx0)R_Y^{(0)}(\pi x_0). (b) For L=1L=1, fw(x)=cos⁡(πx1+w0,1,0)f_w(x)=\cos(\pi x_1+w_{0,1,0}): the output does not depend on x0x_0 at all. (c) For L≥2L\ge2, fwf_w does not depend on wL−2,0,1w_{L-2,0,1} either, so only 4L−44L-4 of the 4L4L weights are trainable in effect, and x0x_0 is uploaded L−1L-1 effective times.

Proof (6 steps)
  1. Step 1:
    CNOT Z1 CNOT=Z0Z1,CNOT Z0 CNOT=Z0 ⇒ CNOT (Z0Z1) CNOT=Z0Z0Z1=Z1\mathrm{CNOT}\,Z_1\,\mathrm{CNOT}=Z_0Z_1,\quad\mathrm{CNOT}\,Z_0\,\mathrm{CNOT}=Z_0\ \Rightarrow\ \mathrm{CNOT}\,(Z_0Z_1)\,\mathrm{CNOT}=Z_0Z_0Z_1=Z_1
    On ∣a,b⟩\lvert a,b\rangle the CNOT maps b↦a⊕bb\mapsto a\oplus b, so Z1Z_1 picks up the sign (−1)a(-1)^a of the control, and Z0Z_0 is unchanged. The CNOT is its own inverse, so pulling the observable back through the last gate turns Z0Z1Z_0Z_1 into Z1Z_1.
  2. Step 2:
    (G0⊗G1)†(I⊗Z)(G0⊗G1)=I⊗G1†ZG1,RZ†ZRZ=Z(G_0\otimes G_1)^\dagger(I\otimes Z)(G_0\otimes G_1)=I\otimes G_1^\dagger ZG_1,\qquad R_Z^\dagger ZR_Z=Z
    The remaining gates of layer L−1L-1 factorise into a qubit-00 part G0G_0 (the upload of x0x_0, RY(wL−1,0,0)R_Y(w_{L-1,0,0}), RZ(wL−1,0,1)R_Z(w_{L-1,0,1})) and a qubit-11 part G1G_1. G0†G0=IG_0^\dagger G_0=I removes the qubit-00 part, and the last gate on qubit 11, RZ(wL−1,1,1)R_Z(w_{L-1,1,1}), commutes with ZZ. This proves (a).
  3. Step 3:
    RY(a)†ZRY(a)=(c+isY)Z(c−isY)=(c2−s2)Z−2cs X=cos⁡a Z−sin⁡a XR_Y(a)^\dagger ZR_Y(a)=(c+isY)Z(c-isY)=(c^2-s^2)Z-2cs\,X=\cos a\,Z-\sin a\,X
    With c=cos⁡a2c=\cos\frac a2, s=sin⁡a2s=\sin\frac a2, ZY=−iXZY=-iX, YZ=iXYZ=iX and YZY=−ZYZY=-Z. The two qubit-11 rotations RY(πx1)R_Y(\pi x_1) and RY(wL−1,1,0)R_Y(w_{L-1,1,0}) combine to RY(πx1+wL−1,1,0)R_Y(\pi x_1+w_{L-1,1,0}).
  4. Step 4:
    OL−1=cos⁡(πx1+wL−1,1,0) Z1−sin⁡(πx1+wL−1,1,0) X1O_{L-1}=\cos(\pi x_1+w_{L-1,1,0})\,Z_1-\sin(\pi x_1+w_{L-1,1,0})\,X_1
    The observable seen by the state entering layer L−1L-1. For L=1L=1 that state is ∣00⟩\lvert00\rangle, with ⟨Z1⟩=1\langle Z_1\rangle=1 and ⟨X1⟩=0\langle X_1\rangle=0, hence fw(x)=cos⁡(πx1+w0,1,0)f_w(x)=\cos(\pi x_1+w_{0,1,0}). This proves (b): one layer can only cut the square into stripes parallel to the x0x_0 axis.
  5. Step 5:
    CNOT Z1 CNOT=Z0Z1,CNOT X1 CNOT=X1\mathrm{CNOT}\,Z_1\,\mathrm{CNOT}=Z_0Z_1,\qquad\mathrm{CNOT}\,X_1\,\mathrm{CNOT}=X_1
    For L≥2L\ge2, pull OL−1O_{L-1} back through the CNOT of layer L−2L-2: XX on the target commutes with the CNOT. Both terms act on qubit 00 as II or ZZ.
  6. Step 6:
    RZ(wL−2,0,1)† (I0⊗A+Z0⊗B) RZ(wL−2,0,1)=I0⊗A+Z0⊗BR_Z(w_{L-2,0,1})^\dagger\,\big(I_0\otimes A+Z_0\otimes B\big)\,R_Z(w_{L-2,0,1})=I_0\otimes A+Z_0\otimes B
    The rotations RZ(wL−2,1,1)R_Z(w_{L-2,1,1}), RY(wL−2,1,0)R_Y(w_{L-2,1,0}) act on qubit 11 only and change AA and BB but not the qubit-00 factors. The next gate back, RZ(wL−2,0,1)R_Z(w_{L-2,0,1}), commutes with II and ZZ, so it drops out. This proves (c); for L≥2L\ge2 the live weights number 4L−44L-4.

∎

The engine treats all 4L4L weights alike, and the lab requires all of them, but the four dead ones have identically vanishing gradients. The TypeScript cell at the end finds exactly these four with the shift rule. The proposition explains the poor L=1L=1 results below, about 0.580.58 held-out accuracy: a stripe cos⁡(πx1+w)>0\cos(\pi x_1+w)>0 cannot capture a disc.

Re-uploading and the Fourier spectrum#

The angle-encoding lesson showed that a model with LL uploads of a single-qubit rotation is a trigonometric polynomial of degree LL (Schuld, Sweke & Meyer 2021). We now prove the two-feature version for our classifier, in the units of the encoding angle πxj\pi x_j.

Theorem

Fourier spectrum of the re-uploading classifier

For every LL and ww there are xx-independent coefficients cω(w)∈Cc_\omega(w)\in\mathbb C with fw(x)=∑ω∈ΩLcω eiπ ω⋅x,ΩL={−L,…,L}2,c−ω=cω‾.f_w(x)=\sum_{\omega\in\Omega_L}c_\omega\,e^{i\pi\,\omega\cdot x},\qquad\Omega_L=\{-L,\dots,L\}^2,\qquad c_{-\omega}=\overline{c_\omega}. ΩL\Omega_L has (2L+1)2(2L+1)^2 elements, and fwf_w is fixed by (2L+1)2(2L+1)^2 real numbers (121121 for L=5L=5). By the proposition on dead weights, cω=0c_\omega=0 whenever ∣ω0∣=L\lvert\omega_0\rvert=L.

Proof (8 steps)
  1. Step 1:
    RY(πx)=e−ix G,G=π2Y=π2∣+i⟩⟨+i∣−π2∣−i⟩⟨−i∣R_Y(\pi x)=e^{-ix\,G},\qquad G=\tfrac\pi2Y=\tfrac\pi2\lvert{+i}\rangle\langle{+i}\rvert-\tfrac\pi2\lvert{-i}\rangle\langle{-i}\rvert
    The upload is generated by G=π2YG=\frac\pi2Y, whose eigenvalues are ±π2\pm\frac\pi2 with the eigenvectors ∣±i⟩=(∣0⟩±i∣1⟩)/2\lvert\pm i\rangle=(\lvert0\rangle\pm i\lvert1\rangle)/\sqrt2 of YY.
  2. Step 2:
    S(x)=∑j,k∈{±1}e−iπ2(jx0+kx1) Πj⊗Πk,Π±1=∣±i⟩⟨±i∣S(x)=\sum_{j,k\in\{\pm1\}}e^{-i\frac\pi2(jx_0+kx_1)}\,\Pi_j\otimes\Pi_k,\qquad\Pi_{\pm1}=\lvert{\pm i}\rangle\langle{\pm i}\rvert
    Spectral decomposition e−ixG=∑λe−ixλΠλe^{-ixG}=\sum_\lambda e^{-ix\lambda}\Pi_\lambda on each qubit, and the tensor product of the two uploads.
  3. Step 3:
    U(x)∣00⟩=∑j,k∈{±1}Le−iπ2(x0∑ℓjℓ+x1∑ℓkℓ) ∣ajk⟩U(x)\lvert00\rangle=\sum_{\mathbf j,\mathbf k\in\{\pm1\}^L}e^{-i\frac\pi2\left(x_0\sum_\ell j_\ell+x_1\sum_\ell k_\ell\right)}\,\lvert a_{\mathbf j\mathbf k}\rangle
    Insert the decomposition for each of the LL uploads. The vectors ∣ajk⟩=CNOT WL−1(ΠjL−1⊗ΠkL−1)⋯CNOT W0(Πj0⊗Πk0)∣00⟩\lvert a_{\mathbf j\mathbf k}\rangle=\mathrm{CNOT}\,W_{L-1}(\Pi_{j_{L-1}}\otimes\Pi_{k_{L-1}})\cdots\mathrm{CNOT}\,W_0(\Pi_{j_0}\otimes\Pi_{k_0})\lvert00\rangle do not depend on xx.
  4. Step 4:
    fw(x)=∑j,k,j′,k′eiπ(x0∑ℓjℓ′−jℓ2+x1∑ℓkℓ′−kℓ2) ⟨aj′k′∣Z⊗Z∣ajk⟩f_w(x)=\sum_{\mathbf j,\mathbf k,\mathbf j',\mathbf k'}e^{i\pi\left(x_0\sum_\ell\frac{j'_\ell-j_\ell}2+x_1\sum_\ell\frac{k'_\ell-k_\ell}2\right)}\,\langle a_{\mathbf j'\mathbf k'}\rvert Z\otimes Z\lvert a_{\mathbf j\mathbf k}\rangle
    The bra carries the conjugate phases. Each difference jℓ′−jℓ2\frac{j'_\ell-j_\ell}2 lies in {−1,0,1}\{-1,0,1\}.
  5. Step 5:
    ω0=∑ℓ=0L−1jℓ′−jℓ2∈{−L,…,L},ω1=∑ℓ=0L−1kℓ′−kℓ2∈{−L,…,L}\omega_0=\sum_{\ell=0}^{L-1}\tfrac{j'_\ell-j_\ell}2\in\{-L,\dots,L\},\qquad\omega_1=\sum_{\ell=0}^{L-1}\tfrac{k'_\ell-k_\ell}2\in\{-L,\dots,L\}
    A sum of LL terms in {−1,0,1}\{-1,0,1\}. Collecting all terms with the same ω=(ω0,ω1)\omega=(\omega_0,\omega_1) defines cωc_\omega as a sum of the xx-independent matrix elements, which proves the expansion.
  6. Step 6:
    14∫02 ⁣ ⁣∫02eiπ(ω−ω′)⋅x dx=δωω′ ⇒ cω=14∫[0,2)2fw(x) e−iπω⋅x dx\frac14\int_0^2\!\!\int_0^2e^{i\pi(\omega-\omega')\cdot x}\,dx=\delta_{\omega\omega'}\ \Rightarrow\ c_\omega=\frac14\int_{[0,2)^2}f_w(x)\,e^{-i\pi\omega\cdot x}\,dx
    The exponentials are orthonormal on the period square [0,2)2[0,2)^2, so the coefficients are unique.
  7. Step 7:
    fw=fw‾ ⇒ c−ω=14∫fw eiπω⋅xdx=14∫fw e−iπω⋅xdx‾=cω‾f_w=\overline{f_w}\ \Rightarrow\ c_{-\omega}=\frac14\int f_w\,e^{i\pi\omega\cdot x}dx=\overline{\frac14\int f_w\,e^{-i\pi\omega\cdot x}dx}=\overline{c_\omega}
    Z⊗ZZ\otimes Z is Hermitian, so fwf_w is real. In particular c0c_0 is real, and the (2L+1)2−1(2L+1)^2-1 other coefficients come in conjugate pairs, so 1+2⋅(2L+1)2−12=(2L+1)21+2\cdot\frac{(2L+1)^2-1}2=(2L+1)^2 real numbers determine fwf_w.
  8. Step 8:
    fw=f~w,f~w: the circuit without the last RY(0)(πx0) ⇒ ω0∈{−(L−1),…,L−1}f_w=\tilde f_w,\quad\tilde f_w:\ \text{the circuit without the last }R_Y^{(0)}(\pi x_0)\ \Rightarrow\ \omega_0\in\{-(L-1),\dots,L-1\}
    By part (a) of the proposition, deleting the last upload of x0x_0 does not change the output. Applied to the circuit f~w\tilde f_w, which uploads x0x_0 only L−1L-1 times, the argument above gives frequencies ∣ω0∣≤L−1\lvert\omega_0\rvert\le L-1. Since the coefficients are unique, cω=0c_\omega=0 whenever ∣ω0∣=L\lvert\omega_0\rvert=L.

∎

Numerical check. A trigonometric polynomial with ∣ωj∣≤L\lvert\omega_j\rvert\le L is reproduced exactly by its samples on an N×NN\times N grid of [0,2)2[0,2)^2 when N>2LN>2L, because no two frequencies in range alias. For L=2L=2 and the weights (0.3,1.1,−0.7,2.0,0.5,−1.4,0.9,0.2)(0.3,1.1,-0.7,2.0,0.5,-1.4,0.9,0.2) (order w0,0,0,w0,0,1,…,w1,1,1w_{0,0,0},w_{0,0,1},\dots,w_{1,1,1}), an 8×88\times8 discrete Fourier transform gives coefficients of size at most 10−1510^{-15} for every ∣ωj∣>2\lvert\omega_j\rvert>2: the model has no frequency beyond LL. Only 99 of the 2525 allowed coefficients are non-zero, at ω0∈{−1,0,1}\omega_0\in\{-1,0,1\} and ω1∈{−2,0,2}\omega_1\in\{-2,0,2\}. The restriction of ω0\omega_0 is part (a) of the proposition. The even values of ω1\omega_1 come from the two layers: at L=2L=2 the output is a product of two factors of first degree in cos⁡πx1,sin⁡πx1\cos\pi x_1,\sin\pi x_1. For L=3,4,5L=3,4,5 and random weights the count is 3535, 6363 and 99=(2L−1)(2L+1)99=(2L-1)(2L+1), so at depth 55 the classifier has 9999 active Fourier coefficients controlled by 1616 live weights. A disc is not a finite Fourier series, so deeper models approximate its boundary better, but each extra layer also adds weights to train.

The widget shows the one-qubit version from the angle-encoding lesson: five RYR_Y uploads and random trainable layers give a spectrum in {−5,…,5}\{-5,\dots,5\}. Lower the number of layers and the highest available frequency drops with it.

Quick check

How many frequency vectors ω=(ω0,ω1)\omega=(\omega_0,\omega_1) does the theorem allow for the lab's reference depth L=5L=5, before taking the dead upload into account?

tolerance ±0.5

Loss, exact gradients and Adam#

Derivation

The MSE gradient through the parameter-shift rule

  1. Step 1:
    L(w)=1M∑m=1M(fw(xm)−ym)2,ym∈{±1}, fw∈[−1,1]L(w)=\frac1M\sum_{m=1}^M\big(f_w(x_m)-y_m\big)^2,\qquad y_m\in\{\pm1\},\ f_w\in[-1,1]
    The mean squared error on the MM training points. Its minimum 00 would require fw(xm)=ymf_w(x_m)=y_m exactly; in practice it pushes ymfw(xm)y_mf_w(x_m) towards 11, i.e. points away from the decision boundary fw=0f_w=0.
  2. Step 2:
    ∂L∂wk=2M∑m=1M(fw(xm)−ym) ∂fw(xm)∂wk\frac{\partial L}{\partial w_k}=\frac2M\sum_{m=1}^M\big(f_w(x_m)-y_m\big)\,\frac{\partial f_w(x_m)}{\partial w_k}
    Chain rule. The loss is classical post-processing, so only ∂fw/∂wk\partial f_w/\partial w_k needs the circuit.
  3. Step 3:
    f(θ)=⟨ψ∣RP(θ)†B RP(θ)∣ψ⟩,θ=wk, P∈{Y,Z}f(\theta)=\langle\psi\rvert R_P(\theta)^\dagger B\,R_P(\theta)\lvert\psi\rangle,\qquad\theta=w_k,\ P\in\{Y,Z\}
    Each weight occurs in exactly one gate, with coefficient 11. Split the circuit at that gate: ∣ψ⟩\lvert\psi\rangle is the state just before it, and BB is Z0Z1Z_0Z_1 pulled back through all later gates (Heisenberg picture). Neither depends on θ\theta.
  4. Step 4:
    RP†BRP=c2B+s2PBP+ics [P,B]=12(B+PBP)+12cos⁡θ (B−PBP)+12sin⁡θ  i[P,B]R_P^\dagger BR_P=c^2B+s^2PBP+ics\,[P,B]=\tfrac12(B+PBP)+\tfrac12\cos\theta\,(B-PBP)+\tfrac12\sin\theta\;i[P,B]
    Insert RP(θ)=cI−isPR_P(\theta)=cI-isP with c=cos⁡θ2c=\cos\frac\theta2, s=sin⁡θ2s=\sin\frac\theta2, then c2=1+cos⁡θ2c^2=\frac{1+\cos\theta}2, s2=1−cos⁡θ2s^2=\frac{1-\cos\theta}2, cs=sin⁡θ2cs=\frac{\sin\theta}2. This uses only P2=IP^2=I, so it holds for RYR_Y and RZR_Z alike. Hence f(θ)=a+bcos⁡θ+c′sin⁡θf(\theta)=a+b\cos\theta+c'\sin\theta with constants a,b,c′a,b,c'.
  5. Step 5:
    12[f(θ+π2)−f(θ−π2)]=12[b(−2sin⁡θ)+c′(2cos⁡θ)]=−bsin⁡θ+c′cos⁡θ=f′(θ)\tfrac12\big[f(\theta+\tfrac\pi2)-f(\theta-\tfrac\pi2)\big]=\tfrac12\big[b(-2\sin\theta)+c'(2\cos\theta)\big]=-b\sin\theta+c'\cos\theta=f'(\theta)
    cos⁡(θ±π2)=∓sin⁡θ\cos(\theta\pm\frac\pi2)=\mp\sin\theta and sin⁡(θ±π2)=±cos⁡θ\sin(\theta\pm\frac\pi2)=\pm\cos\theta; the constant aa cancels. The two-term rule is exact, not a finite-difference approximation, as proved for general Pauli rotations in the parameter-shift lesson.
  6. Step 6:
    per point: 1+2P evaluations;per epoch: M(2P+1),P=4L\text{per point: }1+2P\ \text{evaluations};\qquad\text{per epoch: }M(2P+1),\qquad P=4L
    One unshifted evaluation gives fw(xm)f_w(x_m) for the loss, and two shifted evaluations per weight give the gradient.
  7. Step 7:
    L=5, M=100:100⋅(2⋅20+1)=4100 per epoch,80⋅4100=328 000 in totalL=5,\ M=100:\quad100\cdot(2\cdot20+1)=4100\ \text{per epoch},\qquad80\cdot4100=328\,000\ \text{in total}
    The cost of the lab's reference run. Skipping the four dead weights would give 100⋅(2⋅16+1)=3300100\cdot(2\cdot16+1)=3300 per epoch. On hardware each evaluation is itself an average over shots, so the gradient becomes a sum of M(2P+1)M(2P+1) noisy estimates.
Derivation

Adam (Kingma & Ba 2015)

  1. Step 1:
    gt=∇L(wt−1),m0=v0=0g_t=\nabla L(w_{t-1}),\qquad m_0=v_0=0
    Full-batch gradient at step t=1,2,…t=1,2,\dots, computed with the shift rule.
  2. Step 2:
    mt=β1mt−1+(1−β1)gt,vt=β2vt−1+(1−β2) gt⊙gtm_t=\beta_1m_{t-1}+(1-\beta_1)g_t,\qquad v_t=\beta_2v_{t-1}+(1-\beta_2)\,g_t\odot g_t
    Exponential moving averages of the gradient (first moment) and of its componentwise square (second moment).
  3. Step 3:
    mt=(1−β1)∑s=1tβ1 t−sgs ⇒ E[mt]=gˉ (1−β1)∑s=1tβ1 t−s=gˉ (1−β1t)m_t=(1-\beta_1)\sum_{s=1}^t\beta_1^{\,t-s}g_s\ \Rightarrow\ \mathbb E[m_t]=\bar g\,(1-\beta_1)\sum_{s=1}^t\beta_1^{\,t-s}=\bar g\,(1-\beta_1^t)
    Unroll the recursion from m0=0m_0=0 and take expectations under the assumption E[gs]=gˉ\mathbb E[g_s]=\bar g (geometric sum). The averages are biased towards 00 for small tt.
  4. Step 4:
    m^t=mt1−β1t,v^t=vt1−β2t\hat m_t=\frac{m_t}{1-\beta_1^t},\qquad\hat v_t=\frac{v_t}{1-\beta_2^t}
    Dividing by 1−βt1-\beta^t removes the bias, by the previous line and its analogue for vtv_t.
  5. Step 5:
    wt=wt−1−η m^tv^t+ϵ,η=0.1, β1=0.9, β2=0.999, ϵ=10−8w_t=w_{t-1}-\eta\,\frac{\hat m_t}{\sqrt{\hat v_t}+\epsilon},\qquad\eta=0.1,\ \beta_1=0.9,\ \beta_2=0.999,\ \epsilon=10^{-8}
    Componentwise update with the lab's hyper-parameters. For a gradient of constant sign, m^t/v^t=±1\hat m_t/\sqrt{\hat v_t}=\pm1, so each step moves a weight by about η\eta, whatever the size of the gradient. Rescaling LL by λ>0\lambda>0 leaves the steps unchanged up to ϵ\epsilon.

The workbench trains the classifier in your browser with exactly these formulas: initial weights 2πu2\pi u from mulberry32(seed), the shift-rule gradient and Adam. Per epoch it shows the loss, the training and test accuracy, the gradient norm and the decision map sign fw\mathrm{sign}\,f_w over the unit square, with the true circle drawn on top.

Quick check

You choose the template L=6L=6 and train on all M=100M=100 points with your own two-term shift rule, differentiating every weight. How many circuit evaluations does one epoch cost?

tolerance ±0.5

Generalisation, depth and restarts#

Theorem (Caro, Huang, Cerezo, Sharma, Sornborger, Cincio and Coles, 2022; statement only). Let a quantum machine-learning model contain TT trainable local gates, and let it be trained on MM points drawn i.i.d. from a distribution D\mathcal D, with a bounded loss ℓ\ell. For the trained weights w⋆w^\star define the generalisation error gen(w⋆)=R(w⋆)−R^M(w⋆)\mathrm{gen}(w^\star)=R(w^\star)-\hat R_M(w^\star), the expected loss R(w)=E(x,y)∼D ℓ(w;x,y)R(w)=\mathbb E_{(x,y)\sim\mathcal D}\,\ell(w;x,y) minus the training loss R^M(w)=1M∑mℓ(w;xm,ym)\hat R_M(w)=\frac1M\sum_m\ell(w;x_m,y_m). Then, with probability at least 1−δ1-\delta over the training set, gen(w⋆)∈O(Tlog⁡TM+log⁡(1/δ)M)=O~(TM),\mathrm{gen}(w^\star)\in O\Big(\sqrt{\frac{T\log T}{M}}+\sqrt{\frac{\log(1/\delta)}{M}}\Big)=\tilde O\Big(\sqrt{\frac TM}\Big), where O~\tilde O hides logarithmic factors. The bound depends on the number of trainable gates, not on the 2n2^n-dimensional Hilbert space.

For the lab, T=4L=20T=4L=20 and M=100M=100 give T/M=0.2≈0.447\sqrt{T/M}=\sqrt{0.2}\approx0.447, and with the logarithm 20ln⁡20/100≈0.774\sqrt{20\ln20/100}\approx0.774. The constants are not specified, so these numbers describe scaling, not a guarantee: four times more data halves the bound, twice as many gates raise it by 2\sqrt2. Measured with the 00–11 loss, the gaps between training and held-out accuracy of the L=5L=5 runs below are 0.080.08, 0.040.04 and 0.070.07, far below 0.4470.447, as is typical for worst-case bounds.

Depth and initialisation. The table reports the engine's trainVQC with exactly the lab's recipe (MSE, Adam with η=0.1\eta=0.1, 8080 epochs, initial weights 2πu2\pi u from mulberry32(seed), the first 100100 points for training, the last 100100 for testing). The L=5L=5, seed 11 run is the lab's reference solution, weight for weight.

LLweights 4L4Ltrain accuracy (seeds 0 / 1 / 2)held-out accuracyfinal training MSE
140.68 / 0.69 / 0.710.58 / 0.58 / 0.590.9235 / 0.9231 / 0.9240
280.89 / 0.88 / 0.890.81 / 0.83 / 0.830.4005 / 0.3972 / 0.3971
3120.85 / 0.92 / 0.930.77 / 0.80 / 0.850.4583 / 0.3555 / 0.4031
4160.93 / 0.99 / 0.960.89 / 0.90 / 0.820.2979 / 0.2605 / 0.2671
5200.98 / 1.00 / 0.980.90 / 0.96 / 0.910.2086 / 0.2168 / 0.2123

One layer gives a stripe classifier, as proved above. Two layers already reach 0.810.81–0.830.83. Three layers are not reliably better than two: two of the three L=3L=3 runs end below 0.830.83, in poorer local minima of a non-convex landscape. With L=5L=5, all three initialisations exceed 0.850.85, and the training accuracy reaches 0.980.98–1.001.00. A practical recipe is to run several restarts and keep the run with the lowest final training loss. That selects seed 11 for L=3L=3 (held-out 0.800.80), seed 11 for L=4L=4 (0.900.90) and seed 00 for L=5L=5 (0.900.90). The lowest training loss does not always give the best test score (L=5L=5, seed 11 reaches 0.960.96), but choosing by the held-out score would turn the test set into training data and invalidate it as an estimate.

The second workbench trains L=3L=3 for 8080 epochs from seed 00, the 0.770.77 run of the table. Change the seed to see the spread between initialisations, then switch on the depth comparison below. It trains L=1,…,6L=1,\dots,6 with the same settings and draws the final training and test accuracy per depth.

Quick check

You trained five restarts of your classifier. Which run should you submit?

Select one answer

Gradient variance: no barren plateau on two qubits#

The barren-plateau lesson showed that for random deep circuits the variance of a gradient component decays exponentially with the number of qubits. Our model has only two qubits. As a diagnostic we compute the variance, over random initialisations w=2πuw=2\pi u, of ∂fw/∂w0,0,0\partial f_w/\partial w_{0,0,0} at the fixed point x=(0.3,0.7)x=(0.3,0.7). This is the weight furthest from the readout. We first derive the value expected when the rest of the circuit is a unitary 2-design.

Derivation

Variance of a first-layer gradient under a 2-design

  1. Step 1:
    f(θ)=Tr[e−iθGρ eiθG V†HV],G=12Y0, H=Z0Z1, d=4f(\theta)=\mathrm{Tr}\big[e^{-i\theta G}\rho\,e^{i\theta G}\,V^\dagger HV\big],\qquad G=\tfrac12Y_0,\ H=Z_0Z_1,\ d=4
    θ=w0,0,0\theta=w_{0,0,0}, ρ=∣ψ⟩⟨ψ∣\rho=\lvert\psi\rangle\langle\psi\rvert is the state just before the gate (after the two uploads), and VV is the rest of the circuit.
  2. Step 2:
    ∂θf=Tr[K V†HV],K=i[ρθ,G],ρθ=e−iθGρ eiθG\partial_\theta f=\mathrm{Tr}\big[K\,V^\dagger HV\big],\qquad K=i[\rho_\theta,G],\quad\rho_\theta=e^{-i\theta G}\rho\,e^{i\theta G}
    Differentiate: ddθe−iθGρeiθG=−i[G,ρθ]=i[ρθ,G]\frac{d}{d\theta}e^{-i\theta G}\rho e^{i\theta G}=-i[G,\rho_\theta]=i[\rho_\theta,G]. KK is Hermitian and traceless.
  3. Step 3:
    EV Tr[KV†HV]=TrK TrHd=0\mathbb E_V\,\mathrm{Tr}[KV^\dagger HV]=\frac{\mathrm{Tr}K\,\mathrm{Tr}H}{d}=0
    For VV drawn from a unitary 1-design, EV[V†HV]=TrHdI\mathbb E_V[V^\dagger HV]=\frac{\mathrm{Tr}H}dI. The gradient has mean zero.
  4. Step 4:
    EV Tr[KV†HV]2=(TrK)2(TrH)2+TrK2 TrH2d2−1−TrK2(TrH)2+(TrK)2TrH2d(d2−1)=TrK2 TrH2d2−1\mathbb E_V\,\mathrm{Tr}[KV^\dagger HV]^2=\frac{(\mathrm{Tr}K)^2(\mathrm{Tr}H)^2+\mathrm{Tr}K^2\,\mathrm{Tr}H^2}{d^2-1}-\frac{\mathrm{Tr}K^2(\mathrm{Tr}H)^2+(\mathrm{Tr}K)^2\mathrm{Tr}H^2}{d(d^2-1)}=\frac{\mathrm{Tr}K^2\,\mathrm{Tr}H^2}{d^2-1}
    The second-moment (Weingarten) formula for a 2-design. As a check, K=H=IK=H=I gives d2d^2 on both sides. Both operators here are traceless.
  5. Step 5:
    TrK2=−Tr[ρθ,G]2=2 Tr(ρθ2G2)−2 Tr(ρθGρθG)=12(1−⟨Y0⟩ρθ2)=12\mathrm{Tr}K^2=-\mathrm{Tr}[\rho_\theta,G]^2=2\,\mathrm{Tr}(\rho_\theta^2G^2)-2\,\mathrm{Tr}(\rho_\theta G\rho_\theta G)=\tfrac12\big(1-\langle Y_0\rangle_{\rho_\theta}^2\big)=\tfrac12
    Expand the square. For a pure state, ρ2=ρ\rho^2=\rho, and G2=14IG^2=\frac14I. The two uploads and RY(θ)R_Y(\theta) keep qubit 00 in the xzxz-plane of the Bloch sphere, so ⟨Y0⟩=0\langle Y_0\rangle=0.
  6. Step 6:
    Var[∂θf]=12⋅Tr(I4)42−1=12⋅415=215≈0.1333;n qubits: 2n−14n−1≈2−(n+1)\mathrm{Var}[\partial_\theta f]=\frac12\cdot\frac{\mathrm{Tr}(I_4)}{4^2-1}=\frac12\cdot\frac4{15}=\frac2{15}\approx0.1333;\qquad n\ \text{qubits}:\ \frac{2^{n-1}}{4^n-1}\approx2^{-(n+1)}
    With H2=IH^2=I. For nn qubits and a Pauli-string observable the same computation decays exponentially in nn; this is the barren plateau. At n=2n=2 it is a constant.

Measured. Sample variance over 5000 initialisations w=2πuw=2\pi u (mulberry32(55)), shift-rule derivative at x=(0.3,0.7)x=(0.3,0.7):

LL123456812
variance0.00000.12510.11610.11310.12340.12600.13320.1291

The statistical standard error is about 0.0020.002–0.0030.003. At L=1L=1 the derivative vanishes identically: w0,0,0w_{0,0,0} is a dead weight (part (a) of the proposition). From L=2L=2 on, the variance stays between 0.1130.113 and 0.1260.126, and at L=8L=8 and L=12L=12 it agrees with the 2-design value 2/152/15 within two standard errors. Shallow circuits are not yet 2-designs, so their values differ by up to 15%15\%. Deeper circuits converge to 2/152/15, a constant set by the two-qubit dimension. Depth therefore costs no gradient signal here, and the trainability problems seen in the table are local minima, not a plateau. On nn qubits the same diagnostic would fall like 2−(n+1)2^{-(n+1)}, which is why the barren-plateau lesson recommends local costs and shallow, structured circuits.

Putting it together#

The TypeScript cell evaluates the lab's reference model (weights rounded to four decimals) on both data sets. It compares a hand-written two-term shift rule with the engine's vqcGradient and lists the weights with zero gradient. It finally prints the cost per epoch with and without them.

TypeScriptrunnable
import { expectationValue } from '@/lib/quantum-simulator';
import { circleDataset, dataReuploadingClassifier, reuploadingSpectrum, vqcAccuracy, vqcGradient } from '@/lib/qml';

// The lab's reference model: L = 5 layers, weights rounded to 4 decimals.
const w: Record<string, number> = {
  w_0_0_0: 2.968, w_0_0_1: -0.3193, w_0_1_0: 4.9431, w_0_1_1: 4.8684, w_1_0_0: 6.2012, w_1_0_1: 0.8796, w_1_1_0: 4.2054,
  w_1_1_1: 5.906, w_2_0_0: 2.7566, w_2_0_1: 6.5819, w_2_1_0: 1.3577, w_2_1_1: 3.6023, w_3_0_0: 1.1035, w_3_0_1: 2.5368,
  w_3_1_0: 2.8377, w_3_1_1: 1.3038, w_4_0_0: 3.0734, w_4_0_1: 0.4233, w_4_1_0: 1.973, w_4_1_1: 4.8199,
};
const model = dataReuploadingClassifier(5);
const { X, y } = circleDataset(7, 200);
const Xtr = X.slice(0, 100), ytr = y.slice(0, 100), Xte = X.slice(100), yte = y.slice(100);
console.log('gates ' + model.template.ops.length + ', weights P = ' + model.weightParams.length + ', frequencies per feature: ' + reuploadingSpectrum(5).join(' '));
console.log('train accuracy ' + vqcAccuracy(model, Xtr, ytr, w).toFixed(2) + ', held-out accuracy ' + vqcAccuracy(model, Xte, yte, w).toFixed(2));

// f and its gradient at the first held-out point: our own two-term shift rule against the engine.
const x = Xte[0];
const f = (weights: Record<string, number>) => expectationValue(model.template, model.observable, { x0: x[0], x1: x[1], ...weights });
const own: Record<string, number> = {};
for (const k of model.weightParams) own[k] = 0.5 * (f({ ...w, [k]: w[k] + Math.PI / 2 }) - f({ ...w, [k]: w[k] - Math.PI / 2 }));
const engine = vqcGradient(model, x, w);
const maxDiff = Math.max(...model.weightParams.map((k) => Math.abs(own[k] - engine[k])));
console.log('f(x) = ' + f(w).toFixed(10) + ' at x = (' + x[0].toFixed(4) + ', ' + x[1].toFixed(4) + '), label ' + yte[0]);
console.log('df/dw_2_1_0 = ' + own.w_2_1_0.toFixed(10) + ', max |own shift rule - engine| < 1e-12: ' + (maxDiff < 1e-12));
console.log('weights with zero gradient: ' + model.weightParams.filter((k) => Math.abs(own[k]) < 1e-12).join(', '));
const M = 100, P = model.weightParams.length;
console.log('evaluations per epoch M(2P + 1) = ' + M * (2 * P + 1) + ', without the 4 dead weights ' + M * (2 * (P - 4) + 1));

Expected output

gates 35, weights P = 20, frequencies per feature: -5 -4 -3 -2 -1 0 1 2 3 4 5
train accuracy 1.00, held-out accuracy 0.96
f(x) = 0.3108423695 at x = (0.3174, 0.2596), label 1
df/dw_2_1_0 = 0.2058330341, max |own shift rule - engine| < 1e-12: true
weights with zero gradient: w_3_0_1, w_4_0_0, w_4_0_1, w_4_1_1
evaluations per epoch M(2P + 1) = 4100, without the 4 dead weights 3300

The NumPy cell rebuilds the classifier from 4×44\times4 matrices and regenerates the data set with a port of mulberry32. It checks the shift rule against a central difference and the dead weights, and computes the Fourier coefficients of an L=2L=2 model with np.fft.fft2.

Pythonrunnable
import numpy as np

# The two-qubit data re-uploading classifier with 4 x 4 matrices (qubit 0 = most significant bit).
Z = np.diag([1.0, -1.0])
ZZ = np.kron(Z, Z)
CNOT = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]], dtype=complex)


def ry(t):
    return np.array([[np.cos(t / 2), -np.sin(t / 2)], [np.sin(t / 2), np.cos(t / 2)]], dtype=complex)


def rz(t):
    return np.diag([np.exp(-0.5j * t), np.exp(0.5j * t)])


def f(x, w):  # w[l, q, 0] is the R_Y weight, w[l, q, 1] the R_Z weight of qubit q in layer l
    psi = np.zeros(4, dtype=complex)
    psi[0] = 1
    for wl in w:
        psi = np.kron(ry(np.pi * x[0]), ry(np.pi * x[1])) @ psi  # upload x0 and x1
        psi = np.kron(rz(wl[0, 1]) @ ry(wl[0, 0]), rz(wl[1, 1]) @ ry(wl[1, 0])) @ psi
        psi = CNOT @ psi
    return float(np.real(np.vdot(psi, ZZ @ psi)))


# Port of mulberry32 and circleDataset(7, 200): label +1 inside (x0 - 1/2)^2 + (x1 - 1/2)^2 < 0.15.
def mulberry32(seed):
    a = seed & 0xFFFFFFFF

    def imul(u, v):
        return (u * v) & 0xFFFFFFFF

    def r():
        nonlocal a
        a = (a + 0x6D2B79F5) & 0xFFFFFFFF
        t = imul(a ^ (a >> 15), 1 | a)
        t = ((t + imul(t ^ (t >> 7), 61 | t)) & 0xFFFFFFFF) ^ t
        return (t ^ (t >> 14)) / 4294967296

    return r


r = mulberry32(7)
X = np.array([[r(), r()] for _ in range(200)])
y = np.where((X[:, 0] - 0.5) ** 2 + (X[:, 1] - 0.5) ** 2 < 0.15, 1, -1)

# The lab's reference weights (L = 5), rounded to 4 decimals.
w5 = np.array([2.968, -0.3193, 4.9431, 4.8684, 6.2012, 0.8796, 4.2054, 5.906, 2.7566, 6.5819,
               1.3577, 3.6023, 1.1035, 2.5368, 2.8377, 1.3038, 3.0734, 0.4233, 1.973, 4.8199]).reshape(5, 2, 2)
acc = lambda rows: np.mean([np.sign(f(X[m], w5)) == y[m] for m in rows])
x = X[100]
print(f"first held-out point {np.round(x, 4).tolist()} (label {y[100]:+d}): f = {f(x, w5):+.10f}")
print(f"train accuracy {acc(range(100)):.2f}, held-out accuracy {acc(range(100, 200)):.2f}")


def shifted(idx, s):
    w = w5.copy()
    w[idx] += s
    return f(x, w)


psr = 0.5 * (shifted((2, 1, 0), np.pi / 2) - shifted((2, 1, 0), -np.pi / 2))
fd = (shifted((2, 1, 0), 1e-6) - shifted((2, 1, 0), -1e-6)) / 2e-6
print(f"df/dw_2_1_0: shift rule {psr:+.10f}, central difference {fd:+.6f}")
dead = [(3, 0, 1), (4, 0, 0), (4, 0, 1), (4, 1, 1)]
print("shifting w_3_0_1, w_4_0_0, w_4_0_1, w_4_1_1 by 1 leaves f unchanged:", max(abs(shifted(i, 1.0) - f(x, w5)) for i in dead) < 1e-12)

# Fourier coefficients of an L = 2 model: exact 2-D DFT on an 8 x 8 grid of the period square [0, 2)^2.
w2 = np.array([0.3, 1.1, -0.7, 2.0, 0.5, -1.4, 0.9, 0.2]).reshape(2, 2, 2)
N = 8
F = np.array([[f((2 * a / N, 2 * b / N), w2) for b in range(N)] for a in range(N)])
C = np.fft.fft2(F) / N**2  # C[p, q] = c_(p, q), frequency indices taken mod N
k = np.fft.fftfreq(N, 1 / N).astype(int)
outside = max(abs(C[i, j]) for i in range(N) for j in range(N) if max(abs(k[i]), abs(k[j])) > 2)
support = sorted((int(k[i]), int(k[j])) for i in range(N) for j in range(N) if abs(C[i, j]) > 1e-9)
print(f"L = 2: every |c_omega| with max(|omega_0|, |omega_1|) > 2 is below 1e-12: {outside < 1e-12}")
print(f"non-zero coefficients: {len(support)} at {support}")
print(f"c_(1,2) = {C[1, 2].real:+.6f}{C[1, 2].imag:+.6f}i, c_(-1,-2) = {C[-1, -2].real:+.6f}{C[-1, -2].imag:+.6f}i")

Expected output

first held-out point [0.3174, 0.2596] (label +1): f = +0.3108423695
train accuracy 1.00, held-out accuracy 0.96
df/dw_2_1_0: shift rule +0.2058330341, central difference +0.205833
shifting w_3_0_1, w_4_0_0, w_4_0_1, w_4_1_1 by 1 leaves f unchanged: True
L = 2: every |c_omega| with max(|omega_0|, |omega_1|) > 2 is below 1e-12: True
non-zero coefficients: 9 at [(-1, -2), (-1, 0), (-1, 2), (0, -2), (0, 0), (0, 2), (1, -2), (1, 0), (1, 2)]
c_(1,2) = +0.109698+0.059928i, c_(-1,-2) = +0.109698-0.059928i

The lab. You write trainCapstone: choose the depth L∈{3,4,5,6}L\in\{3,4,5,6\}, implement the shift rule and the optimiser, and return the weights. The grader classifies 100100 held-out points it never sends to the browser and requires an accuracy of at least 0.850.85. Per the table, L=5L=5 with Adam passes from all three tested initialisations, while L=3L=3 usually does not.

References

  1. A. Pérez-Salinas, A. Cervera-Lierta, E. Gil-Fuster and J. I. Latorre, Data re-uploading for a universal quantum classifier, Quantum 4, 226 (2020).doi:10.22331/q-2020-02-06-226
  2. M. Schuld, R. Sweke and J. J. Meyer, Effect of data encoding on the expressive power of variational quantum-machine-learning models, Physical Review A 103, 032430 (2021).doi:10.1103/PhysRevA.103.032430
  3. M. C. Caro, H.-Y. Huang, M. Cerezo, K. Sharma, A. Sornborger, L. Cincio and P. J. Coles, Generalization in quantum machine learning from few training data, Nature Communications 13, 4919 (2022).doi:10.1038/s41467-022-32550-3
  4. K. Mitarai, M. Negoro, M. Kitagawa and K. Fujii, Quantum circuit learning, Physical Review A 98, 032309 (2018).doi:10.1103/PhysRevA.98.032309
  5. D. P. Kingma and J. Ba, Adam: A method for stochastic optimization, 3rd International Conference on Learning Representations (ICLR 2015), arXiv:1412.6980.arxiv.org
  6. J. R. McClean, S. Boixo, V. N. Smelyanskiy, R. Babbush and H. Neven, Barren plateaus in quantum neural network training landscapes, Nature Communications 9, 4812 (2018).doi:10.1038/s41467-018-07090-4

Hands-on labs

  • Capstone: train a data re-uploading classifier on the circle

    1 step · auto-graded

    The capstone of the course: train the two-qubit data re-uploading classifier of the lesson to tell points inside a circle from points outside it. Your TypeScript function receives 100100 labelled training points, chooses the depth L∈{3,4,5,6}L\in\{3,4,5,6\}, computes the gradient with your own parameter-shift rule, runs an optimiser of your choice and returns the trained 4L4L weights. The grader binds them into the chosen circuit and classifies 100100 held-out points by sign⟨Z0Z1⟩\mathrm{sign}\langle Z_0Z_1\rangle; at least 85%85\% must be correct.

    Open lab