QuantumCore

Lesson 43 · 100 min · advanced

Variational Quantum Circuits: Architecture

Loading progress…

A variational quantum circuit (VQC) is a supervised model built from three parts: a feature map U(x)U(x) that writes the input into a quantum state, a parameterised ansatz W(θ)W(\theta), and an observable HH whose expectation value is the model output fθ(x)=⟨0∣U†(x)W†(θ)HW(θ)U(x)∣0⟩f_\theta(x)=\langle0\vert U^\dagger(x)W^\dagger(\theta)HW(\theta)U(x)\vert0\rangle. This lesson proves by the cyclicity of the trace that fθ(x)=Tr(ρ(x) W†HW)f_\theta(x)=\mathrm{Tr}\big(\rho(x)\,W^\dagger HW\big) is a linear function of the encoded density operator, i.e. a linear model in the Hilbert–Schmidt feature space of the previous module; proves that the output lies in [λmin⁡(H),λmax⁡(H)][\lambda_{\min}(H),\lambda_{\max}(H)]; derives the decision rule, the MSE and hinge losses and their gradients; works out the one-qubit classifier f=cos⁡(x+θ)f=\cos(x+\theta) exactly, together with the RXR_X variant f=cos⁡xcos⁡θf=\cos x\cos\theta; computes the shot noise Var[f^]=(1−f2)/N\mathrm{Var}[\hat f]=(1-f^2)/N and a Hoeffding shot count; writes the hybrid quantum–classical training loop with its cost O(M P Nshots)O(M\,P\,N_{\text{shots}}) per epoch; and derives the exact interval of weights that classifies the lab data correctly.

Prerequisites: Angle & Phase Encoding, Postulate 3: Projective Measurement & the Born Rule

Objectives and key results
  • Decompose a variational quantum circuit into a feature map U(x)U(x), an ansatz W(θ)W(\theta) and an observable HH, and write its output as fθ(x)=⟨0∣U†(x)W†(θ)HW(θ)U(x)∣0⟩f_\theta(x)=\langle0\vert U^\dagger(x)W^\dagger(\theta)HW(\theta)U(x)\vert0\rangle.

  • Prove fθ(x)=Tr(ρ(x) W†(θ)HW(θ))f_\theta(x)=\mathrm{Tr}\big(\rho(x)\,W^\dagger(\theta)HW(\theta)\big) and explain why a VQC is a linear model in the Hilbert–Schmidt feature space whose functions lie in the RKHS of the fidelity kernel.

  • Prove that fθ(x)∈[λmin⁡(H),λmax⁡(H)]f_\theta(x)\in[\lambda_{\min}(H),\lambda_{\max}(H)], define the classifier y^=sign fθ(x)\hat y=\mathrm{sign}\,f_\theta(x), and differentiate the MSE and hinge losses with respect to ff and to θ\theta.

  • Derive f=cos⁡(x+θ)f=\cos(x+\theta) for U(x)=RY(x)U(x)=R_Y(x), W(θ)=RY(θ)W(\theta)=R_Y(\theta), H=ZH=Z by matrix algebra, locate its decision boundary, and show that the RXR_X encoding gives f=cos⁡xcos⁡θf=\cos x\cos\theta instead.

  • Compute the variance (1−f2)/N(1-f^2)/N of the NN-shot estimator of a Pauli expectation value and the Hoeffding shot count N≥2ln⁡(2/δ)/ε2N\ge2\ln(2/\delta)/\varepsilon^2.

  • Write the hybrid quantum–classical training loop, count its circuit evaluations per epoch, M(2P+1)M(2P+1), and distinguish gradient-based, stochastic-approximation and gradient-free loops.

  • Derive the exact set of weights θ∈(−π2−1.8, −π2−0.9)(mod2π)\theta\in\big(-\tfrac\pi2-1.8,\,-\tfrac\pi2-0.9\big)\pmod{2\pi} that classifies the lab data correctly.

definition · Variational quantum model
fθ(x)=⟨0∣U†(x) W†(θ) H W(θ) U(x)∣0⟩,y^(x)=sign fθ(x)f_\theta(x)=\langle0\vert U^\dagger(x)\,W^\dagger(\theta)\,H\,W(\theta)\,U(x)\vert0\rangle,\qquad\hat y(x)=\mathrm{sign}\,f_\theta(x)
identity · A VQC is linear in the encoded density operator
fθ(x)=Tr(ρ(x) Oθ)=⟨ρ(x),Oθ⟩HS,ρ(x)=U(x)∣0⟩⟨0∣U†(x),Oθ=W†(θ)HW(θ)f_\theta(x)=\mathrm{Tr}\big(\rho(x)\,O_\theta\big)=\langle\rho(x),O_\theta\rangle_{HS},\qquad\rho(x)=U(x)\lvert0\rangle\langle0\rvert U^\dagger(x),\quad O_\theta=W^\dagger(\theta)HW(\theta)
theorem · Output range
λmin⁡(H)≤fθ(x)≤λmax⁡(H)for all x,θ\lambda_{\min}(H)\le f_\theta(x)\le\lambda_{\max}(H)\qquad\text{for all }x,\theta
formula · One-qubit threshold model
U(x)=RY(x), W(θ)=RY(θ), H=Z:fθ(x)=cos⁡(x+θ);U(x)=RX(x): fθ(x)=cos⁡xcos⁡θU(x)=R_Y(x),\ W(\theta)=R_Y(\theta),\ H=Z:\qquad f_\theta(x)=\cos(x+\theta);\qquad U(x)=R_X(x):\ f_\theta(x)=\cos x\cos\theta
formula · Shot noise of a Pauli expectation value
Var[f^]=1−f2N,N≥2ln⁡(2/δ)ε2 ⇒ Pr⁡(∣f^−f∣≥ε)≤δ\mathrm{Var}[\hat f]=\frac{1-f^2}{N},\qquad N\ge\frac{2\ln(2/\delta)}{\varepsilon^2}\ \Rightarrow\ \Pr\big(\lvert\hat f-f\rvert\ge\varepsilon\big)\le\delta
algorithm · Cost of one training epoch
M (2P+1) circuits×Nshots=O(M P Nshots) shots per epochM\,(2P+1)\ \text{circuits}\times N_{\text{shots}}=O\big(M\,P\,N_{\text{shots}}\big)\ \text{shots per epoch}

Feature map, ansatz and observable#

Stage 3 used quantum computers in one specific way: a feature map x↦U(x)∣0n⟩x\mapsto U(x)\lvert0^n\rangle produced states whose overlaps formed a kernel matrix, and all learning happened classically in the dual coefficients of a support vector machine. Stage 4 changes the division of labour. The circuit itself now carries trainable parameters θ\theta, the quantum computer evaluates the model output for every input, and a classical optimiser adjusts θ\theta in a loop. Such models are called variational quantum circuits (VQC) or parameterised quantum circuits (PQC), and on noisy intermediate-scale (NISQ) hardware they are the most widely studied form of quantum machine learning. This lesson fixes the architecture once and for all, proves the structural facts that every later lesson relies on (linearity in the encoded state, the output range, the shot noise, the cost of training), and works through a one-qubit classifier that can be solved with pencil and paper and is trained in the lab.

Definition

Feature map, ansatz and observable

Fix nn qubits and the register state ∣0⟩=∣0n⟩\lvert0\rangle=\lvert0^n\rangle. A variational quantum circuit consists of three components. (i) A feature map (data encoding) x↦U(x)x\mapsto U(x) from the input domain X⊆Rd\mathcal X\subseteq\mathbb R^d to unitary 2n×2n2^n\times2^n matrices, realised by a circuit whose gate angles are functions of xx; for example the angle encoding U(x)=⨂jRY(xj)U(x)=\bigotimes_jR_Y(x_j) of the lesson Angle & Phase Encoding. (ii) An ansatz θ↦W(θ)\theta\mapsto W(\theta) from a parameter space Θ⊆RP\Theta\subseteq\mathbb R^P to unitaries, realised by a fixed circuit structure whose rotation angles are the PP trainable weights θ=(θ1,…,θP)\theta=(\theta_1,\dots,\theta_P). (iii) An observable H=H†H=H^\dagger on C2n\mathbb C^{2^n}, given as a real combination of Pauli strings H=∑kckPkH=\sum_kc_kP_k. The data symbols of UU are set by the input and never trained; the weights of WW are never set by the input.

Definition

Variational quantum model and classifier

The model output of a VQC is the expectation value of HH in the state ∣ψθ(x)⟩=W(θ)U(x)∣0⟩\lvert\psi_\theta(x)\rangle=W(\theta)U(x)\lvert0\rangle: fθ(x)=⟨ψθ(x)∣H∣ψθ(x)⟩=⟨0∣U†(x) W†(θ) H W(θ) U(x)∣0⟩.f_\theta(x)=\langle\psi_\theta(x)\vert H\vert\psi_\theta(x)\rangle=\langle0\vert U^\dagger(x)\,W^\dagger(\theta)\,H\,W(\theta)\,U(x)\vert0\rangle . For binary labels y∈{−1,+1}y\in\{-1,+1\} the classifier predicts y^(x)=sign fθ(x)\hat y(x)=\mathrm{sign}\,f_\theta(x), where an output of exactly 00 is counted as a wrong prediction (a point on the decision boundary is not classified). The decision boundary is the zero set {x:fθ(x)=0}\{x:f_\theta(x)=0\}. On hardware fθ(x)f_\theta(x) is not available exactly; it is estimated from NN repetitions (shots) of the circuit followed by a measurement of HH.

Theorem

A variational model is linear in the encoded density operator

Let ρ(x)=U(x)∣0⟩⟨0∣U†(x)\rho(x)=U(x)\lvert0\rangle\langle0\rvert U^\dagger(x) be the encoded density operator and Oθ=W†(θ)HW(θ)O_\theta=W^\dagger(\theta)HW(\theta). Then fθ(x)=Tr(ρ(x) Oθ)=⟨ρ(x),Oθ⟩HS.f_\theta(x)=\mathrm{Tr}\big(\rho(x)\,O_\theta\big)=\langle\rho(x),O_\theta\rangle_{HS}. The operator OθO_\theta is Hermitian, has the same eigenvalues as HH and the same Hilbert–Schmidt norm ∥Oθ∥HS=∥H∥HS\lVert O_\theta\rVert_{HS}=\lVert H\rVert_{HS}. Consequently, for every fixed θ\theta the map ρ↦fθ\rho\mapsto f_\theta is a linear functional on the real space Herm(2n)\mathrm{Herm}(2^n) of the lesson Quantum Kernels & Fidelity Kernels, and fθf_\theta belongs to the RKHS HK\mathcal H_K of the fidelity kernel K(x,x′)=Tr(ρ(x)ρ(x′))K(x,x')=\mathrm{Tr}\big(\rho(x)\rho(x')\big) with ∥fθ∥HK≤∥H∥HS\lVert f_\theta\rVert_{\mathcal H_K}\le\lVert H\rVert_{HS}.

