-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
511 lines (368 loc) · 14.9 KB
/
main.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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 11 16:17:28 2021
@author: burak
"""
# Different hamiltonians, and their ground states
import pennylane as qml
import torch
import numpy as np
import timeit
from DataCreation import dataPreparation
from QAutoencoder.QAutoencoder import QuantumAutoencoder
import utils
# %% Loading the previous model
LOAD_DATA = False
LOAD_MODEL = False
# %% Import saved qubits
# change those without the 1's
PATH = './autoencoder_more_qubits1.npy'
PATH2 = './inputs_states1.npy'
n_data = 20
n_qubit_size = 8
if(LOAD_DATA == True):
tensorlist = torch.load(PATH2)
else:
tensorlist = dataPreparation(saved = False, save_tensors = False, method = 'state_prepare', number_of_samples = n_data, number_of_qubits= n_qubit_size)
# tensorlist = torch.as_tensor(tensorlist)
latent_size = 1
n_auxillary_qubits = latent_size
dev = qml.device('default.qubit', wires = n_qubit_size + latent_size + n_auxillary_qubits)
qAutoencoder = QuantumAutoencoder(n_qubit_size, dev, latent_size, n_auxillary_qubits, n_data)
if(LOAD_MODEL == True):
qAutoencoder.load_state_dict(torch.load(PATH))
# %% For utility functions / sampling, getting the state vec etc.
util_dev = qml.device('default.qubit', wires = n_qubit_size)
utils_object = utils.Utils(util_dev, n_qubit_size)
utils_object.n_qubit_size
utils_object.getState(tensorlist[1])
# %% Prepare an arbitrary Hamiltonian
from HamiltonianUtils import *
coeffs = [ np.random.rand() * 2 - 1 for i in range(n_qubit_size)]
pauliSet = [qml.PauliX(0).matrix, qml.PauliZ(0).matrix, qml.PauliY(0).matrix]
randomPauliGroup = [np.random.randint(3) for i in range(n_qubit_size)]
H = findHermitian(coeffs, randomPauliGroup, n_qubit_size)
torchH = torch.zeros_like(torch.Tensor(H), dtype=torch.cdouble)
torchH[:,:] = torch.Tensor(H.real[:,:])
torchH[:,:] += torch.Tensor(H.imag[:,:]) * 1j
Hamiltonian = qml.Hermitian(H, np.arange(n_qubit_size))
hamiltonian_dev = qml.device('default.qubit', wires = n_qubit_size, shots= 1)
@qml.qnode(hamiltonian_dev)
def measureEnergy(H, psi):
qml.QubitStateVector(psi, wires = range(0,n_qubit_size))
return qml.sample(Hamiltonian)
NUMBER_OF_EVOLUTIONS = 3
evolved_states = []
dataset_size = len(tensorlist)
for i in range(dataset_size):
psi = tensorlist[i]
for t in range(1,NUMBER_OF_EVOLUTIONS + 1):
start_time = timeit.time.time()
evolved_states.append(timeEvolution(torchH, psi, t))
end_time = timeit.time.time()
print('Time elapsed for preparing the time evolved state : {:.2f}, for {}th state'.format(end_time - start_time, i * NUMBER_OF_EVOLUTIONS + 1 + t))
input_states = []
ctr = 0
for i in range(dataset_size):
input_states.append(tensorlist[i])
for t in range(NUMBER_OF_EVOLUTIONS):
input_states.append(evolved_states[ctr])
ctr += 1
# %% Checking the dataset
print('Time evolutions checks with the dataset' , qml.math.allclose(timeEvolution(torchH, input_states[NUMBER_OF_EVOLUTIONS + 1] , 1 ), input_states[NUMBER_OF_EVOLUTIONS + 2]))
print('Time evolutions checks with the dataset' , qml.math.allclose(timeEvolution(torchH, input_states[NUMBER_OF_EVOLUTIONS*2 + 3] , 1 ), input_states[NUMBER_OF_EVOLUTIONS*2 + 4]))
# %%
def fidelityLossMulti(mes):
return sum(torch.log(1 - (mes[i])) for i in range(len(mes)))
loss_func = fidelityLossMulti
learning_rate = 0.01
opt = torch.optim.Adam(qAutoencoder.parameters() , lr = learning_rate, betas=(0.9, 0.999), eps=1e-08, weight_decay=0, amsgrad=False)
opt = torch.optim.RMSprop(qAutoencoder.parameters(), lr = learning_rate)
batch_size = 4
average_losses = []
avg_loss = 0
epochs = 50
#x = torch.stack([torch.Tensor(qml.init.strong_ent_layers_normal(n_qubit_size, qAutoencoder.n_total_qubits - (2* n_auxillary_qubits))) for i in range(n_data) ])
loss_checkpoints = []
# %% Training
for epoch in range(epochs):
# N = len(input_states)
N = len(tensorlist)
running_loss = 0
start_time = timeit.time.time()
batch_id = np.arange(N)
np.random.shuffle(batch_id)
cur_losses_for_instances = []
opt.zero_grad()
for idx, i in enumerate(batch_id):
# out = qAutoencoder(input_states[i] , training_mode = True)
out = qAutoencoder(tensorlist[i] , training_mode = True)
loss = loss_func(out)
loss.backward()
if(idx % 1 == 0):
cur_losses_for_instances.append(loss)
print(out)
running_loss += loss
if(idx % 5 == 0):
opt.step()
opt.zero_grad()
epoch_loss = running_loss / N
avg_loss = (avg_loss * epoch + epoch_loss) / (epoch + 1)
average_losses.append(epoch_loss)
end_time = timeit.time.time()
print('Time elapsed for the epoch' + str(epoch) + ' : {:.2f}, with {} loss'.format(end_time - start_time, epoch_loss))
# %% Testing, w/ fidelity
for i in range(1):
batch_id = np.arange(n_data)
np.random.shuffle(batch_id)
# state = input_states[i]
state = tensorlist[i]
out = qAutoencoder(state, training_mode = False)
print(out)
# print('Fidelity is {:.9f}'.format(out.detach().numpy().squeeze()))
els = np.zeros(int(len(dev.state)/4) , dtype = np.complex128)
coo = [0]
for j in range(n_qubit_size, total_qubit_size):
coo.append(2**j)
for e in range(4):
els[e] = 0
for i in range(2):
for j in range(2):
summ = coo[i * 1] + coo[j * 2]
els[e] += (dev.state[summ + e] ** 2)
# for k in range(2):
# for l in range(2):
# summ = coo[i * 1] + coo[j * 2] + coo[k * 3] + coo[l * 4]
# els[e] += (dev2.state[summ + e] ** 2)
els = np.sqrt(els)
tensorlist[0]
# dev2 = qml.device('default.qubit', wires = 4)
# qq = QuantumAutoencoder(2, dev2, 1, 1, n_data)
# qq(dd[0], training_mode = False)
# %% Training with SPSA
from noisyopt import minimizeSPSA
from pennylane import numpy as np
n_qubit_size = 8
N_QUBIT_SIZE = n_qubit_size
n_data = 2
dev_sampler_spsa = qml.device("qiskit.aer", wires = n_qubit_size + latent_size + n_auxillary_qubits, shots=1000)
tensorlist = dataPreparation(saved = False, save_tensors = False, method = 'state_prepare', number_of_samples = n_data, number_of_qubits= n_qubit_size)
total_qubit_size = n_qubit_size + latent_size + n_auxillary_qubits
data =[ tensorlist[i].numpy() for i in range(n_data)]
data_counter = 0
@qml.qnode(dev_sampler_spsa)
def qCircuit(params, inputs = False):
qml.QubitStateVector(inputs, wires = range(n_auxillary_qubits + latent_size, total_qubit_size))
for l in range(num_layers):
for idx, i in enumerate(range(n_auxillary_qubits + latent_size, total_qubit_size)):
qml.Rot(*params[l, idx, 0] , wires = i)
for idx, i in enumerate(range(n_auxillary_qubits + latent_size, total_qubit_size)):
for jdx, j in enumerate(range(n_auxillary_qubits + latent_size, total_qubit_size)):
ctr=1
if(i==j):
pass
else:
qml.CRot( *params[l, idx, ctr], wires= [i, j])
ctr += 1
for idx, i in enumerate(range(n_auxillary_qubits + latent_size, total_qubit_size)):
qml.Rot(*params[l, idx, N_QUBIT_SIZE] , wires = i)
for i in range(n_auxillary_qubits):
qml.Hadamard(wires = i)
for i in range(n_auxillary_qubits):
qml.CSWAP(wires = [i, i + n_auxillary_qubits , n_auxillary_qubits + i + latent_size])
for i in range(n_auxillary_qubits):
qml.Hadamard(wires = i)
return [qml.expval(qml.PauliZ(q)) for q in range(0, n_auxillary_qubits)]
num_layers = 3
flat_shape = num_layers * (n_qubit_size) * (n_qubit_size + 1) * 3
param_shape = (num_layers, n_qubit_size, n_qubit_size + 1, 3)
init_params = np.random.normal(scale=0.1, size=param_shape, requires_grad=True)
init_params_spsa = init_params.reshape(flat_shape)
def fidelityLossMultiple(init_params):
global data
global data_counter
mes = qCircuit(init_params, data[data_counter])
return sum(np.log(1 - (mes[i])) for i in range(len(mes)))
niter_spsa = 100
# Evaluate the initial cost
cost_list = [fidelityLossMultiple(init_params)]
device_execs_spsa = [0]
start_time = timeit.time.time()
def callback_fn(xk):
global data
global data_counter
cost_val = fidelityLossMultiple(xk)
cost_list.append(cost_val)
# We've evaluated the cost function, let's make up for that
num_executions = int(dev_sampler_spsa.num_executions / 2)
device_execs_spsa.append(num_executions)
iteration_num = len(cost_list)
if iteration_num % 1 == 0:
data_counter += 1
data_counter = data_counter % len(data)
end_time = timeit.time.time()
print(
f"Iteration = {iteration_num}, "
f"Number of device executions = {num_executions}, "
f"Cost = {cost_val}"
)
print('time elapsed: {:.2f}'.format(end_time - start_time))
res = minimizeSPSA(
fidelityLossMultiple,
x0=init_params,
niter=niter_spsa,
paired=False,
c=0.10,
a=0.1,
callback=callback_fn,
)
# %%
valid_states = []
for k in cur_losses_for_instances:
valid_states.append(k.detach().numpy())
valid_states = np.array(valid_states)
valid_states_map = valid_states < -3
timeEvolution(torchH, input_states[1], 1)
input_states[2]
# Prepare seperate inputs for valid states
valid_input_states = []
for i in range(0, len(input_states), NUMBER_OF_EVOLUTIONS + 1):
for k in range(0,NUMBER_OF_EVOLUTIONS + 1):
counter = False
print(i, k)
if(valid_states_map[i + k] == True):
first_input_batch = []
counter = True
j = k + 1
while(j < NUMBER_OF_EVOLUTIONS + 1 and valid_states_map[j] == True):
print(i , j)
if(counter == True):
# first_input_batch.append(input_states[i + k])
first_input_batch.append(i+k)
counter = False
# first_input_batch.append(input_states[i + j])
first_input_batch.append(i+j)
j += 1
if(len(valid_input_states) > 0 and set(first_input_batch) <= set(valid_input_states[-1])):
continue
valid_input_states.append(first_input_batch)
# %%
dev1 = qml.device("default.qubit", wires=n_qubit_size)
input_index = 0
set_index = 0
@qml.qnode(dev1, interface='torch')
def circuit(params):
global input_index
global set_index
if(set_index == len(valid_input_states)):
set_index = 0
print(set_index , input_index)
inputs = input_states[valid_input_states[set_index][input_index]]
targets = input_states[valid_input_states[set_index][input_index + 1]]
density = quantumOuter(targets)
qml.QubitStateVector(inputs, wires = range(0, n_qubit_size))
for i in range(n_qubit_size): # 4 qubits
qml.Rot(*params[i][0], wires=i)
for idx, i in enumerate(range(n_qubit_size)):
ctr=0
for jdx, j in enumerate(range(n_qubit_size)):
if(i==j):
pass
else:
qml.CRot( *params[i][ctr + 1], wires= [i,j])
ctr += 1
for i in range(n_qubit_size): # 4 qubits
qml.Rot(*params[i][n_qubit_size], wires=i)
input_index += 1
if(input_index == len(valid_input_states[set_index]) - 1):
set_index += 1
input_index = 0
return qml.expval(qml.Hermitian(density, wires=list(range(0,n_qubit_size)) ))
init_params = np.random.rand(n_qubit_size, n_qubit_size + 1, 3)
init_params = torch.from_numpy(init_params)
init_params.requires_grad = True
# init_params = torch.Tensor(init_params, requires_grad = True)
# target_state = np.ones(2**4)/np.sqrt(2**4)
# density = np.outer(target_state, target_state)
# density = np.outer(target_state, target_state)
quantumOuter = lambda inputs: torch.outer(inputs.conj().T, inputs)
def isHermitian(H):
return qml.math.allclose(H ,H.T.conj())
isHermitian(density)
def cost(var):
return 1-circuit(var)
res = (cost(init_params))
res.backward()
opt = torch.optim.Adam([init_params], lr = 0.1)
steps = 200
def closure():
opt.zero_grad()
loss = cost(init_params)
print(loss)
loss.backward()
return loss
for i in range(steps):
opt.step(closure)
# %% Testing, w/ vector similarity
# for i in range(10):
# batch_id = np.arange(n_data)
# np.random.shuffle(batch_id)
# state = tensorlist[batch_id[i]]
# out = qAutoencoder(state, training_mode = False )
# # input_state = np.kron(np.kron([1,0], [1,0]),tensorlist[n_qubit_size][i].numpy())
# input_state = np.kron(np.kron([1,0], [1,0]), state.numpy())
# result = np.array(dev.state.detach())
# # how similar is the output to the input?
# similarity = sum(np.abs((result-input_state) ** 2)) / (2**n_qubit_size)
# print('similarity is {:.9f}'.format(similarity))
# %% NEXT THINGS TO DO
# PREPARE THE DEV.STATE WITH THE INPUT THAT IS PREPARED WITH QARBITRARYSTATE - DONE
# Prepare examplary hamiltonians,
#
# %% Calculation of durations
'''
n_qubit_sizes = list(range(3,12))
iteration_duration = []
for n in n_qubit_sizes:
n_qubit_size = n
latent_size = 2
n_auxillary_qubits = latent_size
dev = qml.device('default.qubit', wires = n_qubit_size + latent_size + n_auxillary_qubits)
loss_func = fidelityLossMulti
learning_rate = 0.02
qAutoencoder = QuantumAutoencoder(n_qubit_size, dev, latent_size, n_auxillary_qubits)
opt = torch.optim.Adam(qAutoencoder.parameters() , lr = learning_rate, betas=(0.9, 0.999), eps=1e-08, weight_decay=0, amsgrad=False)
average_fidelities = []
batch_size = 4
epochs = 30
n_data = 10
start_time = timeit.time.time()
opt.zero_grad()
out = qAutoencoder(tensorlist[n_qubit_size][0])
loss = loss_func(out)
print(out)
loss.backward()
opt.step()
end_time = timeit.time.time()
iteration_duration.append(end_time - start_time )
print('Time elapsed for {} qubits: {:.2f} '.format(n_qubit_size, end_time-start_time ))
'''
# %% Saving the model
# PATH
# PATH2 = './inputs_states.npy'
X = torch.load(PATH2)
torch.save(torchH, './torchH.npy')
torch.save(coeffs, './coeffs.npy')
torch.save(randomPauliGroup, './randomPauliGroup.npy')
torch.save(qAutoencoder.state_dict(), './qAutoencoder.npy')
torch.save(input_states, './input_states.npy')
H
# %%
# for n_qubits in range(1,10):
# vec_dev = qml.device('default.qubit', wires = n_qubits)
# @qml.qnode(vec_dev)
# def qCircuit(inputs):
# qml.QubitStateVector(inputs, wires = range(0,n_qubits))
# return qml.probs(range(0,n_qubits))
# return [qml.expval(qml.PauliZ(q)) for q in range(0,1)]
# print(qCircuit(tensorlist[n_qubits][0]))