-
Notifications
You must be signed in to change notification settings - Fork 2
/
backprop_scaling_numerics.py
467 lines (380 loc) · 16.7 KB
/
backprop_scaling_numerics.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
import jax
import jax.numpy as jnp
jax.config.update("jax_enable_x64", True)
from pennylane import pennylane as qml
import numpy as np
import optax
import matplotlib.pyplot as plt
from itertools import combinations_with_replacement, permutations, product, combinations
from model_utils import chunk_grad, chunk_loss, chunk_vmapped_fn
"""
This the numerics to accompany the article 'backpropagation scaling in parameterised quantum circuits'
The following code can be used to reproduce plots of the form of Figure 6 of the paper.
"""
#################################### DATA GENERATION ####################################
def generate_data(dim, n, length, noise=0.):
"""
Generate a bars and dots dataset
:param dim: dimension of the data points
:param n: number of data points
:param length: length of the bars
:param noise: std of independent gaussian noise
:return: data (X) and labels (Y)
"""
X = []
Y = []
for __ in range(n):
start = np.random.randint(0, dim)
x = np.ones(dim)
if np.random.rand() < 0.5:
bar = True
Y.append(1)
else:
bar = False
Y.append(-1)
for i in range(length):
if bar:
x[(start + i) % dim] = -1
else:
x[(start + 2 * i) % dim] = -1
X.append(x)
X = np.array(X)
X = X + np.random.normal(0, noise, X.shape)
return X, np.array(Y)
dim = 16 #problem dimension
qubits = dim
seed = 852459
np.random.seed(seed)
#################################### FUNCTIONS USED IN MODEL GENERATION ####################################
def cyclic_perm(a):
"gets all cyclic permutations of a list"
n = len(a)
b = [[a[i - j] for i in range(n)] for j in range(n)]
return b
def seed_gens(weight, qubits=qubits, ops=['I', 'X']):
"""
get all the seed generators up to a given pauli weight
the seeds are fed into get_gens to get the symmetric generators
"""
ops
seeds = []
for prod in product(ops, repeat=weight):
seeds.append(list(prod) + ['I'] * (qubits - weight))
return seeds[1:]
def seed_gens_doubles(ops=['I', 'X']):
"get the seed generators that have weight 2 only"
seeds = []
for k in range(0, qubits - 1):
seed = ops[1] + ops[0] * k + ops[1] + ops[0] * (qubits - k - 2)
seeds.append(seed)
return seeds
def get_gens(seeds):
"get all unique equivariant generators from the a list of seeds"
gens = []
for seed in seeds:
all_gens = cyclic_perm(seed)
genlist = [''.join(all_gens[i]) for i in range(qubits)]
genlist = list(dict.fromkeys(genlist))
genlist.sort()
if genlist not in gens:
gens.append(genlist)
return gens
#################################### MODEL DEFINITIONS ####################################
######## COMMUTING MODEL ##########
obs=qml.dot([1/qubits] * qubits, [qml.PauliZ(i) for i in range(qubits)])
seeds = seed_gens(qubits)
gens = get_gens(seeds)
#convert the gens to Pauli words and wires for more efficient use in pennylane
words_and_wires = [
[(gen.replace("I", ""), [i for i, l in enumerate(gen) if l=="X"]) for gen in gen_list]
for gen_list in gens]
#take only the generators with weight <=3
waw = []
for elem in words_and_wires:
if len(elem[0][0])<=3:
waw.append(elem)
words_and_wires = waw
num_gen_parallel = sum(len(sublist) for sublist in words_and_wires)
num_param_parallel = len(words_and_wires)
num_circuits_parallel = 16
print('number of generators for parallel model: ' + str(num_gen_parallel))
print('number of circuits for parallel model: ' + str(num_circuits_parallel))
print('number of parameters for parallel model: ' + str(num_param_parallel))
dev = qml.device('default.qubit',wires=qubits)
@qml.qnode(dev, interface='jax')
def parallel_model_eval(params,x):
"""
Model used for evaluation but not for training. Sometimes it is useful to separate the two for efficiency
reasons
:param params: trainable parameters
:param x: data input
:return: expval corresponding to class label
"""
#data encoding
for q in range(qubits):
qml.RY(x[q], wires=q)
# apply the rotation for each equivariant generator
for i, sublist in enumerate(words_and_wires):
for word, wires in sublist:
qml.PauliRot(params[i], pauli_word=word, wires=wires)
return qml.expval(obs)
parallel_model_eval = jax.jit(parallel_model_eval)
parallel_model_eval = jax.vmap(parallel_model_eval,(None,0))
dev = qml.device('default.qubit',wires=qubits)
@qml.qnode(dev,interface='jax')
def parallel_model(params,x):
for q in range(qubits):
qml.RY(x[q],wires=q)
#apply the rotation for each equivariant generator
for i, sublist in enumerate(words_and_wires):
for word, wires in sublist:
qml.PauliRot(params[i], pauli_word=word, wires=wires)
return qml.expval(obs)
parallel_model = jax.jit(parallel_model)
parallel_model = jax.vmap(parallel_model,(None,0))
def cost_parallel(params, input_data, labels):
predictions = parallel_model(params['w'],input_data)
return cross_entropy_loss(predictions,labels)
grad_parallel = jax.grad(cost_parallel)
grad_parallel = jax.jit(grad_parallel)
######## NONCOMMUTING MODEL ##########
layers = 4
obs=qml.dot([1/qubits] * qubits, [qml.PauliZ(i) for i in range(qubits)])
obsZZ = sum([qml.PauliZ(i)@qml.PauliZ((i+1)%qubits) for i in range(qubits)])
localz = get_gens(seed_gens(1,ops=['I','Z']))
localy = get_gens(seed_gens(1,ops=['I','Y']))
doublex = get_gens(seed_gens_doubles(ops=['I','X']))
z_words_and_wires = [[(gen.replace("I", ""), [i for i, l in enumerate(gen) if l=="Z"]) for gen in gen_list]
for gen_list in localz]
y_words_and_wires = [[(gen.replace("I", ""), [i for i, l in enumerate(gen) if l=="Y"]) for gen in gen_list]
for gen_list in localy]
x_words_and_wires = [[(gen.replace("I", ""), [i for i, l in enumerate(gen) if l=="X"]) for gen in gen_list]
for gen_list in doublex]
general_words_and_wires = z_words_and_wires+y_words_and_wires+x_words_and_wires
num_gens_per_layer = sum(len(sublist) for sublist in general_words_and_wires)
num_gens_general = num_gens_per_layer*layers
num_param_per_layer = len(general_words_and_wires)
num_param_general = num_param_per_layer*layers
num_circuits_general = num_gens_general
print('number of generators for general model: ' + str(num_gens_general))
print('number of circuits for general model: ' + str(num_circuits_general))
print('number of parameters for general model: ' + str(num_param_general))
dev = qml.device('default.qubit',wires=qubits)
@qml.qnode(dev, interface='jax')
def general_model_eval(params,x):
for q in range(qubits):
qml.RY(x[q],wires=q)
#apply the rotation for each equivariant generator
for l in range(layers):
for i, sublist in enumerate(general_words_and_wires):
for word, wires in sublist:
qml.PauliRot(params[l*num_param_per_layer+i], pauli_word=word, wires=wires)
return qml.expval(obs)
general_model_eval = jax.jit(general_model_eval)
general_model_eval = jax.vmap(general_model_eval,(None,0))
dev = qml.device('default.qubit',wires=qubits)
@qml.qnode(dev, interface='jax')
def general_model(params,x):
for q in range(qubits):
qml.RY(x[q],wires=q)
#apply the rotation for each equivariant generator
for l in range(layers):
for i, sublist in enumerate(general_words_and_wires):
for word, wires in sublist:
qml.PauliRot(params[l*num_param_per_layer+i], pauli_word=word, wires=wires)
return qml.expval(obs)
general_model= jax.jit(general_model)
general_model= jax.vmap(general_model,(None,0))
def cost_general(params, input_data, labels):
predictions = general_model(params['w'],input_data)
return cross_entropy_loss(predictions,labels)
grad_general = jax.grad(cost_general)
grad_general = jax.jit(grad_general)
######## QUANTUM CONVOLUTIONAL MODEL ##########
def QCNN_block(params, wires):
qml.RX(params[0], wires=wires[0])
qml.RX(params[1], wires=wires[1])
qml.RZ(params[2], wires=wires[0])
qml.RZ(params[3], wires=wires[1])
qml.CRZ(params[4], wires=[wires[1], wires[0]])
qml.CRZ(params[5], wires=[wires[0], wires[1]])
qml.RX(params[6], wires=wires[0])
qml.RX(params[7], wires=wires[1])
qml.RZ(params[8], wires=wires[0])
qml.RZ(params[9], wires=wires[1])
def pooling(params, wires):
qml.CRZ(params[0], wires=wires)
qml.PauliX(wires=wires[0])
qml.CRX(params[1], wires=wires)
qml.PauliX(wires=wires[0])
n_params_block = 10
n_params_layer = 12
n_layers_qcnn = int(np.log2(qubits))
dev = qml.device('default.qubit', wires=qubits)
@qml.qnode(dev, interface="jax")
def QCNN_eval(params, x):
count = 0
wires = range(qubits)
for q in range(qubits):
qml.RY(x[q], wires=q)
for j in range(n_layers_qcnn):
for i in range(0, qubits // (2 ** j), 2):
QCNN_block(params[count:count+10], wires=[wires[(2 ** j - 1) + 2 ** j * i], wires[
((2 ** j - 1) + 2 ** (j) * i + (2 ** j)) % qubits]])
if j != int(np.log2(qubits)) - 1:
for i in range(1, qubits // (2 ** j), 2):
QCNN_block(params[count:count+10], wires=[wires[(2 ** j - 1) + 2 ** j * i], wires[
((2 ** j - 1) + 2 ** (j) * i + (2 ** j)) % qubits]])
count = count+10
for i in range(0, qubits // (2 ** j), 2):
pooling(params[count:count+2], wires=[wires[(2 ** j - 1) + 2 ** j * i], wires[
((2 ** j - 1) + 2 ** (j) * i + (2 ** j)) % qubits]])
count = count+2
return qml.expval(qml.PauliZ(qubits - 1))
QCNN_eval = jax.jit(QCNN_eval)
QCNN_eval = jax.vmap(QCNN_eval, (None, 0))
dev = qml.device('default.qubit', wires=qubits)
@qml.qnode(dev, interface="jax")
def QCNN(params, x):
count = 0
wires = range(qubits)
for q in range(qubits):
qml.RY(x[q], wires=q)
for j in range(n_layers_qcnn):
for i in range(0, qubits // (2 ** j), 2):
QCNN_block(params[count:count+10], wires=[wires[(2 ** j - 1) + 2 ** j * i], wires[
((2 ** j - 1) + 2 ** (j) * i + (2 ** j)) % qubits]])
if j != int(np.log2(qubits)) - 1:
for i in range(1, qubits // (2 ** j), 2):
QCNN_block(params[count:count+10], wires=[wires[(2 ** j - 1) + 2 ** j * i], wires[
((2 ** j - 1) + 2 ** (j) * i + (2 ** j)) % qubits]])
count = count+10
for i in range(0, qubits // (2 ** j), 2):
pooling(params[count:count+2], wires=[wires[(2 ** j - 1) + 2 ** j * i], wires[
((2 ** j - 1) + 2 ** (j) * i + (2 ** j)) % qubits]])
count = count+2
return qml.expval(qml.PauliZ(qubits - 1))
QCNN = jax.jit(QCNN)
QCNN = jax.vmap(QCNN, (None, 0))
def cost_QCNN(params, input_data, labels):
predictions = QCNN(params['w'], input_data)
return cross_entropy_loss(predictions, labels)
grad_QCNN = jax.grad(cost_QCNN)
grad_QCNN = jax.jit(grad_QCNN)
num_param_QCNN = n_layers_qcnn * n_params_layer
num_gen_QCNN = 29 * n_params_block + 15 * 2 # For the 16 qubit model
num_circuits_QCNN = 29 * (8*2+2*4) + 15*4
print('number of generators for QCNN model: ' + str(num_gen_QCNN))
print('number of parameters for QCNN model: ' + str(num_param_QCNN))
######## SEPARABLE MODEL ##########
obs=qml.dot([1/qubits] * qubits, [qml.PauliZ(i) for i in range(qubits)])
dev = qml.device('default.qubit',wires=qubits)
@qml.qnode(dev, interface='jax')
def separable_model(params,x):
for q in range(qubits):
qml.RY(x[q],wires=q)
#apply the rotation for each equivariant generator
for q in range(qubits):
qml.Rot(params[3*q],params[3*q+1],params[3*q+2],wires=q)
return qml.expval(obs)
separable_model = jax.jit(separable_model)
separable_model = jax.vmap(separable_model,(None,0))
def cost_separable(params, input_data, labels):
predictions = separable_model(params['w'],input_data)
return cross_entropy_loss(predictions,labels)
grad_separable = jax.grad(cost_separable)
grad_separable = jax.jit(grad_separable)
#################################### TRAINING AND EVAL FUNCTIONS ####################################
def square_loss(predictions, labels):
"""Square loss."""
loss = jnp.sum((labels-predictions)**2)
loss = loss/len(labels)
return loss
def cross_entropy_loss(predictions, labels):
labels = jax.nn.relu(labels) # convert to 0,1
return jnp.mean(optax.sigmoid_binary_cross_entropy(predictions*6, labels))
def accuracy(labels, predictions):
return jnp.sum(predictions == labels)/len(labels)
def get_mini_batch(X,Y,n):
"""Return a random mini-batch of size n from data."""
indices = np.random.choice(X.shape[0], size=n, replace=False)
return X[indices, :], Y[indices]
def run_adam(grad_fn, cost_fn, lr, init_params, model, num_iter=5):
"""
Optimises a model using the adam gradient update. We use optax.
:param grad_func: vmapped function that returns the grads of a batch
:param cost_fn: function that returns the cost of a batch
:param lr: initial learning rate
:param init_params: initial parameters
:param model: the model function used for evaluation
:param num_iter: the number of training steps
:return:
params: trained parameters
history: training history
"""
params = init_params.copy()
optimizer = optax.adam(lr)
opt_state = optimizer.init(params)
history = []
# chunk the functions to save memory
# chunk size should divide batch_size
chunked_model = chunk_vmapped_fn(model, 1, 1)
chunked_grad = chunk_grad(grad_fn, 1)
chunked_loss = chunk_loss(cost_fn, 1)
epsilon = 0.01
for it in range(num_iter):
X_batch, Y_batch = get_mini_batch(X, Y, batch_size)
grads = chunked_grad(params, X_batch, Y_batch)
grad_noise = jax.random.normal(key=jax.random.PRNGKey(np.random.randint(1000000)),
shape=grads['w'].shape) * epsilon
grads['w'] = grads['w'] + grad_noise
cst = chunked_loss(params, X_batch, Y_batch)
predictions = jnp.sign(chunked_model(params['w'], Xtest))
acc = accuracy(Ytest, predictions)
history.append((params, cst, acc))
updates, opt_state = optimizer.update(grads, opt_state)
params = optax.apply_updates(params, updates)
if it % 1 == 0:
print([cst, acc])
return params, history
#################################### TRAINING ####################################
np.random.seed(seed)
X, Y = generate_data(qubits, 1000, dim//2,noise=1.0)
Xtest, Ytest = generate_data(dim, 100, dim//2 ,noise=1.0)
scale = 0.5
X = scale*X
Xtest = scale*Xtest
batch_size = 20
num_iter = 100
lr=0.01
trials = 20
plots_QCNN = []
plots_parallel = []
plots_general = []
plots_separable = []
for t in range(trials):
print('trial=' + str(t))
print('sep')
init_params = {'w': 2 * np.pi * np.random.rand(qubits * 3)}
params, history_separable = run_adam(grad_separable, cost_separable, lr, init_params, separable_model, num_iter=num_iter)
plots_separable.append(history_separable)
print('QCNN')
init_params = {'w': 2 * np.pi * np.random.rand(n_layers_qcnn*n_params_layer)}
params, history_QCNN = run_adam(grad_QCNN, cost_QCNN, lr, init_params, QCNN_eval, num_iter=num_iter)
plots_QCNN.append(history_QCNN)
print('commuting')
init_params = {'w': 2 * np.pi * np.random.rand(num_param_parallel)}
params, history_parallel = run_adam(grad_parallel, cost_parallel, lr, init_params, parallel_model_eval, num_iter=num_iter)
plots_parallel.append(history_parallel)
print('noncommuting')
init_params = {'w': 2 * np.pi * np.random.rand(num_param_general)}
params, history_general = run_adam(grad_general, cost_general, lr, init_params, general_model_eval, num_iter=num_iter)
plots_general.append(history_general)
np.savetxt(f'compare_cost_separable_{dim}.txt',[[plots_separable[t][i][1] for i in range(num_iter)] for t in range(trials)])
np.savetxt(f'compare_cost_parallel_{dim}.txt',[[plots_parallel[t][i][1] for i in range(num_iter)] for t in range(trials)])
np.savetxt(f'compare_cost_general_{dim}.txt',[[plots_general[t][i][1] for i in range(num_iter)]for t in range(trials)])
np.savetxt(f'compare_cost_qcnn_{dim}.txt',[[plots_QCNN[t][i][1] for i in range(num_iter)]for t in range(trials)])
np.savetxt(f'compare_acc_separable_{dim}.txt',[[plots_separable[t][i][2] for i in range(num_iter)] for t in range(trials)])
np.savetxt(f'compare_acc_parallel_{dim}.txt',[[plots_parallel[t][i][2] for i in range(num_iter)] for t in range(trials)])
np.savetxt(f'compare_acc_general_{dim}.txt',[[plots_general[t][i][2] for i in range(num_iter)] for t in range(trials)])
np.savetxt(f'compare_acc_qcnn_{dim}.txt',[[plots_QCNN[t][i][2] for i in range(num_iter)] for t in range(trials)])