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

Seed samples for lightning.qubit/kokkos #1164

Merged
merged 15 commits into from
Oct 3, 2024
Merged

Seed samples for lightning.qubit/kokkos #1164

merged 15 commits into from
Oct 3, 2024

Conversation

paul0403
Copy link
Contributor

@paul0403 paul0403 commented Sep 30, 2024

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 . This was done in #936.

In #936 , the device's rng instance is used during measurements , 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]

runtime/Makefile Outdated Show resolved Hide resolved
@paul0403 paul0403 changed the title Seed samples for lightning.qubit Seed samples for lightning.qubit/kokkos Oct 3, 2024
paul0403 added a commit to PennyLaneAI/pennylane-lightning that referenced this pull request Oct 3, 2024
…make the generated samples deterministic (#927)

### Before submitting

Please complete the following checklist when submitting a PR:

- [x] 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`](../tests) directory!

- [x] 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`.

- [x] Ensure that the test suite passes, by running `make test`.

- [x] Add a new entry to the `.github/CHANGELOG.md` file, summarizing
the change, and including a link back to the PR.

- [x] 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](PennyLaneAI/catalyst#936) a new
`seed` option to `qjit` was added. The seed was used to make measurement
results deterministic, but samples were still probabilistic. This is
because within a `qjit` context, [measurements were controlled from the
catalyst
repo](https://github.com/PennyLaneAI/catalyst/blob/a580bada575793b780d5366aa77dff6157cd4f93/runtime/lib/backend/common/Utils.hpp#L274)
, but samples were controlled by lightning.

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 a
`std::mt19937` rng instance in the catalyst execution
context](https://github.com/PennyLaneAI/catalyst/blob/934726fe750043886415953dbd89a4c4ddeb9a80/runtime/lib/capi/ExecutionContext.hpp#L268).
This rng instance eventually becomes a field of the
`Catalyst::Runtime::Simulator::LightningSimulator` (and kokkos) class
[catalyst/runtime/lib/backend/lightning/lightning_dynamic/LightningSimulator.hpp](https://github.com/PennyLaneAI/catalyst/blob/a580bada575793b780d5366aa77dff6157cd4f93/runtime/lib/backend/lightning/lightning_dynamic/LightningSimulator.hpp#L54).

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 lightning
`generate_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]

---------

Co-authored-by: ringo-but-quantum <[email protected]>
@paul0403 paul0403 added author:build-wheels Run the wheel building workflows on this Pull Request urgent Mark a pull request as high priority labels Oct 3, 2024
@paul0403
Copy link
Contributor Author

paul0403 commented Oct 3, 2024

build wheels since we are tracking a different lightning

Copy link

codecov bot commented Oct 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.86%. Comparing base (4e58b7a) to head (1b1858b).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1164   +/-   ##
=======================================
  Coverage   97.86%   97.86%           
=======================================
  Files          76       76           
  Lines       10812    10812           
  Branches     1282     1282           
=======================================
  Hits        10581    10581           
  Misses        179      179           
  Partials       52       52           

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

Copy link
Collaborator

@dime10 dime10 left a comment

Choose a reason for hiding this comment

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

Thanks 🙏

@dime10 dime10 merged commit 9fbc8e3 into main Oct 3, 2024
59 checks passed
@dime10 dime10 deleted the seed_samples_lightning branch October 3, 2024 22:08
paul0403 added a commit that referenced this pull request Oct 4, 2024
…in documentation (#1178)

A followup to #1164, which forgot to update this line in the public facing documentation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author:build-wheels Run the wheel building workflows on this Pull Request urgent Mark a pull request as high priority
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants