Skip to content

Commit 214defc

Browse files
authored
fix bug in detcost computation (#227)
1 parent d78e940 commit 214defc

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

src/tesseract.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,26 +88,26 @@ bool Node::operator>(const Node& other) const {
8888
double TesseractDecoder::get_detcost(
8989
size_t d, const std::vector<DetectorCostTuple>& detector_cost_tuples) const {
9090
double min_cost = INF;
91-
uint32_t min_det_cost = std::numeric_limits<uint32_t>::max();
91+
uint32_t min_det_cost_det_count = std::numeric_limits<uint32_t>::max();
9292
double error_cost;
9393
ErrorCost ec;
9494
DetectorCostTuple dct;
9595

9696
for (int ei : d2e[d]) {
9797
ec = error_costs[ei];
98-
if (ec.likelihood_cost * min_det_cost >= min_cost * errors[ei].symptom.detectors.size()) break;
98+
if (ec.likelihood_cost * min_det_cost_det_count >= min_cost * errors[ei].symptom.detectors.size()) break;
9999

100100
dct = detector_cost_tuples[ei];
101101
if (!dct.error_blocked) {
102102
error_cost = ec.likelihood_cost;
103-
if (error_cost < min_cost * dct.detectors_count) {
103+
if (error_cost * min_det_cost_det_count < min_cost * dct.detectors_count) {
104104
min_cost = error_cost;
105-
min_det_cost = dct.detectors_count;
105+
min_det_cost_det_count = dct.detectors_count;
106106
}
107107
}
108108
}
109109

110-
return (min_cost / min_det_cost) + config.det_penalty;
110+
return (min_cost / min_det_cost_det_count) + config.det_penalty;
111111
}
112112

113113
TesseractDecoder::TesseractDecoder(TesseractConfig config_) : config(config_) {

src/tesseract.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ struct TesseractDecoder {
108108
return eneighbors;
109109
}
110110

111-
private:
112111
std::vector<std::vector<int>> d2e;
113112
std::vector<std::vector<int>> eneighbors;
114113
std::vector<std::vector<int>> edets;

src/tesseract.test.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,3 +395,26 @@ TEST(tesseract, DecodeToErrorsThrowsOnInvalidSymptom) {
395395
err.what());
396396
}
397397
}
398+
399+
TEST(TesseractDetcostTest, ComparesRatiosNotRawCosts) {
400+
stim::DetectorErrorModel dem = stim::DetectorErrorModel(R"DEM(
401+
error(0.005322067133022559) D0 D1 D3
402+
error(0.0051237598826648) D0 D1 D2
403+
)DEM");
404+
405+
TesseractConfig cfg;
406+
cfg.dem = dem;
407+
cfg.merge_errors = false;
408+
TesseractDecoder dec(cfg);
409+
410+
std::vector<DetectorCostTuple> tuples(dec.errors.size());
411+
// residual x = {D0, D1}
412+
std::cout <<"dec.d2e.size() = "<<dec.d2e.size()<<std::endl;
413+
for (int ei : dec.d2e[0]) tuples[ei].detectors_count++;
414+
for (int ei : dec.d2e[1]) tuples[ei].detectors_count++;
415+
416+
double got = dec.get_detcost(0, tuples);
417+
double expected = 5.230557212477344 / 2.0; // from D0 D1 D3
418+
419+
EXPECT_NEAR(got, expected, 1e-12);
420+
}

0 commit comments

Comments
 (0)