-
Notifications
You must be signed in to change notification settings - Fork 34
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
Swap register order, removing need to pass num_qpd_bits
#434
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7772ec6
Swap register order, removing need to pass `num_qpd_bits`
garrison 4997ca2
Merge branch 'main' into swap-register-order
garrison f4dec60
Fix reconstruction when subsystem contains no observable
garrison 3bb5db5
Merge branch 'main' into swap-register-order
garrison 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 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 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 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 |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
from ..utils.observable_grouping import CommutingObservableGroup, ObservableCollection | ||
from ..utils.bitwise import bit_count | ||
from .cutting_decomposition import decompose_observables | ||
from .cutting_experiments import _get_pauli_indices | ||
from .qpd import WeightType | ||
|
||
|
||
|
@@ -66,8 +67,6 @@ def reconstruct_expectation_values( | |
Raises: | ||
ValueError: ``observables`` and ``results`` are of incompatible types. | ||
ValueError: An input observable has a phase not equal to 1. | ||
ValueError: ``num_qpd_bits`` must be set for all result metadata dictionaries. | ||
TypeError: ``num_qpd_bits`` must be an integer. | ||
""" | ||
if isinstance(observables, PauliList) and not isinstance(results, SamplerResult): | ||
raise ValueError( | ||
|
@@ -111,21 +110,7 @@ def reconstruct_expectation_values( | |
for k, cog in enumerate(so.groups): | ||
quasi_probs = results_dict[label].quasi_dists[i * len(so.groups) + k] | ||
for outcome, quasi_prob in quasi_probs.items(): | ||
try: | ||
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. niiiiice |
||
num_qpd_bits = results_dict[label].metadata[ | ||
i * len(so.groups) + k | ||
]["num_qpd_bits"] | ||
except KeyError as ex: | ||
raise ValueError( | ||
"The num_qpd_bits field must be set in each subexperiment " | ||
"result metadata dictionary." | ||
) from ex | ||
else: | ||
subsystem_expvals[k] += quasi_prob * _process_outcome( | ||
num_qpd_bits, | ||
cog, | ||
outcome, | ||
) | ||
subsystem_expvals[k] += quasi_prob * _process_outcome(cog, outcome) | ||
|
||
for k, subobservable in enumerate(subobservables_by_subsystem[label]): | ||
current_expvals[k] *= np.mean( | ||
|
@@ -138,16 +123,12 @@ def reconstruct_expectation_values( | |
|
||
|
||
def _process_outcome( | ||
num_qpd_bits: int, cog: CommutingObservableGroup, outcome: int | str, / | ||
cog: CommutingObservableGroup, outcome: int | str, / | ||
) -> np.typing.NDArray[np.float64]: | ||
""" | ||
Process a single outcome of a QPD experiment with observables. | ||
|
||
Args: | ||
num_qpd_bits: The number of QPD measurements in the circuit. It is | ||
assumed that the second to last creg in the generating circuit | ||
is the creg containing the QPD measurements, and the last | ||
creg is associated with the observable measurements. | ||
cog: The observable set being measured by the current experiment | ||
outcome: The outcome of the classical bits | ||
|
||
|
@@ -156,15 +137,11 @@ def _process_outcome( | |
this vector correspond to the elements of ``cog.commuting_observables``, | ||
and each result will be either +1 or -1. | ||
""" | ||
num_meas_bits = len(_get_pauli_indices(cog)) | ||
|
||
outcome = _outcome_to_int(outcome) | ||
try: | ||
qpd_outcomes = outcome & ((1 << num_qpd_bits) - 1) | ||
except TypeError as ex: | ||
raise TypeError( | ||
f"num_qpd_bits must be an integer, but a {type(num_qpd_bits)} was passed." | ||
) from ex | ||
|
||
meas_outcomes = outcome >> num_qpd_bits | ||
meas_outcomes = outcome & ((1 << num_meas_bits) - 1) | ||
qpd_outcomes = outcome >> num_meas_bits | ||
|
||
# qpd_factor will be -1 or +1, depending on the overall parity of qpd | ||
# measurements. | ||
|
This file contains 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 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 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 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 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
9 changes: 9 additions & 0 deletions
9
releasenotes/notes/register_order_swapped-5b07fb661c6250f9.yaml
This file contains 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,9 @@ | ||
--- | ||
upgrade: | ||
- | | ||
The order of the classical registers in the generated experiments | ||
has been swapped. The ``"observable_measurements"`` register now | ||
comes first, and the ``"qpd_measurements"`` register now comes | ||
second. As a result of this change, it is no longer necessary to | ||
manually insert ``num_qpd_bits`` into the ``metadata`` for each | ||
experiment's result. |
This file contains 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
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.
Neat code here where the else sits outside the looped if ... I'll have to glance at how this works :)