-
Notifications
You must be signed in to change notification settings - Fork 73
Support code-declared inlined feedback in memory circuits #665
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
Open
kvmto
wants to merge
7
commits into
NVIDIA:main
Choose a base branch
from
kvmto:inlined_feedback
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e236354
Support code-declared inlined feedback in memory circuits
kvmto 5fef135
Extract inlined-feedback composition rule into host-side apply_inline…
kvmto df24ee3
Restore data-shaped memory_circuit API; extract feedback logic into n…
kvmto 453df00
split between two types of stabiizer
kvmto 74e25a5
fix(qec): harden inlined feedback handling
kvmto 21c232a
refactor(qec): use internal CSR layouts for inlined feedback
kvmto 18673dd
Merge remote-tracking branch 'upstream/main' into inlined_feedback_fixes
kvmto 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
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 |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| /****************************************************************-*- C++ -*-**** | ||
| * Copyright (c) 2024 - 2025 NVIDIA Corporation & Affiliates. * | ||
| * All rights reserved. * | ||
| * * | ||
| * This source code and the accompanying materials are made available under * | ||
| * the terms of the Apache License 2.0 which accompanies this distribution. * | ||
| ******************************************************************************/ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "cudaq.h" | ||
|
|
||
| namespace cudaq::qec { | ||
|
|
||
| /// @brief Number of nonzero entries in row @p row of a row-major 0/1 matrix | ||
| /// that has @p num_cols columns (i.e. the weight of one feedback/observable/ | ||
| /// stabilizer row). | ||
| inline __qpu__ std::size_t | ||
| row_support_weight(const std::vector<std::size_t> &matrix_flat, std::size_t row, | ||
| std::size_t num_cols) { | ||
| std::size_t weight = 0; | ||
| for (std::size_t k = 0; k < num_cols; ++k) | ||
| if (matrix_flat[row * num_cols + k] != 0) | ||
| weight++; | ||
| return weight; | ||
| } | ||
|
|
||
| /// @brief Records for the cross-round detector of syndrome record @p j: the | ||
| /// earlier-vs-current comparison `{prev[j], curr[j]}` followed by the | ||
| /// earlier-round herald records `prev[k]` for each column k (ascending) with | ||
| /// `feedback_flat(j, k) != 0`. | ||
| inline __qpu__ std::vector<cudaq::measure_result> cross_round_detector_records( | ||
| const std::vector<cudaq::measure_result> &prev, | ||
| const std::vector<cudaq::measure_result> &curr, std::size_t j, | ||
| const std::vector<std::size_t> &feedback_flat, std::size_t num_cols) { | ||
| std::size_t weight = row_support_weight(feedback_flat, j, num_cols); | ||
| std::vector<cudaq::measure_result> det(2 + weight); | ||
| det[0] = prev[j]; | ||
| det[1] = curr[j]; | ||
| std::size_t idx = 2; | ||
| for (std::size_t k = 0; k < num_cols; ++k) | ||
| if (feedback_flat[j * num_cols + k] != 0) | ||
| det[idx++] = prev[k]; | ||
| return det; | ||
| } | ||
|
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 function is called one per |
||
|
|
||
| /// @brief Records for the boundary detector of syndrome record @p record_row: | ||
| /// `{last_syndrome[record_row]}`, then the data-qubit readouts | ||
| /// `data_results[q]` for each data qubit q (ascending) in the stabilizer | ||
| /// support `stabilizers[row_base + q]`, then the last-round herald records | ||
| /// `last_syndrome[k]` for each column k (ascending) with | ||
| /// `feedback_flat(record_row, k) != 0`. When @p feedback_flat is empty the | ||
| /// herald part contributes nothing, so one helper serves both the | ||
| /// feedback and legacy branches. | ||
| inline __qpu__ std::vector<cudaq::measure_result> boundary_detector_records( | ||
| const std::vector<cudaq::measure_result> &last_syndrome, | ||
| std::size_t record_row, | ||
| const std::vector<cudaq::measure_result> &data_results, | ||
| const std::vector<std::size_t> &stabilizers, std::size_t row_base, | ||
| std::size_t num_data, const std::vector<std::size_t> &feedback_flat, | ||
| std::size_t num_cols) { | ||
| std::size_t support_weight = 0; | ||
| for (std::size_t q = 0; q < num_data; ++q) | ||
| if (stabilizers[row_base + q] != 0) | ||
| support_weight++; | ||
| std::size_t fb_weight = | ||
| feedback_flat.size() > 0 | ||
| ? row_support_weight(feedback_flat, record_row, num_cols) | ||
| : 0; | ||
| std::vector<cudaq::measure_result> support(1 + support_weight + fb_weight); | ||
| support[0] = last_syndrome[record_row]; | ||
| std::size_t idx = 1; | ||
| for (std::size_t q = 0; q < num_data; ++q) | ||
| if (stabilizers[row_base + q] != 0) | ||
| support[idx++] = data_results[q]; | ||
| if (feedback_flat.size() > 0) | ||
| for (std::size_t k = 0; k < num_cols; ++k) | ||
| if (feedback_flat[record_row * num_cols + k] != 0) | ||
| support[idx++] = last_syndrome[k]; | ||
| return support; | ||
| } | ||
|
|
||
| /// @brief Round-major slice offsets into the observable-feedback buffer. Entry | ||
| /// m is the start index of observable m's slice; each observable occupies | ||
| /// `num_rounds * (weight of obs_feedback_flat row m)` records. The returned | ||
| /// vector has `num_observables + 1` entries; the last entry is the total | ||
| /// buffer size. Returns an empty vector when @p obs_feedback_flat is empty | ||
| /// (no observable feedback declared). | ||
| inline __qpu__ std::vector<std::size_t> | ||
| observable_feedback_offsets(const std::vector<std::size_t> &obs_feedback_flat, | ||
| std::size_t num_observables, std::size_t num_cols, | ||
| std::size_t num_rounds) { | ||
| // Sized (not default) construction: the __qpu__ dialect forbids the default | ||
| // std::vector constructor. An empty obs_feedback_flat yields a size-0 vector. | ||
| std::size_t num_offsets = | ||
| obs_feedback_flat.size() > 0 ? num_observables + 1 : 0; | ||
| std::vector<std::size_t> offsets(num_offsets); | ||
| if (obs_feedback_flat.size() == 0) | ||
| return offsets; | ||
| std::size_t total = 0; | ||
| for (std::size_t m = 0; m < num_observables; ++m) { | ||
| offsets[m] = total; | ||
| total += num_rounds * row_support_weight(obs_feedback_flat, m, num_cols); | ||
| } | ||
| offsets[num_observables] = total; | ||
| return offsets; | ||
| } | ||
|
|
||
| /// @brief Write the records that observable feedback collects during @p round | ||
| /// into the round-major @p obs_fb_records buffer. For each observable m, the | ||
| /// round-r block starts at `offsets[m] + round * (weight of obs_feedback_flat | ||
| /// row m)` and holds `syndrome[k]` for each column k (ascending) with | ||
| /// `obs_feedback_flat(m, k) != 0`. Call with round 0 for the first round's | ||
| /// syndrome and with the running round index for each subsequent round. | ||
| inline __qpu__ void collect_observable_feedback_round( | ||
| std::vector<cudaq::measure_result> &obs_fb_records, | ||
| const std::vector<std::size_t> &offsets, | ||
| const std::vector<cudaq::measure_result> &syndrome, std::size_t round, | ||
| const std::vector<std::size_t> &obs_feedback_flat, | ||
| std::size_t num_observables, std::size_t num_cols) { | ||
| for (std::size_t m = 0; m < num_observables; ++m) { | ||
| std::size_t weight = row_support_weight(obs_feedback_flat, m, num_cols); | ||
| std::size_t idx = offsets[m] + round * weight; | ||
| for (std::size_t k = 0; k < num_cols; ++k) | ||
| if (obs_feedback_flat[m * num_cols + k] != 0) | ||
| obs_fb_records[idx++] = syndrome[k]; | ||
| } | ||
| } | ||
|
|
||
| /// @brief Support records for logical observable @p obs: the feedback records | ||
| /// collected in @p obs_fb_records (round-major, occupying | ||
| /// `[offsets[obs], offsets[obs + 1])`), followed by the data-qubit readouts | ||
| /// `data_results[q]` for each data qubit q (ascending) with | ||
| /// `obs_matrix_flat(obs, q) != 0`. When @p offsets is empty (no observable | ||
| /// feedback declared) the feedback prefix is empty and only the data support | ||
| /// is returned. | ||
| inline __qpu__ std::vector<cudaq::measure_result> observable_support_records( | ||
| std::size_t obs, const std::vector<std::size_t> &obs_matrix_flat, | ||
| std::size_t num_data, | ||
| const std::vector<cudaq::measure_result> &data_results, | ||
| const std::vector<cudaq::measure_result> &obs_fb_records, | ||
| const std::vector<std::size_t> &offsets) { | ||
| std::size_t support_weight = | ||
| row_support_weight(obs_matrix_flat, obs, num_data); | ||
| std::size_t fb_count = 0; | ||
| std::size_t base = 0; | ||
| if (offsets.size() > 0) { | ||
| base = offsets[obs]; | ||
| fb_count = offsets[obs + 1] - offsets[obs]; | ||
| } | ||
| std::vector<cudaq::measure_result> obs_support(fb_count + support_weight); | ||
| for (std::size_t i = 0; i < fb_count; ++i) | ||
| obs_support[i] = obs_fb_records[base + i]; | ||
| std::size_t idx = fb_count; | ||
| for (std::size_t q = 0; q < num_data; ++q) | ||
| if (obs_matrix_flat[obs * num_data + q] != 0) | ||
| obs_support[idx++] = data_results[q]; | ||
| return obs_support; | ||
| } | ||
|
|
||
| } // namespace cudaq::qec | ||
Oops, something went wrong.
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.
Is this function part of the public API? I'm a little confused what it's for, as it seems like the feedback tensors are usually accessed via something like
flatten_feedback_tensor(code.get_inlined_feedback(), ...). If it is supposed to be exposed, it should probably have a Python API.