Day 5 — Sampler & Results
Math Prep
Solve this BEFORE lesson.md. SamplerV2 is a probability machine: it samples bitstrings from . Day 5's API (counts, BitArray, get_counts, analysis) is bookkeeping on top of the probability theory below.
Requires: Day 1 + Day 4 math prep. Video back-up: Watrous Lesson 01–02 (measurement sections); notes in resource-artifactory/notes/video-notes/.
1. From statevector to distribution
An -qubit state defines a distribution over bitstrings: , . The Sampler never returns amplitudes — only samples. Two different states can induce the SAME distribution ( and , or any state vs its phase-tweaked cousin): sampling in one basis loses all phase information.
Bitstring convention (drill it again): key 'q_{n-1} … q_1 q_0' — qubit 0 rightmost. The integer value of the bitstring = the statevector index.
2. Counts, frequencies, probabilities
shots ⇒ counts . Estimated probability .
- Expected count: ; fluctuation (binomial).
- Bell state, 1000 shots: expect
{'00': ~500, '11': ~500}with typical wobble . Seeing{'00': 503, '11': 497}is normal; seeing'01': 200means your circuit (or noise) is wrong, not statistics. - A never-appearing bitstring is evidence its probability is , not proof it's zero.
3. Marginal and joint distributions
Marginalizing = ignoring some bits and summing probabilities over them. If has :
Independence check: here ⇒ correlated bits. For the Bell distribution : each marginal is uniform, but the joint is maximally correlated — knowing one bit determines the other. (API hook: BitArray slicing / marginal_counts compute exactly these sums.)
Conditional probability: . For Bell: .
4. Classical registers and multiple measurements
With several classical registers, the joint outcome is a tuple of bitstrings — mathematically still one joint distribution, keyed per register (result[0].data.<regname>). Mid-circuit measurement gives a sequential sample: first outcome ~ its marginal, state collapses, second outcome ~ the conditional distribution given the first. Deferred measurement (Day 2 §6) guarantees the joint distribution matches the measure-at-the-end circuit.
5. Comparing distributions
Two tools you'll meet in analysis questions:
- Total variation / trace distance between distributions: . 0 = identical, 1 = disjoint support.
- Hellinger fidelity (used by
hellinger_fidelity): . 1 = identical. For ideal-vs-noisy Bell counts this is the standard "how good was my run" number.
6. Sampling error vs shot budget (the recurring exam numbers)
| Shots | worst-case σ |
|---|---|
| 100 | 5% |
| 1 024 | ~1.6% |
| 4 096 | ~0.8% |
| 10 000 | 0.5% |
Halving error costs 4× shots (error ). Default shots in most V2 primitives: 4096-class values — enough for ~1% resolution.
PROBLEMS
P1. State: (labels ). Give the full outcome distribution, then both marginals , . Are the bits independent?
P2. For P1's state: 4000 shots are run. Expected counts per outcome, and typical fluctuation size for the '00' count?
P3. GHZ state : what distribution do you get if you sample only qubits 0 and 1 (marginalize q2)? Are q0, q1 independent in that marginal?
P4. Distribution A = {'00': 0.5, '11': 0.5}; distribution B = {'00': 0.25, '01': 0.25, '10': 0.25, '11': 0.25}. Compute the total variation distance and the Hellinger fidelity.
P5. After h(0) and measuring q0 into c0, the circuit applies x(1) only if c0 = 1, then measures q1 into c1. Give the joint distribution of (c1, c0).
P6. A sampler run of a supposed Bell circuit returns {'00': 480, '11': 470, '01': 30, '10': 20} at 1000 shots. Statistical fluke or systematic error? Justify with a quick σ calculation.
P7. You need to distinguish from reliably (3σ separation). Roughly how many shots?
ANSWERS
A1. , , (the minus sign dies in — phases are invisible to sampling). Marginals: , ; , . Independence would need ⇒ correlated.
A2. Expected: '00': 2000, '10': 1000, '11': 1000. Fluctuation for '00': .
A3. Marginal over q2: outcomes '00' and '11' (of ) each with prob — a Bell-like distribution. NOT independent: the marginal is still perfectly correlated (knowing q0 fixes q1). GHZ hides its 3-way correlation in every pairwise marginal.
A4. TV distance: . Hellinger: .
A5. c0 is 0 or 1 with prob ½ each. If c0=0: q1 stays ⇒ c1=0. If c0=1: X fires ⇒ c1=1. Joint: , — a perfectly correlated distribution created by feed-forward (this is the deferred-measurement picture of CNOT).
A6. Systematic. Wrong-parity outcomes total 50/1000 = 5%. Under the ideal distribution their probability is 0, and a true-0 event has σ ≈ 0 — 50 counts can't be a fluctuation. This is hardware noise (readout/gate error), the Day-6 topic.
A7. Need with ⇒ ⇒ ⇒ shots. (Order of magnitude is what matters: ~–.)
Self-check: you can turn any small statevector into its distribution instantly, marginalize and condition it, size up statistical fluctuations, and explain why sampling can't see phases.