From 7a7cb667600a6c59055a68c762099bf175938420 Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Thu, 8 Aug 2024 17:21:09 -0400 Subject: [PATCH] =?UTF-8?q?Revert=20"Make=20`BitArray.{slice=5Fbits,slice?= =?UTF-8?q?=5Fshots,=5F=5Fgetitem=5F=5F}`=20raise=20`IndexErro=E2=80=A6"?= =?UTF-8?q?=20(#12929)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 4f09f57a365fd6a3ec337f7026d3540a812c2662. 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. --- qiskit/primitives/containers/bit_array.py | 21 +++++++----------- ...ray-slice-bits-shots-c9cb7e5d907722f5.yaml | 11 ---------- .../primitives/containers/test_bit_array.py | 22 ++++--------------- 3 files changed, 12 insertions(+), 42 deletions(-) delete mode 100644 releasenotes/notes/fix-bitarray-slice-bits-shots-c9cb7e5d907722f5.yaml diff --git a/qiskit/primitives/containers/bit_array.py b/qiskit/primitives/containers/bit_array.py index 5f73339a306d..f52c852c35fd 100644 --- a/qiskit/primitives/containers/bit_array.py +++ b/qiskit/primitives/containers/bit_array.py @@ -131,15 +131,10 @@ def __repr__(self): def __getitem__(self, indices): """Slices the array along an existing axis of the array.""" - if isinstance(indices, tuple): - if len(indices) == self.ndim + 1: - raise IndexError( - "BitArray cannot be sliced along the shots axis, use slice_shots() instead." - ) - if len(indices) >= self.ndim + 2: - raise IndexError( - "BitArray cannot be sliced along the bits axis, use slice_bits() instead." - ) + if isinstance(indices, tuple) and len(indices) >= self.ndim + 2: + raise ValueError( + "BitArrays cannot be sliced along the bits axis, see slice_bits() instead." + ) return BitArray(self._array[indices], self.num_bits) @property @@ -433,13 +428,13 @@ def slice_bits(self, indices: int | Sequence[int]) -> "BitArray": A bit array sliced along the bit axis. Raises: - IndexError: If there are any invalid indices of the bit axis. + ValueError: If there are any invalid indices of the bit axis. """ if isinstance(indices, int): indices = (indices,) for index in indices: if index < 0 or index >= self.num_bits: - raise IndexError( + raise ValueError( f"index {index} is out of bounds for the number of bits {self.num_bits}." ) # This implementation introduces a temporary 8x memory overhead due to bit @@ -460,13 +455,13 @@ def slice_shots(self, indices: int | Sequence[int]) -> "BitArray": A bit array sliced along the shots axis. Raises: - IndexError: If there are any invalid indices of the shots axis. + ValueError: If there are any invalid indices of the shots axis. """ if isinstance(indices, int): indices = (indices,) for index in indices: if index < 0 or index >= self.num_shots: - raise IndexError( + raise ValueError( f"index {index} is out of bounds for the number of shots {self.num_shots}." ) arr = self._array diff --git a/releasenotes/notes/fix-bitarray-slice-bits-shots-c9cb7e5d907722f5.yaml b/releasenotes/notes/fix-bitarray-slice-bits-shots-c9cb7e5d907722f5.yaml deleted file mode 100644 index 56fd81012428..000000000000 --- a/releasenotes/notes/fix-bitarray-slice-bits-shots-c9cb7e5d907722f5.yaml +++ /dev/null @@ -1,11 +0,0 @@ ---- -upgrade_primitives: - - | - :meth:`.BitArray.slice_bits` and :meth:`.BitArray.slice_shots` - will now raise ``IndexError`` when indices are out of bounds. - They used to raise ``ValueError`` in the case. - - | - :meth:`.BitArray.__getitem__` will now raise ``IndexError`` - when indices are out of bounds or the number of dimensions - of indices does not match that of BitArray. - They used to raise ``ValueError`` in the case. diff --git a/test/python/primitives/containers/test_bit_array.py b/test/python/primitives/containers/test_bit_array.py index d4733388d87c..1c8d489b38e4 100644 --- a/test/python/primitives/containers/test_bit_array.py +++ b/test/python/primitives/containers/test_bit_array.py @@ -527,20 +527,6 @@ def test_getitem(self): for j in range(2): self.assertEqual(ba.get_counts((0, j, 2)), ba2.get_counts(j)) - with self.subTest("errors"): - with self.assertRaisesRegex(IndexError, "index 2 is out of bounds"): - _ = ba[0, 2, 2] - with self.assertRaisesRegex(IndexError, "index -3 is out of bounds"): - _ = ba[0, -3, 2] - with self.assertRaisesRegex( - IndexError, "BitArray cannot be sliced along the shots axis" - ): - _ = ba[0, 1, 2, 3] - with self.assertRaisesRegex( - IndexError, "BitArray cannot be sliced along the bits axis" - ): - _ = ba[0, 1, 2, 3, 4] - def test_slice_bits(self): """Test the slice_bits method.""" # this creates incrementing bitstrings from 0 to 59 @@ -585,9 +571,9 @@ def test_slice_bits(self): self.assertEqual(ba2.get_counts((i, j, k)), expect) with self.subTest("errors"): - with self.assertRaisesRegex(IndexError, "index -1 is out of bounds"): + with self.assertRaisesRegex(ValueError, "index -1 is out of bounds"): _ = ba.slice_bits(-1) - with self.assertRaisesRegex(IndexError, "index 9 is out of bounds"): + with self.assertRaisesRegex(ValueError, "index 9 is out of bounds"): _ = ba.slice_bits(9) def test_slice_shots(self): @@ -635,9 +621,9 @@ def test_slice_shots(self): self.assertEqual(ba2.get_bitstrings((i, j, k)), expected) with self.subTest("errors"): - with self.assertRaisesRegex(IndexError, "index -1 is out of bounds"): + with self.assertRaisesRegex(ValueError, "index -1 is out of bounds"): _ = ba.slice_shots(-1) - with self.assertRaisesRegex(IndexError, "index 10 is out of bounds"): + with self.assertRaisesRegex(ValueError, "index 10 is out of bounds"): _ = ba.slice_shots(10) def test_expectation_values(self):