-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Add Counts object #4501
Conversation
This commit adds a new class qiskit.result.Counts which is a dict subclass for dealing with count results from experiment. It takes in a counts dictionary with hexadecimal string keys and it's data is a binary string, like the output of get_counts(). The idea is that this class contains extra attributes about the execution and helper methods for interacting with counts results. It can be used as a direct replacment anywhere a counts dictionary was used previously. It is also now the return type for qiskit.quantum_info.QuantumState.sample_counts(). This commit is part of the v2 providers interface work and will be a key part of the new providers interface. This class is split out as a seperate commit/PR from Qiskit#4485.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add tests for the two methods int_outcomes
and most_frequent
?
Also how does Counts
object print? Does it need a __str__
or __repr__
? Mainly because right now it is straightforward to see the counts and that behavior should persist.
…into add-counts-object
Oh, oops I wrote tests but forgot to add it to the commit. Fixed in: f433cbd |
As for the printing it will work exactly the same as the dict does from |
…into add-counts-object
qiskit/result/counts.py
Outdated
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, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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 OrderedDict
of Counts objects instead of a of ExperimentResult objects. So the metadata for an individual run will be stored here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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.
Accidentally submitted before writing my main comment in the review. My main issue with this is if we are adding a class abstract why are we forcing ourselves to use the hex-string format when initializing. Regardless of what internal representation this class uses to store counts I think it should be able to be initialized from hex string counts, binary string counts, or integer key counts, and also apply conversion to any of these formats from whatever format it was initialized (you have one method for this for converting to int keys). |
Another issue is that the
will return |
This is a fair point I was thinking about doing this, but kept it simple. I'll work on adding the arbitrary conversions between the 3 forms. The default data format will still have to be the bitstrings for backwards compat with v2
I actually think this will work as is today. Right now it calls |
I actually like the metadata. Much easier to access than from the Results. Also nice since direct access to the results is something we are moving away from. |
This commit allows for the input key format for the data dict to be hex, binary, or integers and use all the functionality as expected. It also adds handling for dit strings which don't handle the hex or binary output formats or allow for formating across registers.
This commit removes the metadata support from the counts object. There is some debate on wheter we want to enable support for attaching arbitrary key value metadata to a Counts object or not. Until that debate is settled this commit removes it from the object to unblock progress. It will be simple to add in a backwards compat manner if it decided that we want to enable this in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest removing all the non-count related metadata from the Counts class in this PR until we discuss more under the V2 provider, other than that this looks good. This way we can merge it as a replacement for the current dict returned by get_counts
until other things are agreed on.
* Add Counts object This commit adds a new class qiskit.result.Counts which is a dict subclass for dealing with count results from experiment. It takes in a counts dictionary with hexadecimal string keys and it's data is a binary string, like the output of get_counts(). The idea is that this class contains extra attributes about the execution and helper methods for interacting with counts results. It can be used as a direct replacment anywhere a counts dictionary was used previously. It is also now the return type for qiskit.quantum_info.QuantumState.sample_counts(). This commit is part of the v2 providers interface work and will be a key part of the new providers interface. This class is split out as a seperate commit/PR from Qiskit#4485. * Fix docs build * Fix lint * Add missing test file * Switch custom exception class to QiskitError * Cleanup release note * Add comment about dict subclassing * Tweak comment wording slightly * Add support for int and binary input keys and handle qudits This commit allows for the input key format for the data dict to be hex, binary, or integers and use all the functionality as expected. It also adds handling for dit strings which don't handle the hex or binary output formats or allow for formating across registers. * Fix tests * Fix lint * Handle 0b prefixed binary strings * Update init docs * Fix docs again * Add tests for 0b prefixed input * Remove metadata from Counts object This commit removes the metadata support from the counts object. There is some debate on wheter we want to enable support for attaching arbitrary key value metadata to a Counts object or not. Until that debate is settled this commit removes it from the object to unblock progress. It will be simple to add in a backwards compat manner if it decided that we want to enable this in the future. * Use Counts instead of dict for Results.get_counts() output * Remove other circuit level metadata * Update results to not include removed metadata * Fix tests * Really remove metadata from class Co-authored-by: Luciano Bello <luciano.bello@ibm.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Summary
This commit adds a new class
qiskit.result.Counts
which is a dictsubclass for dealing with count results from experiment. It takes in a
counts dictionary with hexadecimal string keys and it's data is a binary
string, like the output of
get_counts()
. The idea is that this classcontains extra attributes about the execution and helper methods for
interacting with counts results. It can be used as a direct replacement
anywhere a counts dictionary was used previously. It is also now the
return type for
qiskit.quantum_info.QuantumState.sample_counts()
.This commit is part of the v2 providers interface work and will be a key
part of the new providers interface. This class is split out as a
seperate commit/PR from #4485.
Details and comments