-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathState_Learning.py
More file actions
275 lines (205 loc) · 8.06 KB
/
Copy pathState_Learning.py
File metadata and controls
275 lines (205 loc) · 8.06 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
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
import numpy as np
from qutip import wigner, Qobj, wigner_cmap
from thewalrus.quantum import state_vector, density_matrix
import strawberryfields as sf
from strawberryfields.ops import *
from strawberryfields.utils import operation
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib import cm
from strawberryfields.ops import *
from mpl_toolkits.mplot3d import Axes3D
import math
# Cutoff dimension
cutoff = 10
# Number of layers
depth = 15
# Number of steps in optimization routine performing gradient descent
reps = 100
# Learning rate
lr = 0.05
# Standard deviation of initial parameters
passive_sd = 0.1
active_sd = 0.001
import tensorflow as tf
# set the random seed
tf.random.set_seed(42)
# squeeze gate
sq_r = tf.random.normal(shape=[depth], stddev=active_sd)
sq_phi = tf.random.normal(shape=[depth], stddev=passive_sd)
# displacement gate
d_r = tf.random.normal(shape=[depth], stddev=active_sd)
d_phi = tf.random.normal(shape=[depth], stddev=passive_sd)
# rotation gates
r1 = tf.random.normal(shape=[depth], stddev=passive_sd)
r2 = tf.random.normal(shape=[depth], stddev=passive_sd)
# kerr gate
kappa = tf.random.normal(shape=[depth], stddev=active_sd)
weights = tf.convert_to_tensor([r1, sq_r, sq_phi, r2, d_r, d_phi, kappa])
weights = tf.Variable(tf.transpose(weights))
# Single-mode Strawberry Fields program
prog = sf.Program(1)
prog_2 = sf.Program(1)
# Create the 7 Strawberry Fields free parameters for each layer
sf_params = []
names = ["r1", "sq_r", "sq_phi", "r2", "d_r", "d_phi", "kappa"]
for i in range(depth):
# For the ith layer, generate parameter names "r1_i", "sq_r_i", etc.
sf_params_names = ["{}_{}".format(n, i) for n in names]
# Create the parameters, and append them to our list ``sf_params``.
sf_params.append(prog.params(*sf_params_names))
sf_params.append(prog_2.params(*sf_params_names))
sf_params = np.array(sf_params)
print(sf_params.shape)
# layer architecture
@operation(1)
def layer(i, q):
Rgate(sf_params[i][0]) | q
Sgate(sf_params[i][1], sf_params[i][2]) | q
Rgate(sf_params[i][3]) | q
Dgate(sf_params[i][4], sf_params[i][5]) | q
Kgate(sf_params[i][6]) | q
return q
# Apply circuit of layers with corresponding depth
with prog.context as q:
for k in range(depth):
layer(k) | q[0]
with prog_2.context as q:
for k in range(depth):
layer(k) | q[0]
eng = sf.Engine("tf", backend_options={"cutoff_dim": cutoff})
import numpy as np
#----------------------------------------------------------------------------------------
# First target state
target_state = np.zeros([cutoff])
target_state[1] = 1
print(target_state)
# Second target state
prog_1 = sf.Program(1)
eng_1 = sf.Engine("gaussian")
with prog_1.context as q:
sf.ops.Squeezed(r=1, p=0) | q
state_1 = eng_1.run(prog_1).state
mu, cov = state_1.means(), state_1.cov()
squeezed = state_vector(mu, cov ,normalize=False, cutoff=cutoff)
squeezed_psi = np.linalg.norm(squeezed)
squeezed = squeezed / squeezed_psi
target_state_2 = squeezed
#----------------------------------------------------------------------------------------
def cost(weights):
# Create a dictionary mapping from the names of the Strawberry Fields
# free parameters to the TensorFlow weight values.
mapping = {p.name: w for p, w in zip(sf_params.flatten(), tf.reshape(weights, [-1]))}
mapping_2 = {p.name: w for p, w in zip(sf_params.flatten(), tf.reshape(weights, [-1]))}
# Run engine
state = eng.run(prog, args=mapping).state
state_2 = eng.run(prog_2, args=mapping_2).state
# Extract the statevector
ket = state.ket()
ket_2 = state_2.ket()
# Compute the fidelity between the output statevector
# and the target state.
fidelity = (1/3)*tf.abs(tf.reduce_sum((tf.math.conj(ket) * target_state))) ** 2 + (1/3)*tf.abs(tf.reduce_sum((tf.math.conj(ket_2) * target_state_2))) ** 2 + (1/3)*tf.abs(tf.reduce_sum((tf.math.conj(ket_2) * tf.math.conj(ket)))) ** 2
# Objective function to minimize
#cost = tf.abs(tf.reduce_sum(tf.math.conj(ket) * target_state) - 1)
cost = tf.abs((1/3)*(tf.reduce_sum(tf.math.conj(ket) * target_state)+(1/3)*(tf.reduce_sum(tf.math.conj(ket_2) * target_state_2))+(1/3)*(tf.reduce_sum(tf.math.conj(ket_2) * tf.math.conj(ket)))) - 1)
return cost, fidelity, ket, ket_2
opt = tf.keras.optimizers.Adam(learning_rate=lr)
fid_progress = []
best_fid = 0
for i in range(reps):
# reset the engine if it has already been executed
if eng.run_progs:
eng.reset()
with tf.GradientTape() as tape:
loss, fid, ket, ket_2 = cost(weights)
# Stores fidelity at each step
fid_progress.append(fid.numpy())
if fid > best_fid:
# store the new best fidelity and best state
best_fid = fid.numpy()
learnt_state = ket.numpy()
learnt_state_2 = ket_2.numpy()
# one repetition of the optimization
gradients = tape.gradient(loss, weights)
opt.apply_gradients(zip([gradients], [weights]))
# Prints progress at every rep
if i % 1 == 0:
print("Rep: {} Cost: {:.4f} Fidelity: {:.4f}".format(i, loss, fid))
from matplotlib import pyplot as plt
plt.rcParams["font.family"] = "serif"
plt.rcParams["font.sans-serif"] = ["Computer Modern Roman"]
plt.style.use("default")
plt.plot(fid_progress)
plt.ylabel("Fidelity")
plt.xlabel("Step")
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def wigner(rho):
import copy
# Domain parameter for Wigner function plots
l = 5.0
cutoff = rho.shape[0]
# Creates 2D grid for Wigner function plots
x = np.linspace(-l, l, 100)
p = np.linspace(-l, l, 100)
Q, P = np.meshgrid(x, p)
A = (Q + P * 1.0j) / (2 * np.sqrt(2 / 2))
Wlist = np.array([np.zeros(np.shape(A), dtype=complex) for k in range(cutoff)])
# Wigner function for |0><0|
Wlist[0] = np.exp(-2.0 * np.abs(A) ** 2) / np.pi
# W = rho(0,0)W(|0><0|)
W = np.real(rho[0, 0]) * np.real(Wlist[0])
for n in range(1, cutoff):
Wlist[n] = (2.0 * A * Wlist[n - 1]) / np.sqrt(n)
W += 2 * np.real(rho[0, n] * Wlist[n])
for m in range(1, cutoff):
temp = copy.copy(Wlist[m])
# Wlist[m] = Wigner function for |m><m|
Wlist[m] = (2 * np.conj(A) * temp - np.sqrt(m) * Wlist[m - 1]) / np.sqrt(m)
# W += rho(m,m)W(|m><m|)
W += np.real(rho[m, m] * Wlist[m])
for n in range(m + 1, cutoff):
temp2 = (2 * A * Wlist[n - 1] - np.sqrt(m) * temp) / np.sqrt(n)
temp = copy.copy(Wlist[n])
# Wlist[n] = Wigner function for |m><n|
Wlist[n] = temp2
# W += rho(m,n)W(|m><n|) + rho(n,m)W(|n><m|)
W += 2 * np.real(rho[m, n] * Wlist[n])
return Q, P, W / 2
rho_target = np.outer(target_state, target_state.conj())
rho_learnt = np.outer(learnt_state, learnt_state.conj())
rho_target_2 = np.outer(target_state_2, target_state_2.conj())
rho_learnt_2 = np.outer(learnt_state_2, learnt_state_2.conj())
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
X, P, W = wigner(rho_target)
ax.plot_surface(X, P, W, cmap="RdYlGn", lw=0.5, rstride=1, cstride=1)
ax.contour(X, P, W, 10, cmap="RdYlGn", linestyles="solid", offset=-0.17)
ax.set_axis_off()
plt.title("First Target State")
plt.show()
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
X, P, W = wigner(rho_learnt)
ax.plot_surface(X, P, W, cmap="RdYlGn", lw=0.5, rstride=1, cstride=1)
ax.contour(X, P, W, 10, cmap="RdYlGn", linestyles="solid", offset=-0.17)
ax.set_axis_off()
plt.title("First Trained State")
plt.show()
fig = plt.figure()
ax1 = fig.add_subplot(111, projection="3d")
X1, P1, W1 = wigner(rho_target_2)
ax1.plot_surface(X1, P1, W1, cmap="RdYlGn", lw=0.5, rstride=1, cstride=1)
ax1.contour(X1, P1, W1, 10, cmap="RdYlGn", linestyles="solid", offset=-0.17)
ax1.set_axis_off()
plt.title("Second Target State")
plt.show()
fig = plt.figure()
ax1 = fig.add_subplot(111, projection="3d")
X1, P1, W1 = wigner(rho_learnt_2)
ax1.plot_surface(X1, P1, W1, cmap="RdYlGn", lw=0.5, rstride=1, cstride=1)
ax1.contour(X1, P1, W1, 10, cmap="RdYlGn", linestyles="solid", offset=-0.17)
ax1.set_axis_off()
plt.title("Second Trained State")
plt.show()