-
Notifications
You must be signed in to change notification settings - Fork 30
fix: key detector coordinates by detector ID and bounds check array a… #272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,7 +28,9 @@ | |
| #include "stim.h" | ||
|
|
||
| std::vector<std::vector<double>> get_detector_coords(const stim::DetectorErrorModel& dem) { | ||
| std::vector<std::vector<double>> detector_coords; | ||
| size_t num_detectors = dem.count_detectors(); | ||
| std::vector<std::vector<double>> detector_coords(num_detectors); | ||
| bool has_any_coords = false; | ||
| for (const stim::DemInstruction& instruction : dem.flattened().instructions) { | ||
| switch (instruction.type) { | ||
| case stim::DemInstructionType::DEM_SHIFT_DETECTORS: | ||
|
|
@@ -38,11 +40,16 @@ std::vector<std::vector<double>> get_detector_coords(const stim::DetectorErrorMo | |
| break; | ||
| } | ||
| case stim::DemInstructionType::DEM_DETECTOR: { | ||
| std::vector<double> coord; | ||
| for (const double& t : instruction.arg_data) { | ||
| coord.push_back(t); | ||
| has_any_coords = true; | ||
| std::vector<double> coord(instruction.arg_data.begin(), instruction.arg_data.end()); | ||
| for (const stim::DemTarget& target : instruction.target_data) { | ||
| if (target.is_relative_detector_id()) { | ||
| size_t det_id = target.val(); | ||
| if (det_id < num_detectors) { | ||
| detector_coords[det_id] = coord; | ||
| } | ||
| } | ||
| } | ||
| detector_coords.push_back(coord); | ||
| break; | ||
| } | ||
| case stim::DemInstructionType::DEM_LOGICAL_OBSERVABLE: | ||
|
|
@@ -52,6 +59,9 @@ std::vector<std::vector<double>> get_detector_coords(const stim::DetectorErrorMo | |
| "Unexpected DemInstructionType found in the detector error model."); | ||
| } | ||
| } | ||
| if (!has_any_coords) { | ||
| return {}; | ||
| } | ||
| return detector_coords; | ||
| } | ||
|
|
||
|
|
@@ -148,10 +158,13 @@ static std::vector<std::vector<size_t>> build_det_orders_coordinate( | |
| for (size_t i = 0; i < detector_coords.at(0).size(); ++i) { | ||
| orientation_vector.push_back(dist(rng)); | ||
| } | ||
| for (size_t i = 0; i < detector_coords.size(); ++i) { | ||
| size_t num_dets = std::min(detector_coords.size(), inner_products.size()); | ||
| for (size_t i = 0; i < num_dets; ++i) { | ||
| inner_products[i] = 0; | ||
| for (size_t j = 0; j < orientation_vector.size(); ++j) { | ||
| inner_products[i] += detector_coords[i][j] * orientation_vector[j]; | ||
| if (j < detector_coords[i].size()) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This still assumes D0 defines the coordinate dimensionality. If D0 has no coordinates but a later detector does, the check above returns identity orderings. If D0 has fewer dimensions, the extra dimensions on later detectors are ignored because |
||
| inner_products[i] += detector_coords[i][j] * orientation_vector[j]; | ||
| } | ||
| } | ||
| } | ||
| std::vector<size_t> perm(dem.count_detectors()); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a regression test where D0 has no coordinates but D1 does? That’s the case that exposes the coordinate-ordering issue