# Efficient Way to Create an LUT in Stim

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?

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