-
Notifications
You must be signed in to change notification settings - Fork 2
/
qevo.py
1359 lines (1273 loc) · 54.9 KB
/
qevo.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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from itertools import product
from functools import reduce
from operator import mul
import itertools
import functools
import random
import sys
import qutip
import numpy as np
import scipy
from scipy import optimize
from scipy import interpolate
import sympy
try:
from IPython import display
import matplotlib
import matplotlib.pyplot as plt
plt.ioff()
_gui = True
except ImportError:
_gui = False
T = qutip.tensor
ψ2ρ = qutip.ket2dm
P = lambda *states: T(*states)*T(*states).dag()
withHC = lambda op: op + op.dag()
π = np.pi
l = qutip.basis(2,0)
h = qutip.basis(2,1)
ll = T(l,l)
lh = T(l,h)
hl = T(h,l)
hh = T(h,h)
A = φp = (ll+hh).unit()
B = ψm = (lh-hl).unit()
C = ψp = (lh+hl).unit()
D = φm = (ll-hh).unit()
toABCD_mat = ll*φp.dag() + lh*ψm.dag() + hl*ψp.dag() + hh*φm.dag()
id2 = qutip.identity(2)
q, epsilon_m, epsilon_g = sympy.symbols('q epsilon_m epsilon_g')
symF = sympy.Symbol('F_0')
eps_p2, eps_η = sympy.symbols('epsilon_p2 epsilon_η')
eps = sympy.Symbol('epsilon')
polyF = sympy.poly(symF, symF, q, epsilon_m, epsilon_g, domain='QQ')
inf_third = sympy.poly(q, symF, q, epsilon_m, epsilon_g, domain='QQ')
inf_meas = sympy.poly(epsilon_m, symF, q, epsilon_m, epsilon_g, domain='QQ')
inf_gate = sympy.poly(epsilon_g, symF, q, epsilon_m, epsilon_g, domain='QQ')
def ψ_to_ABCD_basis(ψ):
return toABCD_mat*ψ
def letter_code_amplitude(ρ, exceptions=True):
letters = 'ABCD'
for _l in itertools.product(letters, repeat=len(ρ.dims[0])//2):
if abs((T(*({'A':A,'B':B,'C':C,'D':D}[_] for _ in _l)).dag()*ρ).tr()) > 0.999:
return ''.join(_l)
else:
if exceptions:
raise Exception('not diagonal in Bell basis')
return None
def letter_map(op, exceptions=True):
res = []
for start in itertools.product('ABCD',repeat=len(op.dims[0])//2):
s = ''.join(start)
e = letter_code_amplitude(op*T(*(globals()[_] for _ in start)),
exceptions=exceptions)
if e is not None:
res.append((s,e))
else:
return None
return res
def letter_to_index_map_assuming_2idempotent(l):
d = {'A':0,'B':1,'C':2,'D':3}
pairs = []
for (l1,l2), (r1,r2) in l:
l,r = ((d[l1],d[l2]), (d[r1],d[r2]))
if l != r and (r, l) not in pairs:
pairs.append((l,r))
return pairs
def cgate_to_permutation_map(cgate):
if len(cgate.dims[0]) == 1:
op = qutip.controlled_gate(cgate, N=4, control=0, target=2)*qutip.controlled_gate(cgate, N=4, control=1, target=3)
if len(cgate.dims[0]) == 2:
...
#op = qutip.tensor(cgate, cgate)
elif len(cgate.dims[0]) == 4:
op = cgate
l = letter_map(op)
i = letter_to_index_map_assuming_2idempotent(l)
return l, i
# CPHASE, CNOT, CPNOT
cphase_gate = l*l.dag() - h*h.dag()
cphase_l, cphase_i = cgate_to_permutation_map(cphase_gate)
cnot_gate = h*l.dag() + l*h.dag()
cnot_l, cnot_i = cgate_to_permutation_map(cnot_gate)
cpnot_gate = h*l.dag() - l*h.dag()
cpnot_l, cpnot_i = cgate_to_permutation_map(cpnot_gate)
# Tools for working with 2-pair permutation lists
r = lambda a: sorted([(l[::-1], r[::-1]) for l,r in a], key=lambda _:_[0])
def _chain_permutations(l1, l2):
d1 = dict(l1)
d2 = dict(l2)
d3 = {k1: d2[v1] for k1,v1 in d1.items()}
return sorted(d1.items(), key=lambda _:_[0])
c = lambda *_: functools.reduce(_chain_permutations, _)
def print_table(*args):
args = list(args)
l = len(args)
for _ in zip(*args):
print(_[0][0], '->', ('%s '*l)%tuple([r for l,r in _]))
# All 1-qubit gates
onequbitgates = list(itertools.permutations([0,1,2,3]))
# Utilities
def get_new_probs(N, F, qs=None):
lF = np.log(F)
if qs is None:
q = (1-F)/3
lq = np.log(q)
lqs = [lq]*3
else:
lqs = [np.log(q) for q in qs]
probs = np.empty((4,)*N)
for i, p in zip(itertools.product(range(4), repeat=N),
itertools.product((lF, *lqs), repeat=N)):
probs[i] = np.exp(sum(p))
return probs
def get_new_probs_sym(N, separate_F = False):
q = inf_third
F = polyF if separate_F else 1-3*q
probs = np.empty((4,)*N, dtype=object)
for i, p in zip(itertools.product(range(4), repeat=N),
itertools.product((F, q, q, q), repeat=N)):
probs[i] = reduce(mul,p)
return probs
def fidelity(probs):
return np.sum(probs[0,...])
def ABCD(probs):
return tuple(np.sum(probs[_,...]) for _ in range(4))
def permute1(probs, target, permutation, N):
slices = [slice(None)]*N
slices[target] = permutation
return probs[tuple(slices)]
def permute2_by_pairs(probs, targets, permutation_pairs, N):
slices_a = [slice(None)]*N
slices_b = [slice(None)]*N
t1, t2 = targets
for (l1, l2), (r1, r2) in permutation_pairs:
slices_a[t1] = l1
slices_a[t2] = l2
slices_b[t1] = r1
slices_b[t2] = r2
tmp_a = np.copy(probs[tuple(slices_a)])
tmp_b = np.copy(probs[tuple(slices_b)])
probs[tuple(slices_a)] = tmp_b
probs[tuple(slices_b)] = tmp_a
return probs
def measure(probs, target, pair, N, F, Mη, qs=None):
Mη = Mη**2 + (1-Mη)**2 # both have to be right or both have to be wrong!
q = [(1-F)/3]*3 if qs is None else qs
slices = [slice(None)]*N
slices[target] = pair
probs_meas = np.sum(probs[tuple(slices)], axis=target)
slices[target] = [_ for _ in range(4) if _ not in pair]
probs_meas_err = np.sum(probs[tuple(slices)], axis=target)
probs_meas = Mη*probs_meas + (1-Mη)*probs_meas_err
probs_meas /= np.sum(probs_meas)
for i, f in zip(range(4),(F,*q)):
slices[target] = i
probs[tuple(slices)] = probs_meas*f
return probs
def measure_sym_notnorm(probs, target, pair, N, separate_F=False):
q = inf_third
F = polyF if separate_F else 1-3*q
slices = [slice(None)]*N
slices[target] = pair
probs_meas = np.sum(probs[tuple(slices)], axis=target)
slices[target] = [_ for _ in range(4) if _ not in pair]
probs_meas_err = np.sum(probs[tuple(slices)], axis=target)
probs_meas = (1-inf_meas)*probs_meas + inf_meas*probs_meas_err
#probs_meas /= np.sum(probs_meas)
#probs_meas = np.vectorize(sympy.cancel)(probs_meas)
for i, f in zip(range(4),(F,q,q,q)):
slices[target] = i
probs[tuple(slices)] = probs_meas*f
return probs
def measure_succ_prob(probs, target, pair, N, F, Mη):
Mη = Mη**2 + (1-Mη)**2 # both have to be right or both have to be wrong!
q = (1-F)/3
slices = [slice(None)]*N
slices[target] = pair
probs_meas = np.sum(probs[tuple(slices)], axis=target)
slices[target] = [_ for _ in range(4) if _ not in pair]
probs_meas_err = np.sum(probs[tuple(slices)], axis=target)
probs_meas = Mη*probs_meas + (1-Mη)*probs_meas_err
return np.sum(probs_meas)
def depolarize2(probs, targets, p):
p = p**2 # depolarizing one of the pairs depolarizes the other pair!
probs_ptrace = 1/4**2*np.sum(np.sum(probs, axis=targets[0], keepdims=True),
axis=targets[1], keepdims=True)
return p*probs + (1-p)*probs_ptrace
sixteenth = 1/sympy.sympify(16)
def depolarize2_sym(probs, targets, p):
probs_ptrace = np.sum(np.sum(probs, axis=targets[0], keepdims=True),
axis=targets[1], keepdims=True)*sixteenth
probs = (1-inf_gate)*probs + inf_gate*probs_ptrace
return probs
class Operation:
def apply(self, probs):
raise NotImplementedError
def apply_perf(self, probs):
raise NotImplementedError
@classmethod
def random(cls, N, F, P2, Mη, qs=None): # TODO XXX many of the arguments are not actually used - something like a **kwargs might be in order.
raise NotImplementedError
def mutate(self):
raise NotImplementedError
def strs(self):
raise NotImplementedError
def copy(self):
raise NotImplementedError
def succ_prob(self, probs):
return 1.
def rearrange(self, permutation_dict):
raise NotImplementedError
def __repr__(self):
return str(self)
class Permutation(Operation):
def __init__(self, target, permutation, N):
self.permutation = permutation
self.target = target
self.N = N
def __eq__(self, other):
return type(other) is Permutation and self.target == other.target and self.permutation == other.permutaion
def __hash__(self):
return hash((Permutation, self.target, tuple(self.permutation), self.N))
def strs(self):
return ['-%s-'%(''.join(map(str,self.permutation)))
if _ == self.target else '------'
for _ in range(self.N)]
def __str__(self):
return 'Permutation(%s, %s, %s)'%(self.target, self.permutation, self.N)
def apply(self, probs):
return permute1(probs, self.target, self.permutation, self.N)
@classmethod
def random(cls, N, F, P2, Mη, qs=None):
return cls(random.randint(0,N-1),
random.choice(onequbitgates))
def mutate(self):
return Permutation(self.target, random.choice(onequbitgates), self.N)
def copy(self, F=None, P2=None, Mη=None):
return Permutation(self.target, self.permutation, self.N)
@property
def targets(self):
return (self.target,)
def rearrange(self, permutation_dict):
r = self.copy()
r.target = permutation_dict[r.target]
return r
@property
def args(self):
return self.target, self.permutation, self.N
class CNOT(Operation):
def __init__(self, targets, N, P2):
self.targets = list(targets)
self.N = N
self.P2 = P2
def __eq__(self, other):
return type(other) is CNOT and self.targets == other.targets
def __hash__(self):
return hash((CNOT, tuple(self.targets), self.N))
def strs(self):
return ['-o-' if _ == self.targets[0]
else '-X-' if _ == self.targets[1]
else '-|-' if min(self.targets)<_<max(self.targets)
else '---'
for _ in range(self.N)]
def __str__(self):
return 'CNOT(%s, %s, %s)'%(self.targets, self.N, self.P2)
def apply(self, probs):
return depolarize2(permute2_by_pairs(probs, self.targets, cnot_i, self.N),
self.targets, self.P2)
def apply_sym(self, probs):
return depolarize2_sym(permute2_by_pairs(probs, self.targets, cnot_i, self.N),
self.targets, self.P2)
@classmethod
def random(cls, N, F, P2, Mη, qs=None):
return cls(random.sample(range(N),2), N, P2)
def mutate(self):
return random.choice([_(self.targets, self.N, self.P2) for _ in [CPHASE, CPNOT]])
def copy(self, F=None, P2=None, Mη=None):
return CNOT(self.targets.copy(), self.N,
P2=P2 if P2 else self.P2)
def rearrange(self, permutation_dict):
r = self.copy()
r.targets[0] = permutation_dict[r.targets[0]]
r.targets[1] = permutation_dict[r.targets[1]]
return r
@property
def args(self):
return self.targets, self.N, self.P2
class CPHASE(Operation):
def __init__(self, targets, N, P2):
self.targets = sorted(targets)
self.N = N
self.P2 = P2
def __eq__(self, other):
return type(other) is CPHASE and sorted(self.targets) == sorted(other.targets)
def __hash__(self):
return hash((CPHASE, tuple(sorted(self.targets)), self.N))
def strs(self):
return ['-o-' if _ == self.targets[0]
else '-Z-' if _ == self.targets[1]
else '-|-' if min(self.targets)<_<max(self.targets)
else '---'
for _ in range(self.N)]
def __str__(self):
return 'CPHASE(%s, %s, %s)'%(self.targets, self.N, self.P2)
def apply(self, probs):
return depolarize2(permute2_by_pairs(probs, self.targets, cphase_i, N=self.N),
self.targets, self.P2)
def apply_sym(self, probs):
return depolarize2_sym(permute2_by_pairs(probs, self.targets, cphase_i, self.N),
self.targets, self.P2)
@classmethod
def random(cls, N, F, P2, Mη, qs=None):
return cls(random.sample(range(N),2), N, P2)
def mutate(self):
return random.choice([_(self.targets, self.N, self.P2) for _ in [CNOT, CPNOT]])
def copy(self, F=None, P2=None, Mη=None):
return CPHASE(self.targets.copy(), self.N,
P2=P2 if P2 else self.P2)
def rearrange(self, permutation_dict):
r = self.copy()
r.targets[0] = permutation_dict[r.targets[0]]
r.targets[1] = permutation_dict[r.targets[1]]
r.targets.sort()
return r
@property
def args(self):
return self.targets, self.N, self.P2
class CPNOT(Operation):
def __init__(self, targets, N, P2):
self.targets = list(targets)
self.N = N
self.P2 = P2
def __eq__(self, other):
return type(other) is CPNOT and self.targets == other.targets
def __hash__(self):
return hash((CPNOT, tuple(self.targets), self.N))
def strs(self):
return ['-o-' if _ == self.targets[0]
else '-Y-' if _ == self.targets[1]
else '-|-' if min(self.targets)<_<max(self.targets)
else '---'
for _ in range(self.N)]
def __str__(self):
return 'CPNOT(%s, %s, %s)'%(self.targets, self.N, self.P2)
def apply(self, probs):
return depolarize2(permute2_by_pairs(probs, self.targets, cpnot_i, self.N),
self.targets, self.P2)
def apply_sym(self, probs):
return depolarize2_sym(permute2_by_pairs(probs, self.targets, cpnot_i, self.N),
self.targets, self.P2)
@classmethod
def random(cls, N, F, P2, Mη, qs=None):
return cls(random.sample(range(N),2), N, P2)
def mutate(self):
return random.choice([_(self.targets, self.N, self.P2) for _ in [CPHASE, CNOT]])
def copy(self, F=None, P2=None, Mη=None):
return CPNOT(self.targets.copy(), self.N,
P2=P2 if P2 else self.P2)
def rearrange(self, permutation_dict):
r = self.copy()
r.targets[0] = permutation_dict[r.targets[0]]
r.targets[1] = permutation_dict[r.targets[1]]
return r
@property
def args(self):
return self.targets, self.N, self.P2
class CNOTPerm(Operation):
def __init__(self, targets, permcontrol, permtarget, N, P2):
self.targets = sorted(targets)
self.N = N
self.P2 = P2
self.permcontrol = tuple(permcontrol)
self.permtarget = tuple(permtarget)
def __eq__(self, other):
return type(other) is CNOTPerm and self.targets == other.targets and self.permcontrol == other.permcontrol and self.permtarget == self.permtarget
def __hash__(self):
return hash((CNOTPerm, tuple(self.targets), self.permcontrol, self.permtarget, self.N))
def strs(self):
return ['*o-' if _ == self.targets[0]
else '*X-' if _ == self.targets[1]
else '-|-' if min(self.targets)<_<max(self.targets)
else '---'
for _ in range(self.N)]
def __str__(self):
return 'CNOTPerm(%s, %s, %s, %s, %s)'%(self.targets, self.permcontrol, self.permtarget, self.N, self.P2)
def apply(self, probs):
slices = [slice(None)]*self.N
slices[self.targets[0]] = self.permcontrol
probs[...] = probs[slices]
slices = [slice(None)]*self.N
slices[self.targets[1]] = self.permtarget
probs[...] = probs[slices]
return depolarize2(permute2_by_pairs(probs, self.targets, cnot_i, self.N),
self.targets, self.P2)
def apply_sym(self, probs):
slices = [slice(None)]*self.N
slices[self.targets[0]] = self.permcontrol
probs[...] = probs[slices]
slices = [slice(None)]*self.N
slices[self.targets[1]] = self.permtarget
probs[...] = probs[slices]
return depolarize2_sym(permute2_by_pairs(probs, self.targets, cnot_i, self.N),
self.targets, self.P2)
@classmethod
def random(cls, N, F, P2, Mη, qs=None):
pc = [1,2,3]
pt = [1,2,3]
random.shuffle(pc)
random.shuffle(pt)
pc = [0]+pc
pt = [0]+pt
return cls(random.sample(range(N),2), tuple(pc), tuple(pt), N, P2)
def mutate(self):
pc = [1,2,3]
pt = [1,2,3]
random.shuffle(pc)
random.shuffle(pt)
pc = [0]+pc
pt = [0]+pt
c = self.copy()
c.permcontrol = pc
c.permtarget = pt
return c
def copy(self, F=None, P2=None, Mη=None):
return CNOTPerm(self.targets.copy(), self.permcontrol, self.permtarget, self.N,
P2=P2 if P2 else self.P2)
def rearrange(self, permutation_dict): # XXX breaks hash
r = self.copy()
r.targets[0] = permutation_dict[r.targets[0]]
r.targets[1] = permutation_dict[r.targets[1]]
return r
@property
def args(self):
return self.targets, self.permcontrol, self.permtarget, self.N, self.P2
class Measurement(Operation):
def __init__(self, target, pair, N, F, Mη, qs=None):
self.target = target
self.pair = sorted(pair)
self.N = N
self.F = F
self.Mη = Mη
self.qs = qs
def __eq__(self, other):
return type(other) is Measurement and self.target == other.target and self.pair == other.pair
def __hash__(self):
return hash((Measurement, self.target, tuple(self.pair), self.N))
def strs(self):
return ['-D%s >'%(''.join(map(str,self.pair)))
if _ == self.target else '------'
for _ in range(self.N)]
def __str__(self):
return 'Measurement(%s, %s, %s, %s, %s, %s)'%(self.target, self.pair,
self.N, self.F, self.Mη,
self.qs)
def apply(self, probs):
return measure(probs, self.target, self.pair, self.N, self.F, self.Mη, qs=self.qs)
def apply_sym(self, probs, separate_F=False):
return measure_sym_notnorm(probs, self.target, self.pair, self.N, separate_F=separate_F)
def succ_prob(self, probs):
return measure_succ_prob(probs, self.target, self.pair, self.N, self.F, self.Mη)
@classmethod
def random(cls, N, F, P2, Mη, qs=None):
return cls(random.randint(0,N-1),
random.choice([[0,1], [0,2], [0,3], [1,2], [1,3], [2,3]]),
N, F, Mη, qs)
def mutate(self):
return Measurement(self.target, random.choice([[0,1], [0,2], [0,3], [1,2], [1,3], [2,3]]),
self.N, self.F, self.Mη, self.qs)
def copy(self, F=None, P2=None, Mη=None, qs=None):
return Measurement(self.target, self.pair.copy(), self.N,
F=F if F else self.F,
Mη=Mη if Mη else self.Mη,
qs=qs if qs else self.qs)
@property
def targets(self):
return (self.target,)
def rearrange(self, permutation_dict):
r = self.copy()
r.target = permutation_dict[r.target]
return r
@property
def args(self):
return self.target, self.pair, self.N, self.F, self.Mη
class AMeasurement(Measurement):
def __init__(self, target, other, N, F, Mη, qs=None):
self.target = target
self.other = other
self.N = N
self.F = F
self.Mη = Mη
self.qs = qs
@property
def pair(self):
return [0, self.other]
def __eq__(self, other):
return type(other) is AMeasurement and self.target == other.target and self.other == other.other
def __hash__(self):
return hash((AMeasurement, self.target, self.other, self.N))
def __str__(self):
return 'AMeasurement(%s, %s, %s, %s, %s, %s)'%(self.target, self.other,
self.N, self.F, self.Mη,
self.qs)
@classmethod
def random(cls, N, F, P2, Mη, qs=None):
return cls(random.randint(0,N-1),
random.choice([1,2,3]),
N, F, Mη, qs)
def mutate(self):
return AMeasurement(self.target, random.choice([1,2,3]),
self.N, self.F, self.Mη, self.qs)
def copy(self, F=None, P2=None, Mη=None, qs=None):
return AMeasurement(self.target, self.other, self.N,
F=F if F else self.F,
Mη=Mη if Mη else self.Mη,
qs=qs if qs else self.qs)
@property
def args(self):
return self.target, self.other, self.N, self.F, self.Mη
from enum import Enum
class History(Enum):
manual = 0
survivor = 1
random = 2
child = 3
drop_m = 4
gain_m = 5
swap_m = 6
ops_m = 7
class Individual:
def __init__(self,ops,F,history=History.manual, weights=(1,0,0,0,0),qs=None): # qs is not particularly well tested
self.ops = ops
self._fitness = None
self._fidelity_and_succ_prob = None
self.F = F
self.history = history
self.weights = weights
self.qs = qs
def __str__(self):
return '\n'.join(''.join(__) for __ in zip(*(_.strs() for _ in self.ops)))
def __repr__(self):
return 'Individual(F=%s, history=History.%s, weights=%s, ops=[\n %s])'%(
self.F, self.history.name, self.weights,
',\n '.join(str(_) for _ in self.ops))
def __eq__(self, other):
return other.ops == self.ops
def __hash__(self):
return hash(tuple(self.ops))
def montecarlo_chain(self):
probs = get_new_probs(self.N, self.F, qs=self.qs)
success_probabilities = []
test_steps = []
back_to_index = []
reset_raw_pairs = []
for i,o in enumerate(self.ops):
p = o.succ_prob(probs)
probs = o.apply(probs)
if p != 1.:
success_probabilities.append(p)
test_steps.append(i)
back_to_index.append(0) # XXX Fix this... This is worst case! It might be better in your case!
reset_raw_pairs.append(self.N) # XXX Same issue...
return success_probabilities, test_steps, back_to_index, reset_raw_pairs
def montecarlo_resources(self, runs=1000, custom_chain=None):
if custom_chain:
success_probabilities, test_steps, back_to_index, reset_raw_pairs = custom_chain
else:
success_probabilities, test_steps, back_to_index, reset_raw_pairs = self.montecarlo_chain()
N_tests = len(success_probabilities)
delta_steps = [test_steps[0]] + [n-p for n,p in zip(test_steps[1:], test_steps[:-1])]
results = np.zeros((runs,2),dtype=int)
for run in range(runs):
raw_pairs = self.N
steps = 1
current = 0
while current < N_tests:
if random.random() < success_probabilities[current]:
raw_pairs += 1 if current < N_tests-self.N+1 else 0
steps += delta_steps[current]
current += 1
else:
raw_pairs += reset_raw_pairs[current]
steps += 1 + delta_steps[current]
current = back_to_index[current]
results[run,0] = raw_pairs
results[run,1] = steps
return results
def fidelity_and_succ_prob(self, reval=False):
if reval or not self._fidelity_and_succ_prob:
if not self.ops:
self._probs = 1.
self._fidelity_and_succ_prob = self.F, 1.
self._ABCD = np.array([self.F]+[(1-self.F)/3]*3)
return self.F, 1.
p = 1.
probs = get_new_probs(self.N, self.F, qs=self.qs)
for o in self.ops:
p *= o.succ_prob(probs)
probs = o.apply(probs)
self._probs = probs
self._fidelity_and_succ_prob = fidelity(probs), p
self._ABCD = ABCD(probs)
return self._fidelity_and_succ_prob
def ABCD_sym_notnorm(self, progress=True, separate_F=False):
probs = get_new_probs_sym(self.N, separate_F)
for i,o in enumerate(self.ops):
if progress: print('\r %d/%d'%(i+1, len(self.ops)), end='', flush=True)
probs = o.apply_sym(probs, separate_F=separate_F) if isinstance(o, Measurement) else o.apply_sym(probs)
if progress: print('\rdone',flush=True)
return ABCD(probs)
def fidelity(self):
return self.fidelity_and_succ_prob()[0]
def succ_prob(self):
return self.fidelity_and_succ_prob()[1]
def ABCD(self):
self.fidelity_and_succ_prob()
return self._ABCD
def probs(self):
self.fidelity_and_succ_prob()
return self._probs
def fitness(self):
if not self._fitness:
if self.weights == 'yield':
self._fitness = self.hashing_yield()
else:
f, p = self.fidelity_and_succ_prob()
a,b,c,d = self.ABCD()
try:
wf, wb, wc, wd, wp = self.weights
self._fitness = wf*f + wb*b + wc*c + wd*d + wp*p
except ValueError:
wf, wp = self.weights
self._fitness = wf*f + wp*p
return self._fitness
def hashing_yield(self):
f,p = self.fidelity_and_succ_prob()
abcd = self.ABCD()
n = self.raw_pairs()
return p/n*(1+np.sum(np.log2(abcd)*abcd))
@classmethod
def random(cls, ops_len, permitted_ops, N, F, P2, Mη, weights, qs=None):
return cls([random.choice(permitted_ops).random(N, F, P2, Mη, qs=qs) for _ in range(ops_len)], F,
history=History.random, weights=weights, qs=qs)
def new_drop_op(self):
ops = self.ops.copy()
del ops[random.randint(0,len(ops)-1)]
return Individual(ops, self.F, history=History.drop_m, weights=self.weights)
def new_gain_op(self, permitted_ops, N, F, P2, Mη):
ops = self.ops.copy()
ops.insert(random.randint(0,len(ops)-1) if ops else 0, random.choice(permitted_ops).random(N, F, P2, Mη))
return Individual(ops, self.F, history=History.gain_m,
weights=self.weights, qs=self.qs)
def new_swap_op(self):
ops = self.ops.copy()
i = random.randint(0,len(ops)-2)
ops[i] = self.ops[i+1]
ops[i+1] = self.ops[i]
return Individual(ops, self.F, history=History.swap_m,
weights=self.weights, qs=self.qs)
def new_mutate(self, mutation_chance):
return Individual([_.mutate() if random.random() < mutation_chance
else _
for _ in self.ops],
self.F,
history=History.ops_m,
weights=self.weights,
qs=self.qs)
def new_child(self, p):
ops1, ops2 = [self.ops, p.ops][::random.choice([1,-1])]
i1, i2 = random.randint(0,len(ops1)), random.randint(0,len(ops2))
return Individual(ops1[:i1]+ops2[i2:], self.F, history=History.child,
weights=self.weights, qs=self.qs)
def copy(self, F=None, P2=None, Mη=None, history=None, weights=None, qs=None):
return Individual([_.copy(F=F, P2=P2, Mη=Mη) for _ in self.ops],
F=F if F else self.F,
history=history if history else self.history,
weights=weights if weights else self.weights,
qs=qs if qs else self.qs)
@property
def N(self):
try:
return self.ops[0].N
except IndexError:
return 0
def rearrange(self, permutation_dict):
new_ops = [_.rearrange(permutation_dict) for _ in self.ops]
c = self.copy()
c.ops = new_ops
return c
def canonical(self):
assert assert_is_good(self.ops)
cnotperm = False
if any(isinstance(_, CNOTPerm) for _ in self.ops):
cnotperm = True
if not cnotperm:
permutation_dict = {}
done = set()
for _ in (_ for _ in self.ops[::-1] if isinstance(_, Measurement)):
if _.target in done:
continue
done.add(_.target)
permutation_dict[len(done)] = _.target
if len(done) == self.N-1:
break
permutation_dict[0] = 0
assert len(set(permutation_dict.values())) == self.N
reordered = self.rearrange(permutation_dict)
else:
reordered = self.copy()
last_meas = sorted(reordered.ops[-self.N+1:], key=lambda _:_.target)
reordered.ops = reordered.ops[:-self.N+1] + last_meas
def measurement_before_gate(ops, start):
for i, (measurement, next_gate) in enumerate(zip(ops[start:-1], ops[start+1:])):
if (isinstance(measurement, Measurement)
and not isinstance(next_gate, Measurement)
and measurement.target not in next_gate.targets):
return i+start
return 0
i = measurement_before_gate(reordered.ops, 0)
while i:
reordered.ops[i], reordered.ops[i+1] = reordered.ops[i+1], reordered.ops[i]
i = measurement_before_gate(reordered.ops, i)
def higher_gate_before_lower_gate(ops):
for i, (first_gate, next_gate) in enumerate(zip(ops[:-1], ops[1:])):
if (not isinstance(first_gate, Measurement)
and not isinstance(next_gate, Measurement)
and not set(first_gate.targets).intersection(next_gate.targets)
and sorted(first_gate.targets) > sorted(next_gate.targets)):
return i
return len(ops)
i = higher_gate_before_lower_gate(reordered.ops)
while i<reordered.N:
reordered.ops[i], reordered.ops[i+1] = reordered.ops[i+1], reordered.ops[i]
i = higher_gate_before_lower_gate(reordered.ops)
reordered.fidelity_and_succ_prob(reval=True)
assert np.sum(np.abs(reordered.probs() - self.probs())) < 1e-10
return reordered
def raw_pairs(self):
return len([_ for _ in self.ops if isinstance(_, Measurement)]) + 1
def len_parallel(self):
backstep = 0
last_ops = []
for i,o in enumerate(self.ops):
if last_ops and not set.intersection(set(o.targets), set.union(*(set(_.targets) for _ in last_ops))):
backstep += 1
last_ops.append(o)
else:
last_ops = [o]
return len(self.ops) - backstep
def len_nosingle(self):
return len([_ for _ in self.ops if not isinstance(_,Permutation)])
def individual2plot(ops,title='',fig_axis=None,shading=False,parallelism=False):
if isinstance(ops, Individual):
ops = ops.ops
N = ops[0].N
L = len(ops)
if fig_axis:
f, s = fig_axis
else:
f = plt.figure(facecolor='white')
s = f.add_subplot(111)
f.set_size_inches((L+1),(N+1))
s.clear()
s.axis('off')
z_shade = 0
z_qubit = 10
z_gate = 20
if shading:
shades_color = 'rgbcym'
shades = list(range(N))
for t, shade in enumerate(shades):
s.add_artist(matplotlib.patches.Rectangle((0,t+0.5),0.5,1,
color=shades_color[shade],zorder=0,alpha=0.15))
if not ops:
return f
backstep = 0
last_ops = []
for i,o in enumerate(ops):
if parallelism and last_ops and not set.intersection(set(o.targets), set.union(*(set(_.targets) for _ in last_ops))):
backstep += 1
last_ops.append(o)
else:
last_ops = [o]
i -= backstep
s.add_artist(matplotlib.patches.Rectangle((i+0.5,0),1,N+1,
color='white',zorder=z_shade))
if isinstance(o,(CNOT, CNOTPerm)):
s.add_artist(matplotlib.patches.Circle((i+1,o.targets[0]+1),0.07,
color='black',lw=2,zorder=z_gate))
s.add_artist(matplotlib.patches.Circle((i+1,o.targets[1]+1),0.3,
facecolor='white',edgecolor='black',lw=2,zorder=z_gate))
s.add_artist(matplotlib.lines.Line2D([i+1-0.3,i+1+0.3],
[o.targets[1]+1,o.targets[1]+1],
color='black',lw=2,zorder=z_gate))
s.add_artist(matplotlib.lines.Line2D([i+1,i+1],
[o.targets[1]+1-0.3,o.targets[1]+1+0.3],
color='black',lw=2,zorder=z_gate))
sign = +1 if o.targets[0]>o.targets[1] else -1
s.add_artist(matplotlib.lines.Line2D([i+1,i+1],[o.targets[0]+1,o.targets[1]+1+sign*0.3],
color='black',lw=2,zorder=z_gate))
if isinstance(o, CNOTPerm):
s.text(i+0.6,o.targets[0]+1,'\n'.join('ABCD'[_] for _ in o.permcontrol[1:]),fontsize=12,horizontalalignment='center',verticalalignment='center',zorder=z_gate+1)
s.text(i+0.6,o.targets[1]+1,'\n'.join('ABCD'[_] for _ in o.permtarget[1:]),fontsize=12,horizontalalignment='center',verticalalignment='center',zorder=z_gate+1)
s.add_artist(matplotlib.patches.Circle((i+1,o.targets[1]+1),0.25,
facecolor='white',edgecolor='black',lw=2,zorder=z_gate))
elif isinstance(o,CPHASE):
s.add_artist(matplotlib.patches.Circle((i+1,o.targets[0]+1),0.07,
color='black',lw=2,zorder=z_gate))
s.add_artist(matplotlib.patches.Circle((i+1,o.targets[1]+1),0.07,
color='black',lw=2,zorder=z_gate))
s.add_artist(matplotlib.lines.Line2D([i+1,i+1],[o.targets[0]+1,o.targets[1]+1],
color='black',lw=2,zorder=z_gate))
elif isinstance(o,CPNOT):
s.add_artist(matplotlib.patches.Circle((i+1,o.targets[0]+1),0.07,
color='black',lw=2,zorder=z_gate))
s.add_artist(matplotlib.patches.Circle((i+1,o.targets[1]+1),0.3,
facecolor='white',edgecolor='black',lw=2))
s.add_artist(matplotlib.patches.Circle((i+1,o.targets[1]+1),0.1,
color='black',lw=2,zorder=z_gate))
s.add_artist(matplotlib.lines.Line2D([i+1-0.3,i+1+0.3],
[o.targets[1]+1,o.targets[1]+1],
color='black',lw=2,zorder=z_gate))
s.add_artist(matplotlib.lines.Line2D([i+1,i+1],
[o.targets[1]+1-0.3,o.targets[1]+1+0.3],
color='black',lw=2,zorder=z_gate))
sign = +1 if o.targets[0]>o.targets[1] else -1
s.add_artist(matplotlib.lines.Line2D([i+1,i+1],[o.targets[0]+1,o.targets[1]+1+sign*0.3],
color='black',lw=2,zorder=z_gate))
elif isinstance(o, Measurement):
t = o.target+1
s.add_artist(matplotlib.patches.Polygon([(i+0.5,t),(i+0.6,t+0.3),(i+1.3,t+0.3),(i+1.3,t-0.3),(i+0.6,t-0.3)],
facecolor='white',lw=2,edgecolor='black',zorder=z_gate))
if i+backstep < L-N+1:
s.add_artist(matplotlib.patches.Circle((i+1.45,t),0.09,facecolor='white',edgecolor='black',lw=2,zorder=z_gate))
text = {(0,1):'antiY', (0,2):'coinX', (0,3):'coinZ'}.get(tuple(o.pair),
'ABCD'[o.pair[0]]+'ABCD'[o.pair[1]])
s.text(i+0.94,t,text,fontsize=14,horizontalalignment='center',verticalalignment='center',zorder=z_gate+1)
else:
#raise NotImplementedError
pass
if shading:
if not isinstance(o,Measurement):
shade = min(shades[_] for _ in o.targets)
to_be_shaded = []
for t in set(o.targets) - {shade}:
while True:
to_be_shaded.append(t)
if t == shades[t]:
break
t = shades[t]
for t in to_be_shaded:
shades[t] = shade
else:
if shades[o.target] != o.target:
shades[o.target] = o.target
else:
to_reset = [t for t, shade in enumerate(shades) if shade==shades[o.target] and t != o.target]
shade = min(to_reset)
for t in to_reset:
shades[t] = shade
for t, shade in enumerate(shades):
s.add_artist(matplotlib.patches.Rectangle((i+0.49,t+0.49),0.98,0.98,
facecolor=shades_color[shade],edgecolor='black',zorder=z_shade,alpha=0.15,lw=0.3))
for i in range(N):
s.add_artist(matplotlib.lines.Line2D([0,L+1-backstep],[i+1,i+1],
color='black',linewidth=2,zorder=1))
s.add_artist(matplotlib.patches.Circle((0+0.045,i+1),0.09,
facecolor='white',edgecolor='black',lw=2,zorder=z_gate))
s.text(0,0.5,title,fontsize=15)
s.set_xlim(0,L+1)
s.set_ylim(0,N+1)
s.invert_yaxis()
return f
def translate_individual(self, trans_dict_cgates, trans_dict_measurements):
ind = self.copy()
new_ops = []
for o in ind.ops:
if isinstance(o, (CNOT, CPHASE, CPNOT)):
new_ops.append(trans_dict_cgates[type(o)](*o.args))
elif isinstance(o, Measurement):
new_o = Measurement(o.target, trans_dict_measurements[tuple(o.pair)], o.N, o.F, o.Mη)
new_ops.append(new_o)
else:
raise NotImplementedError
ind.ops = new_ops
return ind
class Population:
def __init__(self,
N,
F,
P2,
Mη,
WEIGHTS,
POPULATION_SIZE,
STARTING_POP_MULTIPLIER,
MAX_GEN,
MAX_OPS,
STARTING_OPS,
PERMITTED_OPS,
PAIRS,
CHILDREN_PER_PAIR,
MUTANTS_PER_INDIVIDUAL_PER_TYPE,
P_SINGLE_OPERATION_MUTATES,
P_LOSE_OPERATION,
P_ADD_OPERATION,
P_SWAP_OPERATIONS,
P_MUTATE_OPERATIONS):
self.N = N
self.F = F
self.P2 = P2
self.Mη = Mη
self.WEIGHTS = WEIGHTS
self.POPULATION_SIZE, = POPULATION_SIZE,
self.STARTING_POP_MULTIPLIER = STARTING_POP_MULTIPLIER
self.MAX_GEN = MAX_GEN
self.MAX_OPS = MAX_OPS
self.STARTING_OPS = STARTING_OPS
self.PERMITTED_OPS = PERMITTED_OPS
self.PAIRS = PAIRS
self.CHILDREN_PER_PAIR = CHILDREN_PER_PAIR
self.MUTANTS_PER_INDIVIDUAL_PER_TYPE = MUTANTS_PER_INDIVIDUAL_PER_TYPE
self.P_SINGLE_OPERATION_MUTATES = P_SINGLE_OPERATION_MUTATES
self.P_LOSE_OPERATION = P_LOSE_OPERATION
self.P_ADD_OPERATION = P_ADD_OPERATION
self.P_SWAP_OPERATIONS = P_SWAP_OPERATIONS
self.P_MUTATE_OPERATIONS = P_MUTATE_OPERATIONS
print('Initializing %d individuals. Keeping only %d of them.'%(
self.POPULATION_SIZE*self.STARTING_POP_MULTIPLIER, self.POPULATION_SIZE),
flush=True)
self.l = [Individual.random(self.STARTING_OPS, self.PERMITTED_OPS,
self.N, self.F, self.P2, self.Mη,
self.WEIGHTS)
for _ in range(self.POPULATION_SIZE*self.STARTING_POP_MULTIPLIER)]
self._sort()
self._cull()
self.generations = 0
self.besthistory = np.zeros(self.MAX_GEN)
self.worsthistory = np.zeros(self.MAX_GEN)
self.lenhistory = np.zeros(self.MAX_GEN, dtype=int)
self.selhistory = np.zeros((self.MAX_GEN,len(History)), dtype=int)
self._record_stats()