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

Make BitArray.{slice_bits,slice_shots,__getitem__} raise IndexError when indices are not valid (backport #12755) #12844

Merged
merged 2 commits into from
Jul 29, 2024

Conversation

mergify[bot]
Copy link
Contributor

@mergify mergify bot commented Jul 29, 2024

Summary

This PR updates BitArray.{slice_bits,slice_shots,__getitem__} to raise IndexError if indices are not valid.

Especially, __getitem__ raise IndexError with appropriate message as follows (ba[0, 0, 0] case).

from qiskit.primitives.containers import BitArray

ba = BitArray.from_counts({"0": 2, "1": 3})
ba = ba.reshape(1, 1)

try:
    ba[1]
except Exception as ex:
    print(type(ex), ex)
try:
    ba[0, 0, 0]
except Exception as ex:
    print(type(ex), ex)
try:
    ba[0, 0, 0, 0]
except Exception as ex:
    print(type(ex), ex)

main branch

<class 'IndexError'> index 1 is out of bounds for axis 0 with size 1
<class 'ValueError'> The input array must have at least two axes.
<class 'ValueError'> BitArrays cannot be sliced along the bits axis, see slice_bits() instead.

this PR

<class 'IndexError'> index 1 is out of bounds for axis 0 with size 1
<class 'IndexError'> BitArray cannot be sliced along the shots axis, use slice_shots() instead.
<class 'IndexError'> BitArray cannot be sliced along the bits axis, use slice_bits() instead.

Details and comments


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

…r` when indices are not valid (#12755)

* Make BitArray.{slice_bits,slice_shots} raise IndexError when indices are out of bounds

* update __getitem__

(cherry picked from commit bfd2eea)

# Conflicts:
#	qiskit/primitives/containers/bit_array.py
@mergify mergify bot requested review from a team as code owners July 29, 2024 15:22
Copy link
Contributor Author

mergify bot commented Jul 29, 2024

Cherry-pick of bfd2eea has failed:

On branch mergify/bp/stable/1.1/pr-12755
Your branch is up to date with 'origin/stable/1.1'.

You are currently cherry-picking commit bfd2eea48.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Changes to be committed:
	new file:   releasenotes/notes/fix-bitarray-slice-bits-shots-c9cb7e5d907722f5.yaml
	modified:   test/python/primitives/containers/test_bit_array.py

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   qiskit/primitives/containers/bit_array.py

To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

@mergify mergify bot added the conflicts used by mergify when there are conflicts in a port label Jul 29, 2024
@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 labels Jul 29, 2024
@github-actions github-actions bot added this to the 1.2.0 milestone Jul 29, 2024
@ElePT ElePT enabled auto-merge July 29, 2024 15:32
@coveralls
Copy link

Pull Request Test Coverage Report for Build 10147304891

Details

  • 7 of 7 (100.0%) changed or added relevant lines in 1 file are covered.
  • 10 unchanged lines in 2 files lost coverage.
  • Overall coverage increased (+0.009%) to 89.953%

Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 4 92.11%
crates/qasm2/src/parse.rs 6 97.61%
Totals Coverage Status
Change from base Build 10110765446: 0.009%
Covered Lines: 61860
Relevant Lines: 68769

💛 - Coveralls

@ElePT ElePT removed the conflicts used by mergify when there are conflicts in a port label Jul 29, 2024
@ElePT ElePT added this pull request to the merge queue Jul 29, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to no response for status checks Jul 29, 2024
@ElePT ElePT added this pull request to the merge queue Jul 29, 2024
Merged via the queue into stable/1.1 with commit 4f09f57 Jul 29, 2024
17 checks passed
@mergify mergify bot deleted the mergify/bp/stable/1.1/pr-12755 branch July 29, 2024 21:21
mtreinish added a commit that referenced this pull request Aug 8, 2024
…ndexErro…" (#12929)

This reverts commit 4f09f57.

This commit reverts #12844, the original PR was making an API change as a bugfix (to make the api consistent with other similar APIs). However it is still a change in the defined stable interface of the class as documented and we shouldn't be changing that per the stability guidelines of the project. Especially in a patch version bugfix release. We can debate whether the change is acceptable for 1.2.0 independently but for a patch version it is definitely out of scope.
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 mod: primitives Related to the Primitives module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants