-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput.cpp
271 lines (248 loc) · 7.69 KB
/
output.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include "main.h"
SMTOutput::SMTOutput(Circuit *pC, Machine *pM){
pCircuit = pC;
pMachine = pM;
}
void SMTOutput::load_qubit_mapping(int *pQx, int *pQy){
for(int i=0; i<pCircuit->nQubits; i++){
qx.push_back(pQx[i]);
qy.push_back(pQy[i]);
}
}
void SMTOutput::_check_mapping_bounds(){
int R = gParams.machineRows;
int C = gParams.machineCols;
for(IntVectItr itr=qx.begin(); itr != qx.end(); itr++){
assert(*itr >= 0);
assert(*itr < R);
}
for(IntVectItr itr=qy.begin(); itr != qy.end(); itr++){
assert(*itr >= 0);
assert(*itr < C);
}
cout << "Mapping bounds check passed\n";
}
void SMTOutput::_check_unique_mapping(){
int i,j;
for(i=0; i<pCircuit->nQubits; i++){
for(j=i+1; j<pCircuit->nQubits; j++){
if(!(qx[i] != qx[j] || qy[i] != qy[j])){
cout << i << " " << j << " " << qx[i] << " " << qx[j] << " " << qy[i] << " " << qy[j] << endl;
}
assert(qx[i] != qx[j] || qy[i] != qy[j]);
}
}
cout << "Unique bounds check passed\n";
}
void SMTOutput::_check_gate_time_bounds(){
int maxT = gParams.maxTimeSlot;
for(int i=0; i<pCircuit->nGates; i++){
assert((g[i] + d[i]) < maxT);
}
cout << "Gate time check passed\n";
}
void SMTOutput::_check_dependency_constraint(){
int i,j;
Circuit *pC = pCircuit;
Machine *pM = pMachine;
Gate *pGate;
Gate *pInGate;
int id_cur_gate;
int id_in_gate;
for(i=0; i<pC->nGates; i++){
pGate = pC->pGates + i;
id_cur_gate = pGate->id;
assert(id_cur_gate == i);
for(j=0; j<pGate->nIn; j++){
pInGate = pGate->pInGates[j];
id_in_gate = pInGate->id;
if(g[id_cur_gate] < g[id_in_gate] + d[id_in_gate]){
cout << "Violation\n";
print_gd(id_cur_gate, g[id_cur_gate], d[id_cur_gate], "Current");
print_gd(id_in_gate, g[id_in_gate], d[id_in_gate], "In gate");
}
assert(g[id_cur_gate] >= g[id_in_gate] + d[id_in_gate]);
}
}
cout << "Gate dependency check passed\n";
}
void SMTOutput::_check_gate_durations(){
int i;
Circuit *pC = pCircuit;
Machine *pM = pMachine;
MachineProp *pProp = pMachine->pProp;
Gate *pG;
for(i=0; i<pC->nGates; i++){
pG = pC->pGates + i;
if(pG->nBits == 1){
assert(d[i] == pProp->gateTime[pG->type]-1);
}else if(pG->nBits == 2){
//compute distance between control and target and find duration
Qubit *pQ1 = pG->pQubitList[0];
Qubit *pQ2 = pG->pQubitList[1];
int id1 = pQ1->id;
int id2 = pQ2->id;
int dist = abs(qx[id1] - qx[id2]) + abs(qy[id1] - qy[id2]);
int duration = 2*((dist-1)*pProp->gateTime["SWAP"]) + pProp->gateTime[pG->type] - 1;
assert(duration == d[i]);
}
}
cout << "Duration check passed\n";
}
class GateEvent{
public:
int timestamp;
int duration;
int etype;
Gate *pGate;
GateEvent(int ts, int d, int et, Gate *pG) : timestamp(ts), duration(d), etype(et), pGate(pG) {}
};
class GateEventCmp{
public:
inline bool operator() (const GateEvent &e1, const GateEvent &e2){
if(e1.timestamp < e2.timestamp) return true;
if(e2.timestamp < e1.timestamp) return false;
//timestamps are equal
return (e1.etype < e2.etype);
}
};
void SMTOutput::_update_block_map_single_qubit(vector <vector <int> > &blkd, int qX, int qY, int t, int duration, int etype){
int qid = _get_ij2id(pMachine, qX, qY);
if(etype == 0){ //start
for(int i=t; i<=t+duration; i++){
assert(blkd[qid][i] == 0);
blkd[qid][i] = 1;
}
}
}
IntSet* SMTOutput::_find_qubits_to_block(int q1X, int q1Y, int q2X, int q2Y){
int lX, lY;
int rX, rY;
lX = MIN(q1X, q2X);
lY = MIN(q1Y, q2Y);
rX = MAX(q1X, q2X);
rY = MAX(q1Y, q2Y);
int D = gParams.couplingThresh;
IntSet *pSet = new IntSet;
for(int q=0; q<pMachine->nQubits; q++){
int pX = _get_id2i(pMachine, q);
int pY = _get_id2j(pMachine, q);
int is_inside = POINT_IN_RECT(lX-D, lY-D, rX+D, rY+D, pX, pY);
if(is_inside) pSet->insert(q);
}
return(pSet);
}
void SMTOutput::_block_qubits_for_duration(vector <vector <int> > &blkd, IntSet *pSet, int t, int duration){
IntSetItr itr = pSet->begin();
int q;
for(; itr!=pSet->end(); itr++){
q = *itr;
for(int i=t; i<=t+duration; i++){
assert(blkd[q][i] == 0);
blkd[q][i] = 1;
}
}
}
void SMTOutput::_check_routing_constraints(){
int maxT = gParams.maxTimeSlot;
Circuit *pC = pCircuit;
Machine *pM = pMachine;
//machine qubits x T
vector< vector<int> > blkd(pM->nQubits, vector<int>(maxT));
int q,t;
for(q=0; q<pM->nQubits; q++){
for(t=0; t<maxT; t++){
blkd[q][t] = 0;
}
}
//find the order of events (t, gate) of gate starts and finish times
std::vector<GateEvent> events;
Gate *pGate;
int id_cur_gate;
for(int i=0; i<pC->nGates; i++){
pGate = pC->pGates + i;
id_cur_gate = pGate->id;
events.push_back(GateEvent(g[id_cur_gate], d[id_cur_gate], 0, pGate));
events.push_back(GateEvent(g[id_cur_gate]+d[id_cur_gate], d[id_cur_gate], 1, pGate));
}
std::sort(events.begin(), events.end(), GateEventCmp());
//run through the gate start/end events
GateEvent *pGE;
Qubit *pQ1, *pQ2;
Gate *pG;
int duration;
for(std::vector<GateEvent>::iterator itr = events.begin(); itr != events.end(); itr++){
pGE = &(*itr);
pG = pGE->pGate;
t = pGE->timestamp;
duration = pGE->duration;
//for each gate, find its machine qubits, use machine qubit locations and gate time to block
if(pG->nBits == 1){
pQ1 = pG->pQubitList[0];
int qid = pQ1->id;
_update_block_map_single_qubit(blkd, qx[qid], qy[qid], t, duration, pGE->etype);
}else if(pG->nBits == 2){
int cid, tid;
pQ1 = pG->pQubitList[0];
pQ2 = pG->pQubitList[1];
cid = pQ1->id;
tid = pQ2->id;
if(pGE->etype == 0){
if(gParams.routingPolicy == RECTANGLE_RSRV){
IntSet *pBlock = _find_qubits_to_block(qx[cid], qy[cid], qx[tid], qy[tid]);
_block_qubits_for_duration(blkd, pBlock, t, duration);
delete pBlock;
}else if(gParams.routingPolicy == ONE_BEND_RSRV){
int jX = jx[cnotIdx[pG->id]];
int jY = jy[cnotIdx[pG->id]];
IntSet *pBlock1 = _find_qubits_to_block(qx[cid], qy[cid], jX, jY);
IntSet *pBlock2 = _find_qubits_to_block(qx[tid], qy[tid], jX, jY);
IntSet *pSet = new IntSet;
for(IntSetItr itr = pBlock1->begin(); itr != pBlock1->end(); itr++) pSet->insert(*itr);
for(IntSetItr itr = pBlock2->begin(); itr != pBlock2->end(); itr++) pSet->insert(*itr);
delete pBlock1;
delete pBlock2;
_block_qubits_for_duration(blkd, pSet, t, duration);
delete pSet;
}
}
}
}
cout << "Routing constraints check passed\n";
}
void SMTOutput::check_sanity(){
_check_mapping_bounds();
//_check_unique_mapping();
//_check_gate_time_bounds();
_check_gate_durations();
_check_dependency_constraint();
//_check_routing_constraints();
}
void SMTOutput::print_to_file(string fname){
//File format: mappings for each program qubit
//Gate id, gate start time, gate duration
//CNOT junctions for 1-bend paths
Circuit *pC = pCircuit;
Machine *pM = pMachine;
ofstream ofile;
ofile.open(fname.c_str());
int i;
int qid;
for(i=0; i<pC->nQubits; i++){
qid = pCircuit->pQubits[i].id;
ofile << "Q " << qid << " " << qx[qid] << " " << qy[qid] << "\n";
}
int gid;
for(i=0; i<pC->nGates; i++){
gid = pCircuit->pGates[i].id;
ofile << "G " << gid << " " << g[gid] << " " << d[gid] << "\n";
}
if(gParams.routingPolicy == ONE_BEND_RSRV){
for(i=0; i<pC->nGates; i++){
gid = pCircuit->pGates[i].id;
if(cnotIdx[gid] != INVALID){
ofile << "R " << i << " " << jx[cnotIdx[i]] << " " << jy[cnotIdx[gid]] << "\n";
}
}
}
}