Skip to content

BitArray.from_samples/from_counts fails with num_bits=None for str outcomes and 0 outcome. #12765

@chriseclectic

Description

@chriseclectic

Environment

  • Qiskit version: 1.1
  • Python version: 3.11
  • Operating system: MacOS

What is happening?

Calling BitArray.from_counts or BitArray.from_samples without specifying the number of bits is supposed to infer the number of bits from the input data, however this currently only seems to work for integer keys > 0.

How can we reproduce the issue?

BitArray.from_samples([s]) or BitArray.from_counts({s: shots})

errors with

File ~/mambaforge/envs/qiskit1/lib/python3.11/site-packages/qiskit/primitives/containers/bit_array.py:305, in BitArray.from_samples(samples, num_bits)
    303 data = b"".join(val.to_bytes(num_bytes, "big") for val in ints)
    304 array = np.frombuffer(data, dtype=np.uint8, count=len(data))
--> 305 return BitArray(array.reshape(-1, num_bytes), num_bits)

ValueError: cannot reshape array of size 0 into shape (0)

for s = 0 or a string, eg s = "000".

Calling BitArray.from_counts ultimately ends up calling BitArray.from_samples and returns the same error.

What should happen?

The number of bits should be inferred correctly for bitstring dicts, or for trivial case of {0: shots} (which should default to 1 bit)

Any suggestions?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingmod: primitivesRelated to the Primitives module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions