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

Add shots.bins() generator method #5476

Merged

Conversation

Tarun-Kumar07
Copy link
Contributor

@Tarun-Kumar07 Tarun-Kumar07 commented Apr 5, 2024

Context:
The current approach for sampling multiple shots involves handling shot vectors by sampling multiple times and processing each shot independently. However, a more efficient strategy would be to sample all shots at once and then process each bin separately. The shots.bins() method facilitates this by providing the lower and upper bounds for each bin.

Description of the Change:

  • The shots.bins() method generates a sequence of tuples, where each tuple represents the lower and upper bounds of a bin.
  • In _measure_with_samples_diagonalizing_gates sample once with total number of shots and use shots.bins() to process each bin separately

Benefits:
This method is particularly useful for processing shot quantities in a partitioned manner, as it allows for the separate handling of each bin's range

Possible Drawbacks:

Related GitHub Issues:
Fixes #5433

Copy link

codecov bot commented Apr 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.66%. Comparing base (a47d9bc) to head (a9209ae).
Report is 4 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5476      +/-   ##
==========================================
- Coverage   99.67%   99.66%   -0.01%     
==========================================
  Files         406      406              
  Lines       37881    37637     -244     
==========================================
- Hits        37758    37512     -246     
- Misses        123      125       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@albi3ro albi3ro self-requested a review April 5, 2024 13:20
@Tarun-Kumar07 Tarun-Kumar07 marked this pull request as draft April 5, 2024 14:34
@Tarun-Kumar07 Tarun-Kumar07 force-pushed the Add-Shots.bins()-generator-method branch from eaf183d to bbae7d4 Compare April 10, 2024 07:57
@Tarun-Kumar07 Tarun-Kumar07 force-pushed the Add-Shots.bins()-generator-method branch from bbae7d4 to 0ca1341 Compare April 10, 2024 08:48
Copy link
Contributor

@albi3ro albi3ro left a comment

Choose a reason for hiding this comment

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

For the PRNG key failures:

FAILED tests/devices/qubit/test_sampling.py::TestBroadcastingPRNG::test_nonsample_measure_shot_vector[measurement1-expected1-shots3] - assert False
 +  where False = <function allclose at 0x7f8312da2e70>(Array([-1.    ,  1.    , -0.0238], dtype=float64), array([-1,  1,  0]), atol=0.01)
 +    where <function allclose at 0x7f8312da2e70> = np.allclose
FAILED tests/devices/qubit/test_sampling.py::TestBroadcastingPRNG::test_nonsample_measure_shot_vector[measurement1-expected1-shots4] - assert False
 +  where False = <function allclose at 0x7f8312da2e70>(Array([-1.    ,  1.    , -0.0163], dtype=float64), array([-1,  1,  0]), atol=0.01)
 +    where <function allclose at 0x7f8312da2e70> = np.allclose
= 2 failed, 1066 passed, 108 skipped, 15 xfailed, 88 warnings in 737.38s (0:12:17) =

I think we will just have to change the test a little to accept the new results. Basically, we should expect the samples with shot vectors to be slightly different now, as sampling 2 then sampling 1 will give different values then sampling 3 at the same time, especially when PRNG keys are involved.

@Tarun-Kumar07 Tarun-Kumar07 marked this pull request as ready for review April 16, 2024 03:09
Copy link
Contributor

@albi3ro albi3ro left a comment

Choose a reason for hiding this comment

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

Looks great! Happy to finally have that alternate way of calculating shot vectors implemented.

My one comment was purely stylistic.

@albi3ro
Copy link
Contributor

albi3ro commented Apr 16, 2024

Requested a second review from the team.

@albi3ro albi3ro requested a review from dwierichs April 17, 2024 18:57
Copy link
Contributor

@dwierichs dwierichs left a comment

Choose a reason for hiding this comment

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

Looks good to me, thanks @Tarun-Kumar07 ! 🎉

I had two small comments, one of which we briefly should discuss with @albi3ro .

pennylane/devices/qubit/sampling.py Outdated Show resolved Hide resolved
pennylane/measurements/shots.py Show resolved Hide resolved
tests/measurements/test_shots.py Outdated Show resolved Hide resolved
Copy link
Contributor

@dwierichs dwierichs left a comment

Choose a reason for hiding this comment

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

Thanks @Tarun-Kumar07 💯

@albi3ro albi3ro enabled auto-merge (squash) April 18, 2024 18:04
@albi3ro albi3ro merged commit 016d2cc into PennyLaneAI:master Apr 18, 2024
38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Shots.bins() generator method
4 participants