qcor.cpp 1.82 KB
Newer Older
Mccaskey, Alex's avatar
Mccaskey, Alex committed
1
#include "qcor.hpp"
2
#include "qpu_handler.hpp"
Mccaskey, Alex's avatar
Mccaskey, Alex committed
3

4
#include "AcceleratorBuffer.hpp"
Mccaskey, Alex's avatar
Mccaskey, Alex committed
5
6
7
8
9
10
11
#include "IRProvider.hpp"
#include "XACC.hpp"

using namespace xacc;

namespace qcor {

12
13
14
15
16
17
18
void Initialize(int argc, char** argv) {
    xacc::Initialize(argc,argv);
}
void Initialize(std::vector<std::string> argv) {
    xacc::Initialize(argv);
}

Mccaskey, Alex's avatar
Mccaskey, Alex committed
19
const std::string persistCompiledCircuit(std::shared_ptr<Function> function) {
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
      std::function<char()> randChar = []() -> char {
    const char charset[] = "0123456789"
                           "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                           "abcdefghijklmnopqrstuvwxyz";
    const size_t max_index = (sizeof(charset) - 1);
    return charset[rand() % max_index];
  };

  auto generateRandomString = [&](const int length = 10) -> const std::string {
    std::string str(length, 0);
    std::generate_n(str.begin(), length, randChar);
    return str;
  };



  return generateRandomString();
Mccaskey, Alex's avatar
Mccaskey, Alex committed
37
38
}

39
std::shared_ptr<Function> loadCompiledCircuit(const std::string& fileName) {
Mccaskey, Alex's avatar
Mccaskey, Alex committed
40
41
  auto provider = xacc::getService<IRProvider>("gate");
  auto function = provider->createFunction("tmp", {});
42
43
44
45
46
47
48
49
50
51
  auto h = provider->createInstruction("H", {0});
  auto cx = provider->createInstruction("CNOT", {0,1});
  auto m1 = provider->createInstruction("Measure", {0}, {InstructionParameter(0)});
  auto m2 = provider->createInstruction("Measure", {1}, {InstructionParameter(1)});

  function->addInstruction(h);
  function->addInstruction(cx);
  function->addInstruction(m1);
  function->addInstruction(m2);

Mccaskey, Alex's avatar
Mccaskey, Alex committed
52
53
54
  return function;
}

55
std::future<std::shared_ptr<AcceleratorBuffer>> submit(HandlerLambda &&totalJob) {
Mccaskey, Alex's avatar
Mccaskey, Alex committed
56
57
58
  // Create the QPU Handler to pass to the given
  // Handler HandlerLambda
  qpu_handler handler;
59
  return std::async(std::launch::async, [&]() {
Mccaskey, Alex's avatar
Mccaskey, Alex committed
60
    totalJob(handler);
61
    return handler.getResults();
Mccaskey, Alex's avatar
Mccaskey, Alex committed
62
63
64
65
  });
}

} // namespace qcor