QuantumCore

Hands-on lab · 1 step

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

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.

0/1 step passedBest score 0/100

Step 1: Implement trainCapstone

Complete the TypeScript function

trainCapstone(Xtrain: number[][], ytrain: number[]): { template: '3' | '4' | '5' | '6'; weights: Record<string, number> }

The data. Xtrain holds 100100 points [x0, x1] of the unit square [0,1)2[0,1)^2; ytrain[m] is +1+1 if (x0−12)2+(x1−12)2<0.15(x_0-\frac12)^2+(x_1-\frac12)^2<0.15 (inside the circle of radius 0.15≈0.387\sqrt{0.15}\approx0.387 around the centre) and −1-1 otherwise. The training set has 5050 points of each class. The held-out set has 100100 further points from the same generator (4949 inside).

The model. For a depth LL, dataReuploadingClassifier(L) from 'quantumcore/qml' is exactly the circuit the grader uses: two qubits, data parameters x0, x1 and the 4L4L weights w_l_q_k (layer ℓ\ell, qubit qq, k=0k=0 for RYR_Y, k=1k=1 for RZR_Z). Qubit 00 is the most significant bit and RP(θ)=e−iθP/2R_P(\theta)=e^{-i\theta P/2}. Layer ℓ=0,…,L−1\ell=0,\dots,L-1 applies, in time order,

RY(0)(πx0), RY(1)(πx1), RY(0)(wℓ,0,0), RZ(0)(wℓ,0,1), RY(1)(wℓ,1,0), RZ(1)(wℓ,1,1), CNOT(0,1).R_Y^{(0)}(\pi x_0),\ R_Y^{(1)}(\pi x_1),\ R_Y^{(0)}(w_{\ell,0,0}),\ R_Z^{(0)}(w_{\ell,0,1}),\ R_Y^{(1)}(w_{\ell,1,0}),\ R_Z^{(1)}(w_{\ell,1,1}),\ \mathrm{CNOT}(0,1).

The output is fw(x)=⟨Z0Z1⟩f_w(x)=\langle Z_0Z_1\rangle (Pauli word ZZ, i.e. model.observable) and the prediction is sign fw(x)\mathrm{sign}\,f_w(x); f=0f=0 counts as wrong. Evaluate ff with expectationValue(model.template, model.observable, bindings) from 'quantumcore', where bindings holds x0, x1 and every weight.

What to write. A training loop that lowers a loss over the training set, for example the mean squared error L(w)=1M∑m(fw(xm)−ym)2L(w)=\frac1M\sum_m\big(f_w(x_m)-y_m\big)^2 with ∂L/∂wk=2M∑m(fw(xm)−ym) ∂fw(xm)/∂wk\partial L/\partial w_k=\frac2M\sum_m\big(f_w(x_m)-y_m\big)\,\partial f_w(x_m)/\partial w_k. Compute ∂f/∂wk\partial f/\partial w_k with your own parameter-shift rule: each weight occurs in exactly one RYR_Y or RZR_Z gate, so ∂f/∂wk=12[f(wk+π2)−f(wk−π2)]\partial f/\partial w_k=\frac12\big[f(w_k+\frac\pi2)-f(w_k-\frac\pi2)\big] holds exactly (lesson). One epoch then costs M(2P+1)M(2P+1) circuit evaluations with P=4LP=4L, i.e. 41004100 for L=5L=5. Any optimiser is allowed: gradient descent, Adam, restarts. Pick the template by changing TEMPLATE in the starter.

What to return. { template, weights } where template is one of '3', '4', '5', '6' and weights has exactly the keys dataReuploadingClassifier(L).weightParams of that template (1212, 1616, 2020 or 2424 finite numbers), no more and no fewer.

Grading. The function runs once, with the training set as its arguments, within a time limit of 120 seconds. The reference solution (L=5L=5, Adam with η=0.1\eta=0.1 for 8080 epochs, 80⋅4100=328 00080\cdot4100=328\,000 circuit evaluations) needs about 1616 seconds in Node.js. The check shape verifies the template name and the weight keys. The check accuracy binds your weights into the chosen circuit on the server and computes the accuracy on the 100100 held-out points, which are never sent to your browser. The required accuracy is at least 0.850.85; the reference reaches 1.001.00 on the training set and 0.960.96 on the held-out set.

About the shortcut. 'quantumcore/qml' also exports vqcGradient, vqcLossAndGradient and trainVQC, and 'quantumcore' exports parameterShiftGradient; the lesson uses them. Calling them would pass this lab without doing the exercise. The grader only sees the returned weights, so this rule is not enforced; the exercise only works if you write the shift rule and the loop yourself.

TypeScript: export trainCapstone · 1 test · imports: quantumcore, quantumcore/qml
Run the tests first; the results of your latest run are submitted.