staq_compiler.cpp 8.25 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
63
64
65
66
67
68
69
70
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*******************************************************************************
 * Copyright (c) 2020 UT-Battelle, LLC.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompanies this
 * distribution. The Eclipse Public License is available at
 * http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution
 *License is available at https://eclipse.org/org/documents/edl-v10.php
 *
 * Contributors:
 *   Alexander J. McCaskey - initial API and implementation
 *******************************************************************************/
#include "staq_compiler.hpp"

#include "ast/decl.hpp"
#include "xacc.hpp"
#include "xacc_service.hpp"

#include "InstructionIterator.hpp"
#include "AcceleratorBuffer.hpp"

#include "parser/parser.hpp"
#include "ast/traversal.hpp"
#include "transformations/desugar.hpp"
#include "transformations/inline.hpp"
#include "transformations/oracle_synthesizer.hpp"
#include "optimization/simplify.hpp"

using namespace staq::ast;

namespace xacc {
namespace internal_staq {
static const std::map<std::string, std::string> staq_to_xacc{
    // "u3", "u2",   "u1", "ccx", cu1, cu3
    {"cx", "CX"}, {"id", "I"},    {"x", "X"},   {"y", "Y"},
    {"z", "Z"},   {"h", "H"},     {"s", "S"},   {"sdg", "Sdg"},
    {"t", "T"},   {"tdg", "Tdg"}, {"rx", "Rx"}, {"ry", "Ry"},
    {"rz", "Rz"}, {"cz", "CZ"},   {"cy", "CY"}, {"swap", "Swap"},
    {"ch", "CH"}, {"crz", "CRZ"}};

class CountQregs : public staq::ast::Traverse {
public:
  std::vector<std::string> qregs;
  void visit(staq::ast::RegisterDecl &d) override {
    if (d.is_quantum()) {
      qregs.push_back(d.id());
    }
  }
};

class CountAncillas : public staq::ast::Traverse {
public:
  std::map<std::string, int> ancillas;
  void visit(staq::ast::AncillaDecl &d) override {
    ancillas.insert({d.id(), d.size()});
  }
};

class StaqToXasm : public staq::ast::Visitor {
public:
  std::stringstream ss;
  void visit(VarAccess &) override {}
  // Expressions
  void visit(BExpr &) override {}
  void visit(UExpr &) override {}
  void visit(PiExpr &) override {}
  void visit(IntExpr &) override {}
  void visit(RealExpr &r) override {}
  void visit(VarExpr &v) override {}
  void visit(ResetStmt &) override {}
  void visit(IfStmt &) override {}
  void visit(BarrierGate &) override {}
  void visit(GateDecl &) override {}
  void visit(OracleDecl &) override {}
  void visit(RegisterDecl &) override {}
  void visit(AncillaDecl &) override {}
  void visit(Program &prog) override {
    // Program body
    prog.foreach_stmt([this](auto &stmt) { stmt.accept(*this); });
  }
  void visit(MeasureStmt &m) override {
    ss << "Measure(" << m.q_arg().var() << "[" << m.q_arg().offset().value()
       << "]);\n";
  }
  void visit(UGate &u) override {
    ss << "U(" << u.arg().var() << "[" << u.arg().offset().value() << "], " << 0
       << ", " << 0 << ", " << 0 << ");\n";
  }
  void visit(CNOTGate &cx) override {
    ss << "CX(" << cx.ctrl().var() << "[" << cx.ctrl().offset().value() << "],"
       << cx.tgt().var() << "[" << cx.tgt().offset().value() << "]);\n";
  }
  //   void visit(BarrierGate&) = 0;
  void visit(DeclaredGate &g) override {

    auto xacc_name = staq_to_xacc.at(g.name());
    ss << xacc_name << "(" << g.qarg(0).var() << "["
       << g.qarg(0).offset().value() << "]";
    for (int i = 1; i < g.num_qargs(); i++) {
      ss << ", " << g.qarg(i).var() << "[" << g.qarg(i).offset().value() << "]";
    }

    if (g.num_cargs() > 0) {
      ss << ", " << g.carg(0).constant_eval().value();
      for (int i = 1; i < g.num_cargs(); i++) {
        ss << ", " << g.carg(i).constant_eval().value() << "\n";
      }
    }

    ss << ");\n";
  }
};

} // namespace internal_staq

StaqCompiler::StaqCompiler() {}

std::shared_ptr<IR> StaqCompiler::compile(const std::string &src,
                                          std::shared_ptr<Accelerator> acc) {
  // IF src contains typical xacc quantum kernel prototype, then take
  // buffers names and add qreg declarations before parsing
  // IF it does not, then assume qreg decls already declared.
  auto xasm = xacc::getCompiler("xasm");
  std::string prototype;

  std::string _src = src;
  bool isXaccKernel = false;
  if (src.find("__qpu__") != std::string::npos) {
    prototype = _src.substr(0, _src.find_first_of("{")) + "{}";
    auto bufferNames = xasm->getKernelBufferNames(prototype);

    isXaccKernel = true;

    std::string tmp = "";
    auto first = _src.find_first_of("{");
    auto last = _src.find_last_of("}");
    auto sub = _src.substr(first + 1, last - first - 1);
    auto lines = xacc::split(sub, '\n');
139
    bool addedNames = false;
140
141
142
143
144
145
146
147
    for (auto &l : lines) {
      xacc::trim(l);
      tmp += l + "\n";
      if (l.find("include") != std::string::npos) {
        for (auto &b : bufferNames) {
          auto size = xacc::getBuffer(b)->size();
          tmp += "qreg " + b + "[" + std::to_string(size) + "];\n";
        }
148
        addedNames = true;
149
150
151
      }
    }
    _src = tmp;
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
    if (!addedNames) {
      for (auto &b : bufferNames) {
        auto size = xacc::getBuffer(b)->size();
        _src = "qreg " + b + "[" + std::to_string(size) + "];\n" + _src;
      }
    }
  }

  // we allow users to leave out OPENQASM 2.0 and include qelib.inc
  // If they did we need to add it for them
  std::string tmp = "";
  auto lines = xacc::split(_src, '\n');
  bool foundOpenQasm = false, foundInclude = false;
  for (auto &l : lines) {
    if (l.find("OPENQASM") != std::string::npos) {
      foundOpenQasm = true;
    }
    if (l.find("include") != std::string::npos) {
      foundInclude = true;
    }
172
173
  }

174
175
176
177
178
179
  if (!foundInclude) {
    _src = "include \"qelib1.inc\";\n" + _src;
  }
  if (!foundOpenQasm) {
    _src = "OPENQASM 2.0;\n" + _src;
  }
180

181
//   std::cout << " HELLO:\n" << _src << "\n";
182
183
184
185
186
187
  using namespace staq;
  auto prog = parser::parse_string(_src);
  transformations::desugar(*prog);
  transformations::synthesize_oracles(*prog);

  optimization::simplify(*prog);
188

189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
  // at this point we have to find out if we have any ancilla
  // registers
  internal_staq::CountAncillas ancillas;
  dynamic_cast<ast::Traverse &>(ancillas).visit(*prog);

  // add ancilla registers to xacc
  for (auto &kv : ancillas.ancillas) {
    if (!xacc::hasBuffer(kv.first)) {
      auto q = xacc::qalloc(kv.second);
      q->setName(kv.first);
      xacc::storeBuffer(q);
    } else {
      xacc::getBuffer(kv.first)->resetBuffer();
    }
  }

  transformations::inline_ast(
      *prog, {false, transformations::default_overrides, "anc"});

  // Visit Program to find out how many qreg there are and
  // use that to build up openqasm xacc function prototype

211
212
213
  //   std::cout << "HELLO:\n";
  //   prog->pretty_print(std::cout);
  //   exit(0);
214
215
216
  internal_staq::StaqToXasm translate;
  translate.visit(*prog);

217
  //   std::cout << "XASM:\n" << translate.ss.str() << "\n";
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237

  std::string kernel;
  if (isXaccKernel) {
    if (!ancillas.ancillas.empty()) {
      kernel = prototype.substr(0, prototype.find_first_of(")"));
      for (auto &kv : ancillas.ancillas) {
        kernel += ", qreg " + kv.first;
      }
      kernel += ") {\n" + translate.ss.str() + "}";
    } else {
      kernel = prototype.substr(0, prototype.length() - 1) + "\n" +
               translate.ss.str() + "}";
    }
  } else {
    internal_staq::CountQregs c;
    dynamic_cast<ast::Traverse &>(c).visit(*prog);

    std::stringstream xx;
    std::string name = "tmp";
    if (xacc::hasCompiled(name)) {
238
239
240
241
242
      int counter = 0;
      while (true) {
        name = "tmp" + std::to_string(counter);
        if (!xacc::hasCompiled(name)) {
          break;
243
        }
244
245
        counter++;
      }
246
    }
247
    xx << "__qpu__ void " << name << "(";
248
249
250
251
252
253
254
255
256
257
    xx << "qreg " << c.qregs[0];
    for (int i = 1; i < c.qregs.size(); i++) {
      xx << ", qreg " << c.qregs[i];
    }
    for (auto &kv : ancillas.ancillas) {
      xx << ", qreg " << kv.first;
    }
    xx << ") {\n" << translate.ss.str() << "}";
    kernel = xx.str();
  }
258
  //   std::cout << "\n\nFinal:\n" << kernel << "\n";
259
260
261
262
263
264
265
266
267
268
269
270
  return xasm->compile(kernel, acc);
}

std::shared_ptr<IR> StaqCompiler::compile(const std::string &src) {
  return compile(src, nullptr);
}

const std::string
StaqCompiler::translate(std::shared_ptr<xacc::CompositeInstruction> function) {
  return "";
}
} // namespace xacc