Skip to content
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

Make marginal_counts act on Result memory #7711

Merged
merged 28 commits into from
Mar 21, 2022

Conversation

peendebak
Copy link
Contributor

@peendebak peendebak commented Feb 25, 2022

Summary

The marginal_counts method only acted on the ExperimentResult.data.counts, not the ExperimentResult.data.memory field. This PR updates the method so that the memory is also marginalized. Fixes #7448

Details and comments

@peendebak peendebak requested a review from a team as a code owner February 25, 2022 19:28
@peendebak peendebak changed the title make marginal_counts act on memory [WIP] make marginal_counts act on memory Feb 25, 2022
@coveralls
Copy link

coveralls commented Feb 27, 2022

Pull Request Test Coverage Report for Build 2015216391

  • 16 of 16 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.004%) to 83.519%

Totals Coverage Status
Change from base Build 2011537247: 0.004%
Covered Lines: 52537
Relevant Lines: 62904

💛 - Coveralls

@peendebak peendebak changed the title [WIP] make marginal_counts act on memory make marginal_counts act on memory Feb 27, 2022
@peendebak peendebak force-pushed the fix/margin_counts_memory branch from 377cf3c to c53dd44 Compare March 1, 2022 08:24
@mtreinish mtreinish self-assigned this Mar 2, 2022
@mtreinish mtreinish added the Changelog: New Feature Include in the "Added" section of the changelog label Mar 2, 2022
Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pushing this up and sorry for the delay in an initial review. I think this is a good start, but I have a few inline comments that I think we should address. The biggest things are:

  1. While this works correctly for the use case you wrote a test for, calling marginal_counts(result) on a result with memory will fail with a TypeError.

  2. I also think we should put the marginalization of memory behind a feature flag (or maybe a separate function) because it is a change in behavior on a potentially inplace modification on a results object which might be unexpected for users. So we should make this opt-in somehow to not surprise people

qiskit/result/utils.py Outdated Show resolved Hide resolved
qiskit/result/utils.py Outdated Show resolved Hide resolved
test/python/result/test_result.py Show resolved Hide resolved
@peendebak
Copy link
Contributor Author

Thanks for pushing this up and sorry for the delay in an initial review. I think this is a good start, but I have a few inline comments that I think we should address. The biggest things are:

  1. While this works correctly for the use case you wrote a test for, calling marginal_counts(result) on a result with memory will fail with a TypeError.
  2. I also think we should put the marginalization of memory behind a feature flag (or maybe a separate function) because it is a change in behavior on a potentially inplace modification on a results object which might be unexpected for users. So we should make this opt-in somehow to not surprise people

@mtreinish I will pick up the inline comments. Initially my idea was that non marginalizing the memory is a bug and fixing it does not require backwards compatibility. But for inplace operations I agree that we should take some care. My proposal: add a flag marginalize_memory with options:

  • marginalize_memory=True: marginalize both inplace and normal
  • marginalize_memory=False: do not marginalize, and remove the memory field from the result as it is invalid (e.g. inconsistent with the counts)
  • marginalize_memory=None: the old behaviour: just leave the memory intact

I would prefer to make True the default, but we could also leave None as the default for backwards compatibility.

@peendebak peendebak requested a review from mtreinish March 18, 2022 11:54
Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick update, the code LGTM now. I think you've covered all the issues in the previous version and the behavior is much more stable and clearly defined now. The only thing missing now is an upgrade section release note documenting the change in behavior around the memory field in the result when calling marginal_counts(). We need to document that the default behavior of maginal_counts() is changing from previous releases, why that change was made, and how users can restore the previous behavior (just by setting marginalize_memory=None) if that's what they need. Once that's in the release note file I think this is good to go.

Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you for the prompt updates.

@mtreinish mtreinish added automerge Changelog: API Change Include in the "Changed" section of the changelog labels Mar 21, 2022
@mtreinish mtreinish changed the title make marginal_counts act on memory Make marginal_counts act on result memory Mar 21, 2022
@mtreinish mtreinish changed the title Make marginal_counts act on result memory Make marginal_counts act on Result memory Mar 21, 2022
@mergify mergify bot merged commit a6b24ee into Qiskit:main Mar 21, 2022
@peendebak peendebak deleted the fix/margin_counts_memory branch March 21, 2022 12:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: API Change Include in the "Changed" section of the changelog Changelog: New Feature Include in the "Added" section of the changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

marginal_counts does not act on memory field
4 participants