Commit a5b34622 authored by Thien Nguyen's avatar Thien Nguyen
Browse files

Ability to set random seed for random measurement sampling

parent a020296b
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@ usFunctionEmbedResources(TARGET ${LIBRARY_NAME}
    manifest.json
    )

target_include_directories(${LIBRARY_NAME} PUBLIC . visitors)
target_include_directories(${LIBRARY_NAME} PUBLIC . visitors utils)

target_link_libraries(${LIBRARY_NAME} PUBLIC xacc::xacc xacc::quantum_gate xacc::pauli)

+6 −0
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@
#include "xacc.hpp"
#include "xacc_service.hpp"
#include "TNQVMVisitor.hpp"
#include "RandomEngine.hpp"
#include <cassert>

// Documentation: https://xacc.readthedocs.io/en/latest/extensions.html#tnqvm
@@ -110,6 +111,11 @@ public:
      }
    }

    if (config.keyExists<int>("seed")) {
      const auto seed = config.get<int>("seed");
      randomEngine::setSeed(seed);
    }

    // Updated the cached configurations (to be sent on to visitor)
    // Note: Accelerator-level configs (visitor name, shots, vqe mode, etc.)
    // have been handled here, i.e. retrieving from the new config map. The rest
+3 −5
Original line number Diff line number Diff line
@@ -34,10 +34,9 @@
#include<complex>
#include<vector>
#include <assert.h>
#include <random>
#include <chrono>
#include <functional>

#include "RandomEngine.hpp"
typedef std::vector<std::complex<double>> StateVectorType;
typedef std::vector<std::vector<std::complex<double>>> GateMatrixType;

@@ -57,9 +56,8 @@ std::vector<T> linspace(T a, T b, size_t N) {

inline double generateRandomProbability()
{
  auto randFunc = std::bind(std::uniform_real_distribution<double>(0, 1),
                            std::mt19937(std::chrono::high_resolution_clock::now().time_since_epoch().count()));
  return randFunc();
  auto& rng = tnqvm::randomEngine::get_instance();
  return rng.randProb();
}

void ApplySingleQubitGate(StateVectorType& io_psi, size_t in_index, const GateMatrixType& in_gateMatrix)
+31 −0
Original line number Diff line number Diff line
#pragma once
#include <random>
namespace tnqvm {
struct randomEngine {
  randomEngine(const randomEngine &) = delete;
  randomEngine &operator=(const randomEngine &) = delete;
  double randProb() {
    const auto val = std::uniform_real_distribution<double>(0.0, 1.0)(m_engine);
    return val;
  }

  thread_local static randomEngine &get_instance() {
    thread_local static randomEngine instance;
    thread_local static size_t seed = 0;
    if (seed != globalSeed) {
      instance.m_engine.seed(globalSeed);
      seed = globalSeed;
    }
    return instance;
  }
  std::mt19937_64 m_engine;
  static inline size_t globalSeed = []() {
    std::random_device rd;
    return rd();
  }();
  static void setSeed(size_t seed) { globalSeed = seed; }

private:
  randomEngine() = default;
};
} // namespace tnqvm
 No newline at end of file
+31 −0
Original line number Diff line number Diff line
@@ -34,6 +34,37 @@ TEST(MpsMeasurementTester, checkSimple)
    }
}

TEST(MpsMeasurementTester, checkRandomSeed) 
{
  std::vector<std::map<std::string, int>> results;
  constexpr int NB_TESTS = 4;
  for (int i = 0; i < NB_TESTS; i++) {
    auto xasmCompiler = xacc::getCompiler("xasm");
    auto ir = xasmCompiler->compile(R"(__qpu__ void test1(qbit q) {
            H(q[0]);
            for (int i = 0; i < 3; i++) {
                CNOT(q[i], q[i + 1]);
            }

            Measure(q[0]);
            Measure(q[1]);
            Measure(q[2]);
            Measure(q[3]);
        })");

    auto program = ir->getComposite("test1");
    auto accelerator = xacc::getAccelerator(
        "tnqvm",
        {{"tnqvm-visitor", "exatn-mps"}, {"shots", 8192}, {"seed", 123}});
    auto qreg = xacc::qalloc(4);
    accelerator->execute(qreg, program);
    qreg->print();
    results.emplace_back(qreg->getMeasurementCounts());
  }
  EXPECT_TRUE(std::adjacent_find(results.begin(), results.end(),
                                 std::not_equal_to<>()) == results.end());
}

int main(int argc, char **argv) 
{
  xacc::Initialize();