---
title: BitArray (v1.0)
description: API reference for qiskit.primitives.BitArray in qiskit v1.0
source: https://eu-de.quantum.cloud.ibm.com/docs/en/api/qiskit/1.0/qiskit.primitives.BitArray
---

# BitArray

*class* `qiskit.primitives.BitArray(array, num_bits)`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/1.0/qiskit/primitives/containers/bit_array.py#L40-L349)

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*](https://docs.python.org/3/library/functions.html#int)) – How many bit are in each outcome.

**Raises**

- [**TypeError**](https://docs.python.org/3/library/exceptions.html#TypeError) – If the input is not a NumPy array with type `numpy.uint8`.
- [**ValueError**](https://docs.python.org/3/library/exceptions.html#ValueError) – If the input array has fewer than two axes, or the size of the last axis is not the smallest number of bytes that can contain `num_bits`.

## 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`](#qiskit.primitives.BitArray.array "qiskit.primitives.BitArray.array").

### shape

### size

## Methods

### bitcount

`bitcount()`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/1.0/qiskit/primitives/containers/bit_array.py#L153-L159)

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*](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray)\[[*Any*](https://docs.python.org/3/library/typing.html#typing.Any), [*dtype*](https://numpy.org/doc/stable/reference/generated/numpy.dtype.html#numpy.dtype)\[*uint64*]]

### from\_bool\_array

*static* `from_bool_array(array, order='big')`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/1.0/qiskit/primitives/containers/bit_array.py#L161-L192)

Construct a new bit array from an array of bools.

**Parameters**

- **array** ([*ndarray*](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray)*\[*[*Any*](https://docs.python.org/3/library/typing.html#typing.Any)*,* [*dtype*](https://numpy.org/doc/stable/reference/generated/numpy.dtype.html#numpy.dtype)*\[*[*bool\_*](https://numpy.org/doc/stable/reference/arrays.scalars.html#numpy.bool_)*]]*) – The array to convert, with “bitstrings” along the last axis.
- **order** ([*Literal*](https://docs.python.org/3/library/typing.html#typing.Literal)*\['big', 'little']*) – One of `"big"` or `"little"`, indicating whether `array[..., 0]` correspond to the most significant bits or the least significant bits of each bitstring, respectively.

**Returns**

A new bit array.

**Return type**

[*BitArray*](#qiskit.primitives.BitArray "qiskit.primitives.containers.bit_array.BitArray")

### from\_counts

*static* `from_counts(counts, num_bits=None)`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/1.0/qiskit/primitives/containers/bit_array.py#L194-L235)

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**

- **counts** (*Mapping\[*[*str*](https://docs.python.org/3/library/stdtypes.html#str)  *|*[*int*](https://docs.python.org/3/library/functions.html#int)*,* [*int*](https://docs.python.org/3/library/functions.html#int)*] | Iterable\[Mapping\[*[*str*](https://docs.python.org/3/library/stdtypes.html#str)  *|*[*int*](https://docs.python.org/3/library/functions.html#int)*,* [*int*](https://docs.python.org/3/library/functions.html#int)*]]*) – One or more counts-like mappings with the same number of shots.
- **num\_bits** ([*int*](https://docs.python.org/3/library/functions.html#int) *| None*) – The desired number of bits per shot. If unset, the biggest value found sets this value.

**Returns**

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

**Raises**

- [**ValueError**](https://docs.python.org/3/library/exceptions.html#ValueError) – If different mappings have different numbers of shots.
- [**ValueError**](https://docs.python.org/3/library/exceptions.html#ValueError) – If no counts dictionaries are supplied.

**Return type**

[BitArray](#qiskit.primitives.BitArray "qiskit.primitives.BitArray")

### from\_samples

*static* `from_samples(samples, num_bits=None)`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/1.0/qiskit/primitives/containers/bit_array.py#L237-L278)

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()`](#qiskit.primitives.BitArray.reshape "qiskit.primitives.BitArray.reshape") if your samples represent nested data.

**Parameters**

- **samples** (*Iterable\[*[*str*](https://docs.python.org/3/library/stdtypes.html#str)*] | Iterable\[*[*int*](https://docs.python.org/3/library/functions.html#int)*]*) – A list of bitstrings, a list of integers, or a list of hexstrings.
- **num\_bits** ([*int*](https://docs.python.org/3/library/functions.html#int) *| None*) – The desired number of bits per sample. If unset, the biggest sample provided is used to determine this value.

**Returns**

A new bit array.

**Raises**

[**ValueError**](https://docs.python.org/3/library/exceptions.html#ValueError) – If no strings are given.

**Return type**

[BitArray](#qiskit.primitives.BitArray "qiskit.primitives.BitArray")

### get\_bitstrings

`get_bitstrings(loc=None)`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/1.0/qiskit/primitives/containers/bit_array.py#L308-L321)

Return a list of bitstrings.

**Parameters**

**loc** ([*int*](https://docs.python.org/3/library/functions.html#int)  *|*[*tuple*](https://docs.python.org/3/library/stdtypes.html#tuple)*\[*[*int*](https://docs.python.org/3/library/functions.html#int)*, ...] | 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](https://docs.python.org/3/library/stdtypes.html#list)\[[str](https://docs.python.org/3/library/stdtypes.html#str)]

### get\_counts

`get_counts(loc=None)`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/1.0/qiskit/primitives/containers/bit_array.py#L280-L292)

Return a counts dictionary with bitstring keys.

**Parameters**

**loc** ([*int*](https://docs.python.org/3/library/functions.html#int)  *|*[*tuple*](https://docs.python.org/3/library/stdtypes.html#tuple)*\[*[*int*](https://docs.python.org/3/library/functions.html#int)*, ...] | 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](https://docs.python.org/3/library/stdtypes.html#dict)\[[str](https://docs.python.org/3/library/stdtypes.html#str), [int](https://docs.python.org/3/library/functions.html#int)]

### get\_int\_counts

`get_int_counts(loc=None)`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/1.0/qiskit/primitives/containers/bit_array.py#L294-L306)

Return a counts dictionary, where bitstrings are stored as `int`s.

**Parameters**

**loc** ([*int*](https://docs.python.org/3/library/functions.html#int)  *|*[*tuple*](https://docs.python.org/3/library/stdtypes.html#tuple)*\[*[*int*](https://docs.python.org/3/library/functions.html#int)*, ...] | 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](https://docs.python.org/3/library/stdtypes.html#dict)\[[int](https://docs.python.org/3/library/functions.html#int), [int](https://docs.python.org/3/library/functions.html#int)]

### reshape

`reshape(*shape)`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/1.0/qiskit/primitives/containers/bit_array.py#L323-L349)

Return a new reshaped bit array.

The [`num_shots`](#qiskit.primitives.BitArray.num_shots "qiskit.primitives.BitArray.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*](https://docs.python.org/3/library/functions.html#int)  *|*[*Iterable*](https://docs.python.org/3/library/typing.html#typing.Iterable)*\[*[*int*](https://docs.python.org/3/library/functions.html#int) *| Iterable\[ShapeInput]]*) – The new desired shape.

**Returns**

A new bit array.

**Raises**

[**ValueError**](https://docs.python.org/3/library/exceptions.html#ValueError) – If the size corresponding to your new shape is not equal to either [`size`](#qiskit.primitives.BitArray.size "qiskit.primitives.BitArray.size"), or the product of [`size`](#qiskit.primitives.BitArray.size "qiskit.primitives.BitArray.size") and [`num_shots`](#qiskit.primitives.BitArray.num_shots "qiskit.primitives.BitArray.num_shots").

**Return type**

[*BitArray*](#qiskit.primitives.BitArray "qiskit.primitives.containers.bit_array.BitArray")
