-
Notifications
You must be signed in to change notification settings - Fork 3k
Add Counts object #4501
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Counts object #4501
Changes from 4 commits
9740b29
46254a0
57c251c
10a7b7b
f433cbd
b72a2e1
47d1484
9d0d9af
a4e5ce5
3a07fc7
0525230
aea7d97
442db9f
4bdd9a4
8c73ea2
f3eaf08
a51dd21
c249b64
0612298
5a09207
e0c1e8c
3acb00b
c870623
b299e2e
07b0397
f0ffda3
c1d7860
b6a5e05
f53cede
91cf8fb
c3f1508
dc1efdc
6c95b65
791c1e7
cef4a2a
96b1672
03da057
5b0c74c
e142763
31009d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| # -*- coding: utf-8 -*- | ||
|
|
||
| # This code is part of Qiskit. | ||
| # | ||
| # (C) Copyright IBM 2020. | ||
| # | ||
| # This code is licensed under the Apache License, Version 2.0. You may | ||
| # obtain a copy of this license in the LICENSE.txt file in the root directory | ||
| # of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
| # | ||
| # Any modifications or derivative works of this code must retain this | ||
| # copyright notice, and modified files need to carry a notice indicating | ||
| # that they have been altered from the originals. | ||
|
|
||
| """A container class for counts from a circuit execution.""" | ||
|
|
||
| from qiskit.result import postprocess | ||
| from qiskit.result import exceptions | ||
|
|
||
|
|
||
| class Counts(dict): | ||
| """A class to store a counts result from a circuit execution.""" | ||
|
|
||
| def __init__(self, data, name=None, shots=None, time_taken=None, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we storing a lot of extra metadata in this container? There is already an ExperimentResult class for that so it seems to me Counts should just be the counts part of the result. If I want to add additional return data types in the future (which I do!) like ExpectationValue, or Probabilities, there will be a lot of duplication of unneeded metadat. In this case I think the only the counts needs is perhaps the creg_sizes so you can add whitespace to bitstrings during conversion, and memory_slots if you don't have creg_sizes so you know number of bits when converting from hex-format. Shots can be inferred from sum of dict elements, and time taken, name, and metadata seem unnecessary.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The metadata here is used in the v2 providers interface see #4485. In that model the results object will return an
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess I have issues with the V2 provider then, since it will not be suitable for simulators if there is no circuit level result object other than counts that can store different output data. I can leave those comments somewhere else. |
||
| creg_sizes=None, memory_slots=None, **metadata): | ||
| """Build a counts object | ||
|
|
||
| Args: | ||
| data (dict): The dictionary input for the counts. The key should | ||
| be a hexademical string of the form ``"0x4a"`` representing the | ||
| measured classical value from the experiment and the | ||
| dictionary's value is an integer representing the number of | ||
| shots with that result. | ||
| name (str): A string name for the counts object | ||
|
mtreinish marked this conversation as resolved.
Outdated
|
||
| shots (int): The number of shots used in the experiment | ||
|
mtreinish marked this conversation as resolved.
Outdated
|
||
| time_taken (float): The duration of the experiment that generated | ||
|
mtreinish marked this conversation as resolved.
|
||
| the counts | ||
| creg_sizes (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): The number of total ``memory_slots`` in the | ||
| experiment. | ||
| metadata: Any arbitrary key value metadata passed in as kwargs. | ||
|
mtreinish marked this conversation as resolved.
Outdated
|
||
| """ | ||
| self.hex_raw = dict(data) | ||
| header = {} | ||
| self.creg_sizes = creg_sizes | ||
| if self.creg_sizes: | ||
| header['creg_sizes'] = self.creg_sizes | ||
| self.memory_slots = memory_slots | ||
| if self.memory_slots: | ||
| header['memory_slots'] = self.memory_slots | ||
| bin_data = postprocess.format_counts(self.hex_raw, header=header) | ||
| super().__init__(bin_data) | ||
| self.name = name | ||
| self.shots = shots | ||
| self.time_taken = time_taken | ||
| self.metadata = metadata | ||
|
|
||
| def most_frequent(self): | ||
| """Return the most frequent count | ||
|
|
||
| Returns: | ||
| str: The bit string for the most frequent result | ||
| Raises: | ||
| NoMostFrequentCount: when there is >1 count with the same max counts | ||
| """ | ||
| max_value = max(self.values()) | ||
| max_values_counts = [x[0] for x in self.items() if x[1] == max_value] | ||
| if len(max_values_counts) != 1: | ||
| raise exceptions.NoMostFrequentCount( | ||
| "Multiple values have the same maximum counts: %s" % | ||
| ','.join(max_values_counts)) | ||
| return max_values_counts[0] | ||
|
|
||
| def int_outcomes(self): | ||
| """Build a counts dictionary with integer keys instead of count strings | ||
|
|
||
| Returns: | ||
| dict: A dictionary with the keys as integers instead of | ||
| """ | ||
| return {int(key, 0): value for key, value in self.hex_raw.items()} | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| --- | ||
| features: | ||
| - | | ||
| A new class, :class:`qiskit.result.Counts` has been added. This class | ||
| is a subclass of ``dict`` and . It is created by passing a dictionary | ||
|
mtreinish marked this conversation as resolved.
Outdated
|
||
| of counts with the keys being hexademical strings of the form ``'0x4a'`` | ||
| and the value is the number of shots. For example:: | ||
|
|
||
| from qiskit.result import Counts | ||
|
|
||
| counts = Counts({"0x0': 1, '0x1', 3, '0x2': 1020}) | ||
|
|
||
| A Counts object can be treated as a dictionary of binary string keys | ||
| like the output of :meth:`qiskit.result.Result.get_counts`. | ||
Uh oh!
There was an error while loading. Please reload this page.