This GitLab instance is undergoing maintenance and is operating in read-only mode.

You are on a read-only GitLab instance.
qcor_utils.cpp 3.18 KB
Newer Older
1
2
3
#include "qcor_utils.hpp"
#include "qrt.hpp"
#include "xacc.hpp"
4
#include "xacc_service.hpp"
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

namespace qcor {
void set_verbose(bool verbose) { xacc::set_verbose(verbose); }
bool get_verbose() { return xacc::verbose; }
void set_shots(const int shots) { ::quantum::set_shots(shots); }
void error(const std::string &msg) { xacc::error(msg); }
ResultsBuffer sync(Handle &handle) { return handle.get(); }

void set_backend(const std::string &backend) {
  xacc::internal_compiler::compiler_InitializeXACC();
  xacc::internal_compiler::setAccelerator(backend.c_str());
}

std::shared_ptr<xacc::CompositeInstruction> compile(const std::string &src) {
  return xacc::getCompiler("xasm")->compile(src)->getComposites()[0];
}

namespace __internal__ {
23
24
25
26
27

void append_plugin_path(const std::string path) {
    xacc::addPluginSearchPath(path);
}

28
29
30
std::shared_ptr<qcor::CompositeInstruction> create_composite(std::string name) {
  return xacc::getIRProvider("quantum")->createComposite(name);
}
31
32
33
34
std::shared_ptr<qcor::CompositeInstruction> create_ctrl_u() {
  return std::dynamic_pointer_cast<xacc::CompositeInstruction>(
      xacc::getService<xacc::Instruction>("C-U"));
}
35
36
37
38
39
40
std::shared_ptr<qcor::IRTransformation>
get_transformation(const std::string &transform_type) {
  if (!xacc::isInitialized())
    xacc::internal_compiler::compiler_InitializeXACC();
  return xacc::getService<xacc::IRTransformation>(transform_type);
}
Nguyen, Thien Minh's avatar
Nguyen, Thien Minh committed
41
42
43
44
45

std::shared_ptr<qcor::IRProvider> get_provider() {
  return xacc::getIRProvider("quantum");
}

46
std::shared_ptr<qcor::CompositeInstruction>
47
48
decompose_unitary(const std::string algorithm, UnitaryMatrix &mat,
                  const std::string buffer_name) {
49
50
51
52
53
54
55
56
57
58
59
60
61
  auto tmp = xacc::getService<xacc::Instruction>(algorithm);
  auto decomposed = std::dynamic_pointer_cast<CompositeInstruction>(tmp);

  // default Adam
  auto optimizer = xacc::getOptimizer("mlpack");
  const bool expandOk = decomposed->expand(
      {std::make_pair("unitary", mat), std::make_pair("optimizer", optimizer)});

  if (!expandOk) {
    xacc::error("Could not decmpose unitary with " + algorithm);
  }

  for (auto inst : decomposed->getInstructions()) {
62
63
64
65
66
    std::vector<std::string> buffer_names;
    for (int i = 0; i < inst->nRequiredBits(); i++) {
      buffer_names.push_back(buffer_name);
    }
    inst->setBufferNames(buffer_names);
67
68
69
70
  }

  return decomposed;
}
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

std::shared_ptr<qcor::CompositeInstruction>
decompose_unitary(const std::string algorithm, UnitaryMatrix &mat,
                  const std::string buffer_name,
                  std::shared_ptr<xacc::Optimizer> optimizer) {
  auto tmp = xacc::getService<xacc::Instruction>(algorithm);
  auto decomposed = std::dynamic_pointer_cast<CompositeInstruction>(tmp);

  // default Adam
  const bool expandOk = decomposed->expand(
      {std::make_pair("unitary", mat), std::make_pair("optimizer", optimizer)});

  if (!expandOk) {
    xacc::error("Could not decmpose unitary with " + algorithm);
  }

  for (auto inst : decomposed->getInstructions()) {
    std::vector<std::string> buffer_names;
    for (int i = 0; i < inst->nRequiredBits(); i++) {
      buffer_names.push_back(buffer_name);
    }
    inst->setBufferNames(buffer_names);
  }

  return decomposed;
}

98
99
} // namespace __internal__
} // namespace qcor