qpu_handler.hpp 1.66 KB
Newer Older
Mccaskey, Alex's avatar
Mccaskey, Alex committed
1
2
3
#ifndef RUNTIME_QPU_HANDLER_HPP_
#define RUNTIME_QPU_HANDLER_HPP_

4
#include "qcor.hpp"
5

6
7
8
#include "Function.hpp"
#include "AcceleratorBuffer.hpp"
#include "InstructionIterator.hpp"
9
10
#include "XACC.hpp"

11
#include <string>
Mccaskey, Alex's avatar
Mccaskey, Alex committed
12
13
14
15
16

namespace qcor {

class qpu_handler {

17
18
19
20
protected:

  std::shared_ptr<xacc::AcceleratorBuffer> buffer;

Mccaskey, Alex's avatar
Mccaskey, Alex committed
21
public:
22
23
24
25
26

  std::shared_ptr<xacc::AcceleratorBuffer> getResults() {
      return buffer;
  }

Mccaskey, Alex's avatar
Mccaskey, Alex committed
27
  template <typename QuantumKernel>
28
  void vqe(QuantumKernel &&kernel, double observable, std::shared_ptr<Optimizer> optimizer) {
29
    xacc::info("[qcor] Executing vqe! :)");
30
31
32
33
34
35
    xacc::info("[qcor] vqe running with " + optimizer->name() + " optimizer.");

    auto function = qcor::loadCompiledCircuit(kernel());

    // Here we just need to make a lambda function
    // to optimize that makes calls to the targeted QPU.
36

Mccaskey, Alex's avatar
Mccaskey, Alex committed
37
38
  }

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  template <typename QuantumKernel> void execute(QuantumKernel &&kernel) {
    // xacc::info("[qcor] Executing circuit! :)");
    auto function = qcor::loadCompiledCircuit(kernel());

    int maxBitIdx = 0;
    xacc::InstructionIterator it(function);
      while (it.hasNext()) {
        auto nextInst = it.next();
        if (nextInst->isEnabled()) {
          for (auto& i : nextInst->bits()) {
              if (maxBitIdx < i) {
                  maxBitIdx = i;
              }
          }
        }
      }

    maxBitIdx++;

    // auto function = kernel();
    auto accelerator = xacc::getAccelerator();
    buffer = accelerator->createBuffer("q", maxBitIdx);
    accelerator->execute(buffer, function);
  }
Mccaskey, Alex's avatar
Mccaskey, Alex committed
63
64
65
66
67
68
69
70

  template <typename QuantumKernel>
  void execute(const std::string &algorithm, QuantumKernel &&kernel) {}
};

} // namespace qcor

#endif