qpu_handler.hpp 1.68 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
#include "AcceleratorBuffer.hpp"
7
#include "Function.hpp"
8
9
#include "XACC.hpp"

Mccaskey, Alex's avatar
Mccaskey, Alex committed
10
11
12
namespace qcor {

class qpu_handler {
13
14
protected:
  std::shared_ptr<xacc::AcceleratorBuffer> buffer;
Mccaskey, Alex's avatar
Mccaskey, Alex committed
15
public:
16
17
18
  qpu_handler() = default;
  qpu_handler(std::shared_ptr<xacc::AcceleratorBuffer> b) : buffer(b) {}

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

Mccaskey, Alex's avatar
Mccaskey, Alex committed
21
  template <typename QuantumKernel>
22
  void vqe(QuantumKernel kernel, std::shared_ptr<Observable> observable,
23
           std::shared_ptr<Optimizer> optimizer) {
24
    auto function = qcor::loadCompiledCircuit(kernel());
25
26

    // std::cout << "Function:\n" << function->toString() << "\n";
27
28
    auto nPhysicalQubits = function->nPhysicalBits();
    auto accelerator = xacc::getAccelerator();
29
30
31
32

    if(!buffer) {
      buffer = accelerator->createBuffer("q", nPhysicalQubits);
    }
33

34
    auto vqeAlgo = qcor::getAlgorithm("vqe");
35
36
37
38
    if(!vqeAlgo->initialize({{"ansatz",function}, {"accelerator",accelerator}, {"observable",observable},{"optimizer",optimizer}})) {
        xacc::error("Error initializing VQE algorithm.");
    }
    vqeAlgo->execute(buffer);
Mccaskey, Alex's avatar
Mccaskey, Alex committed
39
40
  }

41
42
  template <typename QuantumKernel> void execute(QuantumKernel &&kernel) {
    auto function = qcor::loadCompiledCircuit(kernel());
43
    auto nPhysicalQubits = function->nPhysicalBits();
44
45

    auto accelerator = xacc::getAccelerator();
46
47
48
49

    if (!buffer) {
      buffer = accelerator->createBuffer("q", nPhysicalQubits);
    }
50
51
    accelerator->execute(buffer, function);
  }
Mccaskey, Alex's avatar
Mccaskey, Alex committed
52
53
54
55
56
57
58
59

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

} // namespace qcor

#endif