Skip to content

Commit

Permalink
convert to std::optional to avoid (pointers + default value of nullptr);
Browse files Browse the repository at this point in the history
split setRandomSeed and setRNG into different functions
  • Loading branch information
paul0403 committed Sep 30, 2024
1 parent cfb16b4 commit 3047599
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
15 changes: 8 additions & 7 deletions pennylane_lightning/core/src/measurements/MeasurementsBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,20 @@ template <class StateVectorT, class Derived> class MeasurementsBase {

/**
* @brief Randomly set the seed of the internal random generator
* If catalyst already provides a random generator, use the provided one
*
* @param catalyst_rng Seed
*/
void setRandomSeed(const std::mt19937 *&catalyst_rng = nullptr) {
if (catalyst_rng != nullptr) {
rng = *catalyst_rng;
return;
}
void setRandomSeed() {
std::random_device rd;
setSeed(rd());
}

/**
* @brief Set the internal random generator to an already existing instance
*
* @param catalyst_rng Seed
*/
void setRNG(std::mt19937 rng) { this->rng = std::move(rng); }

/**
* @brief Calculate the expectation value for a general Observable.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ class Measurements final
*/
std::vector<std::size_t>
generate_samples(const std::size_t num_samples,
const std::mt19937 *&catalyst_rng = nullptr) {
const std::optional<std::mt19937> &catalyst_rng) {
const std::size_t num_qubits = this->_statevector.getNumQubits();
std::vector<std::size_t> wires(num_qubits);
std::iota(wires.begin(), wires.end(), 0);
Expand All @@ -598,10 +598,14 @@ class Measurements final
std::vector<std::size_t>
generate_samples(const std::vector<std::size_t> &wires,
const std::size_t num_samples,
const std::mt19937 *&catalyst_rng = nullptr) {
const std::optional<std::mt19937> &catalyst_rng) {
const std::size_t n_wires = wires.size();
std::vector<std::size_t> samples(num_samples * n_wires);
this->setRandomSeed(catalyst_rng);
if (catalyst_rng.has_value()) {
this->setRNG(catalyst_rng);
} else {
this->setRandomSeed();
}
DiscreteRandomVariable<PrecisionT> drv{this->rng, probs(wires)};
// The Python layer expects a 2D array with dimensions (n_samples x
// n_wires) and hence the linear index is `s * n_wires + (n_wires - 1 -
Expand Down

0 comments on commit 3047599

Please sign in to comment.