|q⟩ Bad Qubits

advanced · Programming · Quantum Machine Learning (Advanced)

Benchmarking QML Models

A QML model is only as credible as its evaluation. The headline metric for a classifier is accuracy, but computing it correctly — and against a fair baseline — is where many quantum claims quietly fall apart. This lesson nails down the mechanics and the honest comparison.

From expectation to label

A variational classifier outputs an expectation value f(x)=Z[1,1]f(\boldsymbol{x}) = \langle Z\rangle \in [-1,1]. To get a discrete prediction we threshold at zero:

y^(x)=signf(x)={+1f(x)0,1f(x)<0.\hat{y}(\boldsymbol{x}) = \operatorname{sign} f(\boldsymbol{x}) = \begin{cases} +1 & f(\boldsymbol{x}) \ge 0,\\ -1 & f(\boldsymbol{x}) < 0.\end{cases}

With labels y{1,+1}y\in\{-1,+1\}, accuracy on a dataset of NN points is the fraction predicted correctly:

accuracy=1Ni=1N1[y^(xi)=yi].\text{accuracy} = \frac{1}{N}\sum_{i=1}^{N} \mathbb{1}\bigl[\hat{y}(\boldsymbol{x}_i) = y_i\bigr].

Worked numbers

Suppose a trained model produces these raw outputs on a five-point test set, with the listed true labels:

| ii | f(xi)f(\boldsymbol{x}_i) | y^i=signf\hat{y}_i = \operatorname{sign} f | yiy_i | correct? | | --- | --- | --- | --- | --- | | 1 | +0.82+0.82 | +1+1 | +1+1 | yes | | 2 | 0.30-0.30 | 1-1 | 1-1 | yes | | 3 | +0.10+0.10 | +1+1 | 1-1 | no | | 4 | 0.65-0.65 | 1-1 | 1-1 | yes | | 5 | +0.45+0.45 | +1+1 | +1+1 | yes |

Four of five are correct, so accuracy =4/5=0.8= 4/5 = 0.8.

Beyond a single number

Accuracy alone hides important behavior — round out a benchmark with:

Try it

Compute the test accuracy for the model outputs and labels in the table. You are given the raw outputs and the true labels; threshold at zero, count correct predictions, and return the accuracy as a number. Do not hardcode 0.8 — derive it from the arrays so the procedure is what is graded.

Run your code to see the quantum state.

Sign in on the full site to ask questions and join the discussion.