-
Notifications
You must be signed in to change notification settings - Fork 28
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
Changes from 2 commits
7772ec6
4997ca2
f4dec60
3bb5db5
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 | ||||
---|---|---|---|---|---|---|
|
@@ -66,8 +66,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 +109,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 +122,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 +136,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(cog.pauli_indices) | ||||||
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 realized in my final-review-before-merging that this line has a bug. It should really be
Suggested change
i.e., it needs to consider the actual number of measurement bits given the workaround to #422. I fixed this and added a test (really, a more thorough version of the existing
|
||||||
|
||||||
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. | ||||||
|
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. |
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 :)