-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses.py
535 lines (431 loc) · 18.9 KB
/
classes.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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
import numpy as np
# Qibo modules
from qibo.models import Circuit
from qibo import gates
from qibo.optimizers import cmaes
#optimization method
from scipy.optimize import minimize
from qibo.hamiltonians import Hamiltonian
#Ploting results
import matplotlib.pyplot as plt
from matplotlib.pyplot import get_cmap
from matplotlib.colors import Normalize
from qibo.hamiltonians import SymbolicHamiltonian
from qibo.symbols import Z
from scipy.special import binom, loggamma
class classifier:
def __init__(self, qubits, layers, V, training_size=100, data_size = 5000, seed=7, weights = True):
""""Class that includes all the necessary for the training of the data re-uploading classifier
Args:
- qubits (int): Number of qubits that the classifier has
- layers(int): Number of layers that our circuit ansatz has
- V (array): unitary matrix that creates the data set.
- training size (int): number of training points
- data_size (int): data for visualization
- seed (int): Random seed
- weights (bool): if the model includes trainable weights or not
"""
if seed == 'random':
np.random.seed(np.random.randint(100, size=1))
else:
np.random.seed(seed)
self.V = V
self.qubits = qubits
self.data_size = data_size
self.layers = layers
self.training_size = training_size
self.training_set= self.create_mesh(int(np.sqrt(self.training_size)))
self.params = np.random.rand(self.layers*self.qubits*4)
self.f = SymbolicHamiltonian(np.prod([Z(i) for i in range(self.qubits)]))
self.weights = weights
# Circuit to create the data-set
def featuremapZZ(self, x, n_qubits = 2):
C = Circuit(n_qubits)
pi = np.pi
data = np.array(list(x)*int(n_qubits/2))
for n in range(n_qubits):
C.add(gates.H(n))
C.add(gates.U1(n, theta = 2*data[n], trainable= False))
for n in range(n_qubits):
for n_ in range(n):
C.add(gates.CNOT(n_,n))
C.add(gates.U1(n, theta = 2*(pi-data[n_])*(pi-data[n]), trainable=False))
C.add(gates.CNOT(n_,n))
return C
# For visualization
def create_mesh(self, data_size, start = 0, end = 2*np.pi):
start = 0
end = 2 * np.pi
spacing = end/data_size
# Generate the points in each dimension
x1 = np.arange(start, end + spacing, spacing)
x2 = np.arange(start, end + spacing, spacing)
# Create the meshgrid
data = np.array([[i, j] for i in x1 for j in x2])
f = SymbolicHamiltonian(np.prod([Z(i) for i in range(2)]))
V = self.V
obs = np.conjugate(V.T)@(f@V)
obs = Hamiltonian(2, matrix = obs)
y = []
for _,x_1 in enumerate(x1):
for j, x_2 in enumerate(x2):
fm = self.featuremapZZ([x_1, x_2], 2)
fm = fm+fm
exp_value = obs.expectation(fm().state())
if exp_value >= 0:
y.append(1)
elif exp_value < 0:
y.append(-1)
return data, y
def create_dataset(self, data_size):
data = [np.random.uniform(0,2*np.pi, size = 2) for i in range(data_size)]
f = SymbolicHamiltonian(np.prod([Z(i) for i in range(2)]))
V = self.V
obs = np.conjugate(V.T)@(f@V)
obs = Hamiltonian(2, matrix = obs)
y = []
for x_train in data:
fm = self.featuremapZZ(x_train, 2)
fm = fm+fm
exp_value = obs.expectation(fm().state())
if exp_value >= 0:
y.append(1)
elif exp_value < 0:
y.append(-1)
return data, y
def circuit_ansatz(self,x, params = None):
if params is None:
params = self.params
else:
params = params.reshape(self.layers, self.qubits, 4)
C = Circuit(self.qubits)
for p in params:
for idx, p_i in enumerate(p):
if self.weights == True:
C.add(gates.U3(idx, theta= p_i[0]*x[0] + p_i[1], phi= p_i[2]*x[1]+ p_i[3], lam=0))
else:
C.add(gates.U3(idx, theta= x[0] + p_i[1], phi= x[1]+ p_i[3], lam=0, trainable = True))
#We apply a Cnot if we have the two-qubit ansatz
if idx<self.qubits-1:
C.add(gates.CNOT(idx,idx+1))
elif idx == self.qubits-1:
C.add(gates.CNOT(idx, 0))
return C
def cost_function(self, params = None, x_t = None, y_t = None ):
"""This function computes the normalized sum of the fidelities between the label state for the classification and the state obtained
via passing the data through the circuit (with the given params).
Args:
-params: The params will be updated after each step of the minimization method.
Returns:
- Value of the cost function.
"""
if x_t is None:
x_t, y_t = self.training_set
#If the arg of the function is given, we upload the value of the params of the class
if params is not None:
self.params = params.reshape(self.layers, self.qubits, 4)
cf = 0
obs = self.f
for x, y in zip(x_t, y_t):
C = self.circuit_ansatz(x)
exp_value = obs.expectation(C().state())
py = 1/2*(1+y*exp_value)
#We add to the cost function the contribution of each data-point
cf -=py
cf /= len(y_t)
return cf
def minimize_funct(self,method='l-bfgs', options=None):
""""This function minimizes the cost function in the space of the parameters. Then
it returns the value of the function when the optimization has finished and the
values of the parameters that acomplish the desired optimization. Also computes
the values of the gradients of the parameters.
Args:
- Method (str): Method used to minimize the cost function. Options: ['bfgs',
'L-BFGS-B', 'Nelder-Mead', ...]
- Options (dict): Options for the minimization
Returns:
-result (float): value of the cost function after the minimization.
-params (list): list of the parameters that accomplish the minimization.
- steps (list): list of the steps done by the algorithm
- cost_values (list): value of the cost function at each step
- thetas (list): optimized angles after each iteration
"""
steps = []
cost_values = []
thetas = []
def save_step(k):
cost_values.append(self.cost_function(k))
steps.append(k)
thetas.append(self.params)
m = minimize(self.cost_function, self.params,
method = method, options=options, callback= save_step)
result = m.fun
params = m.x
return result, params, steps, cost_values, thetas
def eval_test_set_fidelity(self,params_opt, xandy = None):
"""This function returns the labels of the classification states predicted for each data-point.
We run our circuit for all the data with the optimized parameters
Args:
-params_opt (list): Parameters used in the classifier circuit.
- xandy (array): in case we want to evaluate x and y that are different from the training set.
Returns:
- labels (list): predicted labels for each data-point."""
if xandy is not None:
x_s= xandy[0]
y = xandy[1]
else:
x_s = self.training_set[0]
y = self.training_set[1]
self.params = params_opt
labels = np.ones(len(y))
for i,x in enumerate(x_s):
C = self.circuit_ansatz(x)
obs = self.f
exp_value = obs.expectation(C().state())
if exp_value >0:
labels[i] = 1
else:
labels[i] = -1
return labels
def accuracy_score(y_pred, y_real):
"""Computes the accuracy as the ratio of the number of right predicted labels and the number of total attempts.
Args:
-y_pred (array of ints): array with the predicted labels.
-y_real (array of ints): array with the true labels.
Returns:
- proportion of right predictions.
"""
score = y_pred == y_real
return score.sum() / len(y_real)
class moment_analytic():
"""This class computes the moments of a given observable as if they were averaged over Haar-random states, that is:
- E_S[<psi|O|psi>^t], and S = {|\psi>} is a Haar-random set of states.
The analytical formula can be found in: https://arxiv.org/abs/2404.16211"""
def find_multinomial_elements(self, t,G):
elements = []
if G == 2:
for k in range(t + 1):
elements.append((k, t - k))
if G > 2:
for k in range(t + 1):
elem = [e for e in self.find_multinomial_elements(t-k, G - 1)]
for e in elem:
elements.append((k, *e))
return elements
def multinomial(self, elements):
if len(elements) == 1:
return 1
return binom(sum(elements), elements[-1]) * self.multinomial(elements[:-1])
def compute_moments(self, eigenvalues, multiplicities, t):
assert len(eigenvalues) == len(multiplicities)
G = len(eigenvalues)
elements = self.find_multinomial_elements(t, G)
result = 0
# eig: product of lambda^k_i
# multinomial(elem):
for elem in elements:
eig = 1
for l, k in zip(eigenvalues, elem):
eig *= l**k
loggam = loggamma(sum(multiplicities)/2) - loggamma(sum(multiplicities)/2 + t)
for m, k in zip(multiplicities, elem):
loggam += loggamma(m/2 + k) - loggamma(m/2)
r = self.multinomial(elem) * np.exp(loggam) * eig
result += r
return result
def get_degeneracy(eigvals):
eigvals = np.round(eigvals, 4)
unique_eigvals = np.unique(eigvals)
deg={}
alphas = {}
for v in unique_eigvals:
deg[v] = len(np.where(eigvals == v)[0])
alphas[v] = deg[v]/2#*(1+v)
return deg, alphas, unique_eigvals
def compute_est_t_moment(values, moment):
mu_bar_t = sum(np.power(values, moment))/len(values)
mu_bar_2t = sum(np.power(values, 2*moment))/len(values)
var_mu_bar_t = mu_bar_2t - mu_bar_t**2
return mu_bar_t, var_mu_bar_t
def compute_randomness_bounds(mu_bar, mu_lower, mu_upper):
rnd_lower = np.abs(mu_bar - mu_lower)
rnd_upper = np.abs(mu_bar - mu_upper)
return rnd_lower, rnd_upper
class fm_cost_new:
""" This class contains the necessary code to train the feature-map classifier.
Args:
- n_qubits (int): Number of qubits that the classifier has
- layers(int): Number of layers that our circuit ansatz has
- V (array): unitary matrix that creates the data set.
- method (str): optimization algorithm
- training size (int): number of training points
- data_size (int): data for visualization
- seed (int) or (str): we can specify the seed or ask for a random one.
- data_reverser (bool): If we want the order of the training data to be changed when uploaded to the model.
- brick (bool): True if we want the brick ansatz, False if we want the non-brick ansatz.
"""
def __init__(self, n_qubits, layers, V = None, method = 'L-BFGS-B', data_size = 5000, training_size = 20, seed = 'random', data_reversed = False, brick = False):
if seed == 'random':
np.random.seed(np.random.randint(100, size=1))
else:
np.random.seed(seed)
self.brick = brick #True-> feature-map brick. False-> Feature map first neighbours
self.data_reversed = data_reversed
self.n_qubits = n_qubits
self.layers = layers
self.method = method
self.params =np.random.rand((self.layers)*self.n_qubits)
self.data_size = data_size
self.training_size = training_size
self.V = V
self.f = SymbolicHamiltonian(np.prod([Z(i) for i in range(self.n_qubits)]))
self.train_data, self.labels = self.create_mesh(int(np.sqrt(self.training_size)))
def featuremapZZ(self, x, n_qubits = None ):
if n_qubits is None:
n_qubits = self.n_qubits
C = Circuit(n_qubits)
pi = np.pi
if self.data_reversed is False:
if n_qubits%4 ==0:
data = np.array(list(list(x)+ list(reversed(x)))*int((n_qubits)/4))
else:
data = np.array(list(list(x)+ list(reversed(x)))*int((n_qubits-2)/4)+list(x))
else:
data = np.array(list(list(x))*int((n_qubits/2)))
for n in range(n_qubits):
C.add(gates.H(n))
C.add(gates.U1(n, theta = 2*data[n], trainable= False))
for n in range(n_qubits):
for n_ in range(n):
C.add(gates.CNOT(n_,n))
C.add(gates.U1(n, theta = 2*(pi-data[n_])*(pi-data[n]), trainable=False))
C.add(gates.CNOT(n_,n))
return C
def featuremap_brick(self, x, brick = False):
n_qubits = self.n_qubits
C = Circuit(n_qubits)
pi = np.pi
data = list(x)*int((n_qubits)/2)
for n in range(n_qubits):
C.add(gates.H(n))
C.add(gates.U1(n, theta = 2*data[n], trainable= False))
for n in range(0,n_qubits,2):
C.add(gates.CNOT(n,(n+1)%n_qubits))
C.add(gates.U1((n+1)%n_qubits, theta = 2*(pi-data[n])*(pi-data[n+1]), trainable=False))
C.add(gates.CNOT(n,(n+1)%n_qubits))
if brick:
for n in range(n_qubits):
C.add(gates.H(n))
C.add(gates.U1(n, theta = 2*data[n], trainable= False))
for n in range(1,n_qubits,2):
C.add(gates.CNOT(n,(n+1)%n_qubits))
C.add(gates.U1((n+1)%n_qubits, theta = 2*(pi-data[n])*(pi-data[(n+1)%n_qubits]), trainable=False))
C.add(gates.CNOT(n,(n+1)%n_qubits))
return C
else:
return C+C
def variational_circuit(self, params= None):
if params is None:
params = self.params
else:
params = params.reshape(self.layers, self.n_qubits)
n_qubits = self.n_qubits
layers = self.layers
C = Circuit(n_qubits)
for l in range(layers):
for n in range(n_qubits):
C.add(gates.RY(n, params[l][n], trainable= True))
for n in range(n_qubits):
if n != n_qubits-1:
C.add(gates.CZ(n, n+1))
elif n_qubits>2:
C.add(gates.CZ(n, 0))
return C
def create_mesh(self, data_size, start = 0, end = 2*np.pi):
start = 0
end = 2 * np.pi
spacing = end/data_size
# Generate the points in each dimension
x1 = np.arange(start, end + spacing, spacing)
x2 = np.arange(start, end + spacing, spacing)
# Create the meshgrid
data = np.array([[i, j] for i in x1 for j in x2])
f = SymbolicHamiltonian(np.prod([Z(i) for i in range(2)]))
V = self.V
obs = np.conjugate(V.T)@(f@V)
obs = Hamiltonian(2, matrix = obs)
y = []
for _,x_1 in enumerate(x1):
for _, x_2 in enumerate(x2):
fm = self.featuremapZZ([x_1, x_2], 2)
fm = fm+fm
exp_value = obs.expectation(fm().state())
if exp_value >= 0:
y.append(1)
elif exp_value < 0:
y.append(-1)
return data, y
def create_dataset(self, data_size):
data = [np.random.uniform(0,2*np.pi, size = 2) for i in range(data_size)]
f = SymbolicHamiltonian(np.prod([Z(i) for i in range(2)]))
V = self.V
obs = np.conjugate(V.T)@(f@V)
obs = Hamiltonian(2, matrix = obs)
y = []
clean_data = []
for x_train in data:
fm = self.featuremapZZ(x_train, 2)
fm = fm+fm
exp_value = obs.expectation(fm().state())
if exp_value >= 0:
y.append(1)
clean_data.append(x_train)
elif exp_value < 0:
y.append(-1)
clean_data.append(x_train)
return clean_data, y
def cost(self, params, train_data = None, labels=None):
if train_data is None:
train_data = self.train_data
labels = self.labels
n_qubits = self.n_qubits
layers = self.layers
self.params= params
params = params.reshape(layers,n_qubits)
cost = 0
obs = self.f
for x, y in zip(train_data, labels):
fm = self.featuremap_brick(x, self.brick)
circ = fm+self.variational_circuit(params)
exp_value = obs.expectation(circ().state())
py = 1/2*(1+y*exp_value)
cost -= py
cost /= len(labels)
return cost
def minimize_funct(self, method='L-BFGS-B'):
""""This function minimizes the cost function in the space of the parameters. Then
it returns the value of the function when the optimization has finished and the
values of the parameters that acomplish the desired optimization. Also computes
the values of the gradients of the parameters.
Args:
- Method (str): Method used to minimize the cost function. Options: ['bfgs',
'L-BFGS-B', 'Nelder-Mead', ...]
- Options (dict): Options for the minimization
Returns:
-result (float): value of the cost function after the minimization.
-params (list): list of the parameters that accomplish the minimization.
- steps (list): list of the steps done by the algorithm
- cost_values (list): value of the cost function at each step
- thetas (list): optimized angles after each iteration
"""
cost_values = []
thetas = []
def save_step(k):
# global steps
print(self.cost(k))
cost_values.append(self.cost(k))
thetas.append(self.params)
m = minimize(self.cost, self.params, args = (), method= method, callback=save_step)
result = m.fun
params = m.x
return result, params, cost_values, thetas