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 labelled training points, chooses the depth , computes the gradient with your own parameter-shift rule, runs an optimiser of your choice and returns the trained weights. The grader binds them into the chosen circuit and classifies held-out points by ; at least must be correct.
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 points [x0, x1] of the unit square ; ytrain[m] is if (inside the circle of radius around the centre) and otherwise. The training set has points of each class. The held-out set has further points from the same generator ( inside).
The model. For a depth , dataReuploadingClassifier(L) from 'quantumcore/qml' is exactly the circuit the grader uses: two qubits, data parameters x0, x1 and the weights w_l_q_k (layer , qubit , for , for ). Qubit is the most significant bit and . Layer applies, in time order,
The output is (Pauli word ZZ, i.e. model.observable) and the prediction is ; counts as wrong. Evaluate 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 with . Compute with your own parameter-shift rule: each weight occurs in exactly one or gate, so holds exactly (lesson). One epoch then costs circuit evaluations with , i.e. for . 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 (, , or 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 (, Adam with for epochs, circuit evaluations) needs about 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 held-out points, which are never sent to your browser. The required accuracy is at least ; the reference reaches on the training set and 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.