-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.h
356 lines (293 loc) · 10 KB
/
main.h
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#ifndef __MAIN_H__
#define __MAIN_H__
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
#include <assert.h>
#include <map>
#include <vector>
#include <time.h>
#include <cmath>
#include <algorithm>
#include <set>
#include <utility>
#include"z3++.h"
using namespace std;
using namespace z3;
#define MAX_QUBITS 1024
#define MAX_GATE_COUNT (MAX_QUBITS*20)
#define MILLION (1000*1000)
#define BILLION (1000*1000*1000)
//typedef optimize SolverType;
typedef solver SolverType;
typedef vector<int> IntVect;
typedef map<int, IntVect> Int2VectMap;
typedef vector<int>::iterator IntVectItr;
typedef set<int> IntSet;
typedef set<int>::iterator IntSetItr;
typedef pair<int, int> ProgEdge;
#define MIN(x, y) ((x)<(y) ? (x) : (y))
#define MAX(x, y) ((x)>(y) ? (x) : (y))
#define TRUE 1
#define FALSE 0
/* Program classes */
class Qubit{
public:
int id;
string name;
friend std::ostream& operator<< (std::ostream&, const Qubit&);
};
class Gate{
public:
int id;
string type;
int nBits;
Qubit *pQubitList[2]; // pointers to atmost 2 qubits (assuming input has <= 2 qubit gates)
int nIn;
Gate *pInGates[4]; // atmost 4 incoming/outgoing dependencies
friend std::ostream& operator<< (std::ostream&, const Gate&);
};
class Circuit{
public:
int nQubits;
int nGates;
Qubit *pQubits;
Gate *pGates;
Int2VectMap cnotOverlaps;
Int2VectMap gateDepends;
//greedy mapping
int qx[MAX_QUBITS];
int qy[MAX_QUBITS];
//program graph related vars
int nEdges;
int nNodes;
Int2VectMap graphEdges;
Int2VectMap nodeEdgeIds;
void input_from_file(string fname);
void input_cnot_overlap_info(string fname);
void input_gate_descendants(string fname);
void input_program_graph(string fname);
void input_greedy_mapping(string fname);
friend std::ostream& operator<< (std::ostream&, const Circuit&);
};
/* Hardware classes */
class HwQubit{
public:
int id;
int nEdges;
HwQubit *pEdgeList[4]; //assuming atmost 4 way conectivity for a qubit
int pos_x;
int pos_y;
};
#define INVALID (-1)
#define _get_ij2id(machine, i, j) (i * machine->nCols + j)
#define _get_id2i(machine, id) (id / machine->nCols)
#define _get_id2j(machine, id) (id % machine->nCols)
#define MAX_ROWS 2
#define MAX_COLS 8
class MachineProp{
public:
map<string, int> gateTime;
int cohrTime[MAX_ROWS][MAX_COLS];
int cnotRRTime[MAX_ROWS][MAX_COLS][MAX_ROWS][MAX_COLS];
int cnotOneBendTime[MAX_ROWS][MAX_COLS][MAX_ROWS][MAX_COLS][2][3]; //cx, cy, tx, ty, junctions 0/1, (jx, jy, time)
};
class Machine{
public:
int nQubits;
int nRows, nCols;
HwQubit *pQubitList;
MachineProp *pProp;
void setup_grid_transmons(int m, int n);
void setup_gate_times();
void input_coherence_data(string fname);
void input_rr_cnot_data(string fname);
void input_one_bend_cnot_data(string fname);
int get_id2uid(int id);
int get_id2did(int id);
int get_id2lid(int id);
int get_id2rid(int id);
void _add_neighbor_if_valid(HwQubit *pQ, int id);
};
/* Solver classes */
/* Routing policies */
#define RECTANGLE_RSRV 1
#define ONE_BEND_RSRV 2
#define IS_UNSAT 0
#define IS_SAT 1
#define ABSOLUTE(e) (ite((e) >= 0, (e), (-e)))
#define DISTANCE(q1x, q1y, q2x, q2y) (ABSOLUTE((q1x - q2x)) + ABSOLUTE((q1y - q2y)))
#define MIN_MAX_COND(ax, bx, minx, maxx) (ite((ax)<=(bx), (minx==(ax))&&(maxx==(bx)), (minx==(bx))&&(maxx==(ax))))
#define POINT_IN_RECT(lx, ly, rx, ry, px, py) ((((px) >= (lx)) && ((px) <= (rx))) && (((py) >= (ly)) && ((py) <= (ry))))
#define RECT_NOT_OVERLAP(l1x, l1y, r1x, r1y, l2x, l2y, r2x, r2y) ( ((l1x) > (r2x)) || ((l2x) > (r1x)) || ((l1y) > (r2y)) || ((l2y) > (r1y)) )
class SMTOutput{
public:
//qubit mappings
IntVect qx;
IntVect qy;
//gate start and durations
IntVect g;
IntVect d;
//junction mappings for CNOT in 1-bend path reservation
IntVect jx;
IntVect jy;
IntVect cnotIdx;
int result;
Circuit *pCircuit;
Machine *pMachine;
void check_sanity();
void print_to_file(string fname);
void load_qubit_mapping(int *, int *);
SMTOutput(Circuit *pC, Machine *pM);
int _check_routing_conflict(int g1, int g2, int c1, int t1, int c2, int t2);
int _num_conflicts();
int _fix_earliest_conflict();
int _last_gate_time();
private:
void _check_mapping_bounds();
void _check_gate_time_bounds();
void _check_dependency_constraint();
void _check_unique_mapping();
void _check_gate_durations();
void _check_routing_constraints();
void _update_block_map_single_qubit(vector <vector <int> > &blkd, int qX, int qY, int t, int duration, int etype);
IntSet* _find_qubits_to_block(int q1X, int q1Y, int q2X, int q2Y);
void _block_qubits_for_duration(vector <vector <int> > &blkd, IntSet *pSet, int t, int duration);
};
class SMTSchedule{
public:
Circuit *pCircuit;
Machine *pMachine;
int maxT; // maximum allowed time slot
SMTSchedule(Circuit *pC, Machine *pM);
SMTOutput* compute_schedule();
void create_optimization_instance(context &c, SolverType &opt, int routing_flag);
SMTOutput* check_instance(context &c, SolverType &opt, int maxTimeSlot);
//two stage, mapping
void ts_create_mapping_instance(context &c, SolverType &opt);
void _add_edge_durations(context &c, SolverType &opt, expr_vector &qx, expr_vector &qy, expr_vector &d);
//lazy routing:cegar
void lr_create_optimization_instance(context &c, SolverType &opt);
SMTOutput* lr_check_current_instance(context &c, SolverType &opt, int maxTimeSlot);
int _lr_cegar_process_routing_conflicts(context &c, SolverType &opt, SMTOutput *pOut);
SMTOutput* lr_cegar_solve(context &c, SolverType &opt, int maxTimeSlot);
void _lr_add_conflict_clause_time(context &c, SolverType &opt, int g1, int g2);
//lazy routing:greedy
SMTOutput* lr_greedy_solve(context &c, SolverType &opt, int maxTimeSlot);
//MS+SR method
void create_sr_instance(context &c, SolverType &opt, SMTOutput *pOut);
void _set_qubit_mapping(SolverType &opt, expr_vector &qx, expr_vector &qy, SMTOutput *pOut);
private:
//data unaware constraints - schedule.cpp
void _add_qubit_mapping_bounds(SolverType&, expr_vector&, expr_vector&);
void _add_single_qubit_gate_durations(SolverType &opt, expr_vector &d);
void _add_gate_time_bounds(SolverType&, expr_vector&, expr_vector &);
void _add_gate_dependency_constraints(SolverType &, expr_vector &, expr_vector &);
void _set_dummy_gate_bound(SolverType&, expr_vector&, expr_vector &, context&, int);
void _add_rectangle_reservation_constraints(SolverType &, context &, expr_vector &, expr_vector &, expr_vector &, expr_vector &);
void _add_1bend_reservation_constraints(SolverType &, context &, expr_vector &, expr_vector &, expr_vector &, expr_vector &);
void _add_cnot_durations_no_data(SolverType &opt, expr_vector &qx, expr_vector &qy, expr_vector &d);
void _add_mirror_symmetry(SolverType &opt, expr_vector &qx, expr_vector &qy);
//time data aware constraints - schedule.cpp
void _add_coherence_aware_time_bounds(SolverType &opt, expr_vector &qx, expr_vector &qy, expr_vector &g, expr_vector &d);
void _add_cnot_durations_rect_rsrv_data(SolverType &opt, expr_vector &qx, expr_vector &qy, expr_vector &d);
void _add_cnot_durations_one_bend_data(SolverType &, context &, expr_vector &, expr_vector &, expr_vector &);
//utils - utils.cpp
void _print_qubit_mapping(model &, expr_vector &, expr_vector &);
void _print_gate_times(model &, expr_vector &, expr_vector &);
void _copy_qubit_mapping(SMTOutput *pOut, model &m, expr_vector &qx, expr_vector &qy);
void _copy_gate_times(SMTOutput *pOut, model &m, expr_vector &g, expr_vector &d);
void _copy_cnot_route(SMTOutput *pOut, model &m, context &c);
};
class EJFSchedule{
private:
Circuit *pCircuit;
Machine *pMachine;
SMTOutput *pInitMap;
void _compute_durations();
void _compute_cnot_conflicts();
public:
int start_time[MAX_GATE_COUNT];
int duration[MAX_GATE_COUNT];
Int2VectMap cnotOverlaps2way;
Int2VectMap cnotConflicts;
int ct_lx[MAX_GATE_COUNT];
int ct_ly[MAX_GATE_COUNT];
int ct_rx[MAX_GATE_COUNT];
int ct_ry[MAX_GATE_COUNT];
int cj_lx[MAX_GATE_COUNT];
int cj_ly[MAX_GATE_COUNT];
int cj_rx[MAX_GATE_COUNT];
int cj_ry[MAX_GATE_COUNT];
int jt_lx[MAX_GATE_COUNT];
int jt_ly[MAX_GATE_COUNT];
int jt_rx[MAX_GATE_COUNT];
int jt_ry[MAX_GATE_COUNT];
int jx[MAX_GATE_COUNT];
int jy[MAX_GATE_COUNT];
EJFSchedule(Circuit *, Machine *, SMTOutput *);
void compute_schedule();
void schedule_next_gate();
void compute_rr_vars();
int update_time_rr(int gate_id, int &time);
int one_bend_check_time(int gate_id, int time);
int set_one_bend_junction(int gate_id, int jx, int jy);
int update_time_1bp(int gate_id, int &time);
int check_routing_conflicts();
};
class MapAlgo{
private:
Circuit *pCircuit;
Machine *pMachine;
IntSet VertexList;
vector<ProgEdge> EdgeList;
//qubit mappings
IntVect qx;
IntVect qy;
public:
MapAlgo(Circuit *pC, Machine *pM);
void create_program_graph(Circuit *pC);
void program_id_mapping();
void copy_mapping(SMTOutput *);
};
class QParams{
public:
int maxTimeSlot;
int CNOT_Count_Max;
int time_CNOT;
int time_X;
int time_Y;
int time_H;
int time_MeasZ;
int machineRows;
int machineCols;
int routingPolicy;
int couplingThresh;
int transitiveClFlag;
int dataAwareFlag;
int is_greedy;
};
extern QParams gParams;
class QStats{
public:
int cntMapping;
int cntDepend;
int cntRouting;
int z3_arith_conflicts;
int z3_conflicts;
int z3_arith_add_rows;
int z3_mk_clause;
int z3_added_eqs;
int z3_binary_propagations;
int z3_restarts;
int z3_decisions;
};
extern QStats gStats;
void test();
int MyGetTime();
void print_gd(int id, int g, int d, string gate_name);
void accumulate_stats(stats &S);
void init_stats();
void print_stats();
#endif