-
Notifications
You must be signed in to change notification settings - Fork 40
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
Allow lightning.qubit/kokkos::generate_samples
to take in seeds to make the generated samples deterministic
#927
Conversation
Hello. You may have forgotten to update the changelog!
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #927 +/- ##
==========================================
+ Coverage 96.41% 97.36% +0.95%
==========================================
Files 215 215
Lines 28172 28211 +39
==========================================
+ Hits 27162 27468 +306
+ Misses 1010 743 -267 ☔ View full report in Codecov by Sentry. |
pennylane_lightning/core/src/simulators/lightning_qubit/measurements/MeasurementsLQubit.hpp
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @paul0403 ! Just a few Q.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @paul0403 .
pennylane_lightning/core/src/simulators/lightning_qubit/measurements/MeasurementsLQubit.hpp
Outdated
Show resolved
Hide resolved
pennylane_lightning/core/src/simulators/lightning_qubit/measurements/MeasurementsLQubit.hpp
Outdated
Show resolved
Hide resolved
split setRandomSeed and setRNG into different functions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice seeing this will be pretty straightforward 👍
pennylane_lightning/core/src/simulators/lightning_qubit/measurements/MeasurementsLQubit.hpp
Outdated
Show resolved
Hide resolved
std::mt19937
rng instance from catalystlightning.qubit/kokkos::generate_samples
to take in seeds to make the generated samples deterministic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work @paul0403
Just a few quick questions:
pennylane_lightning/core/src/simulators/lightning_kokkos/catalyst/LightningKokkosSimulator.cpp
Show resolved
Hide resolved
pennylane_lightning/core/src/simulators/lightning_kokkos/measurements/MeasurementsKokkos.hpp
Show resolved
Hide resolved
pennylane_lightning/core/src/simulators/lightning_qubit/measurements/MeasurementsLQubit.hpp
Show resolved
Hide resolved
pennylane_lightning/core/src/simulators/lightning_qubit/measurements/MeasurementsLQubit.hpp
Show resolved
Hide resolved
`const` to shots arguments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM ! Thanks @paul0403 !
pennylane_lightning/core/src/simulators/lightning_kokkos/catalyst/LightningKokkosSimulator.hpp
Outdated
Show resolved
Hide resolved
pennylane_lightning/core/src/simulators/lightning_kokkos/catalyst/LightningKokkosSimulator.cpp
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @paul0403
Last review from me --- I'll be happy once the following changes go in.
pennylane_lightning/core/src/simulators/lightning_kokkos/catalyst/LightningKokkosSimulator.cpp
Outdated
Show resolved
Hide resolved
pennylane_lightning/core/src/simulators/lightning_kokkos/catalyst/LightningKokkosSimulator.hpp
Outdated
Show resolved
Hide resolved
...ghtning/core/src/simulators/lightning_kokkos/catalyst/tests/Test_LightningKokkosMeasures.cpp
Show resolved
Hide resolved
...ghtning/core/src/simulators/lightning_kokkos/catalyst/tests/Test_LightningKokkosMeasures.cpp
Outdated
Show resolved
Hide resolved
...ghtning/core/src/simulators/lightning_kokkos/catalyst/tests/Test_LightningKokkosMeasures.cpp
Outdated
Show resolved
Hide resolved
...ghtning/core/src/simulators/lightning_kokkos/catalyst/tests/Test_LightningKokkosMeasures.cpp
Outdated
Show resolved
Hide resolved
...ghtning/core/src/simulators/lightning_kokkos/catalyst/tests/Test_LightningKokkosMeasures.cpp
Outdated
Show resolved
Hide resolved
...ghtning/core/src/simulators/lightning_kokkos/catalyst/tests/Test_LightningKokkosMeasures.cpp
Outdated
Show resolved
Hide resolved
...ghtning/core/src/simulators/lightning_kokkos/catalyst/tests/Test_LightningKokkosMeasures.cpp
Outdated
Show resolved
Hide resolved
...ghtning/core/src/simulators/lightning_kokkos/catalyst/tests/Test_LightningKokkosMeasures.cpp
Outdated
Show resolved
Hide resolved
**Context:** There's still quite a few frontend tests stochastically failing because the `seed` option in `qjit` only controls the measurements, but not the samples. We add seeding to the samples. **Description of the Change:** When `qjit(seed=...)` receives a (unsigned 32 bit int) seed value from the user, the seed gets propagated through mlir and [eventually becomes a field of the `Catalyst::Runtime::Simulator::LightningSimulator` class, alongside the seeded `std::mt19937` rng instance ](https://github.com/PennyLaneAI/catalyst/blob/a580bada575793b780d5366aa77dff6157cd4f93/runtime/lib/backend/lightning/lightning_dynamic/LightningSimulator.hpp#L54). This was done in #936. In #936 , [the device's rng instance is used during measurements ](https://github.com/PennyLaneAI/catalyst/blob/a580bada575793b780d5366aa77dff6157cd4f93/runtime/lib/backend/lightning/lightning_dynamic/LightningSimulator.cpp#L451), but not during samples. This is because samples are performed from the `Pennylane::LightningQubit::Measures::Measurements` class through the `generate_samples` methods, which is controlled by the lightning repo. To seed samples, we use the device rng instance to generate a deterministic seed to pass it onto the state vector's `generate_samples` methods. This is the only change in catalyst. In lightning, the `generate_samples` method now can take in a seeding number. The catalyst devices pass in a seed into the lightning `generate_samples`; this seed is created deterministically from the aforementioned already seeded catalyst context rng instance. This makes the generated samples deterministc. The above is published on the lightning repo as the branch "seed_sample_lightning": PennyLaneAI/pennylane-lightning#927 PennyLaneAI/pennylane-lightning@6f3e0d5 **Benefits:** Fewer (hopefully no) stochatically failing frontend tests. **Related GitHub Issues:** #999 [sc-72878]
Before submitting
Please complete the following checklist when submitting a PR:
All new features must include a unit test.
If you've fixed a bug or added code that should be tested, add a test to the
tests
directory!All new functions and code must be clearly commented and documented.
If you do make documentation changes, make sure that the docs build and
render correctly by running
make docs
.Ensure that the test suite passes, by running
make test
.Add a new entry to the
.github/CHANGELOG.md
file, summarizing thechange, and including a link back to the PR.
Ensure that code is properly formatted by running
make format
.When all the above are checked, delete everything above the dashed
line and fill in the pull request template.
Context:
A while ago a new
seed
option toqjit
was added. The seed was used to make measurement results deterministic, but samples were still probabilistic. This is because within aqjit
context, measurements were controlled from the catalyst repo , but samples were not.To resolve stochastically failing tests (i.e. flaky tests) in catalyst, we add seeding for samples in lightning.
Description of the Change:
When
qjit(seed=...)
receives a (unsigned 32 bit int) seed value from the user, the seed gets propagated through mlir and generates astd::mt19937
rng instance in the catalyst execution context. This rng instance eventually becomes a field of theCatalyst::Runtime::Simulator::LightningSimulator
(and kokkos) class catalyst/runtime/lib/backend/lightning/lightning_dynamic/LightningSimulator.hpp.To seed samples, catalyst uses this device rng instance on the state vector's
generate_samples
methods: PennyLaneAI/catalyst#1164.In lightning, the
generate_samples
method now takes in a seeding number. The catalyst devices pass in a seed into the lightninggenerate_samples
; this seed is created deterministically from the aforementioned already seeded catalyst context rng instance. This makes the generated samples deterministc.Benefits:
Fewer (hopefully no) stochatically failing frontend tests in catalyst.
Possible Drawbacks:
Related GitHub Issues:
[sc-72878]