Skip to content
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

Remove zero-probability states from Sampler's result #9248

Merged
merged 14 commits into from
Jan 19, 2023

Conversation

t-imamichi
Copy link
Member

@t-imamichi t-imamichi commented Dec 6, 2022

Summary

Fixes #9178

Requires #9258 to pass the unit tests

Details and comments

@t-imamichi t-imamichi requested review from a team and ikkoham as code owners December 6, 2022 03:09
@qiskit-bot
Copy link
Collaborator

Thank you for opening a new pull request.

Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient.

While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone.

One or more of the the following people are requested to review this:

@Cryoris
Copy link
Contributor

Cryoris commented Dec 6, 2022

LGTM, could you add a reno since this changes the results? 🙂

@t-imamichi
Copy link
Member Author

Sure. I also need to fix some tests and a sampler gradient.

@t-imamichi
Copy link
Member Author

I added a reno

@coveralls
Copy link

coveralls commented Dec 8, 2022

Pull Request Test Coverage Report for Build 3956431316

  • 4 of 4 (100.0%) changed or added relevant lines in 1 file are covered.
  • 8 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.002%) to 84.926%

Files with Coverage Reduction New Missed Lines %
src/vf2_layout.rs 8 86.44%
Totals Coverage Status
Change from base Build 3954112093: 0.002%
Covered Lines: 66541
Relevant Lines: 78352

💛 - Coveralls

Comment on lines 261 to 263
if use_sampler == "ideal":
self.assertTupleEqual(keys, ("00", "01", "10", "11"))
np.testing.assert_allclose(values, [0, 0, 0, 1], atol=0.2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that 0 probabilities are not included, shouldn't this just show the "11" key here? That also seems to be what the error is about 🙂

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideal case should include "00", "01", and "10" with very small probabilities. The test on Linux passes but that on Mac fails. I'm investigating the difference.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot reproduce the CI failure locally on MacOS with Python 3.7 🤔

The values with which "00", "01" and "10" are included are about 1e-34 maybe we don't really need to check that this is recognized as non-zero? This seems a bit volatile to me... since this is about algorithm performance rather than the technicality which element is populated and from the sampler tests we know that the zero-probability states are removed, how about just checking if the values are correct in the Grover test? E.g. like

if i in (0, 3):
    values = [dist.get(key, 0) for key in ["00", "01", "10", "11"]]
    np.testing.assert_allclose(values, [0, 0, 0, 1], atol=0.2)

Copy link
Contributor

@jlapeyre jlapeyre Jan 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I makes sense to me to filter out probabilities like this:

For the exact distribution: Remove probabilities satisfying $p_i \ll 1/|p| $. The reasoning is that the sum of any set of $p_i$ s of this magnitude is negligible. On the other hand we may want to just remove what we think is in some sense a numerical artifact. This might not be easy to determine. For example 1e-34 is probably safe to remove. But the former at least has a reason behind the choice. You still have to decide what is much less than one. Maybe 1e-10

For the distribution from samples, you should remove a probability if $p_i / n_\text{shots} \ll 1$. This will make sampling efficient.

But you want to do something to remove small probabilities in both cases. The dicts could otherwise consume a lot of memory and be populated mostly with garbage.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will use decimals of Statevector.probabilities to round the results.

@Cryoris Cryoris added this to the 0.23.0 milestone Dec 20, 2022
@ikkoham ikkoham requested a review from ElePT as a code owner January 18, 2023 00:12
@jlapeyre
Copy link
Contributor

jlapeyre commented Jan 18, 2023

EDIT: After looking more closely, I made further comments along the same lines, above.

This looks good. I have one comment. I think we need the ability to set a threshold, which is supported by the method probabilities_dict. For example, even when there is no noise, a QFT can result in amplitude that decays with distance from peaks.

I suppose that could be added to the API later.

EDIT: Rereading the discussion above, it looks like this is already an issue in the tests.

The code in quantum_info that removes zeros actually does rounding. It then checks the floating point results for equality with zero. Instead, it seems a little more to-the-point to generate bools, like this

inds = (np.abs(x) > tol).nonzero()[0]
return {i: x[i] for i in inds} 

This will leave all of the accepted values unchanged. Dunno, maybe there is a good reason to also round them. Using bools is a bit faster. But not so much to make a decision based on efficiency.

But probably the best place to make that change (if at all) is in quantum_info .

@ikkoham
Copy link
Contributor

ikkoham commented Jan 19, 2023

I'm trying to pass the CI right now, but it's quite a struggle.

Copy link
Contributor

@ikkoham ikkoham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mergify mergify bot merged commit a0466b3 into Qiskit:main Jan 19, 2023
@t-imamichi t-imamichi deleted the sampler-zero-filter branch January 19, 2023 12:17
ElePT pushed a commit to ElePT/qiskit that referenced this pull request Jun 27, 2023
* Remove zero probability from Sampler's result

* fix TestGrover

* add reno

* fix a test

* round probabilities

Co-authored-by: Julien Gacon <[email protected]>
Co-authored-by: Ikko Hamamura <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
ElePT pushed a commit to ElePT/qiskit-algorithms-test that referenced this pull request Jul 17, 2023
)

* Remove zero probability from Sampler's result

* fix TestGrover

* add reno

* fix a test

* round probabilities

Co-authored-by: Julien Gacon <[email protected]>
Co-authored-by: Ikko Hamamura <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mod: primitives Related to the Primitives module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unnecessary zero-probability states returned by Sampler, with different behaviour to Aer Sampler
6 participants