-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.cpp
252 lines (248 loc) · 9.13 KB
/
main.cpp
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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include <assert.h>
#include <fstream>
#include <cstring>
#include <regex>
#include <cmath>
#include "circuit.h"
#include "logger.h"
using namespace std;
const int BUFFER_SIZE = 1000;
char buffer[BUFFER_SIZE];
std::vector<int> parse_qid(char buf[]) {
std::vector<int> ret;
int l = strlen(buf);
for (int i = 0; i < l; i++) {
if (buf[i] >= '0' && buf[i] <= '9') {
int j = i, x = 0;
while (buf[j] >= '0' && buf[j] <= '9') {
x = x * 10 + (int)(buf[j] - '0');
j++;
}
i = j - 1;
ret.push_back(x);
}
}
return ret;
}
std::pair<std::string, std::vector<qreal>> parse_gate(char buf[]) {
qreal pi = acos(-1);
int l = strlen(buf);
std::string name;
int i = 0;
while (i < l) {
if (buf[i] != '(')
name += buf[i];
else
break;
i++;
}
std::vector<qreal> params;
while (i < l) {
i++;
std::string st;
while (buf[i] != ',' && buf[i] != ')') {
st += buf[i];
i++;
}
qreal param = 1;
if (st[0] == 'p' && st[1] == 'i' && st[2] == '*') {
param = pi;
st = st.erase(0, 3);
} else if (st[0] == 'p' && st[1] == 'i' && st[2] == '/') {
param = -pi;
st = st.erase(0, 3);
}
if (param > 0)
param *= std::stod(st);
else
param = pi / std::stod(st);
params.push_back(param);
if (buf[i] == ')')
break;
}
return std::make_pair(name, params);
}
std::unique_ptr<Circuit> parse_circuit(const std::string &filename) {
FILE* f = nullptr;
if ((f = fopen(filename.c_str(), "r")) == NULL) {
printf("fail to open %s\n", filename.c_str());
exit(1);
}
int n = -1;
std::unique_ptr<Circuit> c = nullptr;
while (fscanf(f, "%s", buffer) != EOF) {
if (strcmp(buffer, "//") == 0 || strcmp(buffer, "OPENQASM") == 0 || strcmp(buffer, "include") == 0) {
} else if (strcmp(buffer, "qreg") == 0) {
fscanf(f, "%*c%*c%*c%d", &n);
c = std::make_unique<Circuit>(n);
} else if (strcmp(buffer, "cx") == 0) {
fscanf(f, "%s", buffer);
auto qid = parse_qid(buffer);
assert(qid.size() == 2);
c->addGate(Gate::CNOT(qid[0], qid[1]));
// printf("cx %d %d\n", qid[0], qid[1]);
} else if (strcmp(buffer, "ccx") == 0) {
fscanf(f, "%s", buffer);
auto qid = parse_qid(buffer);
assert(qid.size() == 3);
c->addGate(Gate::CCX(qid[0], qid[1], qid[2]));
// printf("ccx %d %d %d\n", qid[0], qid[1], qid[2]);
} else if (strcmp(buffer, "cy") == 0) {
fscanf(f, "%s", buffer);
auto qid = parse_qid(buffer);
assert(qid.size() == 2);
c->addGate(Gate::CY(qid[0], qid[1]));
// printf("cy %d %d\n", qid[0], qid[1]);
} else if (strcmp(buffer, "cz") == 0) {
fscanf(f, "%s", buffer);
auto qid = parse_qid(buffer);
assert(qid.size() == 2);
c->addGate(Gate::CZ(qid[0], qid[1]));
// printf("cz %d %d\n", qid[0], qid[1]);
} else if (strcmp(buffer, "h") == 0) {
fscanf(f, "%s", buffer);
auto qid = parse_qid(buffer);
assert(qid.size() == 1);
c->addGate(Gate::H(qid[0]));
// printf("h %d\n", qid[0]);
} else if (strcmp(buffer, "x") == 0) {
fscanf(f, "%s", buffer);
auto qid = parse_qid(buffer);
assert(qid.size() == 1);
c->addGate(Gate::X(qid[0]));
// printf("x %d\n", qid[0]);
} else if (strcmp(buffer, "y") == 0) {
fscanf(f, "%s", buffer);
auto qid = parse_qid(buffer);
assert(qid.size() == 1);
c->addGate(Gate::Y(qid[0]));
// printf("y %d\n", qid[0]);
} else if (strcmp(buffer, "z") == 0) {
fscanf(f, "%s", buffer);
auto qid = parse_qid(buffer);
assert(qid.size() == 1);
c->addGate(Gate::Z(qid[0]));
// printf("z %d\n", qid[0]);
} else if (strcmp(buffer, "s") == 0) {
fscanf(f, "%s", buffer);
auto qid = parse_qid(buffer);
assert(qid.size() == 1);
c->addGate(Gate::S(qid[0]));
// printf("s %d\n", qid[0]);
} else if (strcmp(buffer, "sdg") == 0) {
fscanf(f, "%s", buffer);
auto qid = parse_qid(buffer);
assert(qid.size() == 1);
c->addGate(Gate::SDG(qid[0]));
// printf("s %d\n", qid[0]);
} else if (strcmp(buffer, "t") == 0) {
fscanf(f, "%s", buffer);
auto qid = parse_qid(buffer);
assert(qid.size() == 1);
c->addGate(Gate::T(qid[0]));
// printf("t %d\n", qid[0]);
} else if (strcmp(buffer, "tdg") == 0) {
fscanf(f, "%s", buffer);
auto qid = parse_qid(buffer);
assert(qid.size() == 1);
c->addGate(Gate::TDG(qid[0]));
// printf("t %d\n", qid[0]);
} else {
auto gate = parse_gate(buffer);
if (gate.first == "crx") {
assert(gate.second.size() == 1);
fscanf(f, "%s", buffer);
auto qid = parse_qid(buffer);
assert(qid.size() == 2);
c->addGate(Gate::CRX(qid[0], qid[1], gate.second[0]));
// printf("crx %d %d %f\n", qid[0], qid[1], gate.second[0]);
} else if (gate.first == "cry") {
assert(gate.second.size() == 1);
fscanf(f, "%s", buffer);
auto qid = parse_qid(buffer);
assert(qid.size() == 2);
c->addGate(Gate::CRY(qid[0], qid[1], gate.second[0]));
// printf("cry %d %d %f\n", qid[0], qid[1], gate.second[0]);
} else if (gate.first == "crz") {
assert(gate.second.size() == 1);
fscanf(f, "%s", buffer);
auto qid = parse_qid(buffer);
assert(qid.size() == 2);
c->addGate(Gate::CRZ(qid[0], qid[1], gate.second[0]));
// printf("crz %d %d %f\n", qid[0], qid[1], gate.second[0]);
} else if (gate.first == "cu1") {
assert(gate.second.size() == 1);
fscanf(f, "%s", buffer);
auto qid = parse_qid(buffer);
assert(qid.size() == 2);
c->addGate(Gate::CU1(qid[0], qid[1], gate.second[0]));
// printf("cu1 %d %d %f\n", qid[0], qid[1], gate.second[0]);
} else if (gate.first == "u1") {
assert(gate.second.size() == 1);
fscanf(f, "%s", buffer);
auto qid = parse_qid(buffer);
assert(qid.size() == 1);
c->addGate(Gate::U1(qid[0], gate.second[0]));
// printf("u1 %d %f\n", qid[0], gate.second[0]);
} else if (gate.first == "u3") {
assert(gate.second.size() == 3);
fscanf(f, "%s", buffer);
auto qid = parse_qid(buffer);
assert(qid.size() == 1);
c->addGate(Gate::U3(qid[0], gate.second[0], gate.second[1], gate.second[2]));
// printf("u3 %d %f %f %f\n", qid[0], gate.second[0], gate.second[1], gate.second[2]);
} else if (gate.first == "rx") {
assert(gate.second.size() == 1);
fscanf(f, "%s", buffer);
auto qid = parse_qid(buffer);
assert(qid.size() == 1);
c->addGate(Gate::RX(qid[0], gate.second[0]));
// printf("rx %d %f\n", qid[0], gate.second[0]);
} else if (gate.first == "ry") {
assert(gate.second.size() == 1);
fscanf(f, "%s", buffer);
auto qid = parse_qid(buffer);
assert(qid.size() == 1);
c->addGate(Gate::RY(qid[0], gate.second[0]));
// printf("ry %d %f\n", qid[0], gate.second[0]);
} else if (gate.first == "rz") {
assert(gate.second.size() == 1);
fscanf(f, "%s", buffer);
auto qid = parse_qid(buffer);
assert(qid.size() == 1);
c->addGate(Gate::RZ(qid[0], gate.second[0]));
// printf("rz %d %f\n", qid[0], gate.second[0]);
} else {
printf("unrecognized token %s\n", buffer);
exit(1);
}
}
fgets(buffer, BUFFER_SIZE, f);
}
fclose(f);
if (c == nullptr) {
printf("fail to load circuit\n");
exit(1);
}
return std::move(c);
}
int main(int argc, char* argv[]) {
#if USE_MPI
MyMPI::init();
#endif
MyGlobalVars::init();
std::unique_ptr<Circuit> c;
if (argc != 2) {
printf("./parser qasmfile\n");
exit(1);
}
c = parse_circuit(std::string(argv[1]));
c->compile();
c->run();
c->printState();
Logger::print();
#if USE_MPI
checkMPIErrors(MPI_Finalize());
#endif
return 0;
}