Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Level 7 · Quantum Causal AI research · Prerequisite lessons: L5-09 (Entropy and quantum tomography), L7-06 (Sample complexity and resource accounting), L7-07 (Classical shadows and observable structure).

What you’ll learn

  • How random Pauli measurements produce a classical shadow: a cheap classical record from which you can later estimate many expectation values Huang et al., 2020.

  • Why the single-shot estimator for a weight-kk Pauli observable has variance 3k−⟨P⟩23^k-\langle P\rangle^2. Local observables are therefore cheap, and global ones are expensive.

  • How the estimation error falls like 1/N1/\sqrt N, and what median-of-means does.

Open in Colab

1 · One qubit: estimate ⟨X⟩,⟨Y⟩,⟨Z⟩\langle X\rangle,\langle Y\rangle,\langle Z\rangle from the same shots

In each shot, pick a basis XX, YY, or ZZ uniformly at random, measure, and record the outcome b=±1b=\pm1. The single-shot estimator of ⟨P⟩\langle P\rangle is

o^P=3 b⋅1[basis=P].\hat o_P = 3\,b\cdot\mathbb 1[\text{basis}=P].

It is unbiased: E[o^P]=13⋅3 ⟨P⟩=⟨P⟩\mathbb E[\hat o_P]=\tfrac13\cdot3\,\langle P\rangle=\langle P\rangle. The engine’s classical_shadow_z implements this for ZZ. Below we write a three-observable version that consumes the random numbers in the same order. We then check that its ZZ estimate is identical to the engine’s.

truth  <X>,<Y>,<Z> = [0.7355 0.5032 0.4536]
shadow estimates    = [0.72   0.525  0.4698] after 5000 shots
empirical single-shot variances: [2.394  2.7574 2.8339]  theory 3 - <P>^2: [2.459  2.7468 2.7943]
Loading...

Explain. The same random record estimates all three expectation values. You could even choose the observables after collecting the data. Measuring only ZZ would give ⟨Z⟩\langle Z\rangle with variance 1−⟨Z⟩21-\langle Z\rangle^2 instead of 3−⟨Z⟩23-\langle Z\rangle^2. But it would say nothing about ⟨X⟩\langle X\rangle or ⟨Y⟩\langle Y\rangle. The factor 3 is the price of not committing in advance.

2 · Several qubits and the 3k3^k cost

On nn qubits, each shot measures every qubit in an independently random Pauli basis. Take a Pauli string PP that acts non-trivially on a set SS of kk qubits (its weight). Then

o^P=∏j∈S3 bj 1[basisj=Pj].\hat o_P=\prod_{j\in S}3\,b_j\,\mathbb 1[\text{basis}_j=P_j] .

All kk bases must match, which happens with probability 3−k3^{-k}. When they do, the estimator is ±3k\pm3^k. So E[o^P2]=3k\mathbb E[\hat o_P^2]=3^k and Var⁡o^P=3k−⟨P⟩2\operatorname{Var}\hat o_P=3^k-\langle P\rangle^2. The code checks unbiasedness and this second moment exactly. It sums over all 3n3^n basis choices and 2n2^n outcomes, for every one of the 4n−14^n-1 non-identity Pauli strings of a random 4-qubit state.

exact unbiasedness and E[o^2] = 3^k verified for all 255 Pauli strings on 4 qubits

Now we sample. We draw 200 independent experiments of up to 20 000 shots each. For one observable of each weight k=1,…,4k=1,\dots,4, we compute the root-mean-square error. The dotted lines are the exact predictions (3k−⟨P⟩2)/N\sqrt{(3^k-\langle P\rangle^2)/N}. Use the buttons to show one weight at a time.

weight 1: <P> = +0.411, fitted log-log slope -0.494, RMSE at N=20000: 0.0126
weight 2: <P> = +0.135, fitted log-log slope -0.497, RMSE at N=20000: 0.0222
weight 3: <P> = +0.015, fitted log-log slope -0.537, RMSE at N=20000: 0.0332
weight 4: <P> = +0.003, fitted log-log slope -0.486, RMSE at N=20000: 0.0645
RMSE ratio weight-4 / weight-1 at N = 20000: 5.13  (theory 5.35)
Loading...

Explain. Every curve has slope −1/2-1/2 on the log-log plot, so the error falls like 1/N1/\sqrt N. The vertical offset grows with the weight, because the variance is 3k−⟨P⟩23^k-\langle P\rangle^2. With random single-qubit Pauli measurements, low-weight (local) observables are cheap and high-weight ones become expensive. Huang, Kueng, and Preskill show a more general result Huang et al., 2020. Order log⁡M\log M measurements suffice to predict MM properties when each has a bounded shadow norm, and the number of measurements needed does not grow with system size. For random Pauli measurements, the weight-kk cost computed here is what enters that bound.

