-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuesoBB.sml
More file actions
131 lines (107 loc) · 3.84 KB
/
QuesoBB.sml
File metadata and controls
131 lines (107 loc) · 3.84 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
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 ("queso 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 QuesoBB (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 queso_path = "./queso/"
val queso_exec = "run_queso_lopt.sh"
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
val ques_log = CommandLineArgs.parseString "qlogfile" "queso_log.log"
fun call_queso log cqasm timeout =
let
val tmstr = Real.toString (Time.toReal (Time.now()))
val in_file = circ_name ^ ".temp_queso.qasm." ^ tmstr
val out_file = "optimized_nam_" ^ in_file
val _ = Circuit.dump cqasm in_file
val command = queso_path ^ queso_exec ^ " " ^ (in_file) ^ " " ^ (Int.toString timeout)
val _ = print ("running " ^ command ^ "\n")
val (_, tm) = Util.getTime(fn _ => OS.Process.system command)
val _ = Log.register_time (log, tm)
val _ = print ("time taken = " ^ ((Real.toString o Time.toReal) (tm)) ^ "\n")
val cseq = (ReadFile.contentsSeq out_file)
val cleanup = ("rm -f " ^ in_file ^ " " ^ out_file)
val _ = print("removing by command " ^ cleanup ^ "\n")
val _ = OS.Process.system (cleanup)
in
SOME cseq
end
fun init () = Log.init()
(* let
val (eccs, sz) = load_eqset ()
in *)
(* end *)
fun seq_to_str s = CharVector.tabulate (Seq.length s, (fn i => Seq.nth s i))
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 optimzied from " ^ (Int.toString (Circuit.cost c) ^ " to " ^ (Int.toString(Circuit.cost_raw c')) ^ "\n"))
in
if (szd >= 0) then (NONE, szd)
else (SOME (Circuit.reindex (c', c)), szd)
end
val cqasm' = call_queso log c (Real.ceil (Time.toReal timeout))
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) = 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