-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVoqcBB.sml
More file actions
executable file
·134 lines (110 loc) · 4.22 KB
/
VoqcBB.sml
File metadata and controls
executable file
·134 lines (110 loc) · 4.22 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
structure Log =
struct
type optlog = (Time.time * int) List.list ref
type timelog = Time.time ref
type t = optlog * timelog
fun init () = (ref [], ref (Time.zeroTime))
fun time_str (t) = Real.toString (Time.toReal (!t))
fun str (r, t) (it, isz) =
let
val l =
case !r of
[] => [(it, 0), (Time.now (), 0)]
| (t, fsz)::_ => (it, 0)::(List.rev ((Time.now(), fsz)::(!r)))
val s = (Seq.fromList l)
val s' = Seq.map (fn (t, sz) => (Time.toReal (Time.-(t, it)), isz - sz)) s
val ss' = Seq.map (fn (t, sz) => "(" ^ (Real.toString t) ^ ", " ^ (Int.toString sz) ^ ");") s'
val _ = print ("voqc time = " ^ (time_str t) ^ "\n")
in
Seq.reduce (fn (a, b) => a ^ b) "" ss'
end
fun register_opt ((r, _), sz) =
let
val te = Time.now ()
val ol = !r
val nl =
case ol of
[] => [(te, sz)]
| (te', sz')::_ => (te, sz + sz') :: ol
in
r := nl
end
fun register_time ((_, r), tm) = (r := Time.+ (!r, tm))
end
functor VoqcBB (structure Circuit : CIRCUIT) : BLACK_BOX_OPT =
struct
type t = Log.t
exception Unintialized
exception Unimplemented
structure Circuit = Circuit
(* fun proc_id () = MLton.Parallel.processorNumber ()
val P = MLton.Parallel.numberOfProcessors *)
val voqc_exec = "./lib/mlvoqc/_build/default/example.exe -f "
val qasm_file = CommandLineArgs.parseString "circuit" "test-small.qasm"
val circ_name =
Substring.string (#2 (Substring.splitr (fn c => c <> #"/") (Substring.full qasm_file)))
fun preprocess _ = raise Unimplemented
fun seq_to_str s = CharVector.tabulate (Seq.length s, (fn i => Seq.nth s i))
fun call_voqc log cqasm timeout =
let
val tmstr = Real.toString (Time.toReal (Time.now()))
val base_name = circ_name ^ ".temp_voqc." ^ tmstr ^ ".qasm"
val in_file = "/mnt/ramdisk/" ^ base_name
val out_file = "/mnt/ramdisk/" ^ "optimized_nam_" ^ base_name
val (_, tmDump) = Util.getTime(fn _ => Circuit.dump cqasm in_file)
val command = voqc_exec ^ " " ^ (in_file) ^ " -o " ^ (out_file)
val _ = print ("running " ^ command ^ "\n")
val (_, tm) = Util.getTime(fn _ => OS.Process.system command)
val _ = print ("call time = " ^ ((Real.toString o Time.toReal) (tm)) ^ "\n")
val _ = print ("writing time = " ^ ((Real.toString o Time.toReal) (tmDump)) ^ "\n")
val (cseq, tmRead) = Util.getTime (fn _ => ReadFile.contentsSeq out_file)
val cleanup = ("rm -f " ^ in_file ^ " " ^ out_file)
val _ = print ("reading time = " ^ ((Real.toString o Time.toReal) (tmRead)) ^ "\n")
(*val _ = OS.Process.system (cleanup)*)
val _ = Log.register_time (log, tm)
in
SOME cseq
end
fun init () = Log.init()
(* let
val (eccs, sz) = load_eqset ()
in *)
(* end *)
fun cstr s = s ^ (Char.toString (#"0"))
exception InvalidPreprocess
fun apply_ c timeout log =
let
fun select (c, cqasm') =
case cqasm' of
NONE => (NONE, 0)
| SOME charseq =>
let
(* val _ = print ("back from quartz = " ^ (seq_to_str charseq)) *)
val (c' : Circuit.raw_circuit) = Circuit.from_qasm (charseq)
val szd = (Circuit.cost_raw c' - Circuit.cost c)
val _ = print ("Circuit cost reduced from " ^ (Int.toString (Circuit.cost c) ^ " to " ^ (Int.toString(Circuit.cost_raw c')) ^ "\n"))
val _ = print ("Circuit size changed from " ^ (Int.toString (Circuit.size c) ^ " to " ^ (Int.toString(Circuit.size_raw c')) ^ "\n"))
in
if (szd >= 0) then (NONE, szd)
else (SOME (Circuit.reindex (c', c)), szd)
end
val (cqasm', tm) = Util.getTime(fn _ => call_voqc log c (Real.ceil (Time.toReal timeout)))
(* val _ = Log.register_time (log, tm) *)
val (res, sd) = select (c, cqasm')
val _ = if (Option.isSome res) then Log.register_opt (log, ~sd) else ()
in
res
end
fun apply_greedy log (c, topt) =
case topt of
NONE => apply_ c ((Time.fromReal 1.0)) log
| SOME t => apply_ c t log
fun apply_all log (c, timeout) = NONE
(* apply_ c timeout log *)
fun apply_both log (c, timeout) = apply_ c timeout log
fun best_equivalent log c = apply_greedy log (c, NONE)
fun max_breadth x = 5
val sz = CommandLineArgs.parseInt "size" 6
fun max_size x i = sz
fun optlog log (it, isz) = Log.str log (it, isz)
end