This repository has been archived by the owner on Jun 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathMicroQiskit.lua
executable file
·264 lines (207 loc) · 5.09 KB
/
MicroQiskit.lua
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
-- This code is part of Qiskit.
--
-- Copyright IBM 2020
math.randomseed(os.time())
function QuantumCircuit ()
local qc = {}
local function set_registers (n,m)
qc.num_qubits = n
qc.num_clbits = m or 0
end
qc.set_registers = set_registers
qc.data = {}
function qc.initialize (ket)
ket_copy = {}
for j, amp in pairs(ket) do
if type(amp)=="number" then
ket_copy[j] = {amp, 0}
else
ket_copy[j] = {amp[0], amp[1]}
end
end
qc.data = {{'init',ket_copy}}
end
function qc.add_circuit (qc2)
qc.num_qubits = math.max(qc.num_qubits,qc2.num_qubits)
qc.num_clbits = math.max(qc.num_clbits,qc2.num_clbits)
for g, gate in pairs(qc2.data) do
qc.data[#qc.data+1] = ( gate )
end
end
function qc.x (q)
qc.data[#qc.data+1] = ( {'x',q} )
end
function qc.rx (theta,q)
qc.data[#qc.data+1] = ( {'rx',theta,q} )
end
function qc.h (q)
qc.data[#qc.data+1] = ( {'h',q} )
end
function qc.cx (s,t)
qc.data[#qc.data+1] = ( {'cx',s,t} )
end
function qc.measure (q,b)
qc.data[#qc.data+1] = ( {'m',q,b} )
end
function qc.rz (theta,q)
qc.h(q)
qc.rx(theta,q)
qc.h(q)
end
function qc.ry (theta,q)
qc.rx(math.pi/2,q)
qc.rz(theta,q)
qc.rx(-math.pi/2,q)
end
function qc.z (q)
qc.rz(math.pi,q)
end
function qc.y (q)
qc.z(q)
qc.x(q)
end
return qc
end
function simulate (qc, get, shots)
if not shots then
shots = 1024
end
function as_bits (num,bits)
-- returns num converted to a bitstring of length bits
-- adapted from https://stackoverflow.com/a/9080080/1225661
local bitstring = {}
for index = bits, 1, -1 do
b = num - math.floor(num/2)*2
num = math.floor((num - b) / 2)
bitstring[index] = b
end
return bitstring
end
function get_out (j)
raw_out = as_bits(j-1,qc.num_qubits)
out = ""
for b=0,qc.num_clbits-1 do
if outputnum_clbitsap[b] then
out = raw_out[qc.num_qubits-outputnum_clbitsap[b]]..out
end
end
return out
end
ket = {}
for j=1,2^qc.num_qubits do
ket[j] = {0,0}
end
ket[1] = {1,0}
outputnum_clbitsap = {}
for g, gate in pairs(qc.data) do
if gate[1]=='init' then
for j, amp in pairs(gate[2]) do
ket[j] = {amp[1], amp[2]}
end
elseif gate[1]=='m' then
outputnum_clbitsap[gate[3]] = gate[2]
elseif gate[1]=="x" or gate[1]=="rx" or gate[1]=="h" then
j = gate[#gate]
for i0=0,2^j-1 do
for i1=0,2^(qc.num_qubits-j-1)-1 do
b1=i0+2^(j+1)*i1 + 1
b2=b1+2^j
e = {{ket[b1][1],ket[b1][2]},{ket[b2][1],ket[b2][2]}}
if gate[1]=="x" then
ket[b1] = e[2]
ket[b2] = e[1]
elseif gate[1]=="rx" then
theta = gate[2]
ket[b1][1] = e[1][1]*math.cos(theta/2)+e[2][2]*math.sin(theta/2)
ket[b1][2] = e[1][2]*math.cos(theta/2)-e[2][1]*math.sin(theta/2)
ket[b2][1] = e[2][1]*math.cos(theta/2)+e[1][2]*math.sin(theta/2)
ket[b2][2] = e[2][2]*math.cos(theta/2)-e[1][1]*math.sin(theta/2)
elseif gate[1]=="h" then
for k=1,2 do
ket[b1][k] = (e[1][k] + e[2][k])/math.sqrt(2)
ket[b2][k] = (e[1][k] - e[2][k])/math.sqrt(2)
end
end
end
end
elseif gate[1]=="cx" then
s = gate[2]
t = gate[3]
if s>t then
h = s
l = t
else
h = t
l = s
end
for i0=0,2^l-1 do
for i1=0,2^(h-l-1)-1 do
for i2=0,2^(qc.num_qubits-h-1)-1 do
b1 = i0 + 2^(l+1)*i1 + 2^(h+1)*i2 + 2^s + 1
b2 = b1 + 2^t
e = {{ket[b1][1],ket[b1][2]},{ket[b2][1],ket[b2][2]}}
ket[b1] = e[2]
ket[b2] = e[1]
end
end
end
end
end
if get=="statevector" then
return ket
else
probs = {}
for j,amp in pairs(ket) do
probs[j] = amp[1]^2 + amp[2]^2
end
if get=="expected_counts" then
c = {}
for j,p in pairs(probs) do
out = get_out(j)
if c[out] then
c[out] = c[out] + probs[j]*shots
else
if out then -- in case of pico8 weirdness
c[out] = probs[j]*shots
end
end
end
return c
else
m = {}
for s=1,shots do
cumu = 0
un = true
r = math.random()
for j,p in pairs(probs) do
cumu = cumu + p
if r<cumu and un then
m[s] = get_out(j)
un = false
end
end
end
if get=="memory" then
return m
elseif get=="counts" then
c = {}
for s=1,shots do
if c[m[s]] then
c[m[s]] = c[m[s]] + 1
else
if m[s] then -- in case of pico8 weirdness
c[m[s]] = 1
else
if c["error"] then
c["error"] = c["error"]+1
else
c["error"] = 1
end
end
end
end
return c
end
end
end
end