quantumlib / Stim

A fast stabilizer circuit library.
Apache License 2.0
305 stars 88 forks source link

Incorrect qubit coordinate shifting in 'timeslice-svg' plots #757

Open ChenZhao44 opened 2 months ago

ChenZhao44 commented 2 months ago

Different orders of instructions in the same tick will result in different timeslice-svg plots. For example, the following code produces the correct time slice plot as expected:

import stim
circ = stim.Circuit("""
QUBIT_COORDS(0.5, 0.5) 1
QUBIT_COORDS(1.5, 0.5) 2
QUBIT_COORDS(2.5, 0.5) 3
QUBIT_COORDS(0.5, 1.5) 4
QUBIT_COORDS(1.5, 1.5) 5
QUBIT_COORDS(2.5, 1.5) 6
QUBIT_COORDS(0.5, 2.5) 7
QUBIT_COORDS(1.5, 2.5) 8
QUBIT_COORDS(2.5, 2.5) 9
QUBIT_COORDS(1.0, 0.0) 10
QUBIT_COORDS(1.0, 1.0) 11
QUBIT_COORDS(2.0, 1.0) 12
QUBIT_COORDS(3.0, 1.0) 13
QUBIT_COORDS(0.0, 2.0) 14
QUBIT_COORDS(1.0, 2.0) 15
QUBIT_COORDS(2.0, 2.0) 16
QUBIT_COORDS(2.0, 3.0) 17

RX 1
RX 2
RX 3
RX 4
RX 5
RX 6
RX 7
RX 8
RX 9
R 10
R 12
R 15
R 17
DEPOLARIZE1(0.001) 1
DEPOLARIZE1(0.001) 2
DEPOLARIZE1(0.001) 3
DEPOLARIZE1(0.001) 4
DEPOLARIZE1(0.001) 5
DEPOLARIZE1(0.001) 6
DEPOLARIZE1(0.001) 7
DEPOLARIZE1(0.001) 8
DEPOLARIZE1(0.001) 9
DEPOLARIZE1(0.001) 10
DEPOLARIZE1(0.001) 12
DEPOLARIZE1(0.001) 15
DEPOLARIZE1(0.001) 17
TICK
""")
circ.diagram('timeslice-svg')

correct

However, when the instructions R/RX and DEPOLARIZE1 are placed alternatively, the time slice will be plotted incorrectly.

import stim
circ = stim.Circuit("""
QUBIT_COORDS(0.5, 0.5) 1
QUBIT_COORDS(1.5, 0.5) 2
QUBIT_COORDS(2.5, 0.5) 3
QUBIT_COORDS(0.5, 1.5) 4
QUBIT_COORDS(1.5, 1.5) 5
QUBIT_COORDS(2.5, 1.5) 6
QUBIT_COORDS(0.5, 2.5) 7
QUBIT_COORDS(1.5, 2.5) 8
QUBIT_COORDS(2.5, 2.5) 9
QUBIT_COORDS(1.0, 0.0) 10
QUBIT_COORDS(1.0, 1.0) 11
QUBIT_COORDS(2.0, 1.0) 12
QUBIT_COORDS(3.0, 1.0) 13
QUBIT_COORDS(0.0, 2.0) 14
QUBIT_COORDS(1.0, 2.0) 15
QUBIT_COORDS(2.0, 2.0) 16
QUBIT_COORDS(2.0, 3.0) 17
RX 1
DEPOLARIZE1(0.001) 1
RX 2
DEPOLARIZE1(0.001) 2
RX 3
DEPOLARIZE1(0.001) 3
RX 4
DEPOLARIZE1(0.001) 4
RX 5
DEPOLARIZE1(0.001) 5
RX 6
DEPOLARIZE1(0.001) 6
RX 7
DEPOLARIZE1(0.001) 7
RX 8
DEPOLARIZE1(0.001) 8
RX 9
DEPOLARIZE1(0.001) 9
R 10
DEPOLARIZE1(0.001) 10
R 12
DEPOLARIZE1(0.001) 12
R 15
DEPOLARIZE1(0.001) 15
R 17
DEPOLARIZE1(0.001) 17
TICK
""")
circ.diagram('timeslice-svg')

incorrect

It seems that every time a noise instruction is plotted, a global coordinate shift will be applied regardless of the type of subsequent instructions. However, coordinate shifting should only occur with noise instructions, not with any other instructions.

Stim version: 1.13.0

Strilanc commented 2 months ago

This is because internally everytime it sees an operation happen on a qubit again within one tick, it increments an internal subtick value. That was the fastest safest way to be sure there were no ordering bugs and no ambiguity about the order things are declared in the circuit.

Probably what should be done here is to have a subtick per qubit, and to take the max subtick + 1 of the qubits affected by the operation then set all their subticks to that value.