-
Notifications
You must be signed in to change notification settings - Fork 603
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
BugFix - Returning sums of observables on the same wires provides incorrect results #5978
BugFix - Returning sums of observables on the same wires provides incorrect results #5978
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #5978 +/- ##
==========================================
- Coverage 99.67% 99.67% -0.01%
==========================================
Files 425 425
Lines 40924 40630 -294
==========================================
- Hits 40791 40496 -295
- Misses 133 134 +1 ☔ View full report in Codecov by Sentry. |
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.
Looks good to me, thanks for tracking this down @PietropaoloFrisoni 🎉
Potentially we can re-use |
At some point, I agree. It seems to me that this would require changing the grouping strategy and the current logic of |
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.
LGTM
Context:
These are the codes that detected this bug in the first place. I report the current (correct) result and the previous (incorrect) result:
The bug is caused by the current logic in the
qml.devices.qubit.measure_with_samples
function.This function internally calls the
_group_measurements
function and selects a different measure function for each group returned by the former.From now on, we assume that
qml.devices.qubit
is the current directory.The grouping strategy in the
_group_measurements
function is such that_measure_sum_with_samples
is the selected measurement function when a sum is involved in the group. Unfortunately, the logic in the grouping process and_measure_sum_with_samples
cannot correctly handle the case in which there are multiple measurement processes and one involves a sum of operators acting on the same wire.I tried several different solutions, noticing that:
Many functions in the source code depend on the
measure_with_samples
function. Any 'major change' to this function results in test failures across PL. Re-designing the entire logic of this function is beyond the scope of this PR.Changing just the grouping logic in
_group_measurements
or the workflow in_measure_sum_with_samples
cannot really solve this issue, since the logic of the entire function is 'entangled' among all its components.There is a way to save goats and cabbages.
Description of the Change:
In the
_group_measurements
function, we simplify the measurement processes involving a sum. This leaves untouched the cases for which the function was originally designed, but converts the sum of operators acting on the same wires into aSProd
. Therefore, the logic in_group_measurements
becomes compatible with the workflow of themeasure_with_sample
function.That is,
_measure_with_samples_diagonalizing_gates
is the chosen measurement function in place of_measure_sum_with_samples
. The latter is still the selected measurement function for sums of different operators and/or sums of the same operator acting on the different wires (I suspect these were the cases taken into account by the original author in the first place).Benefits: The function now returns the correct result.
Possible Drawbacks: We are simply re-routing an internal possible workflow of the
measure_with_samples
function to make it compatible with the logic intended by the author. Although everything is possible, it is unlikely that any major problem is caused directly by this PR.Related GitHub Issues: #5976
Related ShortCut Stories [sc-68218]