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

# Counts

*class* `qiskit.result.Counts(data, time_taken=None, creg_sizes=None, memory_slots=None)`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.5/qiskit/result/counts.py#L27-L194)

Bases: [`dict`](https://docs.python.org/3/library/stdtypes.html#dict)

A class to store a counts result from a circuit execution.

Build a counts object

**Parameters**

- **data** ([*dict*](https://docs.python.org/3/library/stdtypes.html#dict)) –

  The dictionary input for the counts. Where the keys represent a measured classical value and the value is an integer the number of shots with that result. All keys must be of the same format. This format must be one of the following.

  > - A hexadecimal string of the form `'0x4a'`
  > - A bit string prefixed with `0b`, for example `'0b1011'`
  > - A bit string with no prefix, for example `'1011'`
  > - A bit string formatted across register and memory slots. For example, `'00 10'`.
  > - A dit string, for example `'02'`. Note for objects created with dit strings the `creg_sizes` and `memory_slots` kwargs don’t work and [`hex_outcomes()`](#qiskit.result.Counts.hex_outcomes "qiskit.result.Counts.hex_outcomes") and [`int_outcomes()`](#qiskit.result.Counts.int_outcomes "qiskit.result.Counts.int_outcomes") also do not work.

- **time\_taken** ([*float*](https://docs.python.org/3/library/functions.html#float)) – The duration of the experiment that generated the counts in seconds.

- **creg\_sizes** ([*list*](https://docs.python.org/3/library/stdtypes.html#list)) – a nested list where the inner element is a list of tuples containing both the classical register name and classical register size. For example, `[('c_reg', 2), ('my_creg', 4)]`.

- **memory\_slots** ([*int*](https://docs.python.org/3/library/functions.html#int)) – The number of total `memory_slots` in the experiment.

**Raises**

- [**TypeError**](https://docs.python.org/3/library/exceptions.html#TypeError) – If the input key type is not an `int` or `str`, or if the input keys are not all of the same type.
- [**QiskitError**](/docs/api/qiskit/exceptions#qiskit.exceptions.QiskitError "qiskit.exceptions.QiskitError") – If a dit string key is input with `creg_sizes` and/or `memory_slots`.

## Attributes

### bitstring\_regex

Default value: `re.compile('^[01\\s]+$')`

## Methods

### clear

`clear()`

Remove all items from the dict.

### copy

`copy()`

Return a shallow copy of the dict.

### fromkeys

*classmethod* `fromkeys(iterable, value=None, /)`

Create a new dictionary with keys from iterable and values set to value.

### get

`get(key, default=None, /)`

Return the value for key if key is in the dictionary, else default.

### hex\_outcomes

`hex_outcomes()`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.5/qiskit/result/counts.py#L142-L163)

Return a counts dictionary with hexadecimal string keys

**Returns**

**A dictionary with the keys as hexadecimal strings instead of**

bitstrings

**Return type**

[dict](https://docs.python.org/3/library/stdtypes.html#dict)

**Raises**

[**QiskitError**](/docs/api/qiskit/exceptions#qiskit.exceptions.QiskitError "qiskit.exceptions.QiskitError") – If the Counts object contains counts for dit strings

### int\_outcomes

`int_outcomes()`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.5/qiskit/result/counts.py#L165-L185)

Build a counts dictionary with integer keys instead of count strings

**Returns**

A dictionary with the keys as integers instead of bitstrings

**Return type**

[dict](https://docs.python.org/3/library/stdtypes.html#dict)

**Raises**

[**QiskitError**](/docs/api/qiskit/exceptions#qiskit.exceptions.QiskitError "qiskit.exceptions.QiskitError") – If the Counts object contains counts for dit strings

### items

`items()`

Return a set-like object providing a view on the dict’s items.

### keys

`keys()`

Return a set-like object providing a view on the dict’s keys.

### most\_frequent

`most_frequent()`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.5/qiskit/result/counts.py#L123-L140)

Return the most frequent count

**Returns**

The bit string for the most frequent result

**Return type**

[str](https://docs.python.org/3/library/stdtypes.html#str)

**Raises**

[**QiskitError**](/docs/api/qiskit/exceptions#qiskit.exceptions.QiskitError "qiskit.exceptions.QiskitError") – when there is >1 count with the same max counts, or an empty object.

### pop

`pop(k[, d]) → v, remove specified key and return the corresponding value.`

If the key is not found, return the default if given; otherwise, raise a KeyError.

### popitem

`popitem()`

Remove and return a (key, value) pair as a 2-tuple.

Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.

### setdefault

`setdefault(key, default=None, /)`

Insert key with a value of default if key is not in the dictionary.

Return the value for key if key is in the dictionary, else default.

### shots

`shots()`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.5/qiskit/result/counts.py#L192-L194)

Return the number of shots

### update

`update([E, ]**F) → None.  Update D from mapping/iterable E and F.`

If E is present and has a .keys() method, then does: for k in E.keys(): D\[k] = E\[k] If E is present and lacks a .keys() method, then does: for k, v in E: D\[k] = v In either case, this is followed by: for k in F: D\[k] = F\[k]

### values

`values()`

Return an object providing a view on the dict’s values.
