Skip to content
Kartikey Purohit
← Course overview

Day 5 — Sampler & Results

Math Prep

Solve this BEFORE lesson.md. SamplerV2 is a probability machine: it samples bitstrings from P(x)=αx2P(x) = |\alpha_x|^2. 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 nn-qubit state defines a distribution over 2n2^n bitstrings: P(x)=αx2P(x) = |\alpha_x|^2, xP(x)=1\sum_x P(x) = 1. The Sampler never returns amplitudes — only samples. Two different states can induce the SAME distribution (+|+\rangle and |-\rangle, 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

NN shots ⇒ counts {x:nx}\{x: n_x\}. Estimated probability p^x=nx/N\hat p_x = n_x / N.

  • Expected count: E[nx]=Npx\mathbb{E}[n_x] = N p_x; fluctuation Npx(1px)\sim \sqrt{N p_x(1-p_x)} (binomial).
  • Bell state, 1000 shots: expect {'00': ~500, '11': ~500} with typical wobble ±10001212±16\pm\sqrt{1000\cdot\tfrac12\cdot\tfrac12} \approx \pm16. Seeing {'00': 503, '11': 497} is normal; seeing '01': 200 means your circuit (or noise) is wrong, not statistics.
  • A never-appearing bitstring is evidence its probability is 1/N\lesssim 1/N, not proof it's zero.

3. Marginal and joint distributions

Marginalizing = ignoring some bits and summing probabilities over them. If P(q1q0)P(q_1 q_0) has P(00)=P(01)=P(11)=13P(00) = P(01) = P(11) = \tfrac13:

P(q0=1)=P(01)+P(11)=23,P(q1=1)=P(11)=13P(q_0{=}1) = P(01) + P(11) = \tfrac23,\qquad P(q_1{=}1) = P(11) = \tfrac13

Independence check: P(q1q0)P(q1)P(q0)P(q_1 q_0) \ne P(q_1)P(q_0) here ⇒ correlated bits. For the Bell distribution {00:12,11:12}\{00: \tfrac12, 11: \tfrac12\}: 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: P(q1=1q0=1)=P(11)P(q0=1)P(q_1{=}1 \mid q_0{=}1) = \frac{P(11)}{P(q_0{=}1)}. For Bell: =1/21/2=1= \frac{1/2}{1/2} = 1.

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: d(P,Q)=12xP(x)Q(x)[0,1]d(P,Q) = \tfrac12\sum_x |P(x) - Q(x)| \in [0,1]. 0 = identical, 1 = disjoint support.
  • Hellinger fidelity (used by hellinger_fidelity): F(P,Q)=(xP(x)Q(x))2F(P,Q) = \big(\sum_x \sqrt{P(x)Q(x)}\big)^2. 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)

σ(p^)=p(1p)/N12N\sigma(\hat p) = \sqrt{p(1-p)/N} \le \tfrac{1}{2\sqrt N}
Shots NN worst-case σ
100 5%
1 024 ~1.6%
4 096 ~0.8%
10 000 0.5%

Halving error costs 4× shots (error 1/N\propto 1/\sqrt N). Default shots in most V2 primitives: 4096-class values — enough for ~1% resolution.


PROBLEMS

P1. State: ψ=1200+12101211|\psi\rangle = \tfrac{1}{\sqrt2}|00\rangle + \tfrac12|10\rangle - \tfrac12|11\rangle (labels q1q0|q_1q_0\rangle). Give the full outcome distribution, then both marginals P(q0)P(q_0), P(q1)P(q_1). 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 12(000+111)\tfrac{1}{\sqrt2}(|000\rangle + |111\rangle): 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 p=0.50p = 0.50 from p=0.51p = 0.51 reliably (3σ separation). Roughly how many shots?


ANSWERS

A1. P(00)=12P(00) = \tfrac12, P(10)=14P(10) = \tfrac14, P(11)=14P(11) = \tfrac14 (the minus sign dies in 2|\cdot|^2 — phases are invisible to sampling). Marginals: P(q0=0)=12+14=34P(q_0{=}0) = \tfrac12 + \tfrac14 = \tfrac34, P(q0=1)=14P(q_0{=}1) = \tfrac14; P(q1=0)=12P(q_1{=}0) = \tfrac12, P(q1=1)=12P(q_1{=}1) = \tfrac12. Independence would need P(00)=P(q1=0)P(q0=0)=3812P(00) = P(q_1{=}0)P(q_0{=}0) = \tfrac38 \ne \tfrac12correlated.

A2. Expected: '00': 2000, '10': 1000, '11': 1000. Fluctuation for '00': 4000121232\sqrt{4000 \cdot \tfrac12 \cdot \tfrac12} \approx 32.

A3. Marginal over q2: outcomes '00' and '11' (of q1q0q_1q_0) each with prob 12\tfrac12 — 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: 12(0.50.25+00.25+00.25+0.50.25)=0.5\tfrac12(|0.5{-}0.25| + |0{-}0.25| + |0{-}0.25| + |0.5{-}0.25|) = 0.5. Hellinger: (0.50.25+0+0+0.50.25)2=(218)2=48=0.5\big(\sqrt{0.5\cdot0.25} + 0 + 0 + \sqrt{0.5\cdot0.25}\big)^2 = (2\cdot\tfrac{1}{\sqrt8})^2 = \tfrac{4}{8} = 0.5.

A5. c0 is 0 or 1 with prob ½ each. If c0=0: q1 stays 0|0\rangle ⇒ c1=0. If c0=1: X fires ⇒ c1=1. Joint: P(c1c0=00)=12P(c_1c_0{=}00) = \tfrac12, P(11)=12P(11) = \tfrac12 — 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 3σ0.013\sigma \le 0.01 with σ12N\sigma \approx \tfrac{1}{2\sqrt N}32N0.01\tfrac{3}{2\sqrt N} \le 0.01N150\sqrt N \ge 150N22500N \gtrsim 22\,500 shots. (Order of magnitude is what matters: ~10410^410510^5.)


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.