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

qml.counts returns all outcomes when requested if MCMs present #6732

Merged
merged 20 commits into from
Jan 3, 2025

Conversation

andrijapau
Copy link
Contributor

@andrijapau andrijapau commented Dec 18, 2024

Context:

Previously, despite using all_outcomes , qml.counts incorrectly returned only the states that have counts present,

import pennylane as qml
dev = qml.device("default.qubit", shots=10)

@qml.qnode(device=dev, mcm_method="one-shot")
def circuit():
    m1 = qml.measure(0)
    return qml.counts(m1, all_outcomes=True)

>>> circuit()
{0.0: 10}

Description of the Change:

If all_outcomes is specified, the intermediate tmp variable (used to keep track of measurement values from MCMs) defaults to a Counter object that initializes all possible basis states as keys with their values set to zero. This counter is then updated based on the measurement results, where each mcm sample contributes to updating the counts, weighted by a validity factor.

This results in the correct output,

dev = qml.device("default.qubit", shots=10)

@qml.qnode(device=dev, mcm_method="one-shot")
def circuit():
    m1 = qml.measure(0)
    return qml.counts(m1, all_outcomes=True)

>>> circuit()
{0.0: 10, 1.0: 0}
# {0.0: 10} if all_outcomes=False

or,

dev = qml.device("default.qubit", shots=10)

@qml.qnode(device=dev, mcm_method="one-shot")
def circuit():
    m1 = qml.measure(0)
    m2 = qml.measure(1)
    return qml.counts([m1,m2], all_outcomes=True)

>>> circuit()
{'00': 10, '01': 0, '10': 0, '11': 0}
# {'00': 10} if all_outcomes=False

Benefits: all_outcomes kwarg in qml.counts now works as intended when mcm_method="one-shot" and MCM operations are present.

Possible Drawbacks: tmp object creation doesn't scale well with higher qubit counts.

Related GitHub Issues: Fixes #6700

[sc-80206]

This comment was marked as resolved.

@andrijapau andrijapau changed the title All states shown if all_outcomes used when MCM present in circuit qml.counts returns all outcomes when requested with MCMs present Dec 18, 2024
@andrijapau andrijapau changed the title qml.counts returns all outcomes when requested with MCMs present qml.counts returns all outcomes when requested if MCMs present Dec 18, 2024
@JerryChen97 JerryChen97 self-requested a review December 18, 2024 21:32
Copy link
Contributor

@JerryChen97 JerryChen97 left a comment

Choose a reason for hiding this comment

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

All good! Thanks a lot for fixing this. Just minor suggestions nonblocking

doc/releases/changelog-dev.md Outdated Show resolved Hide resolved
tests/measurements/test_counts.py Show resolved Hide resolved
Copy link

codecov bot commented Dec 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.60%. Comparing base (6f26036) to head (ff03b5c).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #6732   +/-   ##
=======================================
  Coverage   99.60%   99.60%           
=======================================
  Files         476      476           
  Lines       45224    45232    +8     
=======================================
+ Hits        45047    45055    +8     
  Misses        177      177           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Co-authored-by: Yushao Chen (Jerry) <[email protected]>
Copy link
Contributor

@PietropaoloFrisoni PietropaoloFrisoni left a comment

Choose a reason for hiding this comment

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

Thanks @andrijapau!

doc/releases/changelog-dev.md Outdated Show resolved Hide resolved
tests/test_compiler.py Outdated Show resolved Hide resolved
Copy link
Contributor

@lillian542 lillian542 left a comment

Choose a reason for hiding this comment

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

LGTM!

@andrijapau andrijapau added the do not merge ⚠️ Do not merge the pull request until this label is removed label Jan 2, 2025
@andrijapau andrijapau added this to the v0.40 milestone Jan 3, 2025
@andrijapau andrijapau removed the do not merge ⚠️ Do not merge the pull request until this label is removed label Jan 3, 2025
@andrijapau andrijapau merged commit 2e3cdc8 into master Jan 3, 2025
46 checks passed
@andrijapau andrijapau deleted the fix-counts-with-mcm branch January 3, 2025 21:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] qml.counts of a mid-circuit measurement does not return all outcomes even when requested
4 participants