-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperiod.py
280 lines (237 loc) · 7.17 KB
/
period.py
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
import numpy as np
import math
from pyquil.quil import Program, address_qubits
from pyquil.quilatom import QubitPlaceholder
from pyquil.api import QVMConnection
from pyquil.gates import X, I, H, CNOT, CCNOT, MEASURE, SWAP
from pyquil.parameters import Parameter, quil_exp
from pyquil.quilbase import DefGate
def egcd(a, b):
if a == 0:
return (b, 0, 1)
else:
g, y, x = egcd(b % a, a)
return (g, x - (b // a) * y, y)
def modinv(a, m):
g, x, y = egcd(a, m)
if g != 1:
raise Exception('modular inverse does not exist')
else:
return x % m
qvm = QVMConnection()
k = Parameter('k')
ccrk = np.array([
[1, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, quil_exp( (2*np.pi*1j)/( 2**k ) )],
])
ccrk_gate_def = DefGate('CCRK', ccrk, [k])
CCRK = ccrk_gate_def.get_constructor()
crk = np.array([
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, quil_exp( (2*np.pi*1j)/( 2**k ) )],
])
crk_gate_def = DefGate('CRK', crk, [k])
CRK = crk_gate_def.get_constructor()
rk = np.array([
[1, 0],
[0, quil_exp( (2*np.pi*1j)/( 2**k ) )],
])
rk_gate_def = DefGate('RK', rk, [k])
RK = rk_gate_def.get_constructor()
def get_defs():
p = Program()
p += ccrk_gate_def
p += crk_gate_def
p += rk_gate_def
return p
def REVERSE(p):
rev_p = p.copy_everything_except_instructions()
rev_p += Program(f"#BEGIN REVERSE:")
for inst in reversed(p.instructions):
rev_p += inst.dagger()
return rev_p
def CSWAP(c, l, r):
p = Program()
p += CNOT(r, l)
p += CCNOT(c, l, r)
p += CNOT(r, l)
return p
def QFT(reg):
p = Program()
n = len(reg)
for i in range(n-1, -1, -1):
p += H(reg[i])
for j in range(i-1, -1, -1):
k = i-j+1
p += CRK(k)(reg[j], reg[i])
return p
def PSIADDER(b, a):
# this is an adder in the fourier space
# b is a register of size n+1 with value fit in n bits
# a is a classicall number, must also fit in n bits
assert(a < 2**(len(b)-1))
p = Program()
n = len(b)
control = str(bin(a))[2:]
control = list(reversed("0"*(len(b)-len(control)) + control))
for i in range(n-1, -1, -1):
#TODO: we can condense these j gates
for j in range(i, -1, -1):
if control[j] == "1":
k = i-j+1
p += RK(k)(b[i])
return p
def CPSIADDER(c1, b, a):
# this is an adder in the fourier space
# b is a register of size n+1 with value fit in n bits
# a is a classicall number, must also fit in n bits
assert(a < 2**(len(b)-1))
p = Program()
n = len(b)
control = str(bin(a))[2:]
control = list(reversed("0"*(len(b)-len(control)) + control))
for i in range(n-1, -1, -1):
#TODO: we can condense these j gates
for j in range(i, -1, -1):
if control[j] == "1":
k = i-j+1
p += CRK(k)(c1, b[i])
return p
def CCPSIADDER(c1, c2, b, a):
# this is an adder in the fourier space
# b is a register of size n+1 with value fit in n bits
# a is a classicall number, must also fit in n bits
assert(a < 2**(len(b)-1))
p = Program()
n = len(b)
control = str(bin(a))[2:]
control = list(reversed("0"*(len(b)-len(control)) + control))
for i in range(n-1, -1, -1):
#TODO: we can condense these j gates
for j in range(i, -1, -1):
if control[j] == "1":
k = i-j+1
p += CCRK(k)(c1, c2, b[i])
return p
def PSIADDERMOD(c1, c2, b, a, N, zero):
p = Program()
# make b = b+a
p += CCPSIADDER(c1, c2, b, a)
# make b = b+a-N
p += REVERSE(PSIADDER(b, N))
# make zero now has ?(b+a < N)
p += REVERSE(QFT(b))
p += CNOT(b[-1] , zero)
p += QFT(b)
# if ?(b+a < N), add back N
p += CPSIADDER(zero, b, N)
# now b = b+a Mod N
# Must get zero back to 0
p += REVERSE(CCPSIADDER(c1, c2, b, a))
p += REVERSE(QFT(b))
p += X(b[-1])
p += CNOT(b[-1] , zero)
p += X(b[-1])
p += QFT(b)
p += CCPSIADDER(c1, c2, b, a)
return p
def CMULTMOD(c1, x, b, a, N, zero):
# takes in some x, b, outputs x, b + x*a mod N
p = Program()
p += QFT(b)
for i in range(len(x)):
p += PSIADDERMOD(c1, x[i], b, (a*(2**i))%N, N, zero)
p += REVERSE(QFT(b))
return p
def UA(c1, x, b, a, N, zero):
p = Program()
p += CMULTMOD(c1, x, b, a, N, zero)
for i in range(len(x)):
p += CSWAP(c1, x[i], b[i])
ainv = modinv(a, N)
ainv = ainv+abs(math.floor(ainv/N))*N
p += REVERSE(CMULTMOD(c1, x, b, ainv, N, zero))
return p
def period_helper(a, N, size):
c1 = QubitPlaceholder()
zero = QubitPlaceholder()
x = QubitPlaceholder.register(size)
b = QubitPlaceholder.register(size+1)
#takes in x and b as zero, finds
p = Program()
n = 2*size
def_regs = Program()
period_regs = def_regs.declare('ro', 'BIT', n)
#For one reg, we want H, CUA, R_i m_i, X^m_i
for i in range(n-1, -1, -1):
R = Program()
R += H(c1)
for j in range(i-1, -1, -1):
k = i-j+1
doit = Program()
doit += RK(k)(c1).dagger()
R = Program().if_then(period_regs[j], doit, I(c1)) + R
R += MEASURE(c1, period_regs[i])
R += Program().if_then(period_regs[i], X(c1), I(c1))
#R = Program(H(c1)) + R
R = Program(H(c1)) + UA(c1, x, b, a**(2**i), N, zero) + R
p = R + p
p = write_in(1, x) + p
p = def_regs + p
p = get_defs() + p
p = address_qubits(p)
return p
def PERIOD(a, N, size):
p = period_helper(a, N, size)
result = qvm.run(p)
outp = 0
for i in range(len(result[0])):
if(list(reversed(result[0]))[i] == 1):
outp += 2**i
return outp
def PERIOD_slow(a, N, size):
#NOTE: This code is accomplishes the same goal as PERIOD,
# but it does not use the single qubit input register trick.
inp = QubitPlaceholder.register(2*size)
zero = QubitPlaceholder()
x = QubitPlaceholder.register(size)
b = QubitPlaceholder.register(size+1)
#takes in x and b as zero, finds
p = Program()
p += get_defs()
p += write_in(1, x)
for i in range(len(inp)):
p += H(inp[i])
for i in range(len(inp)):
p += UA(inp[i], x, b, (a**(2**i)), N, zero)
p += REVERSE(QFT(inp))
print("Running a period finding alg using {} gates".format(len(p.instructions)))
outp, p = read_out(p, list(reversed(inp)))
return outp
def write_in(val, reg):
p = Program()
bitstring = str(bin(val))[2:]
bitstring = "0"*(len(reg)-len(bitstring)) + bitstring
for idx, bit in enumerate(reversed(list(bitstring))):
if bit == "1":
p += X(reg[idx])
return p
def read_out(p, reg):
ro = p.declare('ro', 'BIT', len(reg))
for i in range(len(reg)):
p += MEASURE(reg[i], ro[i])
p = address_qubits(p)
result = qvm.run(p)
outp = 0
for i in range(len(result[0])):
if(result[0][i] == 1):
outp += 2**i
return (outp, p)