Skip to content

Conversation

t-imamichi
Copy link
Member

@t-imamichi t-imamichi commented Jul 11, 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

@t-imamichi t-imamichi requested review from a team as code owners July 11, 2024 07:39
@qiskit-bot
Copy link
Collaborator

One or more of the following people are relevant to this code:

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

@t-imamichi t-imamichi added the mod: primitives Related to the Primitives module label Jul 11, 2024
@t-imamichi t-imamichi mentioned this pull request Jul 11, 2024
@coveralls
Copy link

Pull Request Test Coverage Report for Build 9887440194

Details

  • 7 of 7 (100.0%) changed or added relevant lines in 1 file are covered.
  • 11 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.009%) to 89.884%

Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 5 91.6%
crates/qasm2/src/parse.rs 6 97.61%
Totals Coverage Status
Change from base Build 9876062430: -0.009%
Covered Lines: 65741
Relevant Lines: 73140

💛 - Coveralls

@ElePT ElePT added this to the 1.2.0 milestone Jul 22, 2024
@t-imamichi t-imamichi requested a review from ihincks July 26, 2024 08:39
@ElePT ElePT added the Changelog: Bugfix Include in the "Fixed" section of the changelog label Jul 26, 2024
Copy link
Contributor

@ElePT ElePT left a comment

Choose a reason for hiding this comment

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

This looks like a straightforward fix that improves the BitArray user experience. I think it should be backported to 1.1 and 1.2.

@t-imamichi t-imamichi added the stable backport potential The bug might be minimal and/or import enough to be port to stable label Jul 29, 2024
@ElePT ElePT added this pull request to the merge queue Jul 29, 2024
@ElePT
Copy link
Contributor

ElePT commented Jul 29, 2024

@Mergifyio backport stable/1.1

Copy link
Contributor

mergify bot commented Jul 29, 2024

backport stable/1.1

✅ Backports have been created

Merged via the queue into Qiskit:main with commit bfd2eea Jul 29, 2024
1 check passed
mergify bot pushed a commit that referenced this pull request Jul 29, 2024
…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 bot pushed a commit that referenced this pull request Jul 29, 2024
…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)
github-merge-queue bot pushed a commit that referenced this pull request Jul 29, 2024
…r` when indices are not valid (#12755) (#12845)

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

* update __getitem__

(cherry picked from commit bfd2eea)

Co-authored-by: Takashi Imamichi <31178928+t-imamichi@users.noreply.github.com>
Procatv pushed a commit to Procatv/qiskit-terra-catherines that referenced this pull request Aug 1, 2024
…r` when indices are not valid (Qiskit#12755)

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

* update __getitem__
jakelishman pushed a commit to jakelishman/qiskit-terra that referenced this pull request Aug 2, 2024
…r` when indices are not valid (backport Qiskit#12755) (Qiskit#12844)

* Make `BitArray.{slice_bits,slice_shots,__getitem__}` raise `IndexError` when indices are not valid (Qiskit#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

* Update bit_array.py

---------

Co-authored-by: Takashi Imamichi <31178928+t-imamichi@users.noreply.github.com>
Co-authored-by: Elena Peña Tapia <57907331+ElePT@users.noreply.github.com>
@mtreinish mtreinish mentioned this pull request Aug 8, 2024
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 stable backport potential The bug might be minimal and/or import enough to be port to stable
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants