Skip to content

Commit 51eaa88

Browse files
authored
Change simplex to re-use DEM preprocessing logic from utils, to fix bug with logical_observable instruction (#193)
These instructions rarely appear in valid in DEMs but are allowed and previously caused an error. Fixes #190
1 parent bf2e4a4 commit 51eaa88

4 files changed

Lines changed: 36 additions & 21 deletions

File tree

AGENTS.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ bazel build src:tesseract src:simplex
1818
## Running Tests with Bazel
1919

2020
```bash
21-
bazel test src:all
22-
bazel test //src/py:all
21+
bazel test src/...
2322
```
2423

2524
## Building with CMake

src/simplex.cc

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "Highs.h"
2020
#include "io/HMPSIO.h"
21+
#include "utils.h"
2122

2223
constexpr size_t T_COORD = 2;
2324

@@ -51,24 +52,13 @@ SimplexDecoder::SimplexDecoder(SimplexConfig _config) : config(_config) {
5152
dem_error_to_error = std::move(dem_error_map);
5253
error_to_dem_error = common::invert_error_map(dem_error_to_error, config.dem.count_errors());
5354

54-
std::vector<double> detector_t_coords(config.dem.count_detectors());
55-
for (const stim::DemInstruction& instruction : config.dem.flattened().instructions) {
56-
switch (instruction.type) {
57-
case stim::DemInstructionType::DEM_SHIFT_DETECTORS:
58-
throw std::runtime_error("DEM_SHIFT_DETECTORS instruction is not supported.");
59-
break;
60-
case stim::DemInstructionType::DEM_ERROR: {
61-
if (!(instruction.arg_data[0] > 0)) {
62-
throw std::invalid_argument("Error instruction probability must be greater than zero.");
63-
}
64-
errors.emplace_back(instruction);
65-
break;
66-
}
67-
case stim::DemInstructionType::DEM_DETECTOR:
68-
detector_t_coords[instruction.target_data[0].val()] = instruction.arg_data[T_COORD];
69-
break;
70-
default:
71-
throw std::runtime_error("Unsupported instruction type encountered.");
55+
errors = get_errors_from_dem(config.dem.flattened());
56+
57+
std::vector<double> detector_t_coords(config.dem.count_detectors(), 0);
58+
std::vector<std::vector<double>> detector_coords = get_detector_coords(config.dem);
59+
for (size_t d = 0; d < detector_coords.size(); ++d) {
60+
if (detector_coords[d].size() > T_COORD) {
61+
detector_t_coords[d] = detector_coords[d][T_COORD];
7262
}
7363
}
7464
std::map<double, std::vector<size_t>> start_time_to_errors_map, end_time_to_errors_map;

src/tesseract.test.cc

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,30 @@ TEST(tesseract, DecodersStripZeroProbabilityErrors) {
219219
EXPECT_EQ(s_dec.errors.size(), 2);
220220
}
221221

222+
TEST(tesseract, GetDetectorCoordsAllowsLogicalObservableInstructionsInDem) {
223+
stim::DetectorErrorModel dem(R"DEM(
224+
error(0.1) D0 L0
225+
detector(1,2,3) D0
226+
logical_observable L0
227+
)DEM");
228+
229+
std::vector<std::vector<double>> detector_coords = get_detector_coords(dem);
230+
ASSERT_EQ(detector_coords.size(), 1);
231+
ASSERT_EQ(detector_coords[0].size(), 3);
232+
EXPECT_EQ(detector_coords[0][0], 1);
233+
EXPECT_EQ(detector_coords[0][1], 2);
234+
EXPECT_EQ(detector_coords[0][2], 3);
235+
}
236+
TEST(tesseract, SimplexAllowsLogicalObservableInstructionsInDem) {
237+
stim::DetectorErrorModel dem(R"DEM(
238+
error(0.1) D0 L0
239+
detector(0,0,0) D0
240+
logical_observable L0
241+
)DEM");
242+
243+
EXPECT_NO_THROW({ SimplexDecoder s_dec(SimplexConfig{dem}); });
244+
}
245+
222246
TEST(tesseract, DecoderErrorIndexMapsAreInOriginalDemCoordinates) {
223247
stim::DetectorErrorModel dem(R"DEM(
224248
error(0.1) D0
@@ -344,4 +368,4 @@ TEST(tesseract, EneighborsCorrectness_ComplexGrid) {
344368
EXPECT_EQ(t_dec.get_eneighbors()[5], expected_e5_neighbors);
345369
EXPECT_EQ(t_dec.get_eneighbors()[6], expected_e6_neighbors);
346370
EXPECT_EQ(t_dec.get_eneighbors()[7], expected_e7_neighbors);
347-
}
371+
}

src/utils.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ std::vector<std::vector<double>> get_detector_coords(const stim::DetectorErrorMo
4545
detector_coords.push_back(coord);
4646
break;
4747
}
48+
case stim::DemInstructionType::DEM_LOGICAL_OBSERVABLE:
49+
break;
4850
default:
4951
throw std::invalid_argument(
5052
"Unexpected DemInstructionType found in the detector error model.");

0 commit comments

Comments
 (0)