# How to import a generic stabilizer code in stim

I just started using stim to simulate the performance of quantum error correcting codes. I hope this is the right place to ask questions about the subject. The example code with the repetition code and depolarizing channel worked fine. Now I want to try one of my codes and the same channel. How would I import my code into stim? I can write the stabilizers as an $$m \times n$$ array ($$m$$ stabilizers, $$n$$ qubits) with entries from $$(0,1,2,3)$$; this seems like the most straight forward way. $$(0=I,1=X,2=Z,3=Y)$$.

For example, for the $$[[5,1,3]]$$ code $$S:=[[1,2,2,1,0],[0,1,2,2,1],[1,0,1,2,2],[2,1,0,1,2]];$$

Is this the preferred way? Do I also need to specify the logicals or destabilizers (I have these for some codes but maybe stim calculates them from the stabilizer...)

Stim is very much a circuit focused simulator. It speaks quantum operations, not stabilizer configurations, so you have to convert your table of stabilizers into a stabilizer circuit. This is a bit inconvenient, but ultimately makes Stim much more flexible as a tool (e.g. it has no issues with gauge codes or non-foliated codes).

The "proper" thing for you to do would be to find the encoding circuit for your code and put that into Stim. A fallback strategy could be to use the MPP instruction (Measure Pauli Product) to measure the stabilizers of the code. For example, here is a circuit that projects into your given code, applies single qubit depolarizing noise, re-measures the stabilizers, and produces detection events based on whether or not each stabilizer was flipped:

MPP X0*Z1*Z2*X3 X1*Z2*Z3*X4 X0*X2*Z3*Z4 Z0*X1*X3*Z4
DEPOLARIZE1(0.001) 0 1 2 3 4
MPP X0*Z1*Z2*X3 X1*Z2*Z3*X4 X0*X2*Z3*Z4 Z0*X1*X3*Z4
DETECTOR rec[-1] rec[-5]
DETECTOR rec[-2] rec[-6]
DETECTOR rec[-3] rec[-7]
DETECTOR rec[-4] rec[-8]

In python, you append MPP instructions like this:

circuit.append_operation("MPP", [
stim.target_x(0),
stim.target_combiner(),
stim.target_z(1),
stim.target_combiner(),
stim.target_z(2),
stim.target_combiner(),
stim.target_x(3),
])

(The target combiners (*) are needed because you can give multiple products to MPP.)

Measuring the stabilizers will randomly project the system into their +1 or -1 eigenstates. If for some reason you need the +1 eigenstate you can do a conditional Pauli correction, e.g. CNOT rec[-1] 3 would apply an X gate to qubit 3 if the most recent measurement was True (presumably indicating the associated stabilizer measurement said the system was in the -1 eigenstate). The stim.TableauSimulator.measure_kickback method is a bit technical but can be used for finding a Pauli correction that won't ruin the other stabilizers of the state.

Anyways, ideally you would know an encoding circuit already. Otherwise, Stim provides a few basic tools like MPP andmeasure_kickback to help discover some of the pieces of such a circuit, but doesn't currently have an end-to-end "give me a well formed list of stabilizers and I give you a circuit" helper method.

• I think I can get the encoding circuit for most codes; so I'll move in that direction if there's no better way. There are algorithms to put the stabilizers into canonical form and then derive the encoding circuit from that; there's a fair amount of details that I was hoping to avoid with complications if you have redundant stabilizers...maybe someone know of a separate package that performs this step. Assuming I can get the encoding circuit, is the decoder used in the repetition code example generic enough to handle an arbitrary code? Oct 9 at 20:15
• @unknown No, most codes can't be decoded by a matching decoder which is what the repetition code example uses. Stim is not a decoding library; even the rep code example is using pymatching to do the decoding not Stim itself. Stim's decoding story focuses on providing building block tools (stim.Circuit.detector_error_model and stim.Circuit.compile_detector_sampler in this case) that are useful for getting configuration data and detection event data into a decoder. Oct 9 at 20:19
• oh that's too bad...I was encouraged by how quickly you were able to simulate the honeycomb code but I guess many parts of that sim are not part of stim Oct 9 at 20:22
• @unknown The honeycomb also has the surprising property that it can be decoded by matching, so I could use PyMatching for it. If it had required eg. a color code decoder I definitely would have struggled more, since I'm not familiar with those. Stim would still have been able to give me measurement results, and detection events, and a detector error model (corresponding to a hypergraph instead of a graph), but I wouldn't have had a ready tool for decoding. Oct 9 at 20:25
• @unknown Having a decoder that could efficiently decode any stabilizer code sure would be magical. But seems a bit... utterly insanely difficult. Oct 9 at 20:39