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

Fix handling of numpy integers in marginal_distribution() #9976

Merged
merged 3 commits into from
Apr 17, 2023

Conversation

mtreinish
Copy link
Member

Summary

This commit fixes an oversight in the input type checking for the marginal_distribution() function. The function which is primarily written in rust has some quick input type checking to raise a more descriptive TypeError if the input distribution/counts object isn't of a known type (and also to dispatch to the correct rust function). This type checking was overly restrictive and would cause an error to be raised if an input counts dictionary was using numpy integer types as a value instead of a python int. This commit updates the type check to treat numpy ints as valid too.

Details and comments

This commit fixes an oversight in the input type checking for the
marginal_distribution() function. The function which is primarily
written in rust has some quick input type checking to raise a more
descriptive TypeError if the input distribtion/counts object isn't of a
known type (and also to dispatch to the correct rust function). This
type checking was overly restrictive and would cause an error to be
raised if an input counts dictionary was using numpy integer types as
a value instead of a python int. This commit updates the type check to
treat numpy ints as valid too.
@mtreinish mtreinish added the Changelog: Bugfix Include in the "Fixed" section of the changelog label Apr 17, 2023
@mtreinish mtreinish requested a review from a team as a code owner April 17, 2023 12:28
@qiskit-bot
Copy link
Collaborator

Thank you for opening a new pull request.

Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient.

While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone.

One or more of the the following people are requested to review this:

  • @Qiskit/terra-core

mtreinish added a commit to mtreinish/qiskit-experiments that referenced this pull request Apr 17, 2023
jakelishman
jakelishman previously approved these changes Apr 17, 2023
Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

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

Technically I think the float check in the elif branch can fail if people are using Numpy half- or single-precision floats (or quad-precision!), but I don't know if we care about that. I don't even know if PyO3 would do the conversion in those cases.

@coveralls
Copy link

coveralls commented Apr 17, 2023

Pull Request Test Coverage Report for Build 4721469484

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • 31 unchanged lines in 3 files lost coverage.
  • Overall coverage decreased (-0.01%) to 85.814%

Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/expr.rs 1 93.76%
crates/qasm2/src/lex.rs 6 90.89%
crates/qasm2/src/parse.rs 24 96.18%
Totals Coverage Status
Change from base Build 4721358293: -0.01%
Covered Lines: 70483
Relevant Lines: 82135

💛 - Coveralls

@jakelishman
Copy link
Member

This needs a black reformat on the test file. (Pretty sure it's just an extra blank line under the second added test.)

@jakelishman jakelishman added this pull request to the merge queue Apr 17, 2023
Merged via the queue into Qiskit:main with commit c4602ea Apr 17, 2023
@mtreinish mtreinish deleted the marginal-np-ints branch May 3, 2023 19:33
king-p3nguin pushed a commit to king-p3nguin/qiskit-terra that referenced this pull request May 22, 2023
* Fix handling of numpy integers in marginal_distribution()

This commit fixes an oversight in the input type checking for the
marginal_distribution() function. The function which is primarily
written in rust has some quick input type checking to raise a more
descriptive TypeError if the input distribtion/counts object isn't of a
known type (and also to dispatch to the correct rust function). This
type checking was overly restrictive and would cause an error to be
raised if an input counts dictionary was using numpy integer types as
a value instead of a python int. This commit updates the type check to
treat numpy ints as valid too.

* Add support for np float values too
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: Bugfix Include in the "Fixed" section of the changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants