XACC
staq_visitors.hpp
1 /*******************************************************************************
2  * Copyright (c) 2020 UT-Battelle, LLC.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * and Eclipse Distribution License v1.0 which accompanies this
6  * distribution. The Eclipse Public License is available at
7  * http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution
8  *License is available at https://eclipse.org/org/documents/edl-v10.php
9  *
10  * Contributors:
11  * Alexander J. McCaskey - initial API and implementation
12  *******************************************************************************/
13 #ifndef QUANTUM_GATE_COMPILER_STAQ_VISITORS_HPP_
14 #define QUANTUM_GATE_COMPILER_STAQ_VISITORS_HPP_
15 
16 #include "ast/decl.hpp"
17 #include "parser/parser.hpp"
18 #include "ast/traversal.hpp"
19 #include <map>
20 
21 #include "AllGateVisitor.hpp"
22 
23 using namespace staq::ast;
24 
25 namespace xacc {
26 namespace internal_staq {
27 static const std::map<std::string, std::string> staq_to_xacc{
28  // "u3", "u2", "u1", "ccx", cu1, cu3
29  {"cx", "CX"}, {"id", "I"}, {"x", "X"}, {"y", "Y"},
30  {"z", "Z"}, {"h", "H"}, {"s", "S"}, {"sdg", "Sdg"},
31  {"t", "T"}, {"tdg", "Tdg"}, {"rx", "Rx"}, {"ry", "Ry"},
32  {"rz", "Rz"}, {"cz", "CZ"}, {"cy", "CY"}, {"swap", "Swap"},
33  {"ch", "CH"}, {"crz", "CRZ"}, {"cu1", "CPhase"}};
34 
35 class CountQregs : public staq::ast::Traverse {
36 public:
37  std::vector<std::string> qregs;
38  void visit(staq::ast::RegisterDecl &d) override {
39  if (d.is_quantum()) {
40  qregs.push_back(d.id());
41  }
42  }
43 };
44 
45 class CountAncillas : public staq::ast::Traverse {
46 public:
47  std::map<std::string, int> ancillas;
48  void visit(staq::ast::AncillaDecl &d) override {
49  ancillas.insert({d.id(), d.size()});
50  }
51 };
52 
53 class StaqToXasm : public staq::ast::Visitor {
54 public:
55  std::stringstream ss;
56  void visit(VarAccess &) override {}
57  // Expressions
58  void visit(BExpr &) override {}
59  void visit(UExpr &) override {}
60  void visit(PiExpr &) override {}
61  void visit(IntExpr &) override {}
62  void visit(RealExpr &r) override {}
63  void visit(VarExpr &v) override {}
64  void visit(ResetStmt &) override {}
65  void visit(IfStmt &) override {}
66  void visit(BarrierGate &) override {}
67  void visit(GateDecl &) override {}
68  void visit(OracleDecl &) override {}
69  void visit(RegisterDecl &) override {}
70  void visit(AncillaDecl &) override {}
71  void visit(Program &prog) override {
72  // Program body
73  prog.foreach_stmt([this](auto &stmt) { stmt.accept(*this); });
74  }
75  void visit(MeasureStmt &m) override {
76  ss << "Measure(" << m.q_arg().var() << "[" << m.q_arg().offset().value()
77  << "]);\n";
78  }
79  void visit(UGate &u) override {
80  ss << "U(" << u.arg().var() << "[" << u.arg().offset().value() << "], "
81  << u.theta().constant_eval().value() << ", "
82  << u.phi().constant_eval().value() << ", "
83  << u.lambda().constant_eval().value() << ");\n";
84  }
85  void visit(CNOTGate &cx) override {
86  ss << "CX(" << cx.ctrl().var() << "[" << cx.ctrl().offset().value() << "],"
87  << cx.tgt().var() << "[" << cx.tgt().offset().value() << "]);\n";
88  }
89  // void visit(BarrierGate&) = 0;
90  void visit(DeclaredGate &g) override {
91 
92  auto xacc_name = staq_to_xacc.at(g.name());
93  ss << xacc_name << "(" << g.qarg(0).var() << "["
94  << g.qarg(0).offset().value() << "]";
95  for (int i = 1; i < g.num_qargs(); i++) {
96  ss << ", " << g.qarg(i).var() << "[" << g.qarg(i).offset().value() << "]";
97  }
98 
99  if (g.num_cargs() > 0) {
100  ss << ", " << g.carg(0).constant_eval().value();
101  for (int i = 1; i < g.num_cargs(); i++) {
102  ss << ", " << g.carg(i).constant_eval().value() << "\n";
103  }
104  }
105 
106  ss << ");\n";
107  }
108 };
109 
110 using namespace xacc::quantum;
111 
113 
114 public:
115  std::stringstream ss;
116  std::map<std::string, std::string> cregNames;
117 
118  XACCToStaqOpenQasm(std::map<std::string, int> bufNamesToSize);
119  void visit(Hadamard &h) override;
120  void visit(CNOT &cnot) override;
121  void visit(Rz &rz) override;
122  void visit(Ry &ry) override;
123  void visit(Rx &rx) override;
124  void visit(X &x) override;
125  void visit(Y &y) override;
126  void visit(Z &z) override;
127  void visit(CY &cy) override;
128  void visit(CZ &cz) override;
129  void visit(Swap &s) override;
130  void visit(CRZ &crz) override;
131  void visit(CH &ch) override;
132  void visit(S &s) override;
133  void visit(Sdg &sdg) override;
134  void visit(T &t) override;
135  void visit(Tdg &tdg) override;
136  void visit(CPhase &cphase) override;
137  void visit(Measure &measure) override;
138  void visit(Identity &i) override;
139  void visit(U &u) override;
140  void visit(IfStmt &ifStmt) override;
141 };
142 
143 } // namespace internal_staq
144 } // namespace xacc
145 
146 #endif
Definition: CommonGates.hpp:306
Definition: staq_visitors.hpp:45
Definition: staq_visitors.hpp:35
Definition: staq_visitors.hpp:53
Definition: CommonGates.hpp:199
Definition: CommonGates.hpp:328
Definition: CommonGates.hpp:451
Definition: Accelerator.hpp:25
Definition: CommonGates.hpp:425
Definition: CommonGates.hpp:412
Definition: CommonGates.hpp:438
Definition: CommonGates.hpp:124
Definition: CommonGates.hpp:112
Definition: CommonGates.hpp:465
Definition: CommonGates.hpp:295
Definition: AllGateVisitor.hpp:24
Definition: CommonGates.hpp:368
Definition: CommonGates.hpp:399
Definition: staq_visitors.hpp:112
Definition: CommonGates.hpp:216
Definition: CommonGates.hpp:254
Definition: CommonGates.hpp:235
Definition: CommonGates.hpp:317
Definition: CommonGates.hpp:381
Definition: CommonGates.hpp:479
Definition: CommonGates.hpp:270
Definition: CommonGates.hpp:56
Definition: CommonGates.hpp:100
Definition: DefaultParameterSetter.cpp:17