From 3a430f292050fc6e748924a95bc5e15267ecf969 Mon Sep 17 00:00:00 2001 From: Craig Gidney Date: Thu, 11 Apr 2024 18:16:00 -0700 Subject: [PATCH] Fix measurement reordering in `stim.Circuit.time_reversed_for_flows` (#750) --- src/stim/util_top/circuit_inverse_qec.cc | 2 +- src/stim/util_top/circuit_inverse_qec_test.py | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/stim/util_top/circuit_inverse_qec.cc b/src/stim/util_top/circuit_inverse_qec.cc index 42e62e68..1fae1717 100644 --- a/src/stim/util_top/circuit_inverse_qec.cc +++ b/src/stim/util_top/circuit_inverse_qec.cc @@ -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); diff --git a/src/stim/util_top/circuit_inverse_qec_test.py b/src/stim/util_top/circuit_inverse_qec_test.py index 4a0f108c..b558da13 100644 --- a/src/stim/util_top/circuit_inverse_qec_test.py +++ b/src/stim/util_top/circuit_inverse_qec_test.py @@ -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)