-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstacking.py
617 lines (510 loc) · 24.2 KB
/
stacking.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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
from xmps.svd_robust import svd
from scipy.linalg import polar
import numpy as np
from variational_mpo_classifiers import evaluate_classifier_top_k_accuracy, classifier_predictions, mps_encoding
from plot_results import produce_psuedo_sum_states, load_brute_force_permutations
from tools import load_data
from scipy import sparse
import qutip
import matplotlib.pyplot as plt
from experiments import create_experiment_bitstrings
from tqdm import tqdm
def classical_stacking():
"""
#Ancillae start in state |00...>
#n=0
#ancillae_qubits = np.eye(2**n)[0]
#Tensor product ancillae with predicition qubits
#Amount of ancillae equal to amount of predicition qubits
#training_predictions = np.array([np.kron(ancillae_qubits, (mps_image.H @ classifier).squeeze().data) for mps_image in tqdm(mps_images)])
#Create predictions
training_predictions = np.array([abs((mps_image.H @ classifier).squeeze().data) for mps_image in mps_images])
np.save('ortho_big_training_predictions_D_32',training_predictions)
training_predictions = np.load('ortho_big_training_predictions_D_32.npy')
labels = np.load('big_labels.npy')
training_acc = evaluate_classifier_top_k_accuracy(training_predictions, labels, 1)
print('Training Accuracy:', training_acc)
"""
"""
x_train, y_train, x_test, y_test = load_data(
100,10000, shuffle=False, equal_numbers=True
)
D_test = 32
mps_test = mps_encoding(x_test, D_test)
test_predictions = np.array(classifier_predictions(classifier, mps_test, bitstrings))
accuracy = evaluate_classifier_top_k_accuracy(test_predictions, y_test, 1)
print('Test Accuracy:', accuracy)
"""
"""
inputs = tf.keras.Input(shape=(28,28,1))
x = tf.keras.layers.AveragePooling2D(pool_size = (2,2))(inputs)
x = tf.keras.layers.Flatten()(x)
outputs = tf.keras .layers.Dense(10, activation = 'relu')(x)
model = tf.keras.Model(inputs=inputs, outputs=outputs)
model.summary()
"""
training_predictions = np.load('Classifiers/fashion_mnist/initial_training_predictions_ortho_mpo_classifier.npy')
y_train = np.load('Classifiers/fashion_mnist/big_dataset_training_labels.npy')
inputs = tf.keras.Input(shape=(training_predictions.shape[1],))
#inputs = tf.keras.Input(shape=(qtn_prediction_and_ancillae_qubits.shape[1],))
outputs = tf.keras.layers.Dense(10, activation = 'sigmoid')(inputs)
model = tf.keras.Model(inputs=inputs, outputs=outputs)
model.summary()
model.compile(
optimizer=tf.keras.optimizers.Adam(),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=[tf.keras.metrics.SparseCategoricalAccuracy()],
)
#earlystopping = tf.keras.callbacks.EarlyStopping(monitor='loss', patience=100, restore_best_weights=True)
history = model.fit(
training_predictions,
y_train,
epochs=2000,
batch_size = 32,
verbose = 1
)
#model.save('models/ortho_big_dataset_D_32')
test_preds = np.load('Classifiers/fashion_mnist/initial_test_predictions_ortho_mpo_classifier.npy')
y_test = np.load('Classifiers/fashion_mnist/big_dataset_test_labels.npy')
trained_test_predictions = model.predict(test_preds)
#np.save('final_label_qubit_states_4',trained_training_predictions)
#np.save('trained_predicitions_1000_classifier_32_1000_train_images', trained_training_predictions)
accuracy = evaluate_classifier_top_k_accuracy(trained_test_predictions, y_test, 1)
print(accuracy)
def partial_trace(rho, qubit_2_keep):
""" Calculate the partial trace for qubit system
Parameters
----------
rho: np.ndarray
Density matrix
qubit_2_keep: list
Index of qubit to be kept after taking the trace
Returns
-------
rho_res: np.ndarray
Density matrix after taking partial trace
"""
num_qubit = int(np.log2(rho.shape[0]))
if num_qubit == 4:
return np.outer(rho,rho.conj())
else:
rho = qutip.Qobj(rho, dims = [[2] * num_qubit ,[1]])
return rho.ptrace(qubit_2_keep)
def evaluate_stacking_unitary(U, partial = False, dataset = 'fashion_mnist', training = False):
"""
Evaluate Performance
"""
n_copies = int(np.log2(U.shape[0])//4)-1
if training:
"""
Load Training Data
"""
initial_label_qubits = np.load('Classifiers/' + dataset + '_mixed_sum_states/D_total/ortho_d_final_vs_training_predictions_compressed.npz', allow_pickle = True)['arr_0'][15].astype(np.float32)
y_train = np.load('Classifiers/' + dataset + '_mixed_sum_states/D_total/ortho_d_final_vs_training_predictions_labels.npy').astype(np.float32)
"""
Rearrange test data to match new bitstring assignment
"""
if partial:
possible_labels = [5,7,8,9,4,0,6,1,2,3]
assignment = possible_labels + list(range(10,16))
reassigned_preds = np.array([i[assignment] for i in initial_label_qubits])
reassigned_preds = np.array([i / np.sqrt(i.conj().T @ i) for i in reassigned_preds])
else:
reassigned_preds = np.array([i / np.sqrt(i.conj().T @ i) for i in initial_label_qubits])
outer_ket_states = reassigned_preds
#.shape = n_train, dim_l**n_copies+1
for k in range(n_copies):
outer_ket_states = [np.kron(i, j) for i,j in zip(outer_ket_states, reassigned_preds)]
"""
Perform Overlaps
"""
#We want qubit formation:
#|l_0^0>|l_1^0>|l_0^1>|l_1^1> |l_2^0>|l_3^0>|l_2^1>|l_3^1>...
#I.e. act only on first 2 qubits on all copies.
#Since unitary is contructed on first 2 qubits of each copy.
#So we want U @ SWAP @ |copy_preds>
print('Performing Overlaps!')
preds_U = np.array([abs(U.dot(i)) for i in tqdm(outer_ket_states)])
"""
Trace out other qubits/copies
"""
print('Performing Partial Trace!')
preds_U = np.array([np.diag(partial_trace(i, [0,1,2,3])) for i in tqdm(preds_U)])
#Rearrange to 0,1,2,3,.. formation. This is req. for evaluate_classifier
if partial:
preds_U = np.array([i[assignment] for i in preds_U])
training_predictions = evaluate_classifier_top_k_accuracy(preds_U, y_train, 1)
print()
print('Training accuracy before:', evaluate_classifier_top_k_accuracy(initial_label_qubits, y_train, 1))
print('Training accuracy U:', training_predictions)
print()
"""
Load Test Data
"""
initial_label_qubits = np.load('Classifiers/' + dataset + '_mixed_sum_states/D_total/ortho_d_final_vs_test_predictions.npy')[15]#.astype(np.float32)
y_test = np.load('Classifiers/' + dataset + '_mixed_sum_states/D_total/ortho_d_final_vs_test_predictions_labels.npy')#.astype(np.float32)
"""
Rearrange test data to match new bitstring assignment
"""
if partial:
possible_labels = [5,7,8,9,4,0,6,1,2,3]
assignment = possible_labels + list(range(10,16))
reassigned_preds = np.array([i[assignment] for i in initial_label_qubits])
reassigned_preds = np.array([i / np.sqrt(i.conj().T @ i) for i in reassigned_preds])
else:
reassigned_preds = np.array([i / np.sqrt(i.conj().T @ i) for i in initial_label_qubits])
outer_ket_states = reassigned_preds
#.shape = n_train, dim_l**n_copies+1
for k in range(n_copies):
outer_ket_states = [np.kron(i, j) for i,j in zip(outer_ket_states, reassigned_preds)]
"""
Perform Overlaps
"""
#We want qubit formation:
#|l_0^0>|l_1^0>|l_0^1>|l_1^1> |l_2^0>|l_3^0>|l_2^1>|l_3^1>...
#I.e. act only on first 2 qubits on all copies.
#Since unitary is contructed on first 2 qubits of each copy.
#So we want U @ SWAP @ |copy_preds>
print('Performing Overlaps!')
preds_U = np.array([abs(U.dot(i)) for i in tqdm(outer_ket_states)])
"""
Trace out other qubits/copies
"""
print('Performing Partial Trace!')
preds_U = np.array([np.diag(partial_trace(i, [0,1,2,3])) for i in tqdm(preds_U)])
#Rearrange to 0,1,2,3,.. formation. This is req. for evaluate_classifier
if partial:
preds_U = np.array([i[assignment] for i in preds_U])
test_predictions = evaluate_classifier_top_k_accuracy(preds_U, y_test, 1)
print()
print('Test accuracy before:', evaluate_classifier_top_k_accuracy(initial_label_qubits, y_test, 1))
print('Test accuracy U:', test_predictions)
print()
return None, test_predictions
def delta_efficent_deterministic_quantum_stacking(n_copies, v_col = True, dataset = 'fashion_mnist'):
from numpy import linalg as LA
print('Dataset: ', dataset)
#initial_label_qubits = np.load('Classifiers/' + dataset + '_mixed_sum_states/D_total/ortho_d_final_vs_training_predictions_compressed.npz', allow_pickle = True)['arr_0'][15]
#y_train = np.load('Classifiers/' + dataset + '_mixed_sum_states/D_total/ortho_d_final_vs_training_predictions_labels.npy')
initial_label_qubits = np.load('data/' + dataset + '/ortho_d_final_vs_training_predictions_compressed.npz', allow_pickle = True)['arr_0'][15].astype(np.float32)
y_train = np.load('data/' + dataset + '/ortho_d_final_vs_training_predictions_labels.npy').astype(np.float32)
initial_label_qubits = np.array([i / np.sqrt(i.conj().T @ i) for i in initial_label_qubits])
possible_labels = list(set(y_train))
dim_l = initial_label_qubits.shape[1]
outer_ket_states = initial_label_qubits
dim_lc = dim_l ** (1 + n_copies)
#.shape = n_train, dim_l**n_copies+1
for k in range(n_copies):
outer_ket_states = np.array([np.kron(i, j) for i,j in zip(outer_ket_states, initial_label_qubits)])
V = []
for l in tqdm(possible_labels):
weighted_outer_states = np.zeros((dim_lc, dim_lc))
for i in tqdm(initial_label_qubits[y_train == l]):
ket = i
for k in range(n_copies):
ket = np.kron(ket, i)
outer = np.outer(ket.conj(), ket)
weighted_outer_states += outer
#print('Performing SVD!')
U, S = svd(weighted_outer_states)[:2]
#print(U.shape)
#print(S.shape)
#assert()
if v_col:
#a = b = 16**n (using andrew's defn)
a, b = U.shape
p = int(np.log10(b)) - 1
D_trunc = 16
Vl = np.array(U[:, :b//16] @ np.sqrt(np.diag(S)[:b//16, :b//16]))
print(Vl.shape)
assert()
#Vl = np.array(U[:, :10**p] @ np.sqrt(np.diag(S)[:10**p, :10**p]))
#Vl = np.array(U[:, :D_trunc] @ np.sqrt(np.diag(S)[:D_trunc, :D_trunc]))
else:
Vl = np.array(U[:, :1] @ np.sqrt(np.diag(S)[:1, :1])).squeeze()
V.append(Vl)
V = np.array(V)
if v_col:
c, d, e = V.shape
#V = np.pad(V, ((0,dim_l - c), (0,0), (0,dim_l**p - D_trunc))).transpose(0, 2, 1).reshape(d , -1)
V = np.pad(V, ((0,dim_l - c), (0,0), (0,0))).transpose(0, 2, 1).reshape(dim_l*e, d)
else:
a, b = V.shape
V = np.pad(V, ((0,dim_l - a), (0,0)))
#np.save('V', V)
print('Performing Polar Decomposition!')
U = polar(V)[0]
print('Finished Computing Stacking Unitary!')
return U.astype(np.float32)
def sum_state_deterministic_quantum_stacking(n_copies, v_col = True, dataset = 'fashion_mnist'):
from numpy import linalg as LA
print('Dataset: ', dataset)
initial_label_qubits = produce_psuedo_sum_states(dataset)
y_train = range(10)
initial_label_qubits = np.array([i / np.sqrt(i.conj().T @ i) for i in initial_label_qubits])
possible_labels = list(set(y_train))
dim_l = initial_label_qubits.shape[1]
outer_ket_states = initial_label_qubits
dim_lc = dim_l ** (1 + n_copies)
weighted_outer_states = np.zeros((dim_lc, dim_lc))
for l in tqdm(possible_labels):
for i in tqdm(initial_label_qubits[y_train == l]):
ket = i
for k in range(n_copies):
ket = np.kron(ket, i)
outer = np.outer(np.kron(bitstrings[l].squeeze().tensors[5].data, np.eye(dim_lc//16)[0]), ket)
weighted_outer_states += outer
print('Performing Polar Decomposition!')
U = polar(weighted_outer_states)[0]
print('Finished Computing Stacking Unitary!')
return U.astype(np.float32)
def specific_quantum_stacking(n_copies, v_col = False):
from numpy import linalg as LA
"""
Load Data
"""
initial_label_qubits = np.load('Classifiers/fashion_mnist_mixed_sum_states/D_total/ortho_d_final_vs_training_predictions_compressed.npz', allow_pickle = True)['arr_0'][15].astype(np.float32)
y_train = np.load('Classifiers/fashion_mnist_mixed_sum_states/D_total/ortho_d_final_vs_training_predictions_labels.npy').astype(np.float32)
initial_label_qubits = [i / np.sqrt(i.conj().T @ i) for i in initial_label_qubits]
"""
Rearrange labels such that 5,7,8,9 are on the bitstrings:
|00>(|00> + |01> + |10> + |11>)
Also post-select (slice) such that stacking unitary is constructed only from bitstrings corresponding to labels 5,7,8,9
Normalisation req. after post-selection
"""
possible_labels = [5,7,8,9,4,0,6,1,2,3]
assignment = possible_labels + list(range(10,16))
reassigned_preds = np.array([i[assignment][:4] for i in initial_label_qubits])
initial_label_qubits = reassigned_preds
initial_label_qubits = np.array([i / np.sqrt(i.conj().T @ i) for i in initial_label_qubits])
"""
Construct V: Non-unitary stacking operator
"""
dim_l = initial_label_qubits.shape[1]
dim_lc = dim_l ** (1 + n_copies)
#.shape = n_train, dim_l**n_copies+1
outer_ket_states = initial_label_qubits
for k in range(n_copies):
outer_ket_states = np.array([np.kron(i, j) for i,j in zip(outer_ket_states, initial_label_qubits)])
print('Computing Stacking Matrix!')
V = []
for l in tqdm(possible_labels[:4]):
weighted_outer_states = np.zeros((dim_lc, dim_lc))
for i in tqdm(initial_label_qubits[y_train == l]):
ket = i
for k in range(n_copies):
ket = np.kron(ket, i)
outer = np.outer(ket, ket.conj())
weighted_outer_states += outer
#print('Performing SVD!')
U, S = svd(weighted_outer_states)[:2]
if v_col:
a, b = U.shape
#Truncated to this amount to ensure V is square for polar decomp.
Vl = np.array(U[:, :b//4] @ np.sqrt(np.diag(S)[:b//4, :b//4]))
else:
Vl = np.array(U[:, :1] @ np.sqrt(np.diag(S)[:1, :1])).squeeze()
V.append(Vl)
V = np.array(V)
if v_col:
c, d, e = V.shape
V = V.transpose(0, 2, 1).reshape(c*e, d)
print('Performing Polar Decomposition!')
U = polar(V)[0]
U = U.astype(np.float32)
"""
Add conditionals and apply unitary to correct qubits via cirq (qiskit is slow af)
"""
"""
print('Obtaining Circuit Unitary!')
import cirq
#stacking_qubits = list(np.array([[i,i+1] for i in range(2, (n_copies + 1) * 4, 4)]).flatten())
stacking_qubits = [2,3,6,7,10,11]
control_qubits = [i for i in range((n_copies + 1) * 4) if i not in stacking_qubits]
sq = [cirq.LineQubit(i) for i in stacking_qubits]
#sq = [cirq.LineQubit(i) for i in [0,1,4,5,8,9]]
cq = [cirq.LineQubit(i) for i in control_qubits]
#cq = [cirq.LineQubit(i) for i in [2,3,6,7,10,11]]
U_cirq = cirq.MatrixGate(U).controlled(len(cq))
circuit = cirq.Circuit()
# You can create a circuit by appending to it
circuit.append(cirq.X(q) for q in cq)
circuit.append(U_cirq(*cq, *sq))
circuit.append(cirq.X(q) for q in cq)
print(circuit)
U_circ = cirq.unitary(circuit)
"""
def swap_gate(a,b,n):
M = [np.eye(2, dtype = U.dtype) for _ in range(n)]
result = sparse.eye(2**n, dtype = U.dtype) - sparse.eye(2**n, dtype = U.dtype)
#Same as qiskit convention
#a = n-a-1
#b = n-b-1
for i in [[1,0],[0,1]]:
for j in [[1,0],[0,1]]:
M[a] = np.outer(i,j) #|i><j|
M[b] = np.outer(j,i) #|j><i|
swap_gate = sparse.csr_matrix(M[0])
for m in M[1:]:
swap_gate = sparse.kron(swap_gate, sparse.csr_matrix(m))
result += swap_gate
return result
I = np.eye(4**(n_copies + 1), dtype = U.dtype)
U_circ = sparse.kron(np.outer(I[0],I[0]), U)
for i in tqdm(I[1:]):
U_circ += sparse.kron(sparse.csr_matrix(np.outer(i, i)),sparse.csr_matrix(I))
for i in tqdm(range(1, n_copies+1)):
s_sparse = sparse.csr_matrix(swap_gate(2+4*(i-1), 2+4*(i-1) + 2*n_copies - 2*(i-1), 4 * (n_copies + 1)))
U_circ = s_sparse @ U_circ @ s_sparse
s_sparse = sparse.csr_matrix(swap_gate(2+4*(i-1)+1, 2+4*(i-1) + 2*n_copies - 2*(i-1) + 1, 4 * (n_copies + 1)))
U_circ = s_sparse @ U_circ @ s_sparse
return U_circ.astype(np.float32)
def stacking_on_confusion_matrix(max_copies, dataset = 'fashion_mnist'):
initial_label_qubits = produce_psuedo_sum_states(dataset)
permutation = load_brute_force_permutations(10,dataset)[1]
rearranged_results = []
for row in initial_label_qubits[permutation]:
rearranged_results.append(row[permutation])
rearranged_results = np.array(rearranged_results)
total_results = []
total_results.append(rearranged_results)
f, axarr = plt.subplots(1,max_copies + 2)
axarr[0].imshow(rearranged_results, cmap = "Greys")
axarr[0].set_title('MNIST' + '\n No Stacking')
axarr[0].set_xticks(range(10))
axarr[0].set_yticks(range(10))
axarr[0].set_xticklabels(permutation)
axarr[0].set_yticklabels(permutation)
color = ['black','black','white','black','black']
for i in range(len(rearranged_results)):
for k, j in enumerate(range(-2,3)):
if i + j > -1 and i + j < 10:
axarr[0].text(i,i+j,np.round(rearranged_results[i,i+j],3), color = color[k], ha="center", va="center", fontsize = 6)
initial_label_qubits = np.pad(initial_label_qubits, ((0,0), (0,6)))
for n_copies in range(max_copies + 1):
U = delta_efficent_deterministic_quantum_stacking(n_copies, True, dataset)
"""
Rearrange test data to match new bitstring assignment
"""
outer_ket_states = initial_label_qubits
#.shape = n_train, dim_l**n_copies+1
for k in range(n_copies):
outer_ket_states = [np.kron(i, j) for i,j in zip(outer_ket_states, initial_label_qubits)]
"""
Perform Overlaps
"""
#We want qubit formation:
#|l_0^0>|l_1^0>|l_0^1>|l_1^1> |l_2^0>|l_3^0>|l_2^1>|l_3^1>...
#I.e. act only on first 2 qubits on all copies.
#Since unitary is contructed on first 2 qubits of each copy.
#So we want U @ SWAP @ |copy_preds>
print('Performing Overlaps!')
preds_U = np.array([abs(U.dot(i)) for i in tqdm(outer_ket_states)])
"""
Trace out other qubits/copies
"""
print('Performing Partial Trace!')
preds_U = np.array([np.diag(partial_trace(i, [0,1,2,3])) for i in tqdm(preds_U)])
preds_U = np.array([i / np.sqrt(i.conj().T @ i) for i in preds_U])
rearranged_results = []
for row in preds_U[permutation]:
rearranged_results.append(row[permutation])
rearranged_results = np.array(rearranged_results, dtype = np.float32)
total_results.append(rearranged_results.T)
axarr[n_copies+1].imshow(rearranged_results.T, cmap = "Greys")
axarr[n_copies+1].set_title('MNIST' + f'\n Stacking: Copies = {n_copies+1}')
axarr[n_copies+1].set_xticks(range(10))
axarr[n_copies+1].set_yticks(range(10))
axarr[n_copies+1].set_xticklabels(permutation)
axarr[n_copies+1].set_yticklabels(permutation)
for i in range(len(rearranged_results)):
for k, j in enumerate(range(-2,3)):
if i + j > -1 and i + j < 10:
axarr[n_copies+1].text(i,i+j,np.round(rearranged_results[i,i+j],3), color = color[k], ha="center", va="center", fontsize = 6)
#np.save(dataset + '_stacked_confusion_matrix', total_results)
#f.tight_layout()
#plt.savefig(dataaset + '_stacking_confusion_matrix_results.pdf')
plt.show()
def plot_confusion_matrix(dataset = 'fashion_mnist'):
total_results = np.load('Classifiers/' + dataset + '_stacked_confusion_matrix(copy).npy')
permutation = load_brute_force_permutations(10,dataset)[1]
if dataset == 'fashion_mnist':
dataset = 'FASHION MNIST'
else:
dataset = 'MNIST'
f, axarr = plt.subplots(1,4)
axarr[0].imshow(total_results[0], cmap = "Greys")
axarr[0].set_title('\n No Stacking')
axarr[0].set_xticks(range(10))
axarr[0].set_yticks(range(10))
axarr[0].set_xticklabels(permutation)
axarr[0].set_yticklabels(permutation)
color = ['black','black','white','black','black']
for i in range(len(total_results[0])):
axarr[0].text(i,i,f'{total_results[0][i,i]:.2f}', color = 'white', ha="center", va="center", fontsize = 7, fontweight = 'bold')
for n_copies in range(2 + 1):
axarr[n_copies+1].imshow(total_results[n_copies+1], cmap = "Greys")
axarr[n_copies+1].set_title(f'Stacking:\n Copies = {n_copies+1}')
axarr[n_copies+1].set_xticks(range(10))
axarr[n_copies+1].set_yticks(range(10))
axarr[n_copies+1].set_xticklabels(permutation)
axarr[n_copies+1].set_yticklabels(permutation)
for i in range(len(total_results[0])):
axarr[n_copies+1].text(i,i,f'{total_results[n_copies+1].T[i,i]:.2f}', color = 'white', ha="center", va="center", fontsize = 7, fontweight = 'bold')
#for i in range(len(total_results[n_copies+1])):
# for k, j in enumerate(range(-2,3)):
# if i + j > -1 and i + j < 10:
# axarr[n_copies+1].text(i,i+j,np.round(total_results[n_copies+1].T[i,i+j],3), color = color[k], ha="center", va="center", fontsize = 6)
#np.save(dataset + '_stacked_confusion_matrix', total_results)
f.tight_layout()
plt.suptitle(dataset, y = 0.9)
f.set_size_inches(12.5, 4.5)
plt.savefig(dataset + '_stacking_confusion_matrix_results.pdf')
#plt.savefig('test.pdf')
plt.show()
def mps_stacking(dataset, n_copies):
def generate_copy_state(QTN, n_copies):
initial_QTN = QTN
for _ in range(n_copies):
QTN = QTN | initial_QTN
return relabel_QTN(QTN)
def relabel_QTN(QTN):
qtn_data = []
previous_ind = rand_uuid()
for j, site in enumerate(QTN.tensors):
next_ind = rand_uuid()
tensor = qtn.Tensor(
site.data, inds=(f"k{j}", previous_ind, next_ind), tags=oset([f"{j}"])
)
previous_ind = next_ind
qtn_data.append(tensor)
return qtn.TensorNetwork(qtn_data)
#Upload Data
initial_label_qubits = np.load('Classifiers/' + dataset + '_mixed_sum_states/D_total/ortho_d_final_vs_training_predictions_compressed.npz', allow_pickle = True)['arr_0'][15].astype(np.float32)
y_train = np.load('Classifiers/' + dataset + '_mixed_sum_states/D_total/ortho_d_final_vs_training_predictions_labels.npy').astype(np.float32)
trunc_label_qubits = initial_label_qubits[:100]
trunc_labels = y_train[:100]
#Convert predictions to MPS
mps_predictions = mps_encoding(trunc_label_qubits, 4)
#Add n_copies of same prediction state
copied_predictions = [generate_copy_state(pred,n_copies) for pred in mps_predictions]
#Add bitstring onto copied states
if __name__ == '__main__':
#mps_stacking('mnist',1)
U = delta_efficent_deterministic_quantum_stacking(1, v_col = True, dataset = 'mnist')
#evaluate_stacking_unitary(U, dataset = 'mnist')
#assert()
#U = test(1, dataset = 'fashion_mnist')
#classical_stacking()
#assert()
#plot_confusion_matrix('mnist')
#results = []
#for i in range(1,10):
# print('NUMBER OF COPIES: ',i)
# U = specific_quantum_stacking(i, True)
# training_predictions, test_predictions = evaluate_stacking_unitary(U, True)
# results.append([training_predictions, test_predictions])
# #np.save('partial_stacking_results_2', results)
#stacking_on_confusion_matrix(0, dataset = 'mnist')
#U = delta_efficent_deterministic_quantum_stacking(1, dataset = 'mnist')
#U = sum_state_deterministic_quantum_stacking(2, dataset = 'mnist')