Proof (7 steps)
  1. Step 1:
    fθ(x)=⟨0∣U†W†HWU∣0⟩=Tr(⟨0∣ U†W†HWU∣0⟩)f_\theta(x)=\langle0\vert U^\dagger W^\dagger HWU\vert0\rangle=\mathrm{Tr}\big(\langle0\rvert\,U^\dagger W^\dagger HWU\lvert0\rangle\big)
    A 1×11\times1 matrix equals its trace. Abbreviate U=U(x)U=U(x) and W=W(θ)W=W(\theta).
  2. Step 2:
    Tr(⟨0∣ (U†W†HWU∣0⟩))=Tr(U†W†HWU∣0⟩⟨0∣)\mathrm{Tr}\big(\langle0\rvert\,\big(U^\dagger W^\dagger HWU\lvert0\rangle\big)\big)=\mathrm{Tr}\big(U^\dagger W^\dagger HWU\lvert0\rangle\langle0\rvert\big)
    Cyclicity of the trace Tr(AB)=Tr(BA)\mathrm{Tr}(AB)=\mathrm{Tr}(BA) for the row A=⟨0∣∈C1×2nA=\langle0\rvert\in\mathbb C^{1\times2^n} and the matrix-times-column B∈C2n×1B\in\mathbb C^{2^n\times1}.
  3. Step 3:
    =Tr(W†HW  U∣0⟩⟨0∣U†)=Tr(Oθ ρ(x))=Tr(ρ(x) Oθ)=\mathrm{Tr}\big(W^\dagger HW\;U\lvert0\rangle\langle0\rvert U^\dagger\big)=\mathrm{Tr}\big(O_\theta\,\rho(x)\big)=\mathrm{Tr}\big(\rho(x)\,O_\theta\big)
    Cyclicity once more, moving the leftmost factor U†U^\dagger to the right end; then the definitions of OθO_\theta and ρ(x)\rho(x), and cyclicity for the last equality.
  4. Step 4:
    Oθ†=W†H†(W†)†=W†HW=Oθ,Oθ=W†(∑kλk∣ek⟩⟨ek∣)W=∑kλk ∣W†ek⟩⟨W†ek∣O_\theta^\dagger=W^\dagger H^\dagger(W^\dagger)^\dagger=W^\dagger HW=O_\theta,\qquad O_\theta=W^\dagger\Big(\sum_k\lambda_k\lvert e_k\rangle\langle e_k\rvert\Big)W=\sum_k\lambda_k\,\lvert W^\dagger e_k\rangle\langle W^\dagger e_k\rvert
    (ABC)†=C†B†A†(ABC)^\dagger=C^\dagger B^\dagger A^\dagger and H=H†H=H^\dagger. Inserting the spectral decomposition of HH shows that OθO_\theta has the eigenvalues λk\lambda_k with the orthonormal eigenvectors W†∣ek⟩W^\dagger\lvert e_k\rangle (W†W^\dagger is unitary).
  5. Step 5:
    ∥Oθ∥HS2=Tr(W†HWW†HW)=Tr(W†H2W)=Tr(H2WW†)=∥H∥HS2\lVert O_\theta\rVert_{HS}^2=\mathrm{Tr}\big(W^\dagger HWW^\dagger HW\big)=\mathrm{Tr}\big(W^\dagger H^2W\big)=\mathrm{Tr}\big(H^2WW^\dagger\big)=\lVert H\rVert_{HS}^2
    WW†=IWW^\dagger=I and cyclicity: the Hilbert–Schmidt norm is invariant under unitary conjugation.
  6. Step 6:
    fθ(x)=Tr(ρ(x)Oθ)=⟨ρ(x),Oθ⟩HS,Tr((aρ+bσ)Oθ)=a Tr(ρOθ)+b Tr(σOθ)f_\theta(x)=\mathrm{Tr}\big(\rho(x)O_\theta\big)=\langle\rho(x),O_\theta\rangle_{HS},\qquad\mathrm{Tr}\big((a\rho+b\sigma)O_\theta\big)=a\,\mathrm{Tr}(\rho O_\theta)+b\,\mathrm{Tr}(\sigma O_\theta)
    For Hermitian matrices ⟨A,B⟩HS=Tr(A†B)=Tr(AB)\langle A,B\rangle_{HS}=\mathrm{Tr}(A^\dagger B)=\mathrm{Tr}(AB). Linearity of the trace makes ρ↦Tr(ρOθ)\rho\mapsto\mathrm{Tr}(\rho O_\theta) a real-linear functional on Herm(2n)\mathrm{Herm}(2^n): the model is a linear model whose feature vector is ρ(x)\rho(x) and whose weight vector is OθO_\theta.
  7. Step 7:
    Oθ=OS+O⊥,O⊥⊥HSS=spanR{ρ(x)} ⇒ fθ=fOS∈HK,∥fθ∥HK=∥OS∥HS≤∥Oθ∥HS=∥H∥HSO_\theta=O_{\mathcal S}+O_\perp,\quad O_\perp\perp_{HS}\mathcal S=\mathrm{span}_{\mathbb R}\{\rho(x)\}\ \Rightarrow\ f_\theta=f_{O_{\mathcal S}}\in\mathcal H_K,\quad\lVert f_\theta\rVert_{\mathcal H_K}=\lVert O_{\mathcal S}\rVert_{HS}\le\lVert O_\theta\rVert_{HS}=\lVert H\rVert_{HS}
    The RKHS proposition of the kernel lesson: HK={fO:O∈S}\mathcal H_K=\{f_O:O\in\mathcal S\} with ∥fO∥HK=∥O∥HS\lVert f_O\rVert_{\mathcal H_K}=\lVert O\rVert_{HS}, and the component of an observable orthogonal to S\mathcal S does not change any expectation value Tr(Oρ(x))\mathrm{Tr}(O\rho(x)). The orthogonal projection does not increase the norm.

∎

The theorem separates the roles of the three components precisely. The feature map alone determines the feature vectors ρ(x)\rho(x) and therefore the kernel K(x,x′)=Tr(ρ(x)ρ(x′))K(x,x')=\mathrm{Tr}\big(\rho(x)\rho(x')\big) and the space HK\mathcal H_K of functions any model on this encoding can represent. The ansatz and the observable together choose one weight vector OθO_\theta inside the set {W†HW:W reachable by the ansatz}\{W^\dagger HW:W\text{ reachable by the ansatz}\}, a subset of the unitary orbit of HH, which consists of Hermitian matrices of fixed spectrum and fixed norm ∥H∥HS\lVert H\rVert_{HS}. A kernel method searches all of HK\mathcal H_K (with a norm penalty) by convex optimisation; a variational circuit searches a curved, norm-bounded subset of it by nonconvex optimisation over θ\theta. The dependence on xx is linear in ρ(x)\rho(x) but generally nonlinear in xx, and the dependence on θ\theta is nonlinear as well: linearity holds only in the encoded state.

Derivation

Heisenberg picture for one qubit: the model as a dot product of Bloch vectors

  1. Step 1:
    ρ(x)=12(I+r⃗(x)⋅σ⃗),Oθ=W†ZW=n⃗(θ)⋅σ⃗\rho(x)=\tfrac12\big(I+\vec r(x)\cdot\vec\sigma\big),\qquad O_\theta=W^\dagger ZW=\vec n(\theta)\cdot\vec\sigma
    Any one-qubit density matrix has a Bloch vector r⃗(x)\vec r(x). OθO_\theta is Hermitian and traceless with eigenvalues ±1\pm1 (the spectrum of ZZ), so it is n⃗⋅σ⃗\vec n\cdot\vec\sigma with a unit vector n⃗(θ)\vec n(\theta).
  2. Step 2:
    fθ(x)=Tr(ρ(x) Oθ)=12Tr(n⃗⋅σ⃗)+12∑a,branb Tr(σaσb)=r⃗(x)⋅n⃗(θ)f_\theta(x)=\mathrm{Tr}\big(\rho(x)\,O_\theta\big)=\tfrac12\mathrm{Tr}\big(\vec n\cdot\vec\sigma\big)+\tfrac12\sum_{a,b}r_an_b\,\mathrm{Tr}(\sigma_a\sigma_b)=\vec r(x)\cdot\vec n(\theta)
    The Pauli matrices are traceless and Tr(σaσb)=2δab\mathrm{Tr}(\sigma_a\sigma_b)=2\delta_{ab}: the one-qubit model is the Euclidean dot product of the Bloch vector of the encoded state with a measurement direction.
  3. Step 3:
    RY(θ)†ZRY(θ)=(cs−sc)(c−s−s−c)=(c2−s2−2cs−2css2−c2)=cos⁡θ Z−sin⁡θ XR_Y(\theta)^\dagger ZR_Y(\theta)=\begin{pmatrix}c&s\\-s&c\end{pmatrix}\begin{pmatrix}c&-s\\-s&-c\end{pmatrix}=\begin{pmatrix}c^2-s^2&-2cs\\-2cs&s^2-c^2\end{pmatrix}=\cos\theta\,Z-\sin\theta\,X
    With c=cos⁡θ2c=\cos\frac\theta2, s=sin⁡θ2s=\sin\frac\theta2, RY(θ)=(c−ssc)R_Y(\theta)=\begin{pmatrix}c&-s\\s&c\end{pmatrix} is real, so RY†=RY⊤R_Y^\dagger=R_Y^\top, and ZRY(θ)=(c−s−s−c)ZR_Y(\theta)=\begin{pmatrix}c&-s\\-s&-c\end{pmatrix}. Double-angle formulas: c2−s2=cos⁡θc^2-s^2=\cos\theta, 2cs=sin⁡θ2cs=\sin\theta. Hence n⃗(θ)=(−sin⁡θ,0,cos⁡θ)\vec n(\theta)=(-\sin\theta,0,\cos\theta).
  4. Step 4:
    RY(x)∣0⟩=cos⁡x2∣0⟩+sin⁡x2∣1⟩ ⇒ r⃗(x)=(2cos⁡x2sin⁡x2, 0, cos⁡2x2−sin⁡2x2)=(sin⁡x,0,cos⁡x)R_Y(x)\lvert0\rangle=\cos\tfrac x2\lvert0\rangle+\sin\tfrac x2\lvert1\rangle\ \Rightarrow\ \vec r(x)=\big(2\cos\tfrac x2\sin\tfrac x2,\ 0,\ \cos^2\tfrac x2-\sin^2\tfrac x2\big)=(\sin x,0,\cos x)
    Bloch vector (2 Re αˉβ, 2 Im αˉβ, ∣α∣2−∣β∣2)(2\,\mathrm{Re}\,\bar\alpha\beta,\,2\,\mathrm{Im}\,\bar\alpha\beta,\,\lvert\alpha\rvert^2-\lvert\beta\rvert^2) of a state with real amplitudes: the RYR_Y encoding moves r⃗\vec r on the great circle of the xzxz-plane at polar angle xx.
  5. Step 5:
    fθ(x)=(sin⁡x,0,cos⁡x)⋅(−sin⁡θ,0,cos⁡θ)=cos⁡xcos⁡θ−sin⁡xsin⁡θ=cos⁡(x+θ)f_\theta(x)=(\sin x,0,\cos x)\cdot(-\sin\theta,0,\cos\theta)=\cos x\cos\theta-\sin x\sin\theta=\cos(x+\theta)
    Addition theorem of the cosine. The model of this lesson's worked example is obtained in the Heisenberg picture, where the ansatz acts on the observable instead of on the state; the Schrödinger-picture derivation follows in the section on the one-qubit classifier.
  6. Step 6:
    S=spanR{ρ(x)}=spanR{I,X,Z},∥fθ∥HK=∥Oθ∥HS=Tr Z2=2\mathcal S=\mathrm{span}_{\mathbb R}\{\rho(x)\}=\mathrm{span}_{\mathbb R}\{I,X,Z\},\qquad\lVert f_\theta\rVert_{\mathcal H_K}=\lVert O_\theta\rVert_{HS}=\sqrt{\mathrm{Tr}\,Z^2}=\sqrt2
    ρ(x)=12(I+sin⁡x X+cos⁡x Z)\rho(x)=\frac12(I+\sin x\,X+\cos x\,Z); the values x=0,π,π2x=0,\pi,\frac\pi2 give 12(I±Z)\frac12(I\pm Z) and 12(I+X)\frac12(I+X), which span I,X,ZI,X,Z. Here Oθ∈SO_\theta\in\mathcal S, so the trained models form a circle of radius 2\sqrt2 in the three-dimensional RKHS spanned by the functions 1,sin⁡x,cos⁡x1,\sin x,\cos x: the variational family is a one-parameter curve inside the linear model space.

Result

fθ(x)=r⃗(x)⋅n⃗(θ)=(sin⁡x,0,cos⁡x)⋅(−sin⁡θ,0,cos⁡θ)=cos⁡(x+θ)\boxed{f_\theta(x)=\vec r(x)\cdot\vec n(\theta)=(\sin x,0,\cos x)\cdot(-\sin\theta,0,\cos\theta)=\cos(x+\theta)}

Quick check

For a fixed weight vector θ\theta, in which of the following quantities is the model output fθ(x)f_\theta(x) always linear?

Select one answer

Output range, decision rule and losses#

Theorem

The model output lies between the extreme eigenvalues of the observable

