What is the fastest and most resource-efficient way to create an LUT decoder for a quantum error correction code using Stim (https://quantum-journal.org/papers/q-2021-07-06-497/)?

Specfically, I am running simulations of a steane QECC using several conditional loops (this was addressed in the question Simulating flag qubits and conditional branches using Stim).

Is there an efficient way to create the relevant LUTs as well using the TableauSimulator?


1 Answer 1


The simplest thing to do would be to make a pair of collections.Counters, sample the circuit millions of times, and add the result-flipped cases into one counter and result-not-flipped to the other. Then for decoding you would look up the current case to see which one had more counts: flip or don't flip.

circuit = ...
assert circuit.num_observables == 1

flip = collections.Counter()
no_flip = collections.Counter()
sampler = circuit.compile_detector_sampler()
for _ in range(1024):
    for shot in sampler.sample(1024, append_observables=True):
        case = tuple(shot[:-1])  # Remove observable bit, make hashable
        was_flipped = shot[-1]
        add_to = flip if was_flipped else no_flip
        add_to[case] += 1

lookup_table = (flip, no_flip)
  • $\begingroup$ Thank you. Is there an equivalent way in which one could do this using only the tableau simulator, though? $\endgroup$ Commented Jan 1, 2022 at 10:43
  • 1
    $\begingroup$ @MatthewS1990 The tableau simulator will be much slower so I'd only use it if you need it for e.g. classically controlled Cliffords. Ultimately it's the same idea: the measurement record uniquely identifies the case, so that's your key into the counter, and you can also extract the observable measurement, which determines which counter increments. You'll have to extract the observable more manually from the measurement results if you're not using the compiled detector sampler that does it for you. $\endgroup$ Commented Jan 1, 2022 at 19:37

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.