-
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
Should the keys in result.Counts be ordered ? #4985
Comments
It was mostly done this way for compatibility with the |
This makes sense since python dicts are now ordered. But, in applications I am playing with now, under python 3.8, the strings are not ordered. Since insertion order is preserved, this tells me that the strings are not ordered when the source dict is created. They also don't appear to be inserted in the order in which the results are obtained (which should be random) because many strings with very low counts come before strings with very high counts. |
Well we can look at sorting them before they're added to the https://github.com/Qiskit/qiskit-terra/blob/master/qiskit/result/result.py#L250 change that to be something like dict_list.append(Counts(sorted(self.data(key)['counts']), **counts_header)) but before we open a PR doing this we should benchmark it to see what the practical overhead of sorting is. If it's minimal lets go for it, but if it adds a noticeable amount of overhead I'd say lets just add a helper method (or kwargs to existing ones) to |
result = ...
raw_dict = result.data(0)['counts']
counts = result.get_counts()
ms = counts.memory_slots
def makeCounts(d, memory_slots):
sk = sorted(d) # returns sorted keys
sd = {k: d[k] for k in sk}
return Counts(sd, memory_slots=memory_slots) Then In [1]: len(raw_dict)
Out[1]: 25
In [2]: %timeit Counts(raw_dict, memory_slots=ms)
38.1 µs ± 230 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [3]: %timeit makeCounts(raw_dict, memory_slots=ms)
40.4 µs ± 94.3 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each) In [2]: len(raw_dict)
Out[2]: 100
In [3]: %timeit Counts(raw_dict, memory_slots=ms)
118 µs ± 408 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [4]: %timeit makeCounts(raw_dict, memory_slots=ms)
123 µs ± 422 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
|
I can imagine situation where a user wants the counts ordered by value instead, as she might be interested in the most statistically significant results only... |
This issue was recently added in the "To Do" list of Backends, Qobj, and Result . Requires more information on the status. |
this use cases is covered with the |
@mtreinish, is the performance analysis done by @jlapeyre enough to make a decision about this issue? |
To reproduce the issue, notice that the output of the following code result in an unsorted key:
|
In the interests of trimming old issues, I'll close this as stale now. There is more than one ordering that might be useful to users, and "arbitrary" is the cheapest to construct, which is convenient for performance-focussed users who want to do their own post-processing. If there's more to discuss, feel free to re-open. fwiw, sorting this particular object in Python in the order requested is as simple as |
What is the expected enhancement?
result.Counts
would be more convenient if it were a subclass ofOrderedDict
. The keys are bit strings, which have a natural order, so it would be useful to preserve the order. For example, currently, when displaying the counts interactively, the keys (bit strings) are not ordered.Is there a reason this choice was made ?
If ordering the counts is not desirable (too expensive or not necessary for most uses, etc.) an alternative is a function that returns the an ordered dict.
The text was updated successfully, but these errors were encountered: