QuantumCore

Hands-on lab · 1 step

Train the ridge readout of a quantum reservoir

The 44-qubit quantum reservoir of the lesson is fixed: its Ising Hamiltonian, the input injection on qubit 00 and the 55 virtual nodes per input are never trained. Only the linear readout is learned. Your TypeScript function receives the 600×21600\times21 feature matrix of the training window (the 2020 expectation values ⟨Zq⟩\langle Z_q\rangle plus a bias column) and the targets of a short-term memory task, and returns the ridge-regression weights w=(X⊤X+λI)−1X⊤yw=(X^\top X+\lambda I)^{-1}X^\top y. The grader recomputes the reservoir and scores your weights on 400400 unseen time steps: recalling the input of one step ago must reach NMSE≤0.03\mathrm{NMSE}\le0.03, recalling the input of two steps ago NMSE≤0.08\mathrm{NMSE}\le0.08.

0/1 step passedBest score 0/100

Step 1: Implement trainReadout

Complete the TypeScript function

trainReadout(features: number[][], targets: number[], lambda: number): number[]

The reservoir (fixed). Four qubits (qubit 00 is the most significant bit) evolve under H=∑i<jJijXiXj+h∑qZqH=\sum_{i<j}J_{ij}X_iX_j+h\sum_qZ_q with J01=0.0695J_{01}=0.0695, J02=0.6305J_{02}=0.6305, J03=−0.0920J_{03}=-0.0920, J12=−0.0749J_{12}=-0.0749, J13=−0.3440J_{13}=-0.3440, J23=0.8774J_{23}=0.8774 and h=0.5h=0.5, starting in ∣0000⟩\lvert0000\rangle. At time step kk the input sk∈[0,1)s_k\in[0,1) replaces qubit 00: ρ↦∣ψsk⟩⟨ψsk∣⊗Tr0 ρ\rho\mapsto\lvert\psi_{s_k}\rangle\langle\psi_{s_k}\rvert\otimes\mathrm{Tr}_0\,\rho with ∣ψs⟩=1−s ∣0⟩+s ∣1⟩\lvert\psi_s\rangle=\sqrt{1-s}\,\lvert0\rangle+\sqrt s\,\lvert1\rangle. The state then evolves for τ=4\tau=4 in V=5V=5 sub-steps U=e−iHτ/VU=e^{-iH\tau/V}, and after each sub-step the four values ⟨Z0⟩,…,⟨Z3⟩\langle Z_0\rangle,\dots,\langle Z_3\rangle are recorded. The inputs are sk=s_k= mulberry32(42)() for k=0,…,1199k=0,\dots,1199.

The arguments. features is the matrix XX with one row per time step k=200,…,799k=200,\dots,799 (the first 200200 steps are the washout, which removes the dependence on the initial state). Each row has 2121 entries: the 4×5=204\times5=20 expectation values, ordered sub-step by sub-step, followed by a bias entry that is always 11. Do not add another bias column. targets is the vector yy with yk=sk−dy_k=s_{k-d}, the input dd steps in the past, for the same rows in the same order. lambda is the ridge parameter λ=10−6\lambda=10^{-6}.

What to compute. The weights w=arg⁡min⁡w∈R21 ∥Xw−y∥2+λ∥w∥2.w=\arg\min_{w\in\mathbb R^{21}}\ \lVert Xw-y\rVert^2+\lambda\lVert w\rVert^2 . Setting the gradient 2X⊤(Xw−y)+2λw2X^\top(Xw-y)+2\lambda w to zero gives the normal equations (X⊤X+λI) w=X⊤y,(X⊤X)ij=∑kXkiXkj,(X⊤y)i=∑kXki yk.\big(X^\top X+\lambda I\big)\,w=X^\top y,\qquad (X^\top X)_{ij}=\sum_kX_{ki}X_{kj},\quad (X^\top y)_i=\sum_kX_{ki}\,y_k . The 21×2121\times21 matrix A=X⊤X+λIA=X^\top X+\lambda I is symmetric positive definite, so the solution is unique. Solve the system with a Cholesky factorisation A=LL⊤A=LL^\top followed by forward substitution Lz=X⊤yLz=X^\top y and back substitution L⊤w=zL^\top w=z, or with Gaussian elimination. The regulariser applies to every weight, the bias included. Return ww as an array of 2121 finite numbers.

Grading. Your function runs twice, with the delay-11 targets (code test weights-d1) and with the delay-22 targets (weights-d2); you are not told which delay a call belongs to. The check shape verifies that each call returns 2121 finite numbers. The checks delay-1 and delay-2 recompute the reservoir on the server, predict y^k=∑jXkjwj\hat y_k=\sum_jX_{kj}w_j on the test steps k=800,…,1199k=800,\dots,1199, which your function never sees, and compute NMSE=∑k(y^k−yk)2/∑k(yk−yˉ)2\mathrm{NMSE}=\sum_k(\hat y_k-y_k)^2/\sum_k(y_k-\bar y)^2. They require NMSE≤0.03\mathrm{NMSE}\le0.03 for delay 11 and ≤0.08\le0.08 for delay 22. The exact ridge solution reaches 0.01050.0105 and 0.03570.0357.

About the shortcut. The module 'quantumcore/qml' exports a ridgeRegression function, which the lesson uses. Calling it would pass this lab without doing the exercise. The grader only sees the returned weights, so this rule is not enforced; the point of the lab is to write the normal equations and the solver yourself.

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