Measurement and finite sampling¶
Read amplitudes and probabilities first. Here the quantum circuit is fixed: a Y rotation by π/3 gives an ideal probability p = 1/4 of measuring 1. We vary only the number of shots and the sampling seed.
For independent binary outcomes, the frequency p̂ = count(1)/N estimates p. Its standard deviation is sqrt(p(1−p)/N). This is the sampling standard error, not a bound that every individual run must satisfy. Quadrupling N halves it.
"""Sample one fixed circuit with several shot budgets and random seeds."""
from __future__ import annotations
import json
from math import pi, sqrt
from cascaqit import Circuit
def experiment() -> dict[str, object]:
circuit = Circuit(1).ry(pi / 3, 0).measure_all()
rows = []
for shots in (32, 128, 1024):
frequencies = []
for seed in (11, 22, 33, 44):
result = circuit.run(shots=shots, seed=seed)
frequencies.append(result.counts.get("1", 0) / shots)
rows.append(
{
"shots": shots,
"frequencies": frequencies,
"binomial_standard_error": sqrt(0.25 * 0.75 / shots),
}
)
return {"ideal_probability_one": 0.25, "seeds": [11, 22, 33, 44], "runs": rows}
if __name__ == "__main__":
print(json.dumps(experiment(), sort_keys=True))
python examples/learning/foundations/measurement_sampling.py
{
"ideal_probability_one": 0.25,
"runs": [
{
"binomial_standard_error": 0.07654655446197431,
"frequencies": [
0.1875,
0.375,
0.25,
0.1875
],
"shots": 32
},
{
"binomial_standard_error": 0.038273277230987154,
"frequencies": [
0.265625,
0.265625,
0.234375,
0.203125
],
"shots": 128
},
{
"binomial_standard_error": 0.013531646934131853,
"frequencies": [
0.2548828125,
0.251953125,
0.271484375,
0.2412109375
],
"shots": 1024
}
],
"seeds": [
11,
22,
33,
44
]
}
Compare the four frequencies at each shot budget. They differ even though the ideal probability stays at 0.25. The standard error printed here uses the known ideal probability. In an experiment where p is unknown, estimating uncertainty from observed data requires an appropriate statistical method, especially near zero or one.
A larger budget reduces the distribution's spread over repeated runs. It need not move every single result closer to 0.25. Changing the seed changes the sample, not the state prepared by this ideal circuit. In noisy trajectory simulation a seed can also affect the simulated noise path; that is a separate source of randomness.
Exercise: to reduce the standard error by a factor of ten without changing p, how many times more shots are needed? Test with additional seeds rather than selecting the one run that best matches theory.
Answer
One hundred times as many shots. The error scales as the inverse square root of N, not as 1/N. A run with zero observed ones also does not prove that their probability is zero.
Next: gates, bit order and coherence.
Concept reference: sampling and expectations.