qiskit
qiskit copied to clipboard
Counts with zeros
Inspired by https://quantumcomputing.stackexchange.com/questions/14029/return-outputs-with-zero-counts
Fixes #6937 Fixes #7464
~This PR adds a zero-default when key not found and extends qiskit.result.Counts
and qiskit.result.Result.get_counts
with include_zeros
(bool, default False
) to extend the key with zero values.~
This PR "mocks" a complete Count dict by returning 0 when the key is missed, considering the possible values of a particular memory size.
I disagree with the idea of adding a default value for all invalid keys.
Fair enough. Ill remove that part and we can discuss it later.
I addressed the rest of the comments.
..., maybe we can validate the key before returning
0
? Otherwise, maybe limit the maxmemory_size
?
followed that way.
Better be very careful that this does not build all the keys (even valid ones), as that grows exponentially. This is why a dict was used in the first place. The limiting factor should always be number of shots not the possible keys.
Also I don't know why we need to do this. The answer in that stackexchange link seems perfectly reasonable.
Better be very careful that this does not build all the keys (even valid ones), as that grows exponentially. This is why a dict was used in the first place. The limiting factor should always be number of shots not the possible keys.
The current code does not create all the keys but behaves more like a default dict up-to the memory size.
I'm not super familiar with this stuff, but I think it might be easier to derive from UserDict
(which itself is a Mapping
) and override __missing__
rather than __getitem__
.
I'm not super familiar with this stuff, but I think it might be easier to derive from
UserDict
(which itself is aMapping
) and override__missing__
rather than__getitem__
.
Good one! I'm adding that in bfe9b0d223bb1d86317cac5f92d76dd5d457cd85
This now looks like what I had in mind. @mtreinish do you agree with this approach?
With #7464 closed, this PR can be closed too.