It doesn't work because you've rearrange your circuit as if the CNOT gates don't interact with each other, but they do.
Here is what you should do:
- Add the missing three detectors to your circuit, comparing the X stabilizer measurements at the end to the ones at the start. You also want to be checking the X stabilizers, to prevent mistakes.
- Run
circuit.detector_error_model()
on the circuit. It checks that all the detectors are actually deterministic. It should finish without throwing an exception.
- Move ONE CNOT gate ONE step.
- Run
circuit.detector_error_model()
on the circuit. If it throws an exception, you just made a mistake. Don't move that CNOT that way. Put it back. Otherwise it's okay.
- Goto step (2).
- Start making multi-step moves. Some things can only be achieved by travelling through wrongspace; making more than one change at a time.
Note you can use circuit.diagram()
to get a text diagram of the circuit, or circuit.diagram('timeline-svg')
to get a picture diagram of the circuit, and if you add TICKs between layers then you can use circuit.diagram('detslice-with-ops-svg')
to get the progression of the stabilizers corresponding to the detecting regions. That last one requires you to give decent qubit coordinates, so it doesn't just draw it on a line.
import stim
circuit_1=stim.Circuit('''
QUBIT_COORDS(0, 0) 0
QUBIT_COORDS(5, 0) 2
QUBIT_COORDS(0, 5) 4
QUBIT_COORDS(5, 5) 6
QUBIT_COORDS(7, 7) 5
QUBIT_COORDS(2, 7) 3
QUBIT_COORDS(7, 2) 1
QUBIT_COORDS(2.5, 2.5, 0) 7
QUBIT_COORDS(6, 3.5, 0) 8
QUBIT_COORDS(3.5, 6, 0) 9
QUBIT_COORDS(2.5, 2.5, 1) 10
QUBIT_COORDS(6, 3.5, 1) 11
QUBIT_COORDS(3.5, 6, 1) 12
#encode into code state
MPP X3*X4*X5*X6
MPP X1*X2*X5*X6
MPP X0*X2*X4*X6
TICK
TICK
#ancilla of X stabilizers
RX 7 8 9
#ancilla of Z stabilizers
R 10 11 12
TICK
#CNOT of X stabilizers
CX 7 6 8 5
TICK
CX 7 2 8 1
TICK
CX 7 4 8 6
TICK
CX 7 0 8 2 9 5
TICK
CX 9 6
TICK
CX 9 3
TICK
CX 9 4
TICK
#CNOT of Z stabilizers
CX 6 12
TICK
CX 3 12
TICK
CX 4 12
TICK
CX 6 10 5 11
TICK
CX 2 10 1 11
TICK
CX 4 10 6 11
TICK
CX 0 10 2 11 5 12
TICK
#measurement of stabilizers
MRX 7 8 9
MR 10 11 12
DETECTOR rec[-3]
DETECTOR rec[-2]
DETECTOR rec[-1]
DETECTOR rec[-4] rec[-9]
DETECTOR rec[-5] rec[-8]
DETECTOR rec[-6] rec[-7]
''')
circuit_1.diagram()
/---------------------------------------------------------------\ /-\ /-\ /-\ /---\ /-\ /-\ /-\ /---\ /----------------------------------\
q0: -QUBIT_COORDS(0,0)-----------------------------------MPP[X]:rec[2]------------------X-----------------------------@----------------DETECTOR:D5=rec[3]*rec[2]-
| | |
q1: -QUBIT_COORDS(7,2)---------------------MPP[X]:rec[1]-|------------------------X-----|-----------------------@-----|----------------DETECTOR:D4=rec[4]*rec[1]-
| | | | | |
q2: -QUBIT_COORDS(5,0)---------------------MPP[X]:rec[1]-MPP[X]:rec[2]----------X-|-----|-X-------------------@-|-----|-@----------------------------------------
| | | | | | | | | |
q3: -QUBIT_COORDS(2,7)-------MPP[X]:rec[0]-|-------------|----------------------|-|-----|-|-----X-----@-------|-|-----|-|--------------DETECTOR:D3=rec[5]*rec[0]-
| | | | | | | | | | | | |
q4: -QUBIT_COORDS(0,5)-------MPP[X]:rec[0]-|-------------MPP[X]:rec[2]----------|-|-X---|-|-----|-X---|-@-----|-|-@---|-|----------------------------------------
| | | | | | | | | | | | | | | | |
q5: -QUBIT_COORDS(7,7)-------MPP[X]:rec[0]-MPP[X]:rec[1]-|--------------------X-|-|-|---|-|-X---|-|---|-|---@-|-|-|---|-|-@--------------------------------------
| | | | | | | | | | | | | | | | | | | | |
q6: -QUBIT_COORDS(5,5)-------MPP[X]:rec[0]-MPP[X]:rec[1]-MPP[X]:rec[2]------X-|-|-|-|-X-|-|-|-X-|-|-@-|-|-@-|-|-|-|-@-|-|-|--------------------------------------
| | | | | | | | | | | | | | | | | | | | | | | |
q7: -QUBIT_COORDS(2.5,2.5,0)---------------------------------------------RX-@-|-@-|-@-|-@-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-MRX:rec[3]---------------------------
| | | | | | | | | | | | | | | | | | | |
q8: -QUBIT_COORDS(6,3.5,0)-----------------------------------------------RX---@---@---@---@-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-MRX:rec[4]---------------------------
| | | | | | | | | | | | | | | |
q9: -QUBIT_COORDS(3.5,6,0)-----------------------------------------------RX-----------------@-@-@-@-|-|-|-|-|-|-|-|-|-|-|-|-MRX:rec[5]---------------------------
| | | | | | | | | | | |
q10: -QUBIT_COORDS(2.5,2.5,1)---------------------------------------------R--------------------------|-|-|-X-|-X-|-X-|-X-|-|-MR:rec[6]--DETECTOR:D0=rec[6]--------
| | | | | | | |
q11: -QUBIT_COORDS(6,3.5,1)-----------------------------------------------R--------------------------|-|-|---X---X---X---X-|-MR:rec[7]--DETECTOR:D1=rec[7]--------
| | | |
q12: -QUBIT_COORDS(3.5,6,1)-----------------------------------------------R--------------------------X-X-X-----------------X-MR:rec[8]--DETECTOR:D2=rec[8]--------
\---------------------------------------------------------------/ \-/ \-/ \-/ \---/ \-/ \-/ \-/ \---/ \----------------------------------/
circuit_1.diagram('timeline-svg')
circuit_1.diagram('detslice-with-ops-svg')