# How to implement lattice surgery in stim?

Is there a circuit in stim that performs lattice surgery?

Was lattice surgery ever implemented in stim?

I wonder how the relevant detectors will look like - especially in the initial and final rounds of the surgery.

Is there a circuit in stim that performs lattice surgery?

You can write python code that builds a stim.Circuit which performs lattice surgery. Stim can understand such circuits.

Was lattice surgery ever implemented in stim?

The built-in stim.Circuit.generated doesn't include a lattice surgery example.

I wonder how the relevant detectors will look like - especially in the initial and final rounds of the surgery.

Let $$S$$ be a stabilizer and let $$S_b$$ be the basis of $$S$$ (X or Z). Let $$B$$ be the basis of the lattice surgery. The data qubits in the stitching region are initialized in basis $$B$$ and later measured in basis $$B$$.

$$S$$ either fully overlaps, partially overlaps, or doesn't overlap the stitching region.

If $$S$$ doesn't overlap with the stitching region, its detectors are totally normal. Just keep comparing the previous round's measurement to the current round's measurement.

If $$S$$ partially overlaps the stitching region, and $$S_b = B$$, then during the first round of lattice surgery there is a detector comparing the measurement at the center of $$S_b$$ to the previous round's measurement on that same qubit (even though, in the previous round, $$S$$ was a two body operator and is now a four body operator). During the first round of surface code after lattice surgery, there is a detector comparing the measurement of two-body $$S$$ to the previous measurement of four-body $$S$$ times the data qubit measurements that removed parts of $$S$$. If $$S_b \neq B$$ then these extra detectors are not included.

If $$S$$ fully overlaps the stitching region, and $$S_b = B$$, then during the first round of lattice surgery there is a detector with just the first measurement of $$S$$. And during the last round of lattice surgery there is a detector comparing the measurement of $$S$$ to the data measurements that removed $$S$$. And if $$S_b \neq B$$ then these detectors are not included. This is identical to what happens during normal patch initialization and normal patch measurement, just within the stitching region.

You can check whether you've missed any detectors or observables by counting the number of redundant measurements (use the example code from this open issue). The number of redundant measurements in your circuit should equal the number of detectors plus the number of observables. Also len(stim.Circuit.shortest_graphlike_error()) will be too low if you forgot a detector somewhere, or didn't do enough rounds of stitching to make the measurement result reliable (assuming you've declared the measurement observable at all).

• Thanks! What about X-Z surgery? Clearly the stabilizers are ZZXX, but what about the data qubit initialization and measurement? do I really need them as part of the detectors? May 4, 2023 at 13:22
• @YanivKurman XZ surgery isn't fundamentally any different, it just relabels the physical qubit bases. May 4, 2023 at 14:27