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

BitArray.get_counts() fails given an empty classical register #12062

Open
garrison opened this issue Mar 21, 2024 · 3 comments · May be fixed by #12317
Open

BitArray.get_counts() fails given an empty classical register #12062

garrison opened this issue Mar 21, 2024 · 3 comments · May be fixed by #12317
Labels
bug Something isn't working mod: primitives Related to the Primitives module

Comments

@garrison
Copy link
Member

garrison commented Mar 21, 2024

Environment

  • Qiskit version: main
  • Python version: 3.11
  • Operating system: Linux

What is happening?

BitArray is not fully tested with empty classical registers. For instance, get_counts() fails during the reshape if the output has zero bits. It appears that get_int_counts() will have the same problem, too.

How can we reproduce the issue?

import numpy as np
from qiskit.primitives.containers import BitArray

data = BitArray(np.zeros([1024, 0], dtype=np.uint8), 0)
data.get_counts()

results in

Traceback (most recent call last):
  File "/home/garrison/Qiskit/empty-creg-counts.py", line 7, in <module>
    data.get_counts()
  File "/home/garrison/serverless/.direnv/python-3.11.0/lib/python3.11/site-packages/qiskit/primitives/containers/bit_array.py", line 292, in get_counts
    return self._get_counts(loc=loc, converter=converter)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/garrison/serverless/.direnv/python-3.11.0/lib/python3.11/site-packages/qiskit/primitives/containers/bit_array.py", line 146, in _get_counts
    arr = self._array.reshape(-1, self._array.shape[-1]) if loc is None else self._array[loc]
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: cannot reshape array of size 0 into shape (0)

What should happen?

I expect a sensible result, such as {"": 1024} for get_counts() or {0: 1024} for get_int_counts().

Any suggestions?

No response

@atharva-satpute
Copy link
Contributor

Shouldn't it return {} instead of some key-value pairs considering the fact that np.zeros([1024, 0]) is an empty array

@garrison
Copy link
Member Author

Shouldn't it return {} instead of some key-value pairs considering the fact that np.zeros([1024, 0]) is an empty array

That array may be "empty" but it still has a shape. It just means there are 1024 instances of nothing.

Also, following my suggestion means there are fewer special cases a user must worry about; for instance, it maintains that the dict's values will sum up to the total number of shots, even if the register is empty.

@jakelishman jakelishman added the mod: primitives Related to the Primitives module label Apr 14, 2024
@ihincks
Copy link
Contributor

ihincks commented May 2, 2024

Thanks @garrison . It looks like from_samples also breaks with num_bits=0, so we'd need to fix that too for full support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mod: primitives Related to the Primitives module
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants