Skip to content

Commit

Permalink
Fix measurement reordering in stim.Circuit.time_reversed_for_flows (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Strilanc authored Apr 12, 2024
1 parent 568dc24 commit 3a430f2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/stim/util_top/circuit_inverse_qec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void CircuitFlowReverser::do_m2r_instruction(const CircuitInstruction &inst) {
inverted_circuit.safe_append(reset, &t, inst.args);
} else {
// Measurements that aren't turned into resets need to be re-indexed.
auto f = rev.rec_bits.find(rev.num_measurements_in_past - k - 1);
auto f = rev.rec_bits.find(rev.num_measurements_in_past - 1);
if (f != rev.rec_bits.end()) {
for (auto &dem_target : f->second) {
d2ms[dem_target].insert(num_new_measurements);
Expand Down
41 changes: 41 additions & 0 deletions src/stim/util_top/circuit_inverse_qec_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,44 @@ def test_more_flow_qubits_than_circuit_qubits():
stim.Flow("X300 -> X300"),
stim.Flow("Z2*Z301 -> X2*Z301"),
]


def test_measurement_ordering():
circuit = stim.Circuit("""
M 0 1
""")
flows = [
stim.Flow("I -> Z0 xor rec[-2]"),
stim.Flow("I -> Z1 xor rec[-1]"),
]
assert circuit.has_all_flows(flows, unsigned=True)
new_circuit, new_flows = circuit.time_reversed_for_flows(flows)
assert new_circuit.has_all_flows(new_flows, unsigned=True)


def test_measurement_ordering_2():
circuit = stim.Circuit("""
MZZ 0 1 2 3
""")
flows = [
stim.Flow("I -> Z0*Z1 xor rec[-2]"),
stim.Flow("I -> Z2*Z3 xor rec[-1]"),
]
assert circuit.has_all_flows(flows, unsigned=True)
new_circuit, new_flows = circuit.time_reversed_for_flows(flows)
assert new_circuit.has_all_flows(new_flows, unsigned=True)


def test_measurement_ordering_3():
circuit = stim.Circuit("""
MR 0 1
""")
flows = [
stim.Flow("Z0 -> rec[-2]"),
stim.Flow("Z1 -> rec[-1]"),
stim.Flow("I -> Z0"),
stim.Flow("I -> Z1"),
]
assert circuit.has_all_flows(flows, unsigned=True)
new_circuit, new_flows = circuit.time_reversed_for_flows(flows)
assert new_circuit.has_all_flows(new_flows, unsigned=True)

0 comments on commit 3a430f2

Please sign in to comment.