-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCTopt.sml
More file actions
307 lines (261 loc) · 7.99 KB
/
CTopt.sml
File metadata and controls
307 lines (261 loc) · 7.99 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
structure CLA = CommandLineArgs
structure CliffordGateSet : GATE_SET =
struct
exception InvalidGate
open Math
datatype gate =
H of int
| S of int
| T of int
| HD of int
| SD of int
| TD of int
fun support g =
case g of
H(x) => QSet.from_list ([x])
| S(x) => QSet.from_list ([x])
| T(x) => QSet.from_list ([x])
| HD(x) => QSet.from_list ([x])
| SD(x) => QSet.from_list ([x])
| TD(x) => QSet.from_list ([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)
| _ => (print (#1 g ^ " " ^ (Int.toString (List.length (#2 g))) ^ "\n"); raise InvalidGate)
fun str g =
case g of
H(x) => "h (" ^ (Int.toString x) ^ ")"
| S(x) => "s (" ^ (Int.toString x) ^ ")"
| T(x) => "t (" ^ (Int.toString x) ^ ")"
| HD(x) => "hdg (" ^ (Int.toString x) ^ ")"
| SD(x) => "sdg (" ^ (Int.toString x) ^ ")"
| TD(x) => "tdg (" ^ (Int.toString 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)
val num_gates = 6
val gate_matrix =
let
val (cos45, sin45) = (sqrt(0.5), sqrt(0.5))
val x = pi/(8.0)
val hm = ComplexMatrix.fromList [[( cos45, 0.0), (sin45, 0.0)], [(cos45, 0.0), (~sin45, 0.0)]]
val sm = ComplexMatrix.fromList [[( 1.0, 0.0), ( 0.0, 0.0)], [( 0.0, 0.0), ( 0.0, 1.0)]]
val tm = ComplexMatrix.fromList [[( 1.0, 0.0), ( 0.0, 0.0)], [( 0.0, 0.0), ( cos45, sin45)]]
val hdm = ComplexMatrix.dagger hm
val sdm = ComplexMatrix.dagger sm
val tdm = ComplexMatrix.dagger tm
in
fn g =>
case g of
H _ => hm
| S _ => sm
| T _ => tm
| HD _ => hdm
| SD _ => sdm
| TD _ => tdm
end
(* exception InvalidGate
fun labelToIdx g =
case g of
"h" => 0
| "s" => 1
| "t" => 2
| "hdg" => 3
| "sdg" => 4
| "tdg" => 5
| _ => raise InvalidGate *)
(* fun strtoidx sss =
let
fun hs s = Seq.map labelToIdx s
in
Seq.map (fn ss => Seq.map hs ss) sss
end *)
(* fun lteq (c, d) =
if (Seq.length c > Seq.length d) then false
else if (Seq.length d > Seq.length c) then true
else
let
fun countT s idx cnt =
if idx = Seq.length s then cnt
else if (Seq.nth s idx = 2 orelse Seq.nth s idx = 5) then countT s (idx + 1) (cnt + 1)
else countT s (idx + 1) cnt
val (tc, td) = (countT c 0 0, countT d 0 0)
in
if tc > td then false
else if td > tc then true
else true
end *)
(* fun makeRepFirst sss =
let
fun ltIndx ss i j = lteq (Seq.nth ss i, Seq.nth ss j)
fun repIdx ss currMin idx =
if idx = Seq.length ss then currMin
else if ltIndx ss currMin idx then repIdx ss currMin (idx + 1)
else repIdx ss idx (idx + 1)
fun swap ss i j =
if i = j then ()
else let
val si = Seq.nth ss i
val sj = Seq.nth ss j
in
(ArraySlice.update (ss, i, sj); ArraySlice.update (ss, j, si))
end
val minIndices = Seq.map (fn ss => repIdx ss 0 0) sss
in
Seq.foreach sss (fn (i, ss) => swap ss 0 (Seq.nth minIndices i))
end *)
(*
fun maxlen sss =
let
val x = Seq.map (fn ss => Seq.map (Seq.length) ss) sss
val y = Seq.map (fn ss => Seq.reduce Int.max 0 ss) x
val z = Seq.reduce Int.max 0 y
in
z
end
fun insert sss t =
let
(* trie insertions need to be sequential *)
fun nonpar_foreach s f =
let
fun loop idx max_len =
if (idx >= Seq.length s) then max_len
else loop (idx + 1) (Int.max (max_len, f (idx, Seq.nth s idx)))
in
loop 0 0
end
fun ins (_, ss) =
let
val rep = Seq.toList (Seq.nth ss 0)
in
nonpar_foreach ss (fn (i, s) => if (i = 0) then (Seq.length s) else (BlackBoxOpt.insert t (Seq.toList s, rep); Seq.length s))
end
in
nonpar_foreach sss ins
end
val f = "../sk-ml/" ^ (CLA.parseString "filename" "test_all_5_1_complete_ECC_set.json")
val sss = strtoidx (ParseQuartz.parse f)
val _ = makeRepFirst sss
val max_len = maxlen sss
val optc = BlackBoxOpt.mkOpt max_len
val useopt = CommandLineArgs.parseBool "useopt" true
val _ = if useopt then insert sss optc else 0
fun load_circuit f =
let
val (sgc, circuit) = ParseQASM.readQASM f
in
Seq.map (fn (label, _) => labelToIdx label) circuit
end *)
(*
fun analyze_dist_brute {eq = mpseq, max_len = _} =
let
val num_dists = (Seq.length mpseq) * (Seq.length mpseq)
val dists = ForkJoin.alloc num_dists
val _ = ForkJoin.parfor 5000 (0, num_dists)
(fn i =>
let
val (x, y) = (i mod (Seq.length mpseq), i div (Seq.length mpseq))
val ((mx, _), (my, _)) = (Seq.nth mpseq x, Seq.nth mpseq y)
in
Array.update (dists, i, SUM.proj_trace_dist (mx, my))
end
)
(* val _ = Seq. *)
val avg_dist = Real./ (Seq.reduce (Real.+) (0.0) (ArraySlice.full dists), Real.fromInt num_dists)
in
print ("avg dist = " ^ (Real.toString avg_dist) ^ "\n")
end *)
(* val cliffordt_gates : GateSet.t =
{
gates = gates,
labels = Seq.fromList ["h", "s", "t", "hdg", "sdg", "tdg"] ,
order = Seq.fromList [2, 4, 8, 2, 4, 8],
inverses = Seq.fromList [3, 4, 5, 0, 1, 2],
size = 6
} *)
end
structure CliffordOPT = CircuitOPT (structure GateSet = CliffordGateSet)
val f = CLA.parseString "circuit" "test-small.qasm"
val c = CliffordOPT.from_qasm f
val _ = CliffordOPT.cprint c
val m = CliffordOPT.eval_circuit c
val _ = print(ComplexMatrix.str(m))
(* val c' = CliffordOPT.optimize c *)
(*
functor CircuitOpt =
struct
structure CliffordCircuit = Circuit (structure GateSet = CliffordGateSet)
open CliffordCircuit
fun get_rep_seq ss =
let
fun labelToGate g =
case g of
"h" => H (0)
| "s" => S (0)
| "t" => T (0)
| "hdg" => HD (0)
| "sdg" => SD (0)
| "tdg" => TD (0)
| _ => raise InvalidGate
in
Seq.map
(fn rep =>
let
val grep = Seq.map labelToGate rep
in
(eval_raw_sequence grep, grep)
end
)
end
val frep = CLA.parseString "filerep" "test_12_1_representative_set.json"
val (ssrep, tm) = Util.getTime (fn _ => ParseQuartz.parse_rep frep)
val _ = print ("parsed filerep in " ^ Time.fmt 4 tm ^ "s\n")
val _ = if (CLA.parseBool "dumpeq" false) then print (ParseQuartz.str_rep (ssrep)) else ()
val ml = DelayedSeq.reduce Int.max 0 (DelayedSeq.map (fn p => Seq.length p) (DelayedSeq.fromArraySeq ssrep))
val optbrute = {eq = get_rep_seq ssrep, max_len = ml, eqdagger = ref (Seq.empty())}
fun idxtostr sss =
let
fun hs s = Seq.map str s
in
Seq.map (fn ss => Seq.map hs ss) sss
end
val sss = idxtostr sss
structure M = MeldOpt (structure OptC = BlackBoxOpt)
val optimize = M.optimize cliffordt_gates optc
end
fun RX thet =
let
val t = thet/2.0
val (c, s) = (Math.cos t, Math.sin t)
in
ComplexMatrix.fromList [[(c, 0.0), (0.0, ~1.0 * s)], [(0.0, ~1.0 * s), (c, 0.0)]]
end
fun RY thet =
let
val t = thet/2.0
val (c, s) = (Math.cos t, Math.sin t)
in
ComplexMatrix.fromList [[(c, 0.0), (~1.0 * s, 0.0)], [(s, 0.0), (c, 0.0)]]
end
fun RZ thet =
let
val t = thet/2.0
val (c, s) = (Math.cos t, Math.sin t)
in
ComplexMatrix.fromList [[(c, ~1.0 * s), (0.0, 0.0)], [(0.0, 0.0), (c, s)]]
end
val _ = if (CLA.parseBool "dumpeq" false) then print (ParseQuartz.str (CliffordT.sss)) else ()
val f = CLA.parseString "bench" "test-single.qasm"
val s = CliffordT.load_circuit f
val s' = CliffordT.optimize s
val _ = print ("length after optimization = " ^ (Int.toString (Seq.length s')) ^ "\n") *)