QuilVisitor.hpp 6.03 KB
Newer Older
1
2
3
4
5
6
7
8
/*******************************************************************************
 * Copyright (c) 2019 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
Mccaskey, Alex's avatar
Mccaskey, Alex committed
9
10
 *
 * Contributors:
11
12
 *   Alexander J. McCaskey - initial API and implementation
 *******************************************************************************/
Mccaskey, Alex's avatar
Mccaskey, Alex committed
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#ifndef QUANTUM_GATE_ACCELERATORS_RIGETTI_QUILVISITOR_HPP_
#define QUANTUM_GATE_ACCELERATORS_RIGETTI_QUILVISITOR_HPP_

#include <memory>
#include "AllGateVisitor.hpp"

namespace xacc {
namespace quantum {

/**
 * The QuilVisitor is an InstructionVisitor that visits
 * quantum gate instructions and creates an equivalent
 * Quil string that can be executed by the Rigetti
 * superconducting quantum computer.
 *
 */
class QuilVisitor : public AllGateVisitor {
protected:
  /**
   * Reference to the Quil string
   * this visitor is trying to construct
   */
  std::string quilStr;
  constexpr static double pi = 3.1415926;
  /**
   * Reference to the classical memory address indices
   * where measurements are recorded.
   */
  std::string classicalAddresses;

  std::map<int, int> qubitToClassicalBitIndex;

  std::vector<int> measuredQubits;

  bool includeMeasures = true;

  int numAddresses = 0;

public:
  QuilVisitor() {}
  QuilVisitor(bool measures) : includeMeasures(measures) {}

  /**
   * Visit hadamard gates
   */
  void visit(Hadamard &h) {
    std::string qubit = std::to_string(h.bits()[0]);
    quilStr += "RZ(pi/2) " + qubit + "\nRX(pi/2) " + qubit + "\nRZ(pi/2) " + qubit + "\n";
  }

  void visit(Identity &i) {
    std::string qubit = std::to_string(i.bits()[0]);
    quilStr += "I " + qubit + "\n";
  }

  void visit(CZ &cz) {
    std::string q1 = std::to_string(cz.bits()[0]);
    std::string q2 = std::to_string(cz.bits()[1]);
    quilStr += "CZ " + q1 + " " +
               q2 + "\n";
  }

  /**
   * Visit CNOT gates
   */
  void visit(CNOT &cn) {
    std::string q1 = std::to_string(cn.bits()[0]);
    std::string q2 = std::to_string(cn.bits()[1]);
    quilStr += "RZ(-pi/2) " + q2 + "\nRX(pi/2) " + q2 + "\nCZ " + q2 + " " + q1 + "\nRX(-pi/2) " + q2 + "\nRZ(pi/2) " + q2 + "\nRZ(pi) "+ q1 + "\n";
  }
  /**
   * Visit X gates
   */
  void visit(X &x) {
      std::string qubit = std::to_string(x.bits()[0]);
      auto angleStr = std::to_string(pi);
      quilStr += "RX(pi) " + qubit + "\n"; }

  /**
   *
   */
  void visit(Y &y) {
      std::string qubit = std::to_string(y.bits()[0]);
      quilStr += "RZ(pi) " + qubit + "\nRX(pi) " + qubit + "\n"; }

  /**
   * Visit Z gates
   */
  void visit(Z &z) {
      std::string qubit = std::to_string(z.bits()[0]);
      quilStr += "RZ(pi) " + qubit + "\n"; }

  /**
   * Visit Measurement gates
   */
  void visit(Measure &m) {
    if (includeMeasures) {
      int classicalBitIdx = m.getClassicalBitIndex();
      quilStr += "MEASURE " + std::to_string(m.bits()[0]) + " ro[" +
                 std::to_string(classicalBitIdx) + "]\n";
      classicalAddresses += std::to_string(classicalBitIdx) + ", ";
      numAddresses++;
      qubitToClassicalBitIndex.insert(
          std::make_pair(m.bits()[0], classicalBitIdx));
      measuredQubits.push_back(m.bits()[0]);
    } else {
      measuredQubits.push_back(m.bits()[0]);
    }
  }

  /**
   * Visit Conditional functions
   */
//   void visit(ConditionalFunction &c) {
//     auto visitor = std::make_shared<QuilVisitor>();
//     auto classicalBitIdx = qubitToClassicalBitIndex[c.getConditionalQubit()];
//     quilStr += "JUMP-UNLESS @" + c.name() + " [" +
//                std::to_string(classicalBitIdx) + "]\n";
//     for (auto inst : c.getInstructions()) {
//       inst->accept(visitor);
//     }
//     quilStr += visitor->getQuilString();
//     quilStr += "LABEL @" + c.name() + "\n";
//   }

  void visit(Rx &rx) {
    std::string qubit = std::to_string(rx.bits()[0]);
    auto angleStr = rx.getParameter(0).toString();
    quilStr += "RX(" + angleStr + ") " + qubit + "\n";
  }

  void visit(Ry &ry) {
    auto angleStr = ry.getParameter(0).toString();
    std::string qubit = std::to_string(ry.bits()[0]);
    quilStr += "RX(pi/2) " + qubit + "\nRZ(" + angleStr + ") " + qubit + "\nRX(-pi/2) " + qubit + "\n";
  }

  void visit(Rz &rz) {
    std::string qubit = std::to_string(rz.bits()[0]);
    auto angleStr = rz.getParameter(0).toString();
    quilStr += "RZ(" + angleStr + ") " + qubit + "\n";
  }

  void visit(CPhase &cp) {
    std::string q1 = std::to_string(cp.bits()[0]);
    std::string q2 = std::to_string(cp.bits()[1]);
    auto angleStr = cp.getParameter(0).toString();
    quilStr += "CPHASE(" + angleStr + ") " + q1 +
               " " + q2 + "\n";
  }

  void visit(Swap &s) {
    std::string q1 = std::to_string(s.bits()[0]);
    std::string q2 = std::to_string(s.bits()[1]);
    quilStr += "SWAP " + q1 + " " +
               q2 + "\n";
  }

 void visit(U& u) {
    auto t = u.getParameter(0).toString();
    auto p = u.getParameter(1).toString();
    auto l = u.getParameter(2).toString();

    std::stringstream s;
    s << "   Rz(" << t << ", " << u.bits()[0] << ")\n";
    s << "   Ry(" << p << ", " << u.bits()[0] << ")\n";
    s << "   Rz(" << l << ", " << u.bits()[0] << ")\n";

    quilStr += s.str();
  }

  /**
   * Return the quil string
   */
  std::string getQuilString() { return quilStr; }

  /**
   * Return the classical measurement indices
   * as a json int array represented as a string.
   */
  std::string getClassicalAddresses() {
    auto retStr = classicalAddresses.substr(0, classicalAddresses.size() - 2);
    return "[" + retStr + "]";
  }

  int getNumberOfAddresses() { return numAddresses; }

  std::vector<int> getMeasuredQubits() { return measuredQubits; }

  /**
   * The destructor
   */
  virtual ~QuilVisitor() {}
};

} // namespace quantum
} // namespace xacc

#endif