Skip to content

Commit 9488efd

Browse files
committed
Keep existing detector-order generation behavior
1 parent 837acf8 commit 9488efd

5 files changed

Lines changed: 31 additions & 36 deletions

File tree

src/py/_tesseract_py_util/gari.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ def build_detector_orders(
715715
method: object | None = None,
716716
seed: int = 0,
717717
) -> list[list[int]]:
718-
"""Builds orders for a source-aligned GARI DEM, with virtual IDs last."""
718+
"""Builds traversal orders for a source-aligned GARI DEM."""
719719
from tesseract_decoder import utils
720720

721721
source_dem = _circuit_to_gari_source_dem(circuit)
@@ -727,11 +727,13 @@ def build_detector_orders(
727727
virtual_detectors = list(
728728
range(source_detector_count, gari_dem.num_detectors)
729729
)
730+
source_positions = utils.build_det_orders(
731+
source_dem, num_det_orders, method=method, seed=seed
732+
)
730733
return [
731-
order + virtual_detectors
732-
for order in utils.build_det_orders(
733-
source_dem, num_det_orders, method=method, seed=seed
734-
)
734+
sorted(range(source_detector_count), key=positions.__getitem__)
735+
+ virtual_detectors
736+
for positions in source_positions
735737
]
736738

737739

src/py/_tesseract_py_util/gari_test.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def test_public_circuit_conversion_and_file_output(tmp_path):
170170
np.testing.assert_array_equal(
171171
checks.toarray(), transform.checks[[2, 0, 3, 1, 4, 5], :].toarray()
172172
)
173-
source_orders = utils.build_det_orders(
173+
source_positions = utils.build_det_orders(
174174
gari._circuit_to_gari_source_dem(circuit),
175175
2,
176176
method=utils.DetOrder.DetCoordinate,
@@ -182,7 +182,10 @@ def test_public_circuit_conversion_and_file_output(tmp_path):
182182
2,
183183
method=utils.DetOrder.DetCoordinate,
184184
seed=0,
185-
) == [order + [4, 5] for order in source_orders]
185+
) == [
186+
sorted(range(4), key=positions.__getitem__) + [4, 5]
187+
for positions in source_positions
188+
]
186189

187190
block_dem = public_gari.circuit_to_gari(
188191
circuit,

src/py/utils_test.py

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -55,38 +55,21 @@ def test_build_det_orders_default_index():
5555

5656

5757
def test_build_det_orders_bfs():
58-
path_dem = stim.DetectorErrorModel("""
59-
error(0.1) D0 D1
60-
error(0.1) D1 D2
61-
error(0.1) D2 D3
62-
error(0.1) D3 D4
63-
""")
64-
orders = tesseract_decoder.utils.build_det_orders(
65-
path_dem,
66-
num_det_orders=8,
58+
assert tesseract_decoder.utils.build_det_orders(
59+
_DETECTOR_ERROR_MODEL,
60+
num_det_orders=1,
6761
method=tesseract_decoder.utils.DetOrder.DetBFS,
6862
seed=0,
69-
)
70-
for order in orders:
71-
distances_from_start = [abs(detector - order[0]) for detector in order]
72-
assert distances_from_start == sorted(distances_from_start)
63+
) == [[0, 1]]
7364

7465

7566
def test_build_det_orders_coordinate():
76-
dem = stim.DetectorErrorModel("""
77-
detector(0) D0
78-
detector(3) D1
79-
detector(1) D2
80-
detector(4) D3
81-
detector(2) D4
82-
""")
83-
order = tesseract_decoder.utils.build_det_orders(
84-
dem,
67+
assert tesseract_decoder.utils.build_det_orders(
68+
_DETECTOR_ERROR_MODEL,
8569
num_det_orders=1,
8670
method=tesseract_decoder.utils.DetOrder.DetCoordinate,
8771
seed=0,
88-
)[0]
89-
assert order in ([3, 1, 4, 2, 0], [0, 2, 4, 1, 3])
72+
) == [[0, 1]]
9073

9174

9275
def test_build_det_orders_index():

src/utils.cc

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include <queue>
2424
#include <random>
2525
#include <string>
26-
#include <utility>
2726

2827
#include "common.h"
2928
#include "stim.h"
@@ -122,7 +121,11 @@ static std::vector<std::vector<size_t>> build_det_orders_bfs(const stim::Detecto
122121
} while (visited[start]);
123122
}
124123
}
125-
det_orders[det_order] = std::move(perm);
124+
std::vector<size_t> inv_perm(graph.size());
125+
for (size_t i = 0; i < perm.size(); ++i) {
126+
inv_perm[perm[i]] = i;
127+
}
128+
det_orders[det_order] = inv_perm;
126129
}
127130
return det_orders;
128131
}
@@ -156,7 +159,11 @@ static std::vector<std::vector<size_t>> build_det_orders_coordinate(
156159
std::sort(perm.begin(), perm.end(), [&](const size_t& i, const size_t& j) {
157160
return inner_products[i] > inner_products[j];
158161
});
159-
det_orders[det_order] = std::move(perm);
162+
std::vector<size_t> inv_perm(dem.count_detectors());
163+
for (size_t i = 0; i < perm.size(); ++i) {
164+
inv_perm[perm[i]] = i;
165+
}
166+
det_orders[det_order] = inv_perm;
160167
}
161168
return det_orders;
162169
}

src/utils.pybind.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ void add_utils_module(py::module& root) {
110110
Returns
111111
-------
112112
list[list[int]]
113-
A list of detector orderings. Each inner list gives the detector
114-
IDs in traversal order.
113+
A list of detector orderings. Each inner list maps a detector index
114+
to its position in the ordering.
115115
)pbdoc");
116116
m.def(
117117
"get_errors_from_dem",

0 commit comments

Comments
 (0)