Stim provides two primary sampling paths for noisy stabilizer circuits: measurement sampling, which returns raw qubit measurement results, and detection event sampling, which returns the XOR-compared detector outcomes that decoders actually consume. Both paths compile a circuit once and then produce many shots efficiently using a frame-simulator under the hood.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/quantumlib/Stim/llms.txt
Use this file to discover all available pages before exploring further.
Measurement sampling
circuit.compile_sampler() compiles the circuit into a CompiledMeasurementSampler. Calling sampler.sample(shots) returns a numpy boolean array of shape (shots, num_measurements), where result[s, m] is the outcome of measurement m in shot s.
The sampler internally XORs a noiseless reference sample (obtained by running the circuit without noise) into the frame-simulator’s flip output. This ensures the returned values are true measurement outcomes, not just flip indicators. You can skip the reference-sample step with
skip_reference_sample=True if you know the all-zeros result is valid for your circuit.Writing measurements to a file
For large datasets that don’t fit comfortably in memory,sample_write streams directly to disk:
Detection event sampling
Detection events are what most decoders expect: a1 in position d means detector d fired (its parity differed from the noiseless expectation). circuit.compile_detector_sampler() compiles a CompiledDetectorSampler from a circuit that contains DETECTOR and OBSERVABLE_INCLUDE annotations.
Writing detection events to a file
Converting measurements to detection events
If you already have a file of raw measurements (for example, from real hardware or a different simulator), usecircuit.compile_m2d_converter() to obtain a CompiledMeasurementsToDetectionEventsConverter:
Output formats
Bothsample_write and sample_write for the detection sampler accept a format argument:
"01" — ASCII text (default)
"01" — ASCII text (default)
One character per measurement per shot, newline-separated.
0 = False, 1 = True. Human-readable but large on disk."b8" — bit-packed binary
"b8" — bit-packed binary
8 measurements packed per byte, little-endian. Compact; good for large shot counts when streaming to disk.
"ptb64" — transposed bit-packed, 64-bit words
"ptb64" — transposed bit-packed, 64-bit words
Transposes the shot/measurement matrix before bit-packing. Efficient for SIMD decoding pipelines that process all shots for one detector at a time.
"hits" — sparse hit list
"hits" — sparse hit list
Lists only the indices of
True measurements on each line. Compact when error rates are low."dets" — detection event list
"dets" — detection event list
Like
hits but labels entries with D (detector) or L (logical observable). Readable format for detection events."r8" — run-length encoded
"r8" — run-length encoded
Encodes distances between consecutive
True measurements. Compact for very sparse data.Reference samples
circuit.reference_sample() returns a noiseless, deterministic measurement result by discarding all noise operations and biasing all collapse events toward +Z:
compile_sampler and compile_m2d_converter as the baseline against which noisy samples are compared. You can also pass a pre-computed reference sample directly to compile_sampler(reference_sample=...) to avoid recomputing it.