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

MeasurementValue raises an error when used as boolean #6386

Merged
merged 10 commits into from
Oct 15, 2024

Conversation

albi3ro
Copy link
Contributor

@albi3ro albi3ro commented Oct 11, 2024

Context:

Users might be able to do:

@qml.qnode
def f():
   ...
   m = qml.measure()
   if m == 1:
      # do something
   ...

without an error, since a measurement value is always treated as truthy, even if that is not what the user was meaning to do.

Description of the Change:

Raises a ValueError if someone tries to treat a MeasurementValue as a boolean.

Benefits:

Reduces confusion and mistakes when trying to condition on a mid circuit measurement.

Possible Drawbacks:

Related GitHub Issues:

Copy link

codecov bot commented Oct 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.39%. Comparing base (9ac2504) to head (57d6743).
Report is 4 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6386      +/-   ##
==========================================
- Coverage   99.71%   99.39%   -0.32%     
==========================================
  Files         448      448              
  Lines       42466    42472       +6     
==========================================
- Hits        42346    42217     -129     
- Misses        120      255     +135     

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

Copy link
Contributor

@dime10 dime10 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 fix!

@albi3ro
Copy link
Contributor Author

albi3ro commented Oct 15, 2024

[sc-76023]

@albi3ro albi3ro merged commit 0f76651 into master Oct 15, 2024
36 of 37 checks passed
@albi3ro albi3ro deleted the fix-measurement-value-truthiness branch October 15, 2024 17:24
austingmhuang pushed a commit that referenced this pull request Oct 23, 2024
**Context:**

Users might be able to do:
```
@qml.qnode
def f():
   ...
   m = qml.measure()
   if m == 1:
      # do something
   ...
```

without an error, since a measurement value is always treated as truthy,
even if that is not what the user was meaning to do.

**Description of the Change:**

Raises a `ValueError` if someone tries to treat a `MeasurementValue` as
a boolean.

**Benefits:**

Reduces confusion and mistakes when trying to condition on a mid circuit
measurement.

**Possible Drawbacks:**

**Related GitHub Issues:**
mudit2812 pushed a commit that referenced this pull request Nov 11, 2024
**Context:**

Users might be able to do:
```
@qml.qnode
def f():
   ...
   m = qml.measure()
   if m == 1:
      # do something
   ...
```

without an error, since a measurement value is always treated as truthy,
even if that is not what the user was meaning to do.

**Description of the Change:**

Raises a `ValueError` if someone tries to treat a `MeasurementValue` as
a boolean.

**Benefits:**

Reduces confusion and mistakes when trying to condition on a mid circuit
measurement.

**Possible Drawbacks:**

**Related GitHub Issues:**
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.

3 participants