Let HH have the spectral decomposition H=∑k=12nλk∣ek⟩⟨ek∣H=\sum_{k=1}^{2^n}\lambda_k\lvert e_k\rangle\langle e_k\rvert with λmin⁡=min⁡kλk\lambda_{\min}=\min_k\lambda_k and λmax⁡=max⁡kλk\lambda_{\max}=\max_k\lambda_k. Then for every input and every weight vector λmin⁡(H) ≤ fθ(x) ≤ λmax⁡(H),\lambda_{\min}(H)\ \le\ f_\theta(x)\ \le\ \lambda_{\max}(H), and the upper (lower) bound is attained exactly when ∣ψθ(x)⟩\lvert\psi_\theta(x)\rangle lies in the eigenspace of λmax⁡\lambda_{\max} (λmin⁡\lambda_{\min}). In particular fθ(x)∈[−1,1]f_\theta(x)\in[-1,1] for every Pauli string H=PH=P, and fθ(x)∈[−∑k∣ck∣,∑k∣ck∣]f_\theta(x)\in\big[-\sum_k\lvert c_k\rvert,\sum_k\lvert c_k\rvert\big] for H=∑kckPkH=\sum_kc_kP_k.

Proof (6 steps)
  1. Step 1:
    H=H† ⇒ H=∑kλk∣ek⟩⟨ek∣,λk∈R,⟨ek∣el⟩=δklH=H^\dagger\ \Rightarrow\ H=\sum_{k}\lambda_k\lvert e_k\rangle\langle e_k\rvert,\quad\lambda_k\in\mathbb R,\quad\langle e_k\vert e_l\rangle=\delta_{kl}
    Spectral theorem for Hermitian matrices: real eigenvalues and an orthonormal eigenbasis.
  2. Step 2:
    fθ(x)=⟨ψ∣H∣ψ⟩=∑kλk ⟨ψ∣ek⟩⟨ek∣ψ⟩=∑kλk pk,pk=∣⟨ek∣ψ⟩∣2f_\theta(x)=\langle\psi\vert H\vert\psi\rangle=\sum_k\lambda_k\,\langle\psi\vert e_k\rangle\langle e_k\vert\psi\rangle=\sum_k\lambda_k\,p_k,\qquad p_k=\lvert\langle e_k\vert\psi\rangle\rvert^2
    Abbreviate ψ=ψθ(x)\psi=\psi_\theta(x) and insert the decomposition; ⟨ψ∣ek⟩=⟨ek∣ψ⟩‾\langle\psi\vert e_k\rangle=\overline{\langle e_k\vert\psi\rangle}.
  3. Step 3:
    pk≥0,∑kpk=∑k⟨ψ∣ek⟩⟨ek∣ψ⟩=⟨ψ∣(∑k∣ek⟩⟨ek∣)∣ψ⟩=⟨ψ∣ψ⟩=1p_k\ge0,\qquad\sum_kp_k=\sum_k\langle\psi\vert e_k\rangle\langle e_k\vert\psi\rangle=\langle\psi\vert\Big(\sum_k\lvert e_k\rangle\langle e_k\rvert\Big)\vert\psi\rangle=\langle\psi\vert\psi\rangle=1
    Completeness of the orthonormal basis and normalisation of ψ\psi (WW and UU are unitary). By the Born rule pkp_k is the probability of measuring the eigenvalue λk\lambda_k.
  4. Step 4:
    λmin⁡=∑kpkλmin⁡≤∑kpkλk≤∑kpkλmax⁡=λmax⁡\lambda_{\min}=\sum_kp_k\lambda_{\min}\le\sum_kp_k\lambda_k\le\sum_kp_k\lambda_{\max}=\lambda_{\max}
    ff is a convex combination of the eigenvalues; replace every λk\lambda_k by the smallest or the largest one, using pk≥0p_k\ge0 and ∑kpk=1\sum_kp_k=1.
  5. Step 5:
    λmax⁡−f=∑kpk(λmax⁡−λk)=0  ⟺  pk=0 whenever λk<λmax⁡\lambda_{\max}-f=\sum_kp_k(\lambda_{\max}-\lambda_k)=0\iff p_k=0\ \text{whenever}\ \lambda_k<\lambda_{\max}
    A sum of nonnegative terms vanishes only if every term vanishes: equality holds exactly when ψ\psi has no component outside the top eigenspace (and likewise for λmin⁡\lambda_{\min}).
  6. Step 6:
    P2=I ⇒ λ∈{−1,+1};∣∑kck⟨ψ∣Pk∣ψ⟩∣≤∑k∣ck∣ ∣⟨ψ∣Pk∣ψ⟩∣≤∑k∣ck∣P^2=I\ \Rightarrow\ \lambda\in\{-1,+1\};\qquad\Big\lvert\sum_kc_k\langle\psi\vert P_k\vert\psi\rangle\Big\rvert\le\sum_k\lvert c_k\rvert\,\big\lvert\langle\psi\vert P_k\vert\psi\rangle\big\rvert\le\sum_k\lvert c_k\rvert
    A Pauli string is Hermitian and squares to the identity, so its eigenvalues satisfy λ2=1\lambda^2=1; it has both eigenvalues because Tr P=0\mathrm{Tr}\,P=0 for P≠IP\ne I. For a sum, the triangle inequality and the Pauli bound give the stated interval (a bound, not always attained).

∎

Derivation

