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

Raise TypeError when a dictionary is passed to MeasurementResult constructor #2523

Merged
merged 4 commits into from
Oct 8, 2024

Conversation

natestemen
Copy link
Member

Motivation: Multiple students at the UMass Amherst summer school attempted to pass a dictionary to instantiate a MeasurementResult object while working through the challenges. This silently accepts, but provides erroneous results. E.g.:

>>> from mitiq import MeasurementResult
>>> MeasurementResult({'001': 23, '000': 43})
MeasurementResult: {'nqubits': 3, 'qubit_indices': (0, 1, 2), 'shots': 2, 'counts': {'001': 1, '000': 1}}

Considerations: I could see the following two potential solutions to help with this issue.

  1. Add a typecheck at runtime and raise a TypeError, letting the user know we have a method MeasurementResult.from_counts for this specific pattern.
  2. Call MeasurementResult.from_counts in the __post_init__ method to re-instantiate self.

I chose to raise solution 1 as it seems to be a simple approach to address the problem. Admittedly, I am less familiar with the technical details of re-instantiating an object within the __post_init__ function, but happy to test that out if reviewers think it is more appropriate.

it's natural to think that you can pass results directly from some
sort of experiment into the MeasurementResult class.
@natestemen natestemen requested review from cosenal and Misty-W October 2, 2024 00:24
@natestemen natestemen self-assigned this Oct 2, 2024
Copy link

codecov bot commented Oct 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.72%. Comparing base (411e234) to head (b6d77e9).
Report is 5 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2523   +/-   ##
=======================================
  Coverage   98.72%   98.72%           
=======================================
  Files          90       90           
  Lines        4156     4158    +2     
=======================================
+ Hits         4103     4105    +2     
  Misses         53       53           

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

Copy link
Contributor

@cosenal cosenal left a comment

Choose a reason for hiding this comment

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

Solution 1 sounds reasonable to me, and a strict improvement on the current behavior.

Note: in the docstring, we should have an example of how to instantiate MeasurementResult, at the moment it's not very clear (unless I am overlooking it).

@@ -125,6 +125,11 @@ class MeasurementResult:

def __post_init__(self) -> None:
# Validate arguments
if isinstance(self.result, dict):
Copy link
Contributor

@cosenal cosenal Oct 3, 2024

Choose a reason for hiding this comment

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

We could have something stricter that checks for result not being an instance of Sequence[Bitstring], which is what we expect as type, but then both the check and the error message become more complicated, so I would leave it as is.

@natestemen
Copy link
Member Author

Thanks for the suggestion of adding an example in the docstring. I like that. Done in 71a0a63.

@natestemen natestemen merged commit 0526250 into main Oct 8, 2024
18 checks passed
@natestemen natestemen deleted the nts-meas-res-defense branch October 8, 2024 03:54
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.

2 participants