XACC
xasm_visitor.hpp
1 /*******************************************************************************
2  * Copyright (c) 2019 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_XASMVISITOR_HPP_
14 #define QUANTUM_GATE_XASMVISITOR_HPP_
15 
16 #include <memory>
17 #include "AllGateVisitor.hpp"
18 
19 namespace xacc {
20 namespace quantum {
21 
22 class XasmVisitor : public AllGateVisitor {
23 protected:
24  std::stringstream ss;
25 
26 public:
27  XasmVisitor() {}
28 
29  void visit(Hadamard &h) {
30  std::string qubit = std::to_string(h.bits()[0]);
31  ss << "H(q[" << qubit << "]);\n";
32  }
33 
34  void visit(Identity &i) {
35  std::string qubit = std::to_string(i.bits()[0]);
36  ss << "I(q[" << qubit << "]);\n";
37  }
38 
39  void visit(CZ &cz) {
40  std::string q1 = std::to_string(cz.bits()[0]);
41  std::string q2 = std::to_string(cz.bits()[1]);
42  ss << "Cz(q[" << q1 << "],q[" << q2 << "]);\n";
43  }
44 
45  void visit(CNOT &cn) {
46  std::string q1 = std::to_string(cn.bits()[0]);
47  std::string q2 = std::to_string(cn.bits()[1]);
48  ss << "CX(q[" << q1 << "],q[" << q2 << "]);\n";
49  }
50 
51  void visit(X &x) {
52  std::string qubit = std::to_string(x.bits()[0]);
53  ss << "X(q[" << qubit << "]);\n";
54  }
55  void visit(Y &y) {
56  std::string qubit = std::to_string(y.bits()[0]);
57  ss << "Y(q[" << qubit << "]);\n";
58  }
59 
60  void visit(Z &z) {
61  std::string qubit = std::to_string(z.bits()[0]);
62  ss << "Z(q[" << qubit << "]);\n";
63  }
64 
65  void visit(Measure &m) { ss << "Measure(q[" << m.bits()[0] << "]);\n"; }
66 
67  void visit(Rx &rx) {
68  std::string qubit = std::to_string(rx.bits()[0]);
69  ss << "Rx(q[" << qubit << "], " << rx.getParameter(0).toString()
70  << ");\n";
71  }
72 
73  void visit(Ry &ry) {
74  std::string qubit = std::to_string(ry.bits()[0]);
75  ss << "Ry(q[" << qubit << "], " << ry.getParameter(0).toString()
76  << ");\n";
77  }
78 
79  void visit(Rz &rz) {
80  std::string qubit = std::to_string(rz.bits()[0]);
81  ss << "Rz(q[" << qubit << "], " << rz.getParameter(0).toString()
82  << ");\n";
83  }
84 
85  void visit(CPhase &cp) {
86  std::string q1 = std::to_string(cp.bits()[0]);
87  std::string q2 = std::to_string(cp.bits()[1]);
88  auto angleStr = cp.getParameter(0).toString();
89  ss << "CPhase(q[" << q1 << "], q[" << q2 << "]," << angleStr << ");\n";
90  }
91 
92  void visit(Swap &s) {
93  CNOT cn1(s.bits());
94  CNOT cn2(s.bits());
95  CNOT cn3(s.bits());
96  visit(cn1);
97  visit(cn2);
98  visit(cn3);
99  }
100 
101  void visit(U &u) {
102  auto t = u.getParameter(0).toString();
103  auto p = u.getParameter(1).toString();
104  auto l = u.getParameter(2).toString();
105 
106  Rz rz(u.bits()[0], t);
107  Ry ry(u.bits()[0], p);
108  Rz rz2(u.bits()[0], l);
109  visit(rz);
110  visit(ry);
111  visit(rz2);
112  }
113 
114  std::string getXasmString() { return ss.str(); }
115  virtual ~XasmVisitor() {}
116 };
117 
118 } // namespace quantum
119 } // namespace xacc
120 
121 #endif
Definition: CommonGates.hpp:306
Definition: CommonGates.hpp:199
Definition: CommonGates.hpp:328
Definition: Accelerator.hpp:25
Definition: CommonGates.hpp:124
Definition: CommonGates.hpp:112
Definition: CommonGates.hpp:295
Definition: AllGateVisitor.hpp:24
Definition: CommonGates.hpp:368
Definition: xasm_visitor.hpp:22
Definition: CommonGates.hpp:216
Definition: CommonGates.hpp:254
Definition: CommonGates.hpp:235
Definition: CommonGates.hpp:317
Definition: CommonGates.hpp:381
Definition: CommonGates.hpp:270
Definition: CommonGates.hpp:100