# Simulating multiple logical qubits with stim

I'll first state the things I'm trying to do and then summarize my questions:

Since Stim takes a circuit-first approach, I think it should be possible to specify a Stim circuit that represents multiple logical qubits. More specifically, rather than simulating a memory experiment with a single logical qubit, I would like to simulate a memory experiment with an arbitrary number of logical qubits (the motivation of doing this is a longer conversation but this experiment will be the starting point of my study).

My questions:

• How would the detector annotations work? I understand how they work for a single logical qubit, but when the circuit has multiple logical qubits, would it be enough to just specify sets of measurements that must be consistent for every logical qubit?
• For the logical observable (using the OBSERVABLE_INCLUDE instruction), how can I specify the observables for every logical qubit?
• How would this work with the pymatching decoder? Rather, how do I pass the syndromes generated by this circuit to a decoder? Having information of multiple logical qubits would mean that the decoding would be independent for every logical qubit and so a different logical error rate would be reported for every logical qubit.

Hopefully my problem and questions are clear enough, I'll be happy to edit my post in case further clarifications are needed. Thanks!

How would the detector annotations work? I understand how they work for a single logical qubit, but when the circuit has multiple logical qubits, would it be enough to just specify sets of measurements that must be consistent for every logical qubit?

Just declare all the detectors you can, regardless of what logical qubits or logical observables you are checking.

Detectors aren't really about one logical qubit or another, they're just small local invariants in the circuit. The bulk of the circuit should have exactly identical detectors regardless of the observables being declared. That said, often you will have slightly different detectors at the very beginning and end depending on which basis you initialize each logical qubit into, since e.g. initializing into the Z basis will give you Z type detectors in the first round.

For the logical observable (using the OBSERVABLE_INCLUDE instruction), how can I specify the observables for every logical qubit?

You refer to observables by index. There's an observable 0, an observable 1, an observable 2, and so forth. The 2 in OBSERVABLE_INCLUDE(2) rec[-1] is saying you are adding the most recent measurement to observable 2, as opposed to observable 42 or whatever.

How would this work with the pymatching decoder?

It will work seamlessly.

Pymatching will attempt to predict every observable that you declare. If you add an OBSERVABLE_INCLUDE(99) to your circuit, then pymatching will output at least 100 bits of "was this observable flipped?" predictions when decoding data from that circuit. The first bit is the prediction for observable 0, the second bit for observable 1, and so on up to the 100'th bit being for observable 99.

Having information of multiple logical qubits would mean that the decoding would be independent for every logical qubit and so a different logical error rate would be reported for every logical qubit.

Technically it's completely up to you how to count up pymatching's failed predictions. If you are sampling the shots then you have access to every bit of the prediction, and to every bit of the ground truth, and can count them however you like.

If you're using sinter to do the sampling: sinter considers a shot to have failed if any observable was wrong. Sinter unfortunately doesn't provide a nice way to get per-observable failure rates.