Skip to content

Commit e2f55f4

Browse files
authored
Merge pull request #45 from noajshu/codex/copy-get_dets_logicals-to-decomposer
Deduplicate detector and logical targets modulo 2 in decomposer
2 parents 6cc141e + c2dcf3d commit e2f55f4

2 files changed

Lines changed: 35 additions & 11 deletions

File tree

src/py/decomposer.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
import stim
22

33

4+
def get_dets_logicals(error: stim.DemInstruction):
5+
dets = set()
6+
logicals = set()
7+
for t in error.targets_copy():
8+
if t.is_logical_observable_id():
9+
logicals = logicals.symmetric_difference({t.val})
10+
elif t.is_relative_detector_id():
11+
dets = dets.symmetric_difference({t.val})
12+
return dets, logicals
13+
14+
415
def _decompose_by_detector_partition(dem, partition_func):
516
detector_coords = {}
617

@@ -19,17 +30,13 @@ def _decompose_by_detector_partition(dem, partition_func):
1930
# Make a new instruction where the detectors are decomposed.
2031
targets_by_basis = [[], []]
2132
observables = []
22-
for d in instruction.targets_copy():
23-
if d.is_separator():
24-
# Ignore the existing decomposition, if present.
25-
continue
26-
if d.is_relative_detector_id():
27-
coord = detector_coords[d.val]
28-
targets_by_basis[partition_func(coord)].append(d)
29-
else:
30-
# Logical observables are placed in component 0.
31-
assert d.is_logical_observable_id()
32-
observables.append(d)
33+
dets, logicals = get_dets_logicals(instruction)
34+
for det in sorted(dets):
35+
coord = detector_coords[det]
36+
targets_by_basis[partition_func(coord)].append(stim.target_relative_detector_id(det))
37+
for obs in sorted(logicals):
38+
# Logical observables are placed in component 0.
39+
observables.append(stim.target_logical_observable_id(obs))
3340

3441
all_targets = []
3542
for targets in targets_by_basis:

src/py/decomposer_test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,20 @@ def test_do_decomposition_stim_surface_code_requires_xy_coords():
6060

6161
with pytest.raises(AssertionError, match='requires detector x/y coordinates'):
6262
do_decomposition_stim_surface_code(dem)
63+
64+
65+
def test_do_decomposition_last_coordinate_index_dedupes_mod_2_targets():
66+
dem = stim.DetectorErrorModel('''
67+
detector(0, 0, 0, 0) D0
68+
detector(1, 0, 0, 1) D1
69+
error(0.125) D0 D0 D1 D1 L0 L0
70+
''')
71+
72+
actual = do_decomposition_last_coordinate_index(dem)
73+
74+
expected = stim.DetectorErrorModel('''
75+
detector(0, 0, 0, 0) D0
76+
detector(1, 0, 0, 1) D1
77+
error(0.125)
78+
''')
79+
assert actual == expected

0 commit comments

Comments
 (0)