MSE and hinge losses and their gradients

  1. Step 1:
    L(θ)=1M∑m=1Mℓ(fθ(xm),ym),∂L∂θj=1M∑m=1M∂ℓ∂f(fθ(xm),ym) ∂fθ(xm)∂θjL(\theta)=\frac1M\sum_{m=1}^M\ell\big(f_\theta(x_m),y_m\big),\qquad\frac{\partial L}{\partial\theta_j}=\frac1M\sum_{m=1}^M\frac{\partial\ell}{\partial f}\big(f_\theta(x_m),y_m\big)\,\frac{\partial f_\theta(x_m)}{\partial\theta_j}
    Empirical risk on the training set {(xm,ym)}m=1M\{(x_m,y_m)\}_{m=1}^M with a per-sample loss ℓ(f,y)\ell(f,y); the chain rule separates a classical factor ∂ℓ/∂f\partial\ell/\partial f from a quantum factor ∂f/∂θj\partial f/\partial\theta_j, which the circuit must supply.
  2. Step 2:
    ℓMSE(f,y)=(f−y)2,∂ℓMSE∂f=2(f−y)\ell_{\text{MSE}}(f,y)=(f-y)^2,\qquad\frac{\partial\ell_{\text{MSE}}}{\partial f}=2(f-y)
    The squared error treats the labels ±1\pm1 as regression targets. Because f∈[−1,1]f\in[-1,1] for a Pauli observable, the targets lie at the end points of the attainable range.
  3. Step 3:
    ∂LMSE∂θj=2M∑m=1M(fθ(xm)−ym) ∂fθ(xm)∂θj\frac{\partial L_{\text{MSE}}}{\partial\theta_j}=\frac2M\sum_{m=1}^M\big(f_\theta(x_m)-y_m\big)\,\frac{\partial f_\theta(x_m)}{\partial\theta_j}
    Insert the derivative into the chain rule: each sample needs its output fθ(xm)f_\theta(x_m) (for the residual) and its gradient ∇θfθ(xm)\nabla_\theta f_\theta(x_m).
  4. Step 4:
    ℓhinge(f,y)=max⁡(0,1−yf),∂ℓhinge∂f={−y,yf<10,yf>1\ell_{\text{hinge}}(f,y)=\max(0,1-yf),\qquad\frac{\partial\ell_{\text{hinge}}}{\partial f}=\begin{cases}-y,&yf<1\\0,&yf>1\end{cases}
    The hinge loss of the support vector machine. At the kink yf=1yf=1 it is not differentiable; every number between −y-y and 00 is a subgradient, and the platform uses 00.
  5. Step 5:
    ∣f∣≤1 ⇒ yf≤1 ⇒ ℓhinge(f,y)=1−yf,Lhinge(θ)=1−1M∑m=1Mym fθ(xm)\lvert f\rvert\le1\ \Rightarrow\ yf\le1\ \Rightarrow\ \ell_{\text{hinge}}(f,y)=1-yf,\qquad L_{\text{hinge}}(\theta)=1-\frac1M\sum_{m=1}^My_m\,f_\theta(x_m)
    For a Pauli observable the output never exceeds 11 in modulus, so the margin yfyf never exceeds 11 and the hinge is always on its linear branch: the hinge loss becomes 11 minus the average signed output, a linear function of the outputs.
  6. Step 6:
    ∂Lhinge∂θj=−1M∑m: ymfθ(xm)<1ym ∂fθ(xm)∂θj\frac{\partial L_{\text{hinge}}}{\partial\theta_j}=-\frac1M\sum_{m:\,y_mf_\theta(x_m)<1}y_m\,\frac{\partial f_\theta(x_m)}{\partial\theta_j}
    The chain rule with the hinge derivative; for a Pauli observable the sum runs over all samples except those with ymfθ(xm)=1y_mf_\theta(x_m)=1 exactly.
  7. Step 7:
    acc(θ)=1M#{m: ymfθ(xm)>0}\mathrm{acc}(\theta)=\frac1M\#\big\{m:\ y_mf_\theta(x_m)>0\big\}
    The accuracy of the classifier y^=sign f\hat y=\mathrm{sign}\,f is piecewise constant in θ\theta, with zero gradient almost everywhere; it is used to evaluate a model, while the differentiable losses above are used to train it.

Result

∇θLMSE=2M∑m(fθ(xm)−ym)∇θfθ(xm),∇θLhinge=−1M∑m: ymfm<1ym∇θfθ(xm)\boxed{\nabla_\theta L_{\text{MSE}}=\frac2M\sum_m\big(f_\theta(x_m)-y_m\big)\nabla_\theta f_\theta(x_m),\qquad\nabla_\theta L_{\text{hinge}}=-\frac1M\sum_{m:\,y_mf_m<1}y_m\nabla_\theta f_\theta(x_m)}

Quick check

A two-qubit VQC measures H=12 Z⊗I+Z⊗ZH=\tfrac12\,Z\otimes I+Z\otimes Z. What is the largest output value fθ(x)f_\theta(x) any choice of feature map, ansatz, input and weights can produce?

tolerance ±0.000001

A one-qubit classifier worked out exactly#

The smallest VQC has one qubit, one feature and one weight: feature map U(x)=RY(x)U(x)=R_Y(x), ansatz W(θ)=RY(θ)W(\theta)=R_Y(\theta), observable H=ZH=Z. It is the model of the lab and of the function singleQubitThresholdModel in the platform's QML library. The Heisenberg-picture derivation above already gave f=cos⁡(x+θ)f=\cos(x+\theta); here the same result is derived directly from the state, by the matrix algebra one would use for any circuit, and then the encoding is changed from RYR_Y to RXR_X to see how strongly the feature map constrains what the ansatz can do.

Lemma

Rotations about the same axis add their angles

For all real a,ba,b and P∈{X,Y,Z}P\in\{X,Y,Z\}, RP(a)RP(b)=RP(a+b).R_P(a)R_P(b)=R_P(a+b). In particular W(θ)U(x)=RY(θ)RY(x)=RY(x+θ)W(\theta)U(x)=R_Y(\theta)R_Y(x)=R_Y(x+\theta): the weight of the worked model acts as a shift of the input.

Proof (5 steps)
  1. Step 1:
    AB=BA ⇒ (A+B)k=∑l=0k(kl)AlBk−lAB=BA\ \Rightarrow\ (A+B)^k=\sum_{l=0}^k\binom klA^lB^{k-l}
    For commuting matrices the binomial theorem holds, by induction on kk: commutativity is exactly what is needed to collect the terms AlBk−lA^lB^{k-l}.
  2. Step 2:
    eAeB=∑l≥0All!∑r≥0Brr!=∑k≥01k!∑l=0k(kl)AlBk−l=∑k≥0(A+B)kk!=eA+Be^Ae^B=\sum_{l\ge0}\frac{A^l}{l!}\sum_{r\ge0}\frac{B^r}{r!}=\sum_{k\ge0}\frac1{k!}\sum_{l=0}^k\binom klA^lB^{k-l}=\sum_{k\ge0}\frac{(A+B)^k}{k!}=e^{A+B}
    Both exponential series converge absolutely, so their Cauchy product may be regrouped by total degree k=l+rk=l+r, using 1l! (k−l)!=1k!(kl)\frac1{l!\,(k-l)!}=\frac1{k!}\binom kl.
  3. Step 3:
    A=−ia2P,B=−ib2P,AB=−ab4P2=BA ⇒ RP(a)RP(b)=e−iaP/2e−ibP/2=e−i(a+b)P/2=RP(a+b)A=-\tfrac{ia}2P,\quad B=-\tfrac{ib}2P,\quad AB=-\tfrac{ab}4P^2=BA\ \Rightarrow\ R_P(a)R_P(b)=e^{-iaP/2}e^{-ibP/2}=e^{-i(a+b)P/2}=R_P(a+b)
    Two multiples of the same matrix commute, so the exponents add.
  4. Step 4:
    RY(a)=(cos⁡a2−sin⁡a2sin⁡a2cos⁡a2),RY(a)RY(b)=(cacb−sasb−(casb+sacb)sacb+casbcacb−sasb)R_Y(a)=\begin{pmatrix}\cos\frac a2&-\sin\frac a2\\\sin\frac a2&\cos\frac a2\end{pmatrix},\qquad R_Y(a)R_Y(b)=\begin{pmatrix}c_ac_b-s_as_b&-(c_as_b+s_ac_b)\\s_ac_b+c_as_b&c_ac_b-s_as_b\end{pmatrix}
    An independent check by matrix multiplication, with ca=cos⁡a2c_a=\cos\frac a2, sa=sin⁡a2s_a=\sin\frac a2 and the same for bb; the matrix form of RYR_Y is cos⁡a2 I−isin⁡a2 Y\cos\frac a2\,I-i\sin\frac a2\,Y with −iY=(0−110)-iY=\begin{pmatrix}0&-1\\1&0\end{pmatrix}.
  5. Step 5:
    cacb−sasb=cos⁡a+b2,sacb+casb=sin⁡a+b2 ⇒ RY(a)RY(b)=RY(a+b)c_ac_b-s_as_b=\cos\tfrac{a+b}2,\quad s_ac_b+c_as_b=\sin\tfrac{a+b}2\ \Rightarrow\ R_Y(a)R_Y(b)=R_Y(a+b)
    Addition theorems of cosine and sine at the half angles. The product is again a rotation about yy, by the summed angle.

∎

Derivation

State and output of U(x) = R_Y(x), W(θ) = R_Y(θ), H = Z

  1. Step 1:
    ∣ψθ(x)⟩=RY(θ)RY(x)∣0⟩=RY(x+θ)∣0⟩\lvert\psi_\theta(x)\rangle=R_Y(\theta)R_Y(x)\lvert0\rangle=R_Y(x+\theta)\lvert0\rangle
    The circuit applies RY(x)R_Y(x) first and RY(θ)R_Y(\theta) second; the lemma combines them.
  2. Step 2:
    RY(x+θ)(10)=(cos⁡x+θ2−sin⁡x+θ2sin⁡x+θ2cos⁡x+θ2)(10)=(cos⁡x+θ2sin⁡x+θ2)R_Y(x+\theta)\begin{pmatrix}1\\0\end{pmatrix}=\begin{pmatrix}\cos\frac{x+\theta}2&-\sin\frac{x+\theta}2\\\sin\frac{x+\theta}2&\cos\frac{x+\theta}2\end{pmatrix}\begin{pmatrix}1\\0\end{pmatrix}=\begin{pmatrix}\cos\frac{x+\theta}2\\\sin\frac{x+\theta}2\end{pmatrix}
    Matrix times the first basis vector is the first column.
  3. Step 3:
    ∣ψθ(x)⟩=cos⁡x+θ2∣0⟩+sin⁡x+θ2∣1⟩\lvert\psi_\theta(x)\rangle=\cos\tfrac{x+\theta}2\lvert0\rangle+\sin\tfrac{x+\theta}2\lvert1\rangle
    A real state at polar angle x+θx+\theta on the xzxz-great circle of the Bloch sphere.
  4. Step 4:
    fθ(x)=⟨ψ∣Z∣ψ⟩=(cos⁡x+θ2sin⁡x+θ2)(100−1)(cos⁡x+θ2sin⁡x+θ2)=cos⁡2x+θ2−sin⁡2x+θ2f_\theta(x)=\langle\psi\vert Z\vert\psi\rangle=\begin{pmatrix}\cos\frac{x+\theta}2&\sin\frac{x+\theta}2\end{pmatrix}\begin{pmatrix}1&0\\0&-1\end{pmatrix}\begin{pmatrix}\cos\frac{x+\theta}2\\\sin\frac{x+\theta}2\end{pmatrix}=\cos^2\tfrac{x+\theta}2-\sin^2\tfrac{x+\theta}2
    The amplitudes are real, so the bra is the transposed column; ZZ flips the sign of the second component.
  5. Step 5:
    fθ(x)=cos⁡(x+θ)f_\theta(x)=\cos(x+\theta)
    Double-angle formula cos⁡2u−sin⁡2u=cos⁡2u\cos^2u-\sin^2u=\cos2u with u=x+θ2u=\frac{x+\theta}2. The value lies in [−1,1]=[λmin⁡(Z),λmax⁡(Z)][-1,1]=[\lambda_{\min}(Z),\lambda_{\max}(Z)], as the output-range theorem requires.
  6. Step 6:
    x=0.3, θ=π:∣ψ⟩=cos⁡0.3+π2∣0⟩+sin⁡0.3+π2∣1⟩=−sin⁡0.15 ∣0⟩+cos⁡0.15 ∣1⟩≈−0.149438∣0⟩+0.988771∣1⟩x=0.3,\ \theta=\pi:\quad\lvert\psi\rangle=\cos\tfrac{0.3+\pi}2\lvert0\rangle+\sin\tfrac{0.3+\pi}2\lvert1\rangle=-\sin0.15\,\lvert0\rangle+\cos0.15\,\lvert1\rangle\approx-0.149438\lvert0\rangle+0.988771\lvert1\rangle
    cos⁡(u+π2)=−sin⁡u\cos(u+\frac\pi2)=-\sin u and sin⁡(u+π2)=cos⁡u\sin(u+\frac\pi2)=\cos u. Then f=cos⁡(0.3+π)=−cos⁡0.3≈−0.955336f=\cos(0.3+\pi)=-\cos0.3\approx-0.955336, so y^=−1\hat y=-1, the label of x=0.3x=0.3 in the lab. The circuit below shows this state.

Result

fθ(x)=⟨0∣RY(x)†RY(θ)†ZRY(θ)RY(x)∣0⟩=cos⁡(x+θ)\boxed{f_\theta(x)=\langle0\vert R_Y(x)^\dagger R_Y(\theta)^\dagger ZR_Y(\theta)R_Y(x)\vert0\rangle=\cos(x+\theta)}

Circuit · 1 qubit

Quantum circuit on 1 qubitRY(x) on q0; RY(theta) on q0q0RY(x)RY(theta)

The model circuit RY(x)R_Y(x) then RY(θ)R_Y(\theta) with the data symbol bound to x=0.3x=0.3 and the weight to θ=π\theta=\pi. The statevector is RY(0.3+π)∣0⟩=−sin⁡0.15 ∣0⟩+cos⁡0.15 ∣1⟩≈(−0.149438, 0.988771)R_Y(0.3+\pi)\lvert0\rangle=-\sin0.15\,\lvert0\rangle+\cos0.15\,\lvert1\rangle\approx(-0.149438,\ 0.988771), and ⟨Z⟩=−cos⁡0.3\langle Z\rangle=-\cos0.3.

∣ψ⟩=−0.1494∣0⟩+0.9888∣1⟩\lvert\psi\rangle = -0.1494\lvert 0\rangle + 0.9888\lvert 1\rangle
RY(π)RY(0.3)=RY(0.3+π)=(cos⁡0.3+π2−sin⁡0.3+π2sin⁡0.3+π2cos⁡0.3+π2)=(−sin⁡0.15−cos⁡0.15cos⁡0.15−sin⁡0.15)≈(−0.149438−0.9887710.988771−0.149438)R_Y(\pi)R_Y(0.3)=R_Y(0.3+\pi)=\begin{pmatrix}\cos\frac{0.3+\pi}2&-\sin\frac{0.3+\pi}2\\\sin\frac{0.3+\pi}2&\cos\frac{0.3+\pi}2\end{pmatrix}=\begin{pmatrix}-\sin0.15&-\cos0.15\\\cos0.15&-\sin0.15\end{pmatrix}\approx\begin{pmatrix}-0.149438&-0.988771\\0.988771&-0.149438\end{pmatrix}
W(π)U(0.3) = R_Y(0.3 + π)2×2

Unitary verified: U†U=IU^\dagger U = I (max deviation 0.0e+0)

Derivation

Decision boundary and decision regions

  1. Step 1:
    fθ(x)=0  ⟺  cos⁡(x+θ)=0  ⟺  x+θ=±π2(mod2π)f_\theta(x)=0\iff\cos(x+\theta)=0\iff x+\theta=\pm\tfrac\pi2\pmod{2\pi}
    The zeros of the cosine are the odd multiples of π2\frac\pi2; modulo 2π2\pi these are π2\frac\pi2 and −π2-\frac\pi2.
  2. Step 2:
    y^(x)=+1  ⟺  x+θ∈(−π2,π2)(mod2π),y^(x)=−1  ⟺  x+θ∈(π2,3π2)(mod2π)\hat y(x)=+1\iff x+\theta\in\big(-\tfrac\pi2,\tfrac\pi2\big)\pmod{2\pi},\qquad\hat y(x)=-1\iff x+\theta\in\big(\tfrac\pi2,\tfrac{3\pi}2\big)\pmod{2\pi}
    The cosine is positive on (−π2,π2)(-\frac\pi2,\frac\pi2) and negative on (π2,3π2)(\frac\pi2,\frac{3\pi}2), each modulo 2π2\pi.
  3. Step 3:
    x↑∗=−π2−θ,x↓∗=π2−θ(mod2π)x^\ast_\uparrow=-\tfrac\pi2-\theta,\qquad x^\ast_\downarrow=\tfrac\pi2-\theta\pmod{2\pi}
    On the input axis the classifier has a rising boundary x↑∗x^\ast_\uparrow, where the prediction changes from −1-1 to +1+1 as xx increases, and a falling boundary x↓∗=x↑∗+πx^\ast_\downarrow=x^\ast_\uparrow+\pi, where it changes back.
  4. Step 4:
    θ↦θ+Δ ⇒ x∗↦x∗−Δ,θ↦θ+π ⇒ f↦−f\theta\mapsto\theta+\Delta\ \Rightarrow\ x^\ast\mapsto x^\ast-\Delta,\qquad\theta\mapsto\theta+\pi\ \Rightarrow\ f\mapsto-f
    The weight translates both boundaries rigidly; the two regions always have length π\pi. Shifting by π\pi exchanges the regions, since cos⁡(u+π)=−cos⁡u\cos(u+\pi)=-\cos u: every prediction is negated.

Result

{x:y^(x)=+1}=(−π2−θ, π2−θ)(mod2π)\boxed{\{x:\hat y(x)=+1\}=\big(-\tfrac\pi2-\theta,\ \tfrac\pi2-\theta\big)\pmod{2\pi}}

The Bloch sphere shows the encoded state RY(1.8)∣0⟩R_Y(1.8)\lvert0\rangle of the data point x=1.8x=1.8: polar angle 1.81.8, azimuth 00, Bloch vector (sin⁡1.8,0,cos⁡1.8)≈(0.974,0,−0.227)(\sin1.8,0,\cos1.8)\approx(0.974,0,-0.227). It lies slightly below the equator, so without an ansatz (θ=0\theta=0) the model would output cos⁡1.8≈−0.227\cos1.8\approx-0.227 and predict −1-1, the wrong label. The ansatz rotates this point by θ\theta about the yy-axis (equivalently, it rotates the measurement direction by −θ-\theta), and the lab asks for the rotation that lifts the two points of class +1+1 above the equator while pushing the two points of class −1-1 below it.

The pipeline above shows the three stages, feature map, ansatz and measurement, for the four lab points at θ=0\theta=0. The output is f(x)=cos⁡xf(x)=\cos x, which is positive for x=0.3,0.9x=0.3,0.9 and negative for x=1.8,2.2x=1.8,2.2: exactly the opposite of the labels, so the accuracy is 00 and the MSE is 14[(0.955+1)2+(0.622+1)2+(−0.227−1)2+(−0.589−1)2]≈2.62\frac14\big[(0.955+1)^2+(0.622+1)^2+(-0.227-1)^2+(-0.589-1)^2\big]\approx2.62. Drag θ\theta and watch the decision regions translate along the data strip.

Derivation

Changing the encoding: U(x) = R_X(x) gives f = cos x cos θ

  1. Step 1:
    RX(x)∣0⟩=(cos⁡x2−isin⁡x2−isin⁡x2cos⁡x2)(10)=(cx−isx),cx=cos⁡x2, sx=sin⁡x2R_X(x)\lvert0\rangle=\begin{pmatrix}\cos\frac x2&-i\sin\frac x2\\-i\sin\frac x2&\cos\frac x2\end{pmatrix}\begin{pmatrix}1\\0\end{pmatrix}=\begin{pmatrix}c_x\\-is_x\end{pmatrix},\qquad c_x=\cos\tfrac x2,\ s_x=\sin\tfrac x2
    RX(x)=cos⁡x2 I−isin⁡x2 XR_X(x)=\cos\frac x2\,I-i\sin\frac x2\,X; its first column is the encoded state.
  2. Step 2:
    RY(θ)RX(x)∣0⟩=(c−ssc)(cx−isx)=(c cx+i s sxs cx−i c sx),c=cos⁡θ2, s=sin⁡θ2R_Y(\theta)R_X(x)\lvert0\rangle=\begin{pmatrix}c&-s\\s&c\end{pmatrix}\begin{pmatrix}c_x\\-is_x\end{pmatrix}=\begin{pmatrix}c\,c_x+i\,s\,s_x\\s\,c_x-i\,c\,s_x\end{pmatrix},\qquad c=\cos\tfrac\theta2,\ s=\sin\tfrac\theta2
    Matrix–vector product with the real matrix RY(θ)R_Y(\theta). The two rotations are about different axes, so the lemma does not apply and the angles do not add.
  3. Step 3:
    ∣c cx+i s sx∣2=c2cx2+s2sx2,∣s cx−i c sx∣2=s2cx2+c2sx2\lvert c\,c_x+i\,s\,s_x\rvert^2=c^2c_x^2+s^2s_x^2,\qquad\lvert s\,c_x-i\,c\,s_x\rvert^2=s^2c_x^2+c^2s_x^2
    ∣a+ib∣2=a2+b2\lvert a+ib\rvert^2=a^2+b^2 for real a,ba,b; the cross terms cancel because one part is real and the other imaginary.
  4. Step 4:
    fθ(x)=c2cx2+s2sx2−s2cx2−c2sx2=(c2−s2)(cx2−sx2)=cos⁡θ cos⁡xf_\theta(x)=c^2c_x^2+s^2s_x^2-s^2c_x^2-c^2s_x^2=(c^2-s^2)(c_x^2-s_x^2)=\cos\theta\,\cos x
    ⟨Z⟩=∣a0∣2−∣a1∣2\langle Z\rangle=\lvert a_0\rvert^2-\lvert a_1\rvert^2; factor and use the double-angle formula twice.
  5. Step 5:
    cos⁡(x+θ)−cos⁡xcos⁡θ=−sin⁡xsin⁡θ ≢0,x=θ=π2: cos⁡π−0=−1\cos(x+\theta)-\cos x\cos\theta=-\sin x\sin\theta\ \not\equiv0,\qquad x=\theta=\tfrac\pi2:\ \cos\pi-0=-1
    The RXR_X model is not cos⁡(x+θ)=cos⁡xcos⁡θ−sin⁡xsin⁡θ\cos(x+\theta)=\cos x\cos\theta-\sin x\sin\theta: the term −sin⁡xsin⁡θ-\sin x\sin\theta is missing, and the two models differ by up to 11 (the platform's simulator confirms the maximum difference 11 on a grid). They agree only where sin⁡xsin⁡θ=0\sin x\sin\theta=0.
  6. Step 6:
    r⃗X(x)=(0,−sin⁡x,cos⁡x),n⃗(θ)=(−sin⁡θ,0,cos⁡θ) ⇒ fθ(x)=r⃗X(x)⋅n⃗(θ)=cos⁡xcos⁡θ\vec r_X(x)=(0,-\sin x,\cos x),\quad\vec n(\theta)=(-\sin\theta,0,\cos\theta)\ \Rightarrow\ f_\theta(x)=\vec r_X(x)\cdot\vec n(\theta)=\cos x\cos\theta
    The Heisenberg picture explains why: RXR_X places the data on the yzyz-great circle, the ansatz RYR_Y only tilts the measurement direction within the xzxz-plane, and the xx-component of the encoded Bloch vector, which would carry sin⁡x\sin x, is identically 00.
  7. Step 7:
    cos⁡θ≠0:fθ(x)=0  ⟺  cos⁡x=0  ⟺  x=π2(modπ)\cos\theta\ne0:\quad f_\theta(x)=0\iff\cos x=0\iff x=\tfrac\pi2\pmod\pi
    The weight only rescales the output by cos⁡θ\cos\theta and possibly flips its sign; the decision boundary is fixed at x=π2x=\frac\pi2 for every θ\theta (for cos⁡θ=0\cos\theta=0 the model is identically 00).

Result

U(x)=RX(x), W(θ)=RY(θ), H=Z:fθ(x)=cos⁡xcos⁡θ≠cos⁡(x+θ)\boxed{U(x)=R_X(x),\ W(\theta)=R_Y(\theta),\ H=Z:\qquad f_\theta(x)=\cos x\cos\theta\ne\cos(x+\theta)}

With the RXR_X encoding the model family is {cos⁡θcos⁡x}\{\cos\theta\cos x\}, a single function scaled by a factor in [−1,1][-1,1]. For the lab data this family happens to suffice: the fixed boundary x=π2≈1.571x=\frac\pi2\approx1.571 lies between 0.90.9 and 1.81.8, and every weight with cos⁡θ<0\cos\theta<0, e.g. θ=π\theta=\pi as in the pipeline above, classifies all four points correctly. For data whose classes change at any other place, no weight works. The RYR_Y encoding produces the family {cos⁡(x+θ)}\{\cos(x+\theta)\} of all translated cosines, whose boundary can be placed anywhere. The same ansatz and observable give a strictly richer model on one encoding than on the other, which illustrates the first lesson of the trace form: the feature map fixes the space of features, and the ansatz can only choose a direction inside it.

Quick check

For the worked model U(x)=RY(x)U(x)=R_Y(x), W(θ)=RY(θ)W(\theta)=R_Y(\theta), H=ZH=Z, compute the output fθ(x)f_\theta(x) at x=1.8x=1.8 and θ=−2.9\theta=-2.9 (four decimals).

tolerance ±0.001

Quick check

With the encoding U(x)=RX(x)U(x)=R_X(x), ansatz W(θ)=RY(θ)W(\theta)=R_Y(\theta) and H=ZH=Z, what does changing the weight θ\theta do to the classifier?

Select one answer

Estimating the output with finitely many shots#

Theorem

Shot noise of a Pauli expectation value

Let H=PH=P be a Pauli string and let the circuit be run NN times, each run ending in a projective measurement of PP with outcome zs∈{−1,+1}z_s\in\{-1,+1\}. Then (a) Pr⁡(zs=±1)=1±f2\Pr(z_s=\pm1)=\frac{1\pm f}2 with f=fθ(x)f=f_\theta(x); (b) the estimator f^=1N∑s=1Nzs\hat f=\frac1N\sum_{s=1}^Nz_s is unbiased with Var[f^]=1−f2N≤1N;\mathrm{Var}[\hat f]=\frac{1-f^2}N\le\frac1N; (c) by Hoeffding's inequality Pr⁡(∣f^−f∣≥ε)≤2e−Nε2/2\Pr\big(\lvert\hat f-f\rvert\ge\varepsilon\big)\le2e^{-N\varepsilon^2/2}, so N≥2ln⁡(2/δ)ε2N\ge\frac{2\ln(2/\delta)}{\varepsilon^2} shots give precision ε\varepsilon with probability at least 1−δ1-\delta; (d) if f≠0f\ne0, the estimated label sign f^\mathrm{sign}\,\hat f differs from sign f\mathrm{sign}\,f with probability at most e−Nf2/2e^{-Nf^2/2}.

Proof (7 steps)
  1. Step 1:
    P2=I ⇒ P=Π+−Π−,Π±=12(I±P),Π±2=Π±, Π++Π−=IP^2=I\ \Rightarrow\ P=\Pi_+-\Pi_-,\qquad\Pi_\pm=\tfrac12(I\pm P),\qquad\Pi_\pm^2=\Pi_\pm,\ \Pi_++\Pi_-=I
    The eigenprojectors of an operator with eigenvalues ±1\pm1: Π±2=14(I±2P+P2)=12(I±P)\Pi_\pm^2=\frac14(I\pm2P+P^2)=\frac12(I\pm P), and they sum to the identity.
  2. Step 2:
    Pr⁡(zs=±1)=Tr(Π± ρθ(x))=12(1±Tr(Pρθ(x)))=1±f2\Pr(z_s=\pm1)=\mathrm{Tr}\big(\Pi_\pm\,\rho_\theta(x)\big)=\tfrac12\big(1\pm\mathrm{Tr}(P\rho_\theta(x))\big)=\frac{1\pm f}2
    Born rule for projective measurements with ρθ(x)=∣ψθ(x)⟩⟨ψθ(x)∣\rho_\theta(x)=\lvert\psi_\theta(x)\rangle\langle\psi_\theta(x)\rvert, Tr ρ=1\mathrm{Tr}\,\rho=1 and f=Tr(Pρ)f=\mathrm{Tr}(P\rho).
  3. Step 3:
    E[zs]=1+f2−1−f2=f,E[zs2]=1,Var[zs]=1−f2\mathbb E[z_s]=\frac{1+f}2-\frac{1-f}2=f,\qquad\mathbb E[z_s^2]=1,\qquad\mathrm{Var}[z_s]=1-f^2
    The outcome is ±1\pm1, so its square is always 11.
  4. Step 4:
    E[f^]=1N∑sE[zs]=f,Var[f^]=1N2∑sVar[zs]=1−f2N\mathbb E[\hat f]=\frac1N\sum_s\mathbb E[z_s]=f,\qquad\mathrm{Var}[\hat f]=\frac1{N^2}\sum_s\mathrm{Var}[z_s]=\frac{1-f^2}N
    Each shot re-prepares the state, so the outcomes are independent and identically distributed: linearity of expectation, and variances of independent variables add.
  5. Step 5:
    Pr⁡(∣f^−f∣≥ε)≤2exp⁡(−2N2ε2∑s=1N(1−(−1))2)=2e−Nε2/2\Pr\big(\lvert\hat f-f\rvert\ge\varepsilon\big)\le2\exp\Big(-\frac{2N^2\varepsilon^2}{\sum_{s=1}^N(1-(-1))^2}\Big)=2e^{-N\varepsilon^2/2}
    Hoeffding's inequality for the mean of NN independent variables with values in [a,b]=[−1,1][a,b]=[-1,1]: the exponent is −2N2ε2/(N(b−a)2)=−Nε2/2-2N^2\varepsilon^2/\big(N(b-a)^2\big)=-N\varepsilon^2/2.
  6. Step 6:
    2e−Nε2/2≤δ  ⟺  N≥2ln⁡(2/δ)ε22e^{-N\varepsilon^2/2}\le\delta\iff N\ge\frac{2\ln(2/\delta)}{\varepsilon^2}
    Solve for NN by taking logarithms. The number of shots grows like ε−2\varepsilon^{-2} and only logarithmically in the confidence.
  7. Step 7:
    f>0:Pr⁡(f^≤0)=Pr⁡(f^−f≤−f)≤e−Nf2/2f>0:\quad\Pr(\hat f\le0)=\Pr\big(\hat f-f\le-f\big)\le e^{-Nf^2/2}
    The one-sided Hoeffding bound Pr⁡(f^−f≤−t)≤e−Nt2/2\Pr(\hat f-f\le-t)\le e^{-Nt^2/2} with t=ft=f; the case f<0f<0 is symmetric. A point far from the decision boundary (large ∣f∣\lvert f\rvert) is classified correctly with few shots; a point near it needs about f−2f^{-2} shots.

∎

Derivation

Shot budget for the lab point x = 1.8 at θ = −2.9

  1. Step 1:
    f=cos⁡(1.8−2.9)=cos⁡1.1≈0.453596,Pr⁡(zs=+1)=1+f2≈0.726798f=\cos(1.8-2.9)=\cos1.1\approx0.453596,\qquad \Pr(z_s=+1)=\frac{1+f}2\approx0.726798
    Exact output of the worked model and the Born probability of the outcome +1+1.
  2. Step 2:
    Var[f^]=1−f2N≈0.794251100≈0.00794,Var[f^]≈0.0891(N=100)\mathrm{Var}[\hat f]=\frac{1-f^2}N\approx\frac{0.794251}{100}\approx0.00794,\qquad\sqrt{\mathrm{Var}[\hat f]}\approx0.0891\quad(N=100)
    With 100100 shots the estimate scatters by about 0.090.09 around 0.45360.4536. The exact binomial computation in the NumPy cell reproduces N Var[f^]=0.794251N\,\mathrm{Var}[\hat f]=0.794251.
  3. Step 3:
    Pr⁡(f^≤0)≤e−100⋅0.205750/2=e−10.2875≈3.41×10−5\Pr(\hat f\le0)\le e^{-100\cdot0.205750/2}=e^{-10.2875}\approx3.41\times10^{-5}
    Part (d) of the theorem. The exact binomial probability is 1.24×10−61.24\times10^{-6}: Hoeffding's bound is valid but loose.
  4. Step 4:
    ε=0.01, δ=0.05:N≥2ln⁡4010−4=73777.6 ⇒ N=73778\varepsilon=0.01,\ \delta=0.05:\quad N\ge\frac{2\ln40}{10^{-4}}=73777.6\ \Rightarrow\ N=73778
    Estimating the output itself to two decimals with 95%95\% confidence costs tens of thousands of shots, far more than deciding its sign.

Result

N=100: Var[f^]≈0.089, P(sign error)≤3.4×10−5;ε=0.01, δ=0.05: N=73778\boxed{N=100:\ \sqrt{\mathrm{Var}[\hat f]}\approx0.089,\ P(\text{sign error})\le3.4\times10^{-5};\qquad\varepsilon=0.01,\ \delta=0.05:\ N=73778}

Quick check

How many shots does the Hoeffding bound require to estimate a Pauli expectation value to precision ε=0.01\varepsilon=0.01 with probability at least 0.950.95?

tolerance ±1

The hybrid quantum–classical training loop#

Training a VQC alternates between two machines. The quantum processor evaluates circuits at fixed parameter values and returns estimated expectation values; it cannot store or update θ\theta. The classical computer holds θ\theta, assembles the loss and its gradient from the returned numbers, and applies an optimisation step. The loss gradient factorises into a classical part ∂ℓ/∂f\partial\ell/\partial f and a quantum part ∇θfθ(xm)\nabla_\theta f_\theta(x_m). Backpropagation through the circuit is unavailable because intermediate quantum states cannot be read out without destroying them (the topic of the next module), so the quantum part is obtained from additional circuit evaluations. For gates of the form e−iθjP/2e^{-i\theta_jP/2} with a Pauli generator the parameter-shift rule gives it exactly from two evaluations at shifted angles, ∂f∂θj=12[f(θj+π2)−f(θj−π2)];\frac{\partial f}{\partial\theta_j}=\frac12\Big[f\big(\theta_j+\tfrac\pi2\big)-f\big(\theta_j-\tfrac\pi2\big)\Big]; the rule is stated here and derived in the lesson on the parameter-shift rule.

Derivation

The parameter-shift rule on the worked model

  1. Step 1:
    ∂∂θcos⁡(x+θ)=−sin⁡(x+θ)\frac{\partial}{\partial\theta}\cos(x+\theta)=-\sin(x+\theta)
    The exact derivative of the closed-form output.
  2. Step 2:
    12[cos⁡(x+θ+π2)−cos⁡(x+θ−π2)]=12[−sin⁡(x+θ)−sin⁡(x+θ)]=−sin⁡(x+θ)\tfrac12\big[\cos\big(x+\theta+\tfrac\pi2\big)-\cos\big(x+\theta-\tfrac\pi2\big)\big]=\tfrac12\big[-\sin(x+\theta)-\sin(x+\theta)\big]=-\sin(x+\theta)
    cos⁡(u±π2)=∓sin⁡u\cos(u\pm\frac\pi2)=\mp\sin u. The two shifted circuits reproduce the derivative exactly, not approximately: there is no step size and no truncation error, unlike a finite difference.
  3. Step 3:
    ∂LMSE∂θ=−2M∑m=1M(cos⁡(xm+θ)−ym)sin⁡(xm+θ)\frac{\partial L_{\text{MSE}}}{\partial\theta}=-\frac2M\sum_{m=1}^M\big(\cos(x_m+\theta)-y_m\big)\sin(x_m+\theta)
    Insert into the MSE gradient of the previous section; at θ=0\theta=0 on the lab data this is ≈0.316>0\approx0.316>0, so gradient descent decreases θ\theta, towards the feasible interval.

Result

∂θfθ(x)=12[fθ+π/2(x)−fθ−π/2(x)]=−sin⁡(x+θ)\boxed{\partial_\theta f_\theta(x)=\tfrac12\big[f_{\theta+\pi/2}(x)-f_{\theta-\pi/2}(x)\big]=-\sin(x+\theta)}
Proposition

Cost of one epoch of gradient-based training

Consider full-batch training on MM samples of a VQC with PP weights, each appearing in exactly one Pauli-rotation gate, with the loss gradient computed by the parameter-shift rule and every expectation value estimated with NshotsN_{\text{shots}} shots. One epoch (one gradient step) requires M(2P+1)M(2P+1) distinct circuit evaluations and M(2P+1)Nshots=O(M P Nshots)M(2P+1)N_{\text{shots}}=O(M\,P\,N_{\text{shots}}) shots. A gradient-free or stochastic-approximation step (SPSA) instead uses 2M2M circuit evaluations, independently of PP.

Proof (5 steps)
  1. Step 1:
    ∇θL=1M∑m=1Mℓ′(fθ(xm),ym) ∇θfθ(xm)\nabla_\theta L=\frac1M\sum_{m=1}^M\ell'\big(f_\theta(x_m),y_m\big)\,\nabla_\theta f_\theta(x_m)
    The chain-rule form of the loss gradient: each sample contributes separately.
  2. Step 2:
    ℓMSE′=2(fθ(xm)−ym):1 circuit per sample for fθ(xm)\ell'_{\text{MSE}}=2\big(f_\theta(x_m)-y_m\big):\quad1\ \text{circuit per sample for }f_\theta(x_m)
    The classical factor needs the unshifted output (for the hinge loss it needs the sign of 1−ymf1-y_mf, which also requires ff).
  3. Step 3:
    ∂fθ(xm)∂θj=12[fθ+π2ej(xm)−fθ−π2ej(xm)]:2 circuits per weight and sample\frac{\partial f_\theta(x_m)}{\partial\theta_j}=\tfrac12\big[f_{\theta+\frac\pi2e_j}(x_m)-f_{\theta-\frac\pi2e_j}(x_m)\big]:\quad2\ \text{circuits per weight and sample}
    Parameter-shift rule with the shift applied only to weight jj (eje_j is the jj-th unit vector); for PP weights 2P2P circuits.
  4. Step 4:
    #circuits=M (1+2P),#shots=M (2P+1) Nshots=O(M P Nshots)\#\text{circuits}=M\,(1+2P),\qquad\#\text{shots}=M\,(2P+1)\,N_{\text{shots}}=O\big(M\,P\,N_{\text{shots}}\big)
    Sum over the MM samples and multiply by the shots per circuit. For the lab model (M=4M=4, P=1P=1) this is 1212 circuits per epoch.
  5. Step 5:
    g^SPSA=L(θ+cΔ)−L(θ−cΔ)2c Δ−1,Δ∈{±1}P:2M circuits\hat g_{\text{SPSA}}=\frac{L(\theta+c\Delta)-L(\theta-c\Delta)}{2c}\,\Delta^{-1},\qquad\Delta\in\{\pm1\}^P:\quad2M\ \text{circuits}
    Simultaneous-perturbation stochastic approximation perturbs all weights at once along a random sign vector, so a step costs two loss evaluations, i.e. 2M2M circuits, at the price of a noisy, only asymptotically unbiased gradient estimate.

∎

Varieties of hybrid loops. (a) Gradient-based with exact shifts: the loop of the proposition, used with gradient descent or Adam; its gradients are unbiased under shot noise, and it is the loop of the code cell below. (b) Finite differences: [f(θj+h)−f(θj−h)]/(2h)\big[f(\theta_j+h)-f(\theta_j-h)\big]/(2h) also costs 2P2P circuits per sample but is biased by O(h2)O(h^2), and shot noise is amplified by 1/h1/h, so small hh is unusable on hardware. (c) Stochastic approximation (SPSA): two evaluations per step whatever PP is, attractive for many weights. (d) Gradient-free direct search (Nelder–Mead, COBYLA): uses only loss values and scales poorly with PP. (e) Mini-batch versions of any of these replace the MM samples by a random batch per step and trade gradient noise for cost. All of them share the same structure: the quantum processor is a function evaluator inside a classical optimisation loop.

The TypeScript cell runs the hybrid loop on the worked model with the platform's QML library: it checks the model output and its parameter-shift derivative against cos⁡(x+θ)\cos(x+\theta) and −sin⁡(x+θ)-\sin(x+\theta), writes out the training loop explicitly (one output evaluation and one two-circuit shift gradient per sample, then a classical step θ←θ−η ∂L/∂θ\theta\leftarrow\theta-\eta\,\partial L/\partial\theta with η=0.3\eta=0.3), and confirms that the library routine trainVQC produces the same trajectory. Starting at the all-wrong θ=0\theta=0, the accuracy rises from 00 through 0.50.5 and 0.750.75 to 11, and θ\theta converges to the MSE minimiser ≈−2.8891\approx-2.8891, inside the feasible interval derived in the next section.

TypeScriptrunnable
import { singleQubitThresholdModel, trainVQC, vqcAccuracy, vqcGradient, vqcPredict } from '@/lib/qml';

// The model of this lesson: U(x) = RY(x), W(theta) = RY(theta), H = Z, so f(x) = cos(x + theta).
const model = singleQubitThresholdModel();
const X = [[0.3], [0.9], [1.8], [2.2]];
const y = [-1, -1, 1, 1];
const M = X.length;

// (1) Quantum evaluation: f and its parameter-shift derivative equal cos(x + theta) and -sin(x + theta).
for (const theta of [0, -2.9, Math.PI]) {
  const f = X.map((x) => vqcPredict(model, x, { theta }));
  const df = X.map((x) => vqcGradient(model, x, { theta }).theta);
  const exact = X.every((x, m) => Math.abs(f[m] - Math.cos(x[0] + theta)) < 1e-12 && Math.abs(df[m] + Math.sin(x[0] + theta)) < 1e-12);
  console.log('theta = ' + theta.toFixed(4) + '  f = [' + f.map((v) => v.toFixed(4)).join(', ') + ']  accuracy = ' + vqcAccuracy(model, X, y, { theta }) + '  closed form: ' + exact);
}

// (2) The hybrid loop written out: circuit evaluations inside, a classical gradient step outside.
const lr = 0.3;
let theta = 0;
for (let epoch = 0; epoch < 30; epoch++) {
  let loss = 0;
  let grad = 0;
  for (let m = 0; m < M; m++) {
    const f = vqcPredict(model, X[m], { theta }); // 1 circuit (N_shots shots on hardware)
    const df = vqcGradient(model, X[m], { theta }).theta; // 2P = 2 shifted circuits
    loss += (f - y[m]) ** 2 / M;
    grad += (2 / M) * (f - y[m]) * df; // dL/df times df/dtheta
  }
  if (epoch % 5 === 0) console.log('epoch ' + epoch + ': theta = ' + theta.toFixed(4) + '  MSE = ' + loss.toFixed(4) + '  accuracy = ' + vqcAccuracy(model, X, y, { theta }));
  theta -= lr * grad; // classical optimiser step
}

// (3) The library routine trainVQC runs the same loop.
const run = trainVQC(model, X, y, { init: { theta: 0 }, epochs: 30, optimizer: 'gd', lr, loss: 'mse' });
console.log('hand-written loop: theta = ' + theta.toFixed(6) + ', trainVQC: theta = ' + run.weights.theta.toFixed(6) + ', final accuracy = ' + run.history[30].accuracy);
console.log('circuit evaluations per epoch: M (2P + 1) = ' + M * (2 * model.weightParams.length + 1));

Expected output

theta = 0.0000  f = [0.9553, 0.6216, -0.2272, -0.5885]  accuracy = 0  closed form: true
theta = -2.9000  f = [-0.8569, -0.4161, 0.4536, 0.7648]  accuracy = 1  closed form: true
theta = 3.1416  f = [-0.9553, -0.6216, 0.2272, 0.5885]  accuracy = 1  closed form: true
epoch 0: theta = 0.0000  MSE = 2.6206  accuracy = 0
epoch 5: theta = -0.7866  MSE = 2.1141  accuracy = 0.5
epoch 10: theta = -2.4224  MSE = 0.3403  accuracy = 0.75
epoch 15: theta = -2.8662  MSE = 0.1791  accuracy = 1
epoch 20: theta = -2.8881  MSE = 0.1787  accuracy = 1
epoch 25: theta = -2.8890  MSE = 0.1787  accuracy = 1
hand-written loop: theta = -2.889079, trainVQC: theta = -2.889079, final accuracy = 1
circuit evaluations per epoch: M (2P + 1) = 12

Quick check

A VQC with P=6P=6 single-occurrence Pauli-rotation weights is trained full-batch on M=50M=50 samples with parameter-shift gradients and the MSE loss. How many distinct circuit evaluations does one epoch need?

tolerance ±0.5

The lab data set: the exact feasible interval#

The lab trains the worked model on four labelled points: x∈{0.3,0.9}x\in\{0.3,0.9\} with label −1-1 and x∈{1.8,2.2}x\in\{1.8,2.2\} with label +1+1. Because the model is the explicit function cos⁡(x+θ)\cos(x+\theta), the set of weights that classify all points correctly can be computed exactly, and so can the set where every prediction is wrong.

Proposition

Feasible weights for the lab data

For fθ(x)=cos⁡(x+θ)f_\theta(x)=\cos(x+\theta) and the data {(0.3,−1),(0.9,−1),(1.8,+1),(2.2,+1)}\{(0.3,-1),(0.9,-1),(1.8,+1),(2.2,+1)\}: acc(θ)=1  ⟺  θ∈(−π2−1.8, −π2−0.9)(mod2π),\mathrm{acc}(\theta)=1\iff\theta\in\big(-\tfrac\pi2-1.8,\ -\tfrac\pi2-0.9\big)\pmod{2\pi}, an interval of length 0.90.9. Represented in [0,2π)[0,2\pi) it is (3π2−1.8, 3π2−0.9)\big(\tfrac{3\pi}2-1.8,\ \tfrac{3\pi}2-0.9\big), and in (−π,π](-\pi,\pi] it is the union (3π2−1.8, π]∪(−π, −π2−0.9)\big(\tfrac{3\pi}2-1.8,\ \pi\big]\cup\big(-\pi,\ -\tfrac\pi2-0.9\big). Moreover acc(θ)=0\mathrm{acc}(\theta)=0 exactly on the interval shifted by π\pi, (π2−1.8, π2−0.9)(mod2π)\big(\tfrac\pi2-1.8,\ \tfrac\pi2-0.9\big)\pmod{2\pi}, which contains the initial weight θ=0\theta=0.

Proof (7 steps)
  1. Step 1:
    acc(θ)=1  ⟺  cos⁡(0.3+θ)<0, cos⁡(0.9+θ)<0, cos⁡(1.8+θ)>0, cos⁡(2.2+θ)>0\mathrm{acc}(\theta)=1\iff\cos(0.3+\theta)<0,\ \cos(0.9+\theta)<0,\ \cos(1.8+\theta)>0,\ \cos(2.2+\theta)>0
    Every prediction signcos⁡(xm+θ)\mathrm{sign}\cos(x_m+\theta) must equal its label, and an output 00 counts as wrong, so the inequalities are strict.
  2. Step 2:
    cos⁡(1.8+θ)>0, cos⁡(2.2+θ)>0  ⟺  θ∈(−π2−1.8, π2−2.2)+2πk\cos(1.8+\theta)>0,\ \cos(2.2+\theta)>0\iff\theta\in\big(-\tfrac\pi2-1.8,\ \tfrac\pi2-2.2\big)+2\pi k
    1.8+θ1.8+\theta and 2.2+θ2.2+\theta must lie in a window (−π2,π2)+2πk(-\frac\pi2,\frac\pi2)+2\pi k; both lie in the same window, because they are only 0.4<π0.4<\pi apart and two consecutive positive windows are separated by a gap of length π\pi. So 1.8+θ>−π2+2πk1.8+\theta>-\frac\pi2+2\pi k and 2.2+θ<π2+2πk2.2+\theta<\frac\pi2+2\pi k.
  3. Step 3:
    cos⁡(0.3+θ)<0, cos⁡(0.9+θ)<0  ⟺  θ∈(π2−0.3, 3π2−0.9)+2πk′\cos(0.3+\theta)<0,\ \cos(0.9+\theta)<0\iff\theta\in\big(\tfrac\pi2-0.3,\ \tfrac{3\pi}2-0.9\big)+2\pi k'
    The same argument with the negative windows (π2,3π2)+2πk′(\frac\pi2,\frac{3\pi}2)+2\pi k' for the two points 0.60.6 apart.
  4. Step 4:
    k′=k−1:(π2−0.3−2π, 3π2−0.9−2π)=(−3π2−0.3, −π2−0.9)≈(−5.0124, −2.4708)k'=k-1:\quad\big(\tfrac\pi2-0.3-2\pi,\ \tfrac{3\pi}2-0.9-2\pi\big)=\big(-\tfrac{3\pi}2-0.3,\ -\tfrac\pi2-0.9\big)\approx(-5.0124,\ -2.4708)
    Among the translates of the second interval only k′=k−1k'=k-1 meets the first one, (−π2−1.8,π2−2.2)≈(−3.3708,−0.6292)(-\frac\pi2-1.8,\frac\pi2-2.2)\approx(-3.3708,-0.6292) for k=0k=0: the translate k′=kk'=k starts at 1.2708>−0.62921.2708>-0.6292, and k′=k−2k'=k-2 ends at −8.7540<−3.3708-8.7540<-3.3708.
  5. Step 5:
    (−π2−1.8, π2−2.2)∩(−3π2−0.3, −π2−0.9)=(−π2−1.8, −π2−0.9)≈(−3.370796, −2.470796)\Big(-\tfrac\pi2-1.8,\ \tfrac\pi2-2.2\Big)\cap\Big(-\tfrac{3\pi}2-0.3,\ -\tfrac\pi2-0.9\Big)=\Big(-\tfrac\pi2-1.8,\ -\tfrac\pi2-0.9\Big)\approx(-3.370796,\ -2.470796)
    The larger lower end point is −π2−1.8-\frac\pi2-1.8 (since −3π2−0.3<−π2−1.8-\frac{3\pi}2-0.3<-\frac\pi2-1.8) and the smaller upper end point is −π2−0.9-\frac\pi2-0.9 (since −π2−0.9<π2−2.2-\frac\pi2-0.9<\frac\pi2-2.2). Length 0.90.9. Equivalently, the rising boundary x↑∗=−π2−θx^\ast_\uparrow=-\frac\pi2-\theta must lie in the gap (0.9,1.8)(0.9,1.8) between the classes.
  6. Step 6:
    +2π: (3π2−1.8, 3π2−0.9)≈(2.912389, 3.812389),π≈3.141593∈(2.912389, 3.812389)+2\pi:\ \Big(\tfrac{3\pi}2-1.8,\ \tfrac{3\pi}2-0.9\Big)\approx(2.912389,\ 3.812389),\qquad\pi\approx3.141593\in(2.912389,\ 3.812389)
    Adding 2π2\pi gives the representative in [0,2π)[0,2\pi). It contains π\pi, so in (−π,π](-\pi,\pi] the set splits into (2.912389,π](2.912389,\pi] and (−π,3.812389−2π)=(−π,−2.470796)(-\pi,3.812389-2\pi)=(-\pi,-2.470796).
  7. Step 7:
    acc(θ+π)=1−acc(θ),acc(θ)=0  ⟺  θ∈(π2−1.8, π2−0.9)≈(−0.2292, 0.6708)∋0\mathrm{acc}(\theta+\pi)=1-\mathrm{acc}(\theta),\qquad\mathrm{acc}(\theta)=0\iff\theta\in\Big(\tfrac\pi2-1.8,\ \tfrac\pi2-0.9\Big)\approx(-0.2292,\ 0.6708)\ni0
    cos⁡(x+θ+π)=−cos⁡(x+θ)\cos(x+\theta+\pi)=-\cos(x+\theta) negates every output, so a correct prediction becomes wrong and vice versa (no output is 00 unless one already was). The all-wrong set is therefore the feasible set shifted by π\pi.

∎

Derivation

Checking particular weights

  1. Step 1:
    θ=0:f=(cos⁡0.3,cos⁡0.9,cos⁡1.8,cos⁡2.2)≈(0.9553, 0.6216, −0.2272, −0.5885),acc=0\theta=0:\quad f=(\cos0.3,\cos0.9,\cos1.8,\cos2.2)\approx(0.9553,\ 0.6216,\ -0.2272,\ -0.5885),\quad\mathrm{acc}=0
    Every sign is opposite to the label (−1,−1,+1,+1)(-1,-1,+1,+1): the initial circuit of the lab misclassifies all points, which triggers the lab's diagnostic.
  2. Step 2:
    θ=−2.9∈(−3.3708,−2.4708):f≈(−0.8569, −0.4161, 0.4536, 0.7648),acc=1\theta=-2.9\in(-3.3708,-2.4708):\quad f\approx(-0.8569,\ -0.4161,\ 0.4536,\ 0.7648),\quad\mathrm{acc}=1
    The lab's reference solution; the smallest margin is ∣f(0.9)∣≈0.416\lvert f(0.9)\rvert\approx0.416.
  3. Step 3:
    θ=π:f=−cos⁡x≈(−0.9553, −0.6216, 0.2272, 0.5885),acc=1\theta=\pi:\quad f=-\cos x\approx(-0.9553,\ -0.6216,\ 0.2272,\ 0.5885),\quad\mathrm{acc}=1
    The flipped initial model: shifting θ=0\theta=0 by π\pi negates every output.
  4. Step 4:
    θ=2.5∉(2.9124,3.8124):f≈(−0.9422, −0.9668, −0.4008, −0.0124),acc=12\theta=2.5\notin(2.9124,3.8124):\quad f\approx(-0.9422,\ -0.9668,\ -0.4008,\ -0.0124),\quad\mathrm{acc}=\tfrac12
    The rising boundary x↑∗=3π2−2.5≈2.212x^\ast_\uparrow=\frac{3\pi}2-2.5\approx2.212 lies to the right of both positive points, so both are predicted −1-1; note how close f(2.2)f(2.2) is to 00.
  5. Step 5:
    θ=2.8:f≈(−0.9991, −0.8481, −0.1122, 0.2837),acc=34\theta=2.8:\quad f\approx(-0.9991,\ -0.8481,\ -0.1122,\ 0.2837),\quad\mathrm{acc}=\tfrac34
    Now x↑∗≈1.912∈(1.8,2.2)x^\ast_\uparrow\approx1.912\in(1.8,2.2): the boundary separates the two positive points, and only x=1.8x=1.8 is misclassified.
  6. Step 6:
    θmid=−π2−1.35≈−2.920796,min⁡m∣fθmid(xm)∣=sin⁡0.45≈0.434966\theta_{\text{mid}}=-\tfrac\pi2-1.35\approx-2.920796,\qquad\min_m\lvert f_{\theta_{\text{mid}}}(x_m)\rvert=\sin0.45\approx0.434966
    The midpoint of the interval puts the boundary at x↑∗=1.35x^\ast_\uparrow=1.35, halfway between 0.90.9 and 1.81.8, which maximises the smallest margin: the two nearest points have ∣f∣=∣cos⁡(∓0.45−π2)∣=sin⁡0.45\lvert f\rvert=\lvert\cos(\mp0.45-\frac\pi2)\rvert=\sin0.45.

Result

acc(θ)=1  ⟺  θ∈(3π2−1.8, 3π2−0.9)=(2.912389, 3.812389)(mod2π)\boxed{\mathrm{acc}(\theta)=1\iff\theta\in\big(\tfrac{3\pi}2-1.8,\ \tfrac{3\pi}2-0.9\big)=(2.912389,\ 3.812389)\pmod{2\pi}}
Derivation

Closed-form minimiser of the hinge loss

  1. Step 1:
    Lhinge(θ)=1−14∑m=14ymcos⁡(xm+θ)=1−14 Re(eiθ∑mymeixm)L_{\text{hinge}}(\theta)=1-\frac14\sum_{m=1}^4y_m\cos(x_m+\theta)=1-\frac14\,\mathrm{Re}\Big(e^{i\theta}\sum_my_me^{ix_m}\Big)
    The hinge loss is linear in the outputs for a Pauli observable (loss section), and cos⁡(x+θ)=Re ei(x+θ)\cos(x+\theta)=\mathrm{Re}\,e^{i(x+\theta)}.
  2. Step 2:
    z=∑mymeixm=−e0.3i−e0.9i+e1.8i+e2.2i≈−2.392650+0.703497 i=Reiφz=\sum_my_me^{ix_m}=-e^{0.3i}-e^{0.9i}+e^{1.8i}+e^{2.2i}\approx-2.392650+0.703497\,i=Re^{i\varphi}
    Sum of the four unit phasors weighted by the labels.
  3. Step 3:
    R=∣z∣≈2.493929,φ=arg⁡z≈2.855627R=\lvert z\rvert\approx2.493929,\qquad\varphi=\arg z\approx2.855627
    Polar form; zz lies in the second quadrant.
  4. Step 4:
    Lhinge(θ)=1−R4cos⁡(θ+φ) ≥ 1−R4≈0.376518,θ∗=−φ≈−2.855627L_{\text{hinge}}(\theta)=1-\frac R4\cos(\theta+\varphi)\ \ge\ 1-\frac R4\approx0.376518,\qquad\theta^\ast=-\varphi\approx-2.855627
    Re(eiθReiφ)=Rcos⁡(θ+φ)\mathrm{Re}(e^{i\theta}Re^{i\varphi})=R\cos(\theta+\varphi) is maximal at θ=−φ\theta=-\varphi. The hinge minimiser lies inside the feasible interval (−3.3708,−2.4708)(-3.3708,-2.4708), so minimising the hinge loss solves the lab.
  5. Step 5:
    θMSE∗≈−2.889081,LMSE(θMSE∗)≈0.178713\theta^\ast_{\text{MSE}}\approx-2.889081,\qquad L_{\text{MSE}}(\theta^\ast_{\text{MSE}})\approx0.178713
    The MSE loss contains the extra θ\theta-dependent term 14∑mcos⁡2(xm+θ)\frac14\sum_m\cos^2(x_m+\theta) and has no equally simple closed form; its minimiser, found numerically and by the training loop of the code cell, is also feasible. The three natural choices (hinge, MSE, maximal margin) differ but all classify the data correctly.

Result

θhinge∗=−arg⁡(∑mymeixm)≈−2.855627∈(−π2−1.8, −π2−0.9)\boxed{\theta^\ast_{\text{hinge}}=-\arg\Big(\sum_my_me^{ix_m}\Big)\approx-2.855627\in\big(-\tfrac\pi2-1.8,\,-\tfrac\pi2-0.9\big)}

At θ=π\theta=\pi the pipeline classifies all four points correctly: f=−cos⁡xf=-\cos x is negative at 0.30.3 and 0.90.9 and positive at 1.81.8 and 2.22.2, and the MSE drops to 14[(1−0.955)2+(1−0.622)2+(1−0.227)2+(1−0.589)2]≈0.23\frac14\big[(1-0.955)^2+(1-0.622)^2+(1-0.227)^2+(1-0.589)^2\big]\approx0.23. Move θ\theta through the interval (2.912,3.812)(2.912,3.812) and observe that the accuracy stays 11 while the boundary moves between 0.90.9 and 1.81.8; outside it, at least one point changes sides. The NumPy cell below checks the matrix identities of this lesson, scans the accuracy on a grid of 1500015000 weights, compares the grid with the exact end points, and computes the shot statistics of the previous section from exact binomial sums.

Pythonrunnable
import numpy as np
from math import comb, ceil, exp, log

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

def rx(t):
    c, s = np.cos(t / 2), np.sin(t / 2)
    return np.array([[c, -1j * s], [-1j * s, c]])

Z = np.diag([1.0, -1.0]).astype(complex)
Xp = np.array([[0, 1], [1, 0]], dtype=complex)
ket0 = np.array([1, 0], dtype=complex)

def f(theta, x, enc=ry):
    psi = ry(theta) @ enc(x) @ ket0
    return float(np.vdot(psi, Z @ psi).real)

# (1) R_Y(t)R_Y(u) = R_Y(t + u), f = cos(x + theta) for R_Y encoding, f = cos(x)cos(theta) for R_X encoding.
grid = np.linspace(-np.pi, np.pi, 41)
add_ok = all(np.allclose(ry(t) @ ry(u), ry(t + u), atol=1e-12) for t in grid for u in grid)
ry_ok = all(abs(f(t, u) - np.cos(u + t)) < 1e-12 for t in grid for u in grid)
rx_ok = all(abs(f(t, u, rx) - np.cos(u) * np.cos(t)) < 1e-12 for t in grid for u in grid)
rx_gap = max(abs(f(t, u, rx) - np.cos(u + t)) for t in grid for u in grid)
print("RY(t) RY(u) = RY(t+u):", add_ok, "| RY encoding f = cos(x+theta):", ry_ok, "| RX encoding f = cos(x)cos(theta):", rx_ok)
print(f"RX encoding: max |f - cos(x+theta)| on the grid = {rx_gap:.6f}")

# (2) Heisenberg picture: W^dagger Z W = cos(theta) Z - sin(theta) X, so f = r(x) . n(theta).
heis_ok = all(np.allclose(ry(t).conj().T @ Z @ ry(t), np.cos(t) * Z - np.sin(t) * Xp, atol=1e-12) for t in grid)
print("W^dagger Z W = cos(theta) Z - sin(theta) X:", heis_ok)

# (3) The lab data: accuracy over a fine theta grid and the exact feasible interval.
xs = np.array([0.3, 0.9, 1.8, 2.2])
ys = np.array([-1, -1, 1, 1])
thetas = np.round(np.arange(-3.5, -2.0, 1e-4), 4)
acc = np.array([np.mean(np.cos(xs + t) * ys > 0) for t in thetas])
ok = thetas[acc == 1.0]
print(f"grid points with accuracy 1: from {ok.min():.4f} to {ok.max():.4f}")
print(f"exact interval: (-pi/2 - 1.8, -pi/2 - 0.9) = ({-np.pi / 2 - 1.8:.6f}, {-np.pi / 2 - 0.9:.6f})")
print(f"plus 2 pi: (3 pi/2 - 1.8, 3 pi/2 - 0.9) = ({1.5 * np.pi - 1.8:.6f}, {1.5 * np.pi - 0.9:.6f})")
for t in (0.0, 2.5, 2.8, -2.9, np.pi):
    print(f"theta = {t:7.4f}: f = {np.array2string(np.cos(xs + t), precision=4, floatmode='fixed')}, accuracy = {np.mean(np.cos(xs + t) * ys > 0):.2f}")

# (4) Shot noise of f_hat = (N_plus - N_minus)/N at x = 1.8, theta = -2.9 with N = 100 shots (exact binomial sums).
N, fx = 100, float(np.cos(1.8 - 2.9))
p = (1 + fx) / 2
pmf = [comb(N, k) * p ** k * (1 - p) ** (N - k) for k in range(N + 1)]
vals = [2 * k / N - 1 for k in range(N + 1)]
mean = sum(w * v for w, v in zip(pmf, vals))
var = sum(w * (v - mean) ** 2 for w, v in zip(pmf, vals))
p_wrong = sum(w for w, v in zip(pmf, vals) if v <= 0)
print(f"f = {fx:.6f}: E[f_hat] = {mean:.6f}, N Var[f_hat] = {N * var:.6f}, 1 - f^2 = {1 - fx ** 2:.6f}")
print(f"P(sign error) = {p_wrong:.3e} <= Hoeffding bound exp(-N f^2 / 2) = {exp(-N * fx ** 2 / 2):.3e}")
print(f"Hoeffding shots for eps = 0.01, delta = 0.05: N = ceil(2 ln(2/delta) / eps^2) = {ceil(2 * log(2 / 0.05) / 0.01 ** 2)}")

Expected output

RY(t) RY(u) = RY(t+u): True | RY encoding f = cos(x+theta): True | RX encoding f = cos(x)cos(theta): True
RX encoding: max |f - cos(x+theta)| on the grid = 1.000000
W^dagger Z W = cos(theta) Z - sin(theta) X: True
grid points with accuracy 1: from -3.3707 to -2.4708
exact interval: (-pi/2 - 1.8, -pi/2 - 0.9) = (-3.370796, -2.470796)
plus 2 pi: (3 pi/2 - 1.8, 3 pi/2 - 0.9) = (2.912389, 3.812389)
theta =  0.0000: f = [ 0.9553  0.6216 -0.2272 -0.5885], accuracy = 0.00
theta =  2.5000: f = [-0.9422 -0.9668 -0.4008 -0.0124], accuracy = 0.50
theta =  2.8000: f = [-0.9991 -0.8481 -0.1122  0.2837], accuracy = 0.75
theta = -2.9000: f = [-0.8569 -0.4161  0.4536  0.7648], accuracy = 1.00
theta =  3.1416: f = [-0.9553 -0.6216  0.2272  0.5885], accuracy = 1.00
f = 0.453596: E[f_hat] = 0.453596, N Var[f_hat] = 0.794251, 1 - f^2 = 0.794251
P(sign error) = 1.239e-06 <= Hoeffding bound exp(-N f^2 / 2) = 3.406e-05
Hoeffding shots for eps = 0.01, delta = 0.05: N = ceil(2 ln(2/delta) / eps^2) = 73778

Quick check

What is the length of the interval of weights θ\theta (within one period) for which the worked model classifies all four lab points correctly?

tolerance ±0.000001

References

  1. K. Mitarai, M. Negoro, M. Kitagawa and K. Fujii, Quantum circuit learning, Physical Review A 98, 032309 (2018).doi:10.1103/PhysRevA.98.032309
  2. E. Farhi and H. Neven, Classification with quantum neural networks on near term processors, arXiv:1802.06002 (2018).arxiv.org
  3. M. Schuld, A. Bocharov, K. M. Svore and N. Wiebe, Circuit-centric quantum classifiers, Physical Review A 101, 032308 (2020).doi:10.1103/PhysRevA.101.032308
  4. V. Havlíček, A. D. Córcoles, K. Temme, A. W. Harrow, A. Kandala, J. M. Chow and J. M. Gambetta, Supervised learning with quantum-enhanced feature spaces, Nature 567, 209–212 (2019).doi:10.1038/s41586-019-0980-2
  5. M. Benedetti, E. Lloyd, S. Sack and M. Fiorentini, Parameterized quantum circuits as machine learning models, Quantum Science and Technology 4, 043001 (2019).doi:10.1088/2058-9565/ab4eb5
  6. M. Cerezo, A. Arrasmith, R. Babbush, S. C. Benjamin, S. Endo, K. Fujii, J. R. McClean, K. Mitarai, X. Yuan, L. Cincio and P. J. Coles, Variational quantum algorithms, Nature Reviews Physics 3, 625–644 (2021).doi:10.1038/s42254-021-00348-9
  7. M. Schuld, Supervised quantum machine learning models are kernel methods, arXiv:2101.11020 (2021).arxiv.org
  8. 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
  9. J. C. Spall, Multivariate stochastic approximation using a simultaneous perturbation gradient approximation, IEEE Transactions on Automatic Control 37(3), 332–341 (1992).doi:10.1109/9.119632
  10. W. Hoeffding, Probability inequalities for sums of bounded random variables, Journal of the American Statistical Association 58(301), 13–30 (1963).doi:10.1080/01621459.1963.10500830

Hands-on labs

  • Train a one-qubit variational classifier

    1 step · auto-graded

    The circuit RY(θ)RY(x)∣0⟩R_Y(\theta)R_Y(x)\lvert0\rangle with observable ZZ is the smallest variational quantum classifier: feature map U(x)=RY(x)U(x)=R_Y(x), ansatz W(θ)=RY(θ)W(\theta)=R_Y(\theta), model output fθ(x)=⟨Z⟩=cos⁡(x+θ)f_\theta(x)=\langle Z\rangle=\cos(x+\theta) and prediction y^=sign fθ(x)\hat y=\mathrm{sign}\,f_\theta(x). Choose the single weight θ\theta so that all four training points are classified correctly.

    Open lab