3 · Many observables at once, and median-of-means

The same shots estimate all Pauli strings. A worst case over many observables is governed by the rare large deviations of each estimator. Median-of-means splits the NN shots into KK groups and averages within each group. It then reports the median of the KK group means. Huang, Kueng, and Preskill use this device to obtain their logarithmic dependence on the number of observables Huang et al., 2020.

Below we take every Pauli string of weight ≤2\le 2 (there are (41)3+(42)9=66\binom41 3+\binom42 9=66). In each of the 200 experiments, we record the largest error across these observables. We do this with the plain mean and with median-of-means using K=10K=10 groups.

N = 2000: worst error over 66 observables
  plain mean       : median 0.1680, 95th percentile 0.2150
  median-of-means  : median 0.1984, 95th percentile 0.2785
<Figure size 800x380 with 1 Axes>

Explain. One data set of N=2000N=2000 shots estimates all 66 low-weight Pauli expectation values at once. The code asserts that the worst of the 66 errors stays below 0.3 in 95% of experiments for both estimators.

In this simulation median-of-means did not help. Its worst-case errors were somewhat larger than those of the plain mean (see the printed quantiles; the code asserts the direction). The estimators here are bounded (at most ±9\pm 9 for weight 2), and each group holds only N/K=200N/K=200 shots. So the median of a few noisy group means loses efficiency. What median-of-means offers is a provable exponential tail bound that needs only a finite variance. That matters when the number of observables is very large or the estimators are heavy-tailed, not necessarily at this scale.

Going deeper: why “3 ∣b⟩⟨b∣−I3\,|b\rangle\langle b| - I”?

Take a single random-Pauli measurement on one qubit. Averaged over the random basis UU and outcome bb, it implements the channel M(ρ)=E[U†∣b⟩⟨b∣U]=13ρ+13I\mathcal M(\rho)=\mathbb E\big[U^\dagger|b\rangle\langle b|U\big]=\tfrac13\rho+\tfrac13 I (for Tr⁡ρ=1\operatorname{Tr}\rho=1). Inverting this channel gives the snapshot ρ^=M−1(U†∣b⟩⟨b∣U)=3 U†∣b⟩⟨b∣U−I\hat\rho=\mathcal M^{-1}\big(U^\dagger|b\rangle\langle b|U\big)=3\,U^\dagger|b\rangle\langle b|U-I, with E[ρ^]=ρ\mathbb E[\hat\rho]=\rho. Then Tr⁡(Pρ^)\operatorname{Tr}(P\hat\rho) is exactly the estimator o^P\hat o_P used above. Snapshots are not physical states; they have a negative eigenvalue. Only their average is a state.

measurement channel M(rho) = rho/3 + I/3 and E[snapshot] = rho verified exactly

Exercises

Solution

Var⁡=3−0.36=2.64\operatorname{Var}=3-0.36=2.64. Standard error 2.64/N≤0.01\sqrt{2.64/N}\le0.01 requires N≥26 400N\ge 26\,400. Measuring ZZ directly would need N≥(1−0.36)/10−4=6400N\ge(1-0.36)/10^{-4}=6400.

Solution

(102)⋅32=405\binom{10}{2}\cdot3^2=405 strings. Each has variance 32−⟨P⟩2≤93^2-\langle P\rangle^2\le 9, regardless of the number of qubits.

Solution

Its variance is 3n−⟨P⟩23^n-\langle P\rangle^2, so the number of shots for fixed precision grows like 3n3^n. Only a fraction 3−n3^{-n} of shots have all bases matching. Global observables, such as fidelity with an entangled state, need other measurement ensembles. The cited work uses random Clifford measurements for this.

Check yourself

All checks passed.

Limits of this model

The systems are small. Four qubits allow exact checks, but shadows are meant for large systems, where exact simulation is impossible.

The measurements are perfect. Readout and gate errors bias the snapshots unless they are characterized and inverted.

We used only the Pauli ensemble. Random Clifford measurements change which observables are cheap. We did not simulate them.

The randomness is seeded. Basis choices and outcomes come from a fixed seed, so the page is reproducible. Real experiments need genuinely independent basis choices.

We make no advantage claim. For many properties, classical shadows reduce measurement cost relative to full tomography. This lab claims nothing beyond what the cited paper proves.

References
  1. Huang, H.-Y., Kueng, R., & Preskill, J. (2020). Predicting Many Properties of a Quantum System from Very Few Measurements. Nature Physics, 16(10), 1050–1057. 10.1038/s41567-020-0932-7