qiskit icon indicating copy to clipboard operation
qiskit copied to clipboard

Counts with zeros

Open 1ucian0 opened this issue 4 years ago • 8 comments

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.

1ucian0 avatar Oct 11 '20 03:10 1ucian0

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.

1ucian0 avatar Oct 12 '20 18:10 1ucian0

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Feb 09 '21 14:02 CLAassistant

..., maybe we can validate the key before returning 0? Otherwise, maybe limit the max memory_size?

followed that way.

1ucian0 avatar May 10 '21 14:05 1ucian0

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.

ajavadia avatar May 12 '21 17:05 ajavadia

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.

1ucian0 avatar May 12 '21 20:05 1ucian0

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__.

levbishop avatar Aug 09 '21 19:08 levbishop

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__.

Good one! I'm adding that in bfe9b0d223bb1d86317cac5f92d76dd5d457cd85

1ucian0 avatar Aug 16 '21 13:08 1ucian0

This now looks like what I had in mind. @mtreinish do you agree with this approach?

levbishop avatar Aug 17 '21 17:08 levbishop

With #7464 closed, this PR can be closed too.

1ucian0 avatar Mar 11 '23 20:03 1ucian0