-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompose.jl
122 lines (94 loc) · 2.74 KB
/
compose.jl
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
using OpenQASM
using JSON
using PythonCall
"""
parse_qasm_prog(prog::Vector{Any})
Split qasm program into declaration and body and return each as a vector.
"""
function parse_qasm_prog(prog::Vector{Any})
for i in eachindex(prog)
if typeof(prog[i]) in (OpenQASM.Types.Include, OpenQASM.Types.RegDecl)
continue
end
return prog[1:i-1], prog[i:end]
end
end
"""
Determine the maximum qubits needed given an array of qasm_progs
"""
function qmax(progs)
qubits = 0
for p in progs
for line in p
if (typeof(line) == OpenQASM.Types.RegDecl) && (line.type.str == "qreg")
q = convert(Int, line.size)
if q > qubits
qubits = q
end
break
end
end
end
return qubits
end
function cmax(progs)
max_cbits = 0
for p in progs
cbits = 0
for line in p
if (typeof(line) == OpenQASM.Types.RegDecl) && (line.type.str == "creg")
cbits += 1
end
end
if cbits > max_cbits
max_cbits = cbits
end
end
return max_cbits
end
"""
function compose(qasm_files, qdata_files; outfile="out.qasm")
stiches together multiple widgets into a single qasm file
"""
function compose(qasm_files, qdata_files; outfile="out.qasm")
qasm_strings = [OpenQASM.parse(read(f, String)).prog for f in qasm_files]
data_qubits = [JSON.parsefile(m) for m in qdata_files]
qasm_split = [parse_qasm_prog(p) for p in qasm_strings]
headers = [p[1] for p in qasm_split]
bodies = [p[2] for p in qasm_split]
file = open(outfile, "w")
# Create header
println(file, "OPENQASM 2.0;\ninclude \"qelib1.inc\";")
# Declare qregs
println(file, "qreg q[$(qmax(headers))];")
# Declare cregs
for i in 0:cmax(headers)-1
println(file, "creg c$i[1];")
end
# add first widget body
for line in bodies[1]
println(file, line)
end
# add remaining widgets with connecting swaps
for (idx, b) in enumerate(bodies)
idx == 1 && continue
output_qubits = data_qubits[idx-1]["output"]
state_qubits = data_qubits[idx]["state"]
for (o, s) in zip(output_qubits, state_qubits)
if o != s
println(file, "swap q[$o], q[$s];")
end
end
for line in b
println(file, line)
end
end
close(file)
end
# Load qasm files
qasm_files = ["mwe1_compiled.qasm", "mwe2_compiled.qasm"]
qdata_files = ["mwe1_compiled_dqubits.json", "mwe2_compiled_dqubits.json"]
compose(qasm_files, qdata_files)
qiskit = pyimport("qiskit")
circ = qiskit.QuantumCircuit.from_qasm_file("out.qasm")
print(circ)