Skip to main contentIBM Quantum Documentation

BitArray

qiskit.primitives.BitArray(array, num_bits)

GitHub(opens in a new tab)

Bases: ShapedMixin

Stores an array of bit values.

This object contains a single, contiguous block of data that represents an array of bitstrings. The last axis is over packed bits, the second last axis is over shots, and the preceding axes correspond to the shape of the pub that was executed to sample these bits.

Parameters

  • array (NDArray[np.uint8]) – The uint8 data array.
  • num_bits (int(opens in a new tab)) – How many bit are in each outcome.

Raises


Attributes

array

The raw NumPy array of data.

ndim

num_bits

The number of bits in the register that this array stores data for.

For example, a ClassicalRegister(5, "meas") would result in num_bits=5.

num_shots

The number of shots sampled from the register in each configuration.

More precisely, the length of the second last axis of array.

shape

size


Methods

bitcount

bitcount()

GitHub(opens in a new tab)

Compute the number of ones appearing in the binary representation of each shot.

Returns

A numpy.uint64-array with shape (*shape, num_shots).

Return type

ndarray(opens in a new tab)[Any(opens in a new tab), dtype(opens in a new tab)[uint64]]

from_bool_array

static from_bool_array(array, order='big')

GitHub(opens in a new tab)

Construct a new bit array from an array of bools.

Parameters

Returns

A new bit array.

Return type

BitArray

from_counts

static from_counts(counts, num_bits=None)

GitHub(opens in a new tab)

Construct a new bit array from one or more Counts-like objects.

The counts can have keys that are (uniformly) integers, hexstrings, or bitstrings. Their values represent numbers of occurrences of that value.

Parameters

Returns

A new bit array with shape () for single input counts, or (N,) for an iterable of NN counts.

Raises

Return type

BitArray

from_samples

static from_samples(samples, num_bits=None)

GitHub(opens in a new tab)

Construct a new bit array from an iterable of bitstrings, hexstrings, or integers.

All samples are assumed to be integers if the first one is. Strings are all assumed to be bitstrings whenever the first string doesn’t start with "0x".

Consider pairing this method with reshape() if your samples represent nested data.

Parameters

Returns

A new bit array.

Raises

ValueError(opens in a new tab) – If no strings are given.

Return type

BitArray

get_bitstrings

get_bitstrings(loc=None)

GitHub(opens in a new tab)

Return a list of bitstrings.

Parameters

loc (int(opens in a new tab) |tuple(opens in a new tab)[int(opens in a new tab), ...] | None) – Which entry of this array to return a dictionary for. If None, counts from all positions in this array are unioned together.

Returns

A list of bitstrings.

Return type

list(opens in a new tab)[str(opens in a new tab)]

get_counts

get_counts(loc=None)

GitHub(opens in a new tab)

Return a counts dictionary with bitstring keys.

Parameters

loc (int(opens in a new tab) |tuple(opens in a new tab)[int(opens in a new tab), ...] | None) – Which entry of this array to return a dictionary for. If None, counts from all positions in this array are unioned together.

Returns

A dictionary mapping bitstrings to the number of occurrences of that bitstring.

Return type

dict(opens in a new tab)[str(opens in a new tab), int(opens in a new tab)]

get_int_counts

get_int_counts(loc=None)

GitHub(opens in a new tab)

Return a counts dictionary, where bitstrings are stored as ints.

Parameters

loc (int(opens in a new tab) |tuple(opens in a new tab)[int(opens in a new tab), ...] | None) – Which entry of this array to return a dictionary for. If None, counts from all positions in this array are unioned together.

Returns

A dictionary mapping ints to the number of occurrences of that int.

Return type

dict(opens in a new tab)[int(opens in a new tab), int(opens in a new tab)]

reshape

reshape(*shape)

GitHub(opens in a new tab)

Return a new reshaped bit array.

The num_shots axis is either included or excluded from the reshaping procedure depending on which picture the new shape is compatible with. For example, for a bit array with shape (20, 5) and 64 shots, a reshape to (100,) would leave the number of shots intact, whereas a reshape to (200, 32) would change the number of shots to 32.

Parameters

*shape (int(opens in a new tab) |Iterable(opens in a new tab)[int(opens in a new tab) | Iterable[ShapeInput]]) – The new desired shape.

Returns

A new bit array.

Raises

ValueError(opens in a new tab) – If the size corresponding to your new shape is not equal to either size, or the product of size and num_shots.

Return type

BitArray

Was this page helpful?
Report a bug or request content on GitHub.