-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCT2.sml
More file actions
executable file
·404 lines (374 loc) · 13.4 KB
/
CT2.sml
File metadata and controls
executable file
·404 lines (374 loc) · 13.4 KB
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
structure CLA = CommandLineArgs
structure TwoGateSet : GATE_SET =
struct
exception InvalidGate
exception Unimplemented
open Math
type qubit = Qubit.qubit
structure Unint =
struct
datatype t =
RZ of (string * qubit)
| RX of (string * qubit)
| U1 of (string * qubit)
| U2 of (string * qubit)
| U3 of (string * qubit)
fun map_support u fidx =
case u of
RZ (s, x) => RZ (s, fidx x)
| RX (s, x) => RX (s, fidx x)
| U1 (s, x) => U1 (s, fidx x)
| U2 (s, x) => U2 (s, fidx x)
| U3 (s, x) => U3 (s, fidx x)
fun support u =
case u of
RZ(_, x) => [x]
| RX(_, x) => [x]
| U1(_, x) => [x]
| U2(_, x) => [x]
| U3(_, x) => [x]
fun labelToGate g =
case g of
(r_some, [x]) =>
if (String.isPrefix "rz" r_some) then SOME (RZ(r_some, x))
else if (String.isPrefix "u1" r_some) then SOME (U1(r_some, x))
else if (String.isPrefix "u2" r_some) then SOME (U2(r_some, x))
else if (String.isPrefix "u3" r_some) then SOME (U3(r_some, x))
else if (String.isPrefix "rx" r_some) then SOME (RX(r_some, x))
else NONE
| _ => NONE
fun str u =
case u of
RZ(s, x) => s ^ " q[" ^ (Qubit.str x) ^ "]"
| RX(s, x) => s ^ " q[" ^ (Qubit.str x) ^ "]"
| U1(s, x) => s ^ " q[" ^ (Qubit.str x) ^ "]"
| U2(s, x) => s ^ " q[" ^ (Qubit.str x) ^ "]"
| U3(s, x) => s ^ " q[" ^ (Qubit.str x) ^ "]"
fun gate_cost g =
case g of
RZ _ => 1
| _ => 0
end
datatype gate =
H of qubit
| S of qubit
| T of qubit
| HD of qubit
| SD of qubit
| TD of qubit
| X of qubit
| Y of qubit
| Z of qubit
| CNOT of qubit * qubit
| SWAP of qubit * qubit
| CZ of qubit * qubit
| CCZ of qubit * qubit * qubit
| CCX of qubit * qubit * qubit
| UNINT of Unint.t
fun map_support g fidx =
case g of
H(x) => H (fidx x)
| S(x) => S (fidx x)
| T(x) => T (fidx x)
| HD(x) => HD (fidx x)
| SD(x) => SD (fidx x)
| TD(x) => TD (fidx x)
| X (x) => X (fidx x)
| Y (x) => Y (fidx x)
| Z (x) => Z (fidx x)
| CNOT (x, y) => CNOT (fidx x, fidx y)
| SWAP (x, y) => SWAP (fidx x, fidx y)
| CZ (x, y) => CZ (fidx x, fidx y)
| CCZ (x, y, z) => CCZ (fidx x, fidx y, fidx z)
| CCX (x, y, z) => CCX (fidx x, fidx y, fidx z)
| UNINT x => UNINT (Unint.map_support x fidx)
fun support g =
case g of
H(x) => [x]
| S(x) => [x]
| T(x) => [x]
| HD(x) => [x]
| SD(x) => [x]
| TD(x) => [x]
| X (x) => [x]
| Y (x) => [x]
| Z (x) => [x]
| CNOT (x, y) => [x, y]
| SWAP (x, y) => [x, y]
| CZ (x, y) => [x, y]
| CCZ (x, y, z) => [x, y, z]
| CCX (x, y, z) => [x, y, z]
| UNINT x => Unint.support x
fun labelToGate g =
case g of
("h", [x]) => H (x)
| ("s", [x]) => S (x)
| ("t", [x]) => T (x)
| ("hdg", [x]) => HD (x)
| ("sdg", [x]) => SD (x)
| ("tdg", [x]) => TD (x)
| ("x", [x]) => X (x)
| ("y", [x]) => Y (x)
| ("z", [x]) => Z (x)
| ("cx", [x, y]) => (if (not (Qubit.eq (x, y))) then () else (print "x =y label\n";raise InvalidGate); CNOT (x, y))
| ("swap", [x, y]) => (if (not (Qubit.eq (x, y))) then () else (print "x =y label\n";raise InvalidGate); SWAP (x, y))
| ("cz", [x, y]) => (if (not (Qubit.eq (x, y))) then () else (print "x =y label\n";raise InvalidGate); CZ (x, y))
| ("ccz", [x, y, z]) => CCZ (x, y, z)
| ("ccx", [x, y, z]) => CCX (x, y, z)
| x =>
case Unint.labelToGate x of
SOME y => UNINT (y)
| NONE => (print (#1 g ^ " " ^ (Int.toString (List.length (#2 g))) ^ "\n"); raise InvalidGate)
(*
(rz_some, [x]) =>
if (String.isPrefix "rz" rz_some) then RZ(rz_some, x)
else labelToGate (rz_some, [x, x]) *)
fun str g =
case g of
H(x) => "h q[" ^ (Qubit.str x) ^ "]"
| S(x) => "s q[" ^ (Qubit.str x) ^ "]"
| T(x) => "t q[" ^ (Qubit.str x) ^ "]"
| HD(x) => "hdg q[" ^ (Qubit.str x) ^ "]"
| SD(x) => "sdg q[" ^ (Qubit.str x) ^ "]"
| TD(x) => "tdg q[" ^ (Qubit.str x) ^ "]"
| X(x) => "x q[" ^ (Qubit.str x) ^ "]"
| Y(x) => "y q[" ^ (Qubit.str x) ^ "]"
| Z(x) => "z q[" ^ (Qubit.str x) ^ "]"
| CNOT(x, y) =>
(if (not (Qubit.eq (x, y))) then () else (print "x =y!!\n";raise InvalidGate); "cx q[" ^ (Qubit.str x) ^ "], q[" ^ (Qubit.str y) ^ "]")
| SWAP(x, y) =>
(if (not (Qubit.eq (x, y))) then () else (print "x =y!!\n";raise InvalidGate); "swap q[" ^ (Qubit.str x) ^ "], q[" ^ (Qubit.str y) ^ "]")
| CZ(x, y) =>
(if (not (Qubit.eq (x, y))) then () else (print "x =y!!\n";raise InvalidGate); "cz q[" ^ (Qubit.str x) ^ "], q[" ^ (Qubit.str y) ^ "]")
| CCZ(x, y, z) => "ccz q[" ^ (Qubit.str x) ^ "], q[" ^ (Qubit.str y) ^ "], q[" ^ (Qubit.str z) ^ "]"
| CCX(x, y, z) => "ccx q[" ^ (Qubit.str x) ^ "], q[" ^ (Qubit.str y) ^ "], q[" ^ (Qubit.str z) ^ "]"
| UNINT x => Unint.str x
fun inverse g =
case g of
H(x) => HD(x)
| S(x) => SD(x)
| T(x) => TD(x)
| HD(x) => H(x)
| SD(x) => S(x)
| TD(x) => T(x)
| X _ => g
| Y _ => g
| Z _ => g
| CNOT _ => g
| SWAP _ => g
| CZ _ => g
| CCZ _ => g
| CCX _ => g
| UNINT _ => raise Unimplemented
fun gate_cost g =
(* 1 *)
case g of
T _ => 1
| TD _ => 1
| CCZ _ => 7
| CCX _ => 7
| UNINT t => Unint.gate_cost t
| _ => 0
fun gate_arity g =
case g of
H _ => 1
| S _ => 1
| T _ => 1
| HD _ => 1
| SD _ => 1
| TD _ => 1
| X _ => 1
| Y _ => 1
| Z _ => 1
| CNOT _ => 2
| SWAP _ => 2
| CZ _ => 2
| CCZ _ => 3
| CCX _ => 3
| UNINT _ => raise Unimplemented
val gate_matrix =
let
val (cos45, sin45) = (sqrt(0.5), sqrt(0.5))
val x = pi/(8.0)
val (one, z) = ((1.0, 0.0), (0.0, 0.0))
val hm = ComplexMatrix.fromList [[( cos45, 0.0), (sin45, 0.0)], [(cos45, 0.0), (~sin45, 0.0)]]
val sm = ComplexMatrix.fromList [[one, z], [z, (0.0, 1.0)]]
val tm = ComplexMatrix.fromList [[one, z], [z, (cos45, sin45)]]
val hdm = ComplexMatrix.dagger hm
val sdm = ComplexMatrix.dagger sm
val tdm = ComplexMatrix.dagger tm
val xm = ComplexMatrix.fromList[[z, one], [one, z]]
val ym = ComplexMatrix.fromList[[z, (0.0, ~1.0)], [(0.0, 1.0), z]]
val zm = ComplexMatrix.fromList[[one, z], [z, (~1.0, 0.0)]]
val cnotm = ComplexMatrix.fromList [[one, z, z, z], [z, one, z, z], [z, z, z, one], [z, z, one, z]]
val swapm = ComplexMatrix.fromList [[one, z, z, z], [z, z, one, z], [z, one, z, z], [z, z, z, one]]
val czm = ComplexMatrix.fromList [[one, z, z, z], [z, one, z, z], [z, z, one, z], [z, z, z, (~1.0, 0.0)]]
val ccxm =
let
fun eij (i, j) =
if j = 3 then if i = 7 then one else z
else if j = 7 then if i = 3 then one else z
else if i <> j then z
else one
in
ComplexMatrix.tabulate (8, 8) eij
end
val cczm =
let
fun eij (i, j) =
if i <> j then z
else if i < 7 then one
else (~1.0, 0.0)
in
ComplexMatrix.tabulate (8, 8) eij
end
in
fn g =>
case g of
H _ => hm
| S _ => sm
| T _ => tm
| HD _ => hdm
| SD _ => sdm
| TD _ => tdm
| X _ => xm
| Y _ => ym
| Z _ => zm
| CNOT _ => cnotm
| SWAP _ => swapm
| CZ _ => czm
| CCZ _ => cczm
| CCX _ => ccxm
| UNINT _ => raise Unimplemented
end
end
structure Experiment =
struct
structure TwoOPT = CircuitOPT (structure GateSet = TwoGateSet)
val qasm_file = CLA.parseString "circuit" "test-small.qasm"
val print_out = CLA.isArg "outfile"
val outfile = CLA.parseString "outfile" "out.qasm"
val no_preprocess = CLA.isArg "nopp"
val timeout = Time.fromReal (Real.fromInt (CLA.parseInt "timeout" 3600))
val writeLog = CLA.isArg "logfile"
val logfile = CLA.parseString "logfile" "out.lopt.log"
val circ_name =
Substring.string (#2 (Substring.splitr (fn c => c <> #"/") (Substring.full qasm_file)))
fun preprocess (c, dump, outfile) =
let
val c' = run "preprocessing" (fn _ => TwoOPT.preprocess c)
in
if dump then TwoOPT.dump c' outfile
else ()
end
fun optimize () =
let
val c =
let val nppc = (TwoOPT.from_qasm qasm_file) in
if no_preprocess then nppc
else (run "preprocessing" (fn _ => TwoOPT.preprocess nppc))
end
val _ = print ("circuit size after preprocessing = " ^ (Int.toString (TwoOPT.size c)) ^ "\n")
val _ = print ("greedy optimize\n")
val rellog = (Time.now(), TwoOPT.size c)
val (c', tm) = Util.getTime (fn _ => TwoOPT.greedy_optimize c timeout)
val grec = (Time.now(), TwoOPT.size c')
val c'' =
if Time.< (tm, timeout) then run "search optimization" (fn _ => TwoOPT.search c' (Time.- (timeout, tm)))
else c'
val _ = print ("greedy shrank circuit by " ^ (Int.toString (TwoOPT.size c - TwoOPT.size c') ^ "\n"))
val _ = print ("search shrank circuit by " ^ (Int.toString (TwoOPT.size c' - TwoOPT.size c'') ^ "\n"))
val _ = print ("new size = " ^ (Int.toString (TwoOPT.size c'') ^ "\n"))
val logstr =
let
val (it, _) = rellog
val (tg, szg) = grec
val t = Time.toReal (Time.-(tg, it))
in
(TwoOPT.optlog rellog) ^ "\n" ^ ("(" ^ (Real.toString t) ^ ", " ^ (Int.toString szg) ^ ");\n")
end
val _ = if writeLog then WriteFile.dump (logfile, logstr) else ()
in
if print_out then TwoOPT.dump c'' outfile
else ()
end
(* fun comb_opt (wt) =
let
val c =
let val nppc = (TwoOPT.from_qasm qasm_file) in
if no_preprocess then nppc
else (run "preprocessing" (fn _ => TwoOPT.preprocess nppc))
end
val _ = print ("circuit size after preprocessing = " ^ (Int.toString (TwoOPT.size c)) ^ "\n")
val rellog = (Time.now(), TwoOPT.size c)
val (c', tm) = Util.getTime (fn _ => TwoOPT.combined_opt c wt timeout)
val final_size = TwoOPT.size c'
val _ = print ("combined shrank circuit by " ^ (Int.toString (final_size - TwoOPT.size c) ^ "\n"))
val _ = print ("new size = " ^ (Int.toString (final_size) ^ "\n"))
val _ = print ("time taken = " ^ ((Real.toString (Time.toReal tm))) ^ "\n")
val logstr = (TwoOPT.optlog rellog) ^ "\n" ^ ("(" ^ (Real.toString (Time.toReal tm)) ^ ", " ^ (Int.toString final_size) ^ ");\n")
val _ = if writeLog then WriteFile.dump (logfile, logstr) else ()
in
if print_out then TwoOPT.dump c' outfile
else ()
end *)
fun loop_opt (optFunc, c) =
let
fun optLoop (c, cnt) =
let
val (c', tm) = Util.getTime (fn _ => optFunc c)
val tmstr = " loop time = " ^ (Real.toString (Time.toReal tm))
(* val c' = optFunc c *)
val new_size = TwoOPT.size c'
val og_size = TwoOPT.size c
val _ = print ("new size = " ^ (Int.toString new_size) ^ (" old size = ") ^ (Int.toString og_size) ^ tmstr ^ " loop num = " ^ (Int.toString cnt) ^ "\n")
val reduction = (og_size - new_size)
in
if reduction*100 >= og_size then optLoop (TwoOPT.compress c', cnt + 1)
else (c', cnt + 1)
end
val rellog = (Time.now(), TwoOPT.size c)
val (c', numLoops) = Benchmark.run "loop optimization" (fn _ => optLoop (c, 0))
val _ = print ("numLoops = " ^ (Int.toString numLoops) ^ "\n")
in
c'
end
fun run_optimizer () =
let
fun get_circuit () =
let
val no_preprocess = CLA.isArg "nopp"
val nppc = (TwoOPT.from_qasm qasm_file)
in
if no_preprocess then nppc
else (run "preprocessing" (fn _ => TwoOPT.preprocess nppc))
end
val c = get_circuit ()
val _ = print ("circuit size before optimization = " ^ (Int.toString (TwoOPT.size c)) ^ "\n")
val initial_cost = TwoOPT.cost c
val wt = CLA.parseReal "wtcomb" 0.0
val rellog = (Time.now(), TwoOPT.size c)
val (c', tm) = Util.getTime (fn _ =>
if CLA.isArg "greedyonly" then loop_opt (fn c => TwoOPT.greedy_optimize c timeout, c)
else if CLA.isArg "wtcomb" then loop_opt (fn c => TwoOPT.combined_opt c wt timeout, c)
else c)
val final_size = TwoOPT.size c'
val final_cost = TwoOPT.cost c'
val _ = print ("circuit optimized by " ^ (Int.toString (final_cost - initial_cost) ^ "\n"))
val _ = print ("new cost = " ^ (Int.toString (final_cost) ^ "\n"))
val _ = print ("time taken = " ^ ((Real.toString (Time.toReal tm))) ^ "\n")
(* val logstr = (TwoOPT.optlog rellog) ^ "\n" ^ ("(" ^ (Real.toString (Time.toReal tm)) ^ ", " ^ (Int.toString final_size) ^ ");\n") *)
val logstr = ("(0, " ^ (Int.toString (initial_cost)) ^ ");\n") ^
("(" ^ (Real.toString (Time.toReal tm)) ^ ", " ^ (Int.toString final_cost) ^ ");\n")
val _ = if writeLog then WriteFile.dump (logfile, logstr) else ()
in
if print_out then TwoOPT.dump c' outfile
else ()
end
end
val _ = Experiment.run_optimizer ()
(* val _ = TwoOPT.cprint c *)
(* val _ = print(ComplexMatrix.str (TwoOPT.eval_circuit c)) *)
(* val _ = print(ComplexMatrix.str (TwoOPT.eval_circuit c)) *)
(* val _ = print "after circuit\n" *)
(* val _ = print (ComplexMatrix.str (TwoOPT.eval_circuit c')) *)