Skip to content

Commit

Permalink
Update kokkos files from catalyst repo to support MCM seeding (#819)
Browse files Browse the repository at this point in the history
### 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:**
The lightning kokkos files in the Catalyst repo has changed since #770.
We make a small update to track these changes.

The Catalyst PR that made the changes added seeding to qjit:
PennyLaneAI/catalyst#936

**Description of the Change:**
The `lightning_kokkos/catalyst` files now has the MCM seeding support added in catalyst 
PennyLaneAI/catalyst#936

**Benefits:** unblocks kokkos with catalyst

**Possible Drawbacks:** None

**Related GitHub Issues:** None

---------

Co-authored-by: ringo-but-quantum <[email protected]>
  • Loading branch information
paul0403 and ringo-but-quantum authored Jul 26, 2024
1 parent d1b4007 commit 10dc3a9
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
7 changes: 5 additions & 2 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@

### Improvements

* Update the Catalyst-specific wrapping class for Lightning Kokkos to track Catalyst's new support for MCM seeding.
[(#819)](https://github.com/PennyLaneAI/pennylane-lightning/pull/819)

* Shot batching is made more efficient by executing all the shots in one go on LightningQubit.
[#814](https://github.com/PennyLaneAI/pennylane-lightning/pull/814)
[(#814)](https://github.com/PennyLaneAI/pennylane-lightning/pull/814)

* LightningQubit calls `generate_samples(wires)` on a minimal subset of wires when executing in finite-shot mode.
[(#813)](https://github.com/PennyLaneAI/pennylane-lightning/pull/813)
Expand Down Expand Up @@ -74,7 +77,7 @@

This release contains contributions from (in alphabetical order):

Ali Asadi, Amintor Dusko, Vincent Michaud-Rioux, Shuli Shu
Ali Asadi, Amintor Dusko, Vincent Michaud-Rioux, Shuli Shu, Paul Haochen Wang

---

Expand Down
2 changes: 1 addition & 1 deletion pennylane_lightning/core/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
Version number (major.minor.patch[-label])
"""

__version__ = "0.38.0-dev17"
__version__ = "0.38.0-dev18"
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ auto LightningKokkosSimulator::GetDeviceShots() const -> std::size_t {
return this->device_shots;
}

void LightningKokkosSimulator::SetDevicePRNG(std::mt19937 *gen) {
this->gen = gen;
}

/// LCOV_EXCL_START
void LightningKokkosSimulator::PrintState() {
using std::cout;
Expand Down Expand Up @@ -480,7 +484,7 @@ auto LightningKokkosSimulator::Measure(QubitIdType wire,
SetDeviceShots(device_shots);

// It represents the measured result, true for 1, false for 0
bool mres = Lightning::simulateDraw(probs, postselect);
bool mres = Lightning::simulateDraw(probs, postselect, this->gen);
auto dev_wires = getDeviceWires(wires);
this->device_sv->collapse(dev_wires[0], mres ? 1 : 0);
return mres ? this->One() : this->Zero();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <iostream>
#include <limits>
#include <optional>
#include <random>
#include <span>
#include <stdexcept>
#include <unordered_map>
Expand Down Expand Up @@ -63,6 +64,8 @@ class LightningKokkosSimulator final : public Catalyst::Runtime::QuantumDevice {

std::size_t device_shots;

std::mt19937 *gen{nullptr};

std::unique_ptr<StateVectorT> device_sv = std::make_unique<StateVectorT>(0);
LightningKokkosObsManager<double> obs_manager{};

Expand Down Expand Up @@ -116,6 +119,7 @@ class LightningKokkosSimulator final : public Catalyst::Runtime::QuantumDevice {
void StartTapeRecording() override;
void StopTapeRecording() override;
void SetDeviceShots(size_t shots) override;
void SetDevicePRNG(std::mt19937 *) override;
[[nodiscard]] auto GetDeviceShots() const -> size_t override;
void PrintState() override;
[[nodiscard]] auto Zero() const -> Result override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <random>

#include "CacheManager.hpp"
#include "LightningKokkosSimulator.hpp"
#include "QuantumDevice.hpp"
Expand Down Expand Up @@ -1750,3 +1752,28 @@ TEST_CASE("Counts and PartialCounts tests with numWires=0-4 shots=100",
CHECK(sum3 == shots);
CHECK(sum4 == shots);
}

TEST_CASE("Measurement with a seeded device", "[Measures]") {
for (size_t _ = 0; _ < 5; _++) {
std::unique_ptr<LKSimulator> sim = std::make_unique<LKSimulator>();
std::unique_ptr<LKSimulator> sim1 = std::make_unique<LKSimulator>();

std::mt19937 gen(37);
sim->SetDevicePRNG(&gen);
std::vector<intptr_t> Qs;
Qs.reserve(1);
Qs.push_back(sim->AllocateQubit());
sim->NamedOperation("Hadamard", {}, {Qs[0]}, false);
auto m = sim->Measure(Qs[0]);

std::mt19937 gen1(37);
sim1->SetDevicePRNG(&gen1);
std::vector<intptr_t> Qs1;
Qs1.reserve(1);
Qs1.push_back(sim1->AllocateQubit());
sim1->NamedOperation("Hadamard", {}, {Qs1[0]}, false);
auto m1 = sim1->Measure(Qs1[0]);

CHECK(*m == *m1);
}
}

0 comments on commit 10dc3a9

Please sign in to comment.