-
Notifications
You must be signed in to change notification settings - Fork 17
refactor of det order helper functions and tests #113
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
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
78e5ec8
Add visualization library to CMake build
noajshu 482cb81
Merge pull request #36 from noajshu/codex/update-cmakelists-to-includ…
noajshu 106ce02
Merge branch 'main' into main
noajshu b4c9d08
Merge remote-tracking branch 'quantum/main'
noajshu 5c211bc
Fix CMake Python module placement and add agent instructions
noajshu d93e31d
Merge pull request #37 from noajshu/codex/add-agents.md-documentation…
noajshu 7c10268
Allow decode_to_errors to accept bitstring
noajshu 8a27808
Merge pull request #38 from noajshu/codex/update-decode_to_errors-to-…
noajshu 1e52d57
Merge branch 'quantumlib:main' into main
noajshu c34dd80
clang-format
noajshu 24c0d69
Update src/tesseract.pybind.h
noajshu 96849b6
remove stringstream
noajshu 3d486b0
more fixes
noajshu 20e506d
Merge remote-tracking branch 'quantum'
noajshu 61867e2
Handle explicit DetIndex case
noajshu a3e2663
Merge pull request #39 from noajshu/codex/create-detorder-enum-and-up…
noajshu 7309377
Merge remote-tracking branch 'quantum'
noajshu 454521f
expand det index test
noajshu 18b12cd
Merge pull request #40 from noajshu/codex/refactor-test_build_det_ord…
noajshu 41f7c3e
update beam climbing for when det orders > beam+1
noajshu e9a0774
Merge remote-tracking branch 'origin'
noajshu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,104 +82,124 @@ std::vector<std::vector<size_t>> build_detector_graph(const stim::DetectorErrorM | |
| return neighbors; | ||
| } | ||
|
|
||
| std::vector<std::vector<size_t>> build_det_orders(const stim::DetectorErrorModel& dem, | ||
| size_t num_det_orders, DetOrder method, | ||
| uint64_t seed) { | ||
| static std::vector<std::vector<size_t>> build_det_orders_bfs(const stim::DetectorErrorModel& dem, | ||
| size_t num_det_orders, | ||
| std::mt19937_64& rng) { | ||
| std::vector<std::vector<size_t>> det_orders(num_det_orders); | ||
| std::mt19937_64 rng(seed); | ||
| std::normal_distribution<double> dist(0, 1); | ||
|
|
||
| auto detector_coords = get_detector_coords(dem); | ||
|
|
||
| if (method == DetOrder::DetBFS) { | ||
| auto graph = build_detector_graph(dem); | ||
| std::uniform_int_distribution<size_t> dist_det(0, graph.size() - 1); | ||
| for (size_t det_order = 0; det_order < num_det_orders; ++det_order) { | ||
| std::vector<size_t> perm; | ||
| perm.reserve(graph.size()); | ||
| std::vector<char> visited(graph.size(), false); | ||
| std::queue<size_t> q; | ||
| size_t start = dist_det(rng); | ||
| while (perm.size() < graph.size()) { | ||
| if (!visited[start]) { | ||
| visited[start] = true; | ||
| q.push(start); | ||
| perm.push_back(start); | ||
| } | ||
| while (!q.empty()) { | ||
| size_t cur = q.front(); | ||
| q.pop(); | ||
| auto neigh = graph[cur]; | ||
| std::shuffle(neigh.begin(), neigh.end(), rng); | ||
| for (size_t n : neigh) { | ||
| if (!visited[n]) { | ||
| visited[n] = true; | ||
| q.push(n); | ||
| perm.push_back(n); | ||
| } | ||
| auto graph = build_detector_graph(dem); | ||
| std::uniform_int_distribution<size_t> dist_det(0, graph.size() - 1); | ||
| for (size_t det_order = 0; det_order < num_det_orders; ++det_order) { | ||
| std::vector<size_t> perm; | ||
| perm.reserve(graph.size()); | ||
| std::vector<char> visited(graph.size(), false); | ||
| std::queue<size_t> q; | ||
| size_t start = dist_det(rng); | ||
| while (perm.size() < graph.size()) { | ||
| if (!visited[start]) { | ||
| visited[start] = true; | ||
| q.push(start); | ||
| perm.push_back(start); | ||
| } | ||
| while (!q.empty()) { | ||
| size_t cur = q.front(); | ||
| q.pop(); | ||
| auto neigh = graph[cur]; | ||
| std::shuffle(neigh.begin(), neigh.end(), rng); | ||
| for (size_t n : neigh) { | ||
| if (!visited[n]) { | ||
| visited[n] = true; | ||
| q.push(n); | ||
| perm.push_back(n); | ||
| } | ||
| } | ||
| if (perm.size() < graph.size()) { | ||
| do { | ||
| start = dist_det(rng); | ||
| } while (visited[start]); | ||
| } | ||
| } | ||
| std::vector<size_t> inv_perm(graph.size()); | ||
| for (size_t i = 0; i < perm.size(); ++i) { | ||
| inv_perm[perm[i]] = i; | ||
| if (perm.size() < graph.size()) { | ||
| do { | ||
| start = dist_det(rng); | ||
| } while (visited[start]); | ||
| } | ||
| det_orders[det_order] = inv_perm; | ||
| } | ||
| } else if (method == DetOrder::DetCoordinate) { | ||
| std::vector<double> inner_products(dem.count_detectors()); | ||
| if (!detector_coords.size() || !detector_coords.at(0).size()) { | ||
| for (size_t det_order = 0; det_order < num_det_orders; ++det_order) { | ||
| det_orders[det_order].resize(dem.count_detectors()); | ||
| std::iota(det_orders[det_order].begin(), det_orders[det_order].end(), 0); | ||
| } | ||
| } else { | ||
| for (size_t det_order = 0; det_order < num_det_orders; ++det_order) { | ||
| std::vector<double> orientation_vector; | ||
| for (size_t i = 0; i < detector_coords.at(0).size(); ++i) { | ||
| orientation_vector.push_back(dist(rng)); | ||
| } | ||
| std::vector<size_t> inv_perm(graph.size()); | ||
| for (size_t i = 0; i < perm.size(); ++i) { | ||
| inv_perm[perm[i]] = i; | ||
| } | ||
| det_orders[det_order] = inv_perm; | ||
| } | ||
| return det_orders; | ||
| } | ||
|
|
||
| for (size_t i = 0; i < detector_coords.size(); ++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]; | ||
| } | ||
| } | ||
| std::vector<size_t> perm(dem.count_detectors()); | ||
| std::iota(perm.begin(), perm.end(), 0); | ||
| std::sort(perm.begin(), perm.end(), [&](const size_t& i, const size_t& j) { | ||
| return inner_products[i] > inner_products[j]; | ||
| }); | ||
| std::vector<size_t> inv_perm(dem.count_detectors()); | ||
| for (size_t i = 0; i < perm.size(); ++i) { | ||
| inv_perm[perm[i]] = i; | ||
| } | ||
| det_orders[det_order] = inv_perm; | ||
| static std::vector<std::vector<size_t>> build_det_orders_coordinate( | ||
| const stim::DetectorErrorModel& dem, size_t num_det_orders, std::mt19937_64& rng) { | ||
| std::vector<std::vector<size_t>> det_orders(num_det_orders); | ||
| auto detector_coords = get_detector_coords(dem); | ||
| std::vector<double> inner_products(dem.count_detectors()); | ||
| std::normal_distribution<double> dist(0, 1); | ||
| if (detector_coords.empty() || detector_coords.at(0).empty()) { | ||
| for (size_t det_order = 0; det_order < num_det_orders; ++det_order) { | ||
| det_orders[det_order].resize(dem.count_detectors()); | ||
| std::iota(det_orders[det_order].begin(), det_orders[det_order].end(), 0); | ||
| } | ||
| return det_orders; | ||
| } | ||
| for (size_t det_order = 0; det_order < num_det_orders; ++det_order) { | ||
| std::vector<double> orientation_vector; | ||
| 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) { | ||
| inner_products[i] = 0; | ||
| for (size_t j = 0; j < orientation_vector.size(); ++j) { | ||
| inner_products[i] += detector_coords[i][j] * orientation_vector[j]; | ||
| } | ||
| } | ||
| } else if (method == DetOrder::DetIndex) { | ||
| std::uniform_int_distribution<int> dist_bool(0, 1); | ||
| size_t n = dem.count_detectors(); | ||
| for (size_t det_order = 0; det_order < num_det_orders; ++det_order) { | ||
| det_orders[det_order].resize(n); | ||
| if (dist_bool(rng)) { | ||
| for (size_t i = 0; i < n; ++i) { | ||
| det_orders[det_order][i] = n - 1 - i; | ||
| } | ||
| } else { | ||
| std::iota(det_orders[det_order].begin(), det_orders[det_order].end(), 0); | ||
| std::vector<size_t> perm(dem.count_detectors()); | ||
| std::iota(perm.begin(), perm.end(), 0); | ||
| std::sort(perm.begin(), perm.end(), [&](const size_t& i, const size_t& j) { | ||
| return inner_products[i] > inner_products[j]; | ||
| }); | ||
| std::vector<size_t> inv_perm(dem.count_detectors()); | ||
| for (size_t i = 0; i < perm.size(); ++i) { | ||
| inv_perm[perm[i]] = i; | ||
| } | ||
| det_orders[det_order] = inv_perm; | ||
| } | ||
| return det_orders; | ||
| } | ||
|
|
||
| static std::vector<std::vector<size_t>> build_det_orders_index(const stim::DetectorErrorModel& dem, | ||
| size_t num_det_orders, | ||
| std::mt19937_64& rng) { | ||
| std::vector<std::vector<size_t>> det_orders(num_det_orders); | ||
| std::uniform_int_distribution<int> dist_bool(0, 1); | ||
| size_t n = dem.count_detectors(); | ||
| for (size_t det_order = 0; det_order < num_det_orders; ++det_order) { | ||
| det_orders[det_order].resize(n); | ||
| if (dist_bool(rng)) { | ||
| for (size_t i = 0; i < n; ++i) { | ||
| det_orders[det_order][i] = n - 1 - i; | ||
| } | ||
| } else { | ||
| std::iota(det_orders[det_order].begin(), det_orders[det_order].end(), 0); | ||
| } | ||
| } | ||
| return det_orders; | ||
| } | ||
|
|
||
| std::vector<std::vector<size_t>> build_det_orders(const stim::DetectorErrorModel& dem, | ||
| size_t num_det_orders, DetOrder method, | ||
| uint64_t seed) { | ||
| std::mt19937_64 rng(seed); | ||
| switch (method) { | ||
| case DetOrder::DetBFS: | ||
| return build_det_orders_bfs(dem, num_det_orders, rng); | ||
| case DetOrder::DetCoordinate: | ||
| return build_det_orders_coordinate(dem, num_det_orders, rng); | ||
| case DetOrder::DetIndex: | ||
| return build_det_orders_index(dem, num_det_orders, rng); | ||
| } | ||
| throw std::invalid_argument("Unknown det order method"); | ||
| } | ||
|
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. I wonder if we should have one of them as default 🤔 |
||
|
|
||
| bool sampling_from_dem(uint64_t seed, size_t num_shots, stim::DetectorErrorModel dem, | ||
| std::vector<stim::SparseShot>& shots) { | ||
| stim::DemSampler<stim::MAX_BITWORD_WIDTH> sampler(dem, std::mt19937_64{seed}, num_shots); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I wonder why we are shuffling here? 🤔
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.
Ah, this is to generate diversity in the ensemble!