It is blazingly fast to sample a lot of shots from a single circuit with stim. However, I want to sample a singe shot for lots of different circuits(circuit strings), is there any optimized way to do this instead of calling a plain for loop?

  • $\begingroup$ What use case do you have where you only want one sample from many circuits? $\endgroup$ Commented Sep 16, 2023 at 19:27
  • $\begingroup$ I'm looking into the Pauli+ simulator used in Google's surface code experiment. It seems like I can implement that with dynamically generating Clifford circuits conditioned on leakage status tracking. $\endgroup$
    – Inm
    Commented Sep 17, 2023 at 6:31

1 Answer 1


Making a stim.TableauSimulator and running the circuit once is cheaper than compiling a sampler and taking only one shot, due to avoiding redundant work preparing for bulk sampling:

from typing import List
import stim
def single_sample(circuit: stim.Circuit) -> List[bool]:
    sim = stim.TableauSimulator()
    return sim.current_measurement_record()

I tested this on a distance 35 surface code with 20 rounds and it was about twice as fast as compiling a sampler to take one shot (an eighth of a second instead of a quarter of a second).

If your circuits are noiseless and you want any sample instead of random samples then you should use stim.Circuit.reference_sample(bit_packed=True).

If the circuits only differ by Pauli gates then you can use "sweep bits" to condition controlled Pauli gates (like CX sweep[0] 5). You can specify different sweep bits for different shots, and they run as fast as running the same circuit. Though, at the moment, it's an open issue to allow specifying the sweep bits from the python API. So you'd have to use the C++ API for this one.

If you know for sure that 000..000 is a valid noiseless sample for all your circuits then you can use stim.Circuit.compile_sampler(skip_reference_sample=True) to skip the tableau simulation and go straight to fast sampling. There's also stim.Circuit.compile_sampler(reference_sample=...) for cases where you know a valid noiseless sample but it's not all-zeros. You can also use skip_reference_sample=True if it's acceptable to only find out if measurements were flipped rather than the actual measurement results. This is more useful if you have enormous circuits rather than small circuits, where the dominant cost is in getting the reference sample rather than allocating memory for the bulk sampling.

If you're doing error correction circuits and have annotated detectors/observables then you probably want to use stim.Circuit.compile_detector_sampler(). The detector sampler is much cheaper to compile than the measurement sampler because it doesn't need the reference sample.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.