diff --git a/pennylane_lightning/core/src/simulators/lightning_kokkos/catalyst/LightningKokkosSimulator.cpp b/pennylane_lightning/core/src/simulators/lightning_kokkos/catalyst/LightningKokkosSimulator.cpp index 6fa4089d8b..14f2d54c35 100644 --- a/pennylane_lightning/core/src/simulators/lightning_kokkos/catalyst/LightningKokkosSimulator.cpp +++ b/pennylane_lightning/core/src/simulators/lightning_kokkos/catalyst/LightningKokkosSimulator.cpp @@ -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; @@ -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(); diff --git a/pennylane_lightning/core/src/simulators/lightning_kokkos/catalyst/LightningKokkosSimulator.hpp b/pennylane_lightning/core/src/simulators/lightning_kokkos/catalyst/LightningKokkosSimulator.hpp index 36b4b5891e..51ca688e22 100644 --- a/pennylane_lightning/core/src/simulators/lightning_kokkos/catalyst/LightningKokkosSimulator.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_kokkos/catalyst/LightningKokkosSimulator.hpp @@ -63,6 +63,8 @@ class LightningKokkosSimulator final : public Catalyst::Runtime::QuantumDevice { std::size_t device_shots; + std::mt19937 *gen = nullptr; + std::unique_ptr device_sv = std::make_unique(0); LightningKokkosObsManager obs_manager{}; @@ -116,6 +118,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; diff --git a/pennylane_lightning/core/src/simulators/lightning_kokkos/catalyst/tests/Test_LightningKokkosMeasures.cpp b/pennylane_lightning/core/src/simulators/lightning_kokkos/catalyst/tests/Test_LightningKokkosMeasures.cpp index 527c8fddb9..2655372252 100644 --- a/pennylane_lightning/core/src/simulators/lightning_kokkos/catalyst/tests/Test_LightningKokkosMeasures.cpp +++ b/pennylane_lightning/core/src/simulators/lightning_kokkos/catalyst/tests/Test_LightningKokkosMeasures.cpp @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include + #include "CacheManager.hpp" #include "LightningKokkosSimulator.hpp" #include "QuantumDevice.hpp" @@ -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 sim = std::make_unique(); + std::unique_ptr sim1 = std::make_unique(); + + std::mt19937 gen(37); + sim->SetDevicePRNG(&gen); + std::vector 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 Qs1; + Qs1.reserve(1); + Qs1.push_back(sim1->AllocateQubit()); + sim1->NamedOperation("Hadamard", {}, {Qs1[0]}, false); + auto m1 = sim1->Measure(Qs1[0]); + + CHECK(*m == *m1); + } +}