This guide walks you through Stim’s core workflow: installing the package, defining a circuit, compiling a sampler, collecting measurement shots, annotating the circuit with detectors, and sampling detection events. All examples use Python and real Stim API calls. By the end you will have a working foundation for exploring quantum error correction simulations.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.
Install Stim
Install Stim from PyPI. Stim requires Python 3.6 or later and pulls in NumPy automatically.Verify the installation:
Create a circuit
Circuits in Stim are instances of Printing the circuit’s You can also visualize the circuit as an ASCII diagram:
stim.Circuit. You can build one programmatically by calling circuit.append(gate_name, targets), or you can pass the entire circuit as a string using Stim’s circuit file syntax.The example below prepares a Bell pair and then measures both qubits in the Z basis:repr shows it in Stim’s circuit file syntax:Compile a sampler and collect shots
Call Notice that each row’s two values always agree — the Bell state measurement results are perfectly correlated, as expected.
circuit.compile_sampler() to produce a compiled sampler object, then call .sample(shots=N) to collect N measurement shots. The returned array has shape (shots, num_measurements) with bool values.Add detector annotations
A
DETECTOR annotation asserts that a particular parity of measurements is consistent from shot to shot under noiseless execution. You reference past measurements using stim.target_rec(offset), where rec[-1] is the most recent measurement.The Bell circuit’s two measurements should always have the same value, so you can annotate their parity as a detector:Compile a detector sampler and sample detection events
Use Now rebuild the circuit with Pauli noise inserted using Stim’s circuit string syntax, and watch detectors fire:
circuit.compile_detector_sampler() to create a detector sampler. Calling .sample(shots=N) returns an array of shape (shots, num_detectors) — True means the detector fired (a detection event occurred), False means it did not.Without noise, detectors never fire:True values appear because the X_ERROR(0.2) channel flips each qubit independently with 20% probability, causing the two measurements to disagree roughly 32% of the time. You can estimate the detection fraction over many shots:Next steps
Circuits in depth
Learn how Stim circuits are structured, how detectors and observables work, and how to use REPEAT blocks.
Sampling guide
Explore measurement sampling, detector sampling, and bulk data collection workflows in detail.
Detector error models
Convert annotated noisy circuits into Tanner graphs for configuring matching decoders.
Python API — Circuit
Full reference for
stim.Circuit, including all sampler methods and circuit-level operations.