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 BitArray.from_counts/from_samples to not fail for input with only 0 outcome and num_bits=None (backport #12800) #12820

Merged
merged 1 commit into from
Jul 26, 2024

Conversation

mergify[bot]
Copy link
Contributor

@mergify mergify bot commented Jul 26, 2024

Summary

Partial fix for #12765.

That issue points out two problems with BitArray.from_counts / BitArray.from_samples. The more severe problem is that these methods fail with an error if the input (e.g. a counts dictionary) contains only the 0 outcome, and num_bits is left at the default value of None.

The methods try to infer num_bits as the number of bits needed to represent the largest outcome (e.g. bitstring) contained in the input data. If the largest outcome is 0 when converted to an integer (e.g. the bitstring "0"), the number of bits inferred is 0, which can be surprising, and also leads to an error.

This small PR interprets 0 as represented by 1 bit instead of by 0 bits, the suggested correct behavior in #12765.

The less severe problem, not fixed here, is that currently any information about num_bits present in the input bitstring lengths gets ignored. That problem seemed less trivial to me. I put some thoughts here.


This is an automatic backport of pull request #12800 done by [Mergify](https://mergify.com).

…only `0` outcome and `num_bits=None` (#12800)

* interpret `0` as represented by 1 bit

* Test conversion of counts w only `0` outcome

* add release note

(cherry picked from commit c40ce83)
@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 following people are relevant to this code:

  • @Qiskit/terra-core
  • @ajavadia
  • @levbishop
  • @t-imamichi

@github-actions github-actions bot added Changelog: Bugfix Include in the "Fixed" section of the changelog mod: primitives Related to the Primitives module Community PR PRs from contributors that are not 'members' of the Qiskit repo labels Jul 26, 2024
@github-actions github-actions bot added this to the 1.2.0 milestone Jul 26, 2024
@coveralls
Copy link

Pull Request Test Coverage Report for Build 10109349537

Details

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • 5 unchanged lines in 2 files lost coverage.
  • Overall coverage increased (+0.01%) to 89.952%

Files with Coverage Reduction New Missed Lines %
qiskit/transpiler/passes/layout/vf2_post_layout.py 1 88.54%
crates/qasm2/src/lex.rs 4 93.13%
Totals Coverage Status
Change from base Build 10097354143: 0.01%
Covered Lines: 66096
Relevant Lines: 73479

💛 - Coveralls

@ElePT ElePT enabled auto-merge July 26, 2024 10:45
@ElePT ElePT added this pull request to the merge queue Jul 26, 2024
Merged via the queue into stable/1.2 with commit 7f75ce2 Jul 26, 2024
18 checks passed
@mergify mergify bot deleted the mergify/bp/stable/1.2/pr-12800 branch July 26, 2024 12:20
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 Community PR PRs from contributors that are not 'members' of the Qiskit repo mod: primitives Related to the Primitives module
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants