---
title: counts (latest version)
description: API reference for qiskit_addon_sqd.counts in the latest version of qiskit-addon-sqd
source: https://eu-de.quantum.cloud.ibm.com/docs/en/api/qiskit-addon-sqd/counts
---

# Counts

`qiskit_addon_sqd.counts`

Functions for transforming counts dictionaries.

### counts\_to\_arrays

`counts_to_arrays(counts)`

[GitHub](https://github.com/Qiskit/qiskit-addon-sqd/tree/stable/0.12/qiskit_addon_sqd/counts.py#L24-L42)

Convert a counts dictionary into a bitstring matrix and a probability array.

**Parameters**

**counts** ([*Mapping*](https://docs.python.org/3/library/collections.abc.html#collections.abc.Mapping)*\[*[*str*](https://docs.python.org/3/library/stdtypes.html#str)*,* [*float*](https://docs.python.org/3/library/functions.html#float)  *|*[*int*](https://docs.python.org/3/library/functions.html#int)*]*) – The counts dictionary to convert

**Returns**

- A 2D array representing the sampled bitstrings. Each row represents a bitstring, and each element is a `bool` representation of the bit’s value
- A 1D array containing the probability with which each bitstring was sampled

**Return type**

[tuple](https://docs.python.org/3/library/stdtypes.html#tuple)\[[*ndarray*](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray), [*ndarray*](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray)]

### generate\_counts\_uniform

`generate_counts_uniform(num_samples, num_bits, rand_seed=None)`

[GitHub](https://github.com/Qiskit/qiskit-addon-sqd/tree/stable/0.12/qiskit_addon_sqd/counts.py#L64-L97)

Generate a bitstring counts dictionary of samples drawn from the uniform distribution.

**Parameters**

- **num\_samples** ([*int*](https://docs.python.org/3/library/functions.html#int)) – The number of samples to draw
- **num\_bits** ([*int*](https://docs.python.org/3/library/functions.html#int)) – The number of bits in the bitstrings
- **rand\_seed** ([*Generator*](https://numpy.org/doc/stable/reference/random/generator.html#numpy.random.Generator)  *|*[*int*](https://docs.python.org/3/library/functions.html#int) *| None*) – A seed for controlling randomness

**Returns**

A dictionary mapping bitstrings of length `num_bits` to the number of times they were sampled.

**Raises**

[**ValueError**](https://docs.python.org/3/library/exceptions.html#ValueError) – `num_samples` and `num_bits` must be positive integers.

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

### generate\_counts\_bipartite\_hamming

`generate_counts_bipartite_hamming(num_samples, num_bits, *, hamming_right, hamming_left, rand_seed=None)`

[GitHub](https://github.com/Qiskit/qiskit-addon-sqd/tree/stable/0.12/qiskit_addon_sqd/counts.py#L120-L173)

Generate a bitstring counts dictionary with specified bipartite hamming weight.

**Parameters**

- **num\_samples** ([*int*](https://docs.python.org/3/library/functions.html#int)) – The number of samples to draw
- **num\_bits** ([*int*](https://docs.python.org/3/library/functions.html#int)) – The number of bits in the bitstrings
- **hamming\_right** ([*int*](https://docs.python.org/3/library/functions.html#int)) – The hamming weight on the right half of each bitstring
- **hamming\_left** ([*int*](https://docs.python.org/3/library/functions.html#int)) – The hamming weight on the left half of each bitstring
- **rand\_seed** ([*Generator*](https://numpy.org/doc/stable/reference/random/generator.html#numpy.random.Generator)  *|*[*int*](https://docs.python.org/3/library/functions.html#int) *| None*) – A seed for controlling randomness

**Returns**

A dictionary mapping bitstrings to the number of times they were sampled. Each half of each bitstring in the output dictionary will have a hamming weight as specified by the inputs.

**Raises**

- [**ValueError**](https://docs.python.org/3/library/exceptions.html#ValueError) – `num_bits` and `num_samples` must be positive integers.
- [**ValueError**](https://docs.python.org/3/library/exceptions.html#ValueError) – Hamming weights must be specified as non-negative integers.
- [**ValueError**](https://docs.python.org/3/library/exceptions.html#ValueError) – `num_bits` must be even.

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

### normalize\_counts\_dict

`normalize_counts_dict(counts)`

[GitHub](https://github.com/Qiskit/qiskit-addon-sqd/tree/stable/0.12/qiskit_addon_sqd/counts.py#L176-L183)

Convert a counts dictionary into a probability dictionary.

**Parameters**

**counts** ([*Mapping*](https://docs.python.org/3/library/collections.abc.html#collections.abc.Mapping)*\[*[*str*](https://docs.python.org/3/library/stdtypes.html#str)*,* [*float*](https://docs.python.org/3/library/functions.html#float)  *|*[*int*](https://docs.python.org/3/library/functions.html#int)*]*)

**Return type**

[*Mapping*](https://docs.python.org/3/library/collections.abc.html#collections.abc.Mapping)\[[str](https://docs.python.org/3/library/stdtypes.html#str), [float](https://docs.python.org/3/library/functions.html#float)]

### bit\_array\_to\_arrays

`bit_array_to_arrays(bit_array)`

[GitHub](https://github.com/Qiskit/qiskit-addon-sqd/tree/stable/0.12/qiskit_addon_sqd/counts.py#L45-L61)

Convert a bit array into a bitstring matrix and a probability array.

**Parameters**

**bit\_array** ([*BitArray*](/docs/api/qiskit/qiskit.primitives.BitArray)) – The bit array to convert

**Returns**

- A 2D array representing the sampled bitstrings. Each row represents a bitstring, and each element is a `bool` representation of the bit’s value
- A 1D array containing the probability with which each bitstring was sampled

**Return type**

[tuple](https://docs.python.org/3/library/stdtypes.html#tuple)\[[*ndarray*](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray), [*ndarray*](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray)]

### generate\_bit\_array\_uniform

`generate_bit_array_uniform(num_samples, num_bits, rand_seed=None)`

[GitHub](https://github.com/Qiskit/qiskit-addon-sqd/tree/stable/0.12/qiskit_addon_sqd/counts.py#L100-L117)

Generate a bit array of samples drawn from the uniform distribution.

**Parameters**

- **num\_samples** ([*int*](https://docs.python.org/3/library/functions.html#int)) – The number of samples to draw
- **num\_bits** ([*int*](https://docs.python.org/3/library/functions.html#int)) – The number of bits in the bitstrings
- **rand\_seed** ([*Generator*](https://numpy.org/doc/stable/reference/random/generator.html#numpy.random.Generator)  *|*[*int*](https://docs.python.org/3/library/functions.html#int) *| None*) – A seed for controlling randomness

**Returns**

The sampled bit array.

**Raises**

[**ValueError**](https://docs.python.org/3/library/exceptions.html#ValueError) – `num_samples` and `num_bits` must be positive integers.

**Return type**

[*BitArray*](/docs/api/qiskit/qiskit.primitives.BitArray)
