# How to implement a controlled gate in Stim with the following control logic?

I'm trying to implement the $$[\![7,1,3]\!]$$ Steane code on Stim. My circuit is below:

circuit = stim.Circuit('''
R 0 1 2 3 4 5 6 7 8 9 10 11 12
MPP X0*X2*X4*X6
MPP X3*X4*X5*X6
MPP X1*X2*X5*X6

DETECTOR rec[-3]
DETECTOR rec[-2]
DETECTOR rec[-1]

X_ERROR(1) 0

REPEAT 3 {
MPP X0*X2*X4*X6
MPP X3*X4*X5*X6
MPP X1*X2*X5*X6

MPP Z0*Z2*Z4*Z6
MPP Z3*Z4*Z5*Z6
MPP Z1*Z2*Z5*Z6

.
.
'''
}


The idea is that the first part puts my qubits into a logical state by measuring the $$X$$ stabilizers. I need to do two things.

1. Keep the outcomes of the first three MPPs somewhere since these eigenvalues define my logical $$\vert 0\rangle$$.

2. I want to implement a correction based on the stabilizer eigenvalues in the REPEAT section. If all of MPP X0 X2 X4 X6, MPP X3 X4 X5 X6 and MPP X1 X2 X5 X6 disagree with the answers in 1., then I have a $$Z$$ error on qubit $$6$$. I want to implement a controlled $$Z$$ gate on qubit $$6$$ with the control being decided by the aforementioned logic.

What's the best way to do this?

EDIT: Updated circuit based on Craig Gidney's answer.

circuit = stim.Circuit('''
R 0 1 2 3 4 5 6 7 8 9 10 11 12

MPP X0*X2*X4*X6
MPP X3*X4*X5*X6
MPP X1*X2*X5*X6

MPP Z0*Z2*Z4*Z6
MPP Z3*Z4*Z5*Z6
MPP Z1*Z2*Z5*Z6

X_ERROR(1) 0

REPEAT 2 {
MPP X0*X2*X4*X6
MPP X3*X4*X5*X6
MPP X1*X2*X5*X6

MPP Z0*Z2*Z4*Z6
MPP Z3*Z4*Z5*Z6
MPP Z1*Z2*Z5*Z6

DETECTOR rec[-6] rec[-12]
DETECTOR rec[-5] rec[-11]
DETECTOR rec[-4] rec[-10]
DETECTOR rec[-3] rec[-9]
DETECTOR rec[-2] rec[-8]
DETECTOR rec[-1] rec[-7]
}
''')


Indeed, I only get one True on the stabilizer $$Z0Z2Z4Z6$$ in the first round as that's where the error is.

Anyways, in your case all you need to do is to declare a DETECTOR comparing each stabilizer measurement to the same measurement from the previous round. And also an OBSERVABLE_INCLUDE comparing the observable measurement at the start to the observable measurement at the end. The goal of decoding is to determine if the observable was flipped, given the detection events.
The DETECTOR instructions that you do have in the circuit currently are wrong, because they are not annotating deterministic measurement sets. Each of the X basis measurements is random, so they are not detectors. But comparing those measurements between rounds does form detectors.