/
optools.py
2847 lines (2333 loc) · 117 KB
/
optools.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
""" Utility functions operating on operation matrices """
from __future__ import division, print_function, absolute_import, unicode_literals
#*****************************************************************
# pyGSTi 0.9: Copyright 2015 Sandia Corporation
# This Software is released under the GPL license detailed
# in the file "license.txt" in the top-level pyGSTi directory
#*****************************************************************
import numpy as _np
import scipy.linalg as _spl
import scipy.sparse as _sps
import scipy.sparse.linalg as _spsl
import warnings as _warnings
import collections as _collections
from . import jamiolkowski as _jam
from . import matrixtools as _mt
from . import lindbladtools as _lt
from . import compattools as _compat
from . import basistools as _bt
from ..baseobjs import Basis as _Basis
from ..baseobjs import ExplicitBasis as _ExplicitBasis
from ..baseobjs import DirectSumBasis as _DirectSumBasis
from ..baseobjs.basis import basis_matrices as _basis_matrices
IMAG_TOL = 1e-7 #tolerance for imaginary part being considered zero
def _flat_mut_blks(i,j,blockDims):
# like _mut(i,j,dim).flatten() but works with basis *blocks*
N = sum(blockDims)
mx = _np.zeros( (N,N), 'd'); mx[i,j] = 1.0
ret = _np.zeros(sum([d**2 for d in blockDims]), 'd')
i = 0; off=0
for d in blockDims:
ret[i:i+d**2] = mx[off:off+d, off:off+d].flatten()
i += d**2; off += d
return ret
def _hack_sqrtm(A):
sqrt,_ = _spl.sqrtm(A, disp=False) #Travis found this scipy function
# to be incorrect in certain cases (we need a workaround)
if _np.any(_np.isnan(sqrt)): #this is sometimes a good fallback when sqrtm doesn't work.
ev,U = _np.linalg.eig(A)
sqrt = _np.dot( U, _np.dot( _np.diag(_np.sqrt(ev)), _np.linalg.inv(U)))
return sqrt
def fidelity(A, B):
"""
Returns the quantum state fidelity between density
matrices A and B given by :
F = Tr( sqrt{ sqrt(A) * B * sqrt(A) } )^2
To compute process fidelity, pass this function the
Choi matrices of the two processes, or just call
:function:`entanglement_fidelity` with the operation matrices.
Parameters
----------
A : numpy array
First density matrix.
B : numpy array
Second density matrix.
Returns
-------
float
The resulting fidelity.
"""
evals,U = _np.linalg.eig(A)
if len([ev for ev in evals if abs(ev) > 1e-8]) == 1: #special case when A is rank 1
ivec = _np.argmax(evals)
vec = U[:,ivec:(ivec+1)]
F = _np.dot( _np.conjugate(_np.transpose(vec)), _np.dot(B, vec) ).real # vec^T * B * vec
return float(F)
evals,U = _np.linalg.eig(B)
if len([ev for ev in evals if abs(ev) > 1e-8]) == 1: #special case when B is rank 1 (recally fidelity is sym in args)
ivec = _np.argmax(evals)
vec = U[:,ivec:(ivec+1)]
F = _np.dot( _np.conjugate(_np.transpose(vec)), _np.dot(A, vec) ).real # vec^T * A * vec
return float(F)
sqrtA = _hack_sqrtm(A) #_spl.sqrtm(A)
assert( _np.linalg.norm( _np.dot(sqrtA,sqrtA) - A ) < 1e-8 ) #test the scipy sqrtm function
F = (_mt.trace( _hack_sqrtm( _np.dot( sqrtA, _np.dot(B, sqrtA) ) ) ).real)**2 # Tr( sqrt{ sqrt(A) * B * sqrt(A) } )^2
return float(F)
def frobeniusdist(A, B):
"""
Returns the frobenius distance between gate
or density matrices A and B given by :
sqrt( sum( (A_ij-B_ij)^2 ) )
Parameters
----------
A : numpy array
First matrix.
B : numpy array
Second matrix.
Returns
-------
float
The resulting frobenius distance.
"""
return _mt.frobeniusnorm(A-B)
def frobeniusdist2(A, B):
"""
Returns the square of the frobenius distance between gate
or density matrices A and B given by :
sum( (A_ij-B_ij)^2 )
Parameters
----------
A : numpy array
First matrix.
B : numpy array
Second matrix.
Returns
-------
float
The resulting frobenius distance.
"""
return _mt.frobeniusnorm2(A-B)
def residuals(A, B):
"""
Calculate residuals between the elements of two matrices
Parameters
----------
A : numpy array
First matrix.
B : numpy array
Second matrix.
Returns
-------
np.array
residuals
"""
return (A-B).flatten()
def tracenorm(A):
"""
Compute the trace norm of matrix A given by:
Tr( sqrt{ A^dagger * A } )
Parameters
----------
A : numpy array
The matrix to compute the trace norm of.
"""
if _np.linalg.norm(A - _np.conjugate(A.T)) < 1e-8:
#Hermitian, so just sum eigenvalue magnitudes
return _np.sum( _np.abs( _np.linalg.eigvals( A ) ) )
else:
#Sum of singular values (positive by construction)
return _np.sum( _np.linalg.svd(A, compute_uv=False) )
def tracedist(A, B):
"""
Compute the trace distance between matrices A and B,
given by:
D = 0.5 * Tr( sqrt{ (A-B)^dagger * (A-B) } )
Parameters
----------
A, B : numpy array
The matrices to compute the distance between.
"""
return 0.5 * tracenorm(A-B)
def diamonddist(A, B, mxBasis='pp', return_x=False):
"""
Returns the approximate diamond norm describing the difference between gate
matrices A and B given by :
D = ||A - B ||_diamond = sup_rho || AxI(rho) - BxI(rho) ||_1
Parameters
----------
A, B : numpy array
The *gate* matrices to use when computing the diamond norm.
mxBasis : Basis object
The source and destination basis, respectively. Allowed
values are Matrix-unit (std), Gell-Mann (gm), Pauli-product (pp),
and Qutrit (qt) (or a custom basis object).
return_x : bool, optional
Whether to return a numpy array encoding the state (rho) at
which the maximal trace distance occurs.
Returns
-------
dm : float
Diamond norm
W : numpy array
Only returned if `return_x = True`. Encodes the state rho, such that
`dm = trace( |(J(A)-J(B)).T * W| )`.
"""
mxBasis = _bt.build_basis_for_matrix(A, mxBasis)
#currently cvxpy is only needed for this function, so don't import until here
import sys as _sys
if _sys.version_info < (3, 0):
#Attempt "safe" import of cvxpy so that pickle isn't messed up...
import pickle as _pickle
p = _pickle.Pickler.dispatch.copy()
import cvxpy as _cvxpy
_pickle.Pickler.dispatch = p
else: # no need to do this in python3
import cvxpy as _cvxpy
#Check if using version < 1.0
old_cvxpy = bool(tuple(map(int,_cvxpy.__version__.split('.'))) < (1,0))
# This SDP implementation is a modified version of Kevin's code
#Compute the diamond norm
#Uses the primal SDP from arXiv:1207.5726v2, Sec 3.2
#Maximize 1/2 ( < J(phi), X > + < J(phi).dag, X.dag > )
#Subject to [[ I otimes rho0, X],
# [X.dag, I otimes rho1]] >> 0
# rho0, rho1 are density matrices
# X is linear operator
#Jamiolkowski representation of the process
# J(phi) = sum_ij Phi(Eij) otimes Eij
#< A, B > = Tr(A.dag B)
#def vec(matrix_in):
# # Stack the columns of a matrix to return a vector
# return _np.transpose(matrix_in).flatten()
#
#def unvec(vector_in):
# # Slice a vector into columns of a matrix
# d = int(_np.sqrt(vector_in.size))
# return _np.transpose(vector_in.reshape( (d,d) ))
#Code below assumes *un-normalized* Jamiol-isomorphism, so multiply by
# density mx dimension (`smallDim`) below
JAstd = _jam.fast_jamiolkowski_iso_std(A, mxBasis)
JBstd = _jam.fast_jamiolkowski_iso_std(B, mxBasis)
#Do this *after* the fast_jamiolkowski_iso calls above because these will convert
# A & B to a "single-block" basis representation when mxBasis has multiple blocks.
dim = JAstd.shape[0]
smallDim = int(_np.sqrt(dim))
JAstd *= smallDim # see above comment
JBstd *= smallDim # see above comment
assert(dim == JAstd.shape[1] == JBstd.shape[0] == JBstd.shape[1])
#CHECK: Kevin's jamiolowski, which implements the un-normalized isomorphism:
# smallDim * _jam.jamiolkowski_iso(M, "std", "std")
#def kevins_jamiolkowski(process, representation = 'superoperator'):
# # Return the Choi-Jamiolkowski representation of a quantum process
# # Add methods as necessary to accept different representations
# process = _np.array(process)
# if representation == 'superoperator':
# # Superoperator is the linear operator acting on vec(rho)
# dimension = int(_np.sqrt(process.shape[0]))
# print "dim = ",dimension
# jamiolkowski_matrix = _np.zeros([dimension**2, dimension**2], dtype='complex')
# for i in range(dimension**2):
# Ei_vec= _np.zeros(dimension**2)
# Ei_vec[i] = 1
# output = unvec(_np.dot(process,Ei_vec))
# tmp = _np.kron(output, unvec(Ei_vec))
# print "E%d = \n" % i,unvec(Ei_vec)
# #print "contrib =",_np.kron(output, unvec(Ei_vec))
# jamiolkowski_matrix += tmp
# return jamiolkowski_matrix
#JAstd_kev = jamiolkowski(A)
#JBstd_kev = jamiolkowski(B)
#print "diff A = ",_np.linalg.norm(JAstd_kev/2.0-JAstd)
#print "diff B = ",_np.linalg.norm(JBstd_kev/2.0-JBstd)
#Kevin's function: def diamondnorm( jamiolkowski_matrix ):
jamiolkowski_matrix = JBstd-JAstd
# Here we define a bunch of auxiliary matrices because CVXPY doesn't use complex numbers
K = jamiolkowski_matrix.real # J.real
L = jamiolkowski_matrix.imag # J.imag
if old_cvxpy:
Y = _cvxpy.Variable(dim, dim) # X.real
Z = _cvxpy.Variable(dim, dim) # X.imag
sig0 = _cvxpy.Variable(smallDim,smallDim) # rho0.real
sig1 = _cvxpy.Variable(smallDim,smallDim) # rho1.real
tau0 = _cvxpy.Variable(smallDim,smallDim) # rho1.imag
tau1 = _cvxpy.Variable(smallDim,smallDim) # rho1.imag
else:
Y = _cvxpy.Variable(shape=(dim, dim)) # X.real
Z = _cvxpy.Variable(shape=(dim, dim)) # X.imag
sig0 = _cvxpy.Variable(shape=(smallDim, smallDim)) # rho0.real
sig1 = _cvxpy.Variable(shape=(smallDim, smallDim)) # rho1.real
tau0 = _cvxpy.Variable(shape=(smallDim, smallDim)) # rho1.imag
tau1 = _cvxpy.Variable(shape=(smallDim, smallDim)) # rho1.imag
ident = _np.identity(smallDim, 'd')
objective = _cvxpy.Maximize( _cvxpy.trace( K.T * Y + L.T * Z) )
constraints = [ _cvxpy.bmat( [
[ _cvxpy.kron(ident, sig0), Y, -_cvxpy.kron(ident, tau0), -Z],
[ Y.T, _cvxpy.kron(ident, sig1), Z.T, -_cvxpy.kron(ident, tau1)],
[ _cvxpy.kron(ident, tau0), Z, _cvxpy.kron(ident, sig0), Y],
[ -Z.T, _cvxpy.kron(ident, tau1), Y.T, _cvxpy.kron(ident, sig1)]] ) >> 0,
_cvxpy.bmat( [[sig0, -tau0],
[tau0, sig0]] ) >> 0,
_cvxpy.bmat( [[sig1, -tau1],
[tau1, sig1]] ) >> 0,
sig0 == sig0.T,
sig1 == sig1.T,
tau0 == -tau0.T,
tau1 == -tau1.T,
_cvxpy.trace(sig0) == 1.,
_cvxpy.trace(sig1) == 1. ]
prob = _cvxpy.Problem(objective, constraints)
try:
prob.solve(solver="CVXOPT")
# prob.solve(solver="ECOS")
# prob.solve(solver="SCS")#This always fails
except _cvxpy.error.SolverError as e:
_warnings.warn("CVXPY failed: %s - diamonddist returning -2!" % str(e))
return (-2, _np.zeros((dim,dim))) if return_x else -2
except:
_warnings.warn("CVXOPT failed (uknown err) - diamonddist returning -2!")
return (-2, _np.zeros((dim,dim))) if return_x else -2
#Validate result
#assert( abs(_np.trace(_np.dot(K.T,Y.value) + _np.dot(L.T,Z.value))-prob.value) < 1e-6 ), \
# "Diamondnorm mismatch"
if return_x:
X = Y.value + 1j*Z.value #encodes state at which maximum trace-distance occurs
return prob.value, X
else:
return prob.value
def jtracedist(A, B, mxBasis='pp'): #Jamiolkowski trace distance: Tr(|J(A)-J(B)|)
"""
Compute the Jamiolkowski trace distance between operation matrices A and B,
given by:
D = 0.5 * Tr( sqrt{ (J(A)-J(B))^2 } )
where J(.) is the Jamiolkowski isomorphism map that maps a operation matrix
to it's corresponding Choi Matrix.
Parameters
----------
A, B : numpy array
The matrices to compute the distance between.
mxBasis : {'std', 'gm', 'pp', 'qt'} or Basis object
The source and destination basis, respectively. Allowed
values are Matrix-unit (std), Gell-Mann (gm), Pauli-product (pp),
and Qutrit (qt) (or a custom basis object).
"""
JA = _jam.fast_jamiolkowski_iso_std(A, mxBasis)
JB = _jam.fast_jamiolkowski_iso_std(B, mxBasis)
return tracedist(JA,JB)
def entanglement_fidelity(A, B, mxBasis='pp'):
"""
Returns the "entanglement" process fidelity between gate
matrices A and B given by :
F = Tr( sqrt{ sqrt(J(A)) * J(B) * sqrt(J(A)) } )^2
where J(.) is the Jamiolkowski isomorphism map that maps a operation matrix
to it's corresponding Choi Matrix.
Parameters
----------
A, B : numpy array
The matrices to compute the fidelity between.
mxBasis : {'std', 'gm', 'pp', 'qt'} or Basis object
The basis of the matrices. Allowed values are Matrix-unit (std),
Gell-Mann (gm), Pauli-product (pp), and Qutrit (qt)
(or a custom basis object).
"""
d2 = A.shape[0]
def isTP(x): return _np.isclose(x[0,0],1.0) and all(
[ _np.isclose(x[0,i],0) for i in range(d2)] )
def isUnitary(x): return _np.allclose(_np.identity(d2,'d'), _np.dot(x,x.conjugate().T))
if isTP(A) and isTP(B) and isUnitary(B): #then assume TP-like gates & use simpler formula
TrLambda = _np.trace( _np.dot(A, B.conjugate().T) ) # same as using _np.linalg.inv(B)
d2 = A.shape[0]
return TrLambda / d2
JA = _jam.jamiolkowski_iso(A, mxBasis, mxBasis)
JB = _jam.jamiolkowski_iso(B, mxBasis, mxBasis)
return fidelity(JA,JB)
def average_gate_fidelity(A ,B, mxBasis='pp'):
"""
Computes the average gate fidelity (AGF) between two gates.
Average gate fidelity (F_g) is related to entanglement fidelity
(F_p), via:
F_g = (d * F_p + 1)/(1 + d),
where d is the Hilbert space dimension. This formula, and the
definition of AGF, can be found in Phys. Lett. A 303 249-252 (2002).
Parameters
----------
A : array or gate
The gate to compute the AGI to B of. E.g., an imperfect
implementation of B.
B : array or gate
The gate to compute the AGI to A of. E.g., the target gate
corresponding to A.
mxBasis : {"std","gm","pp"} or Basis object, optional
The basis of the matrices.
Returns
-------
AGI : float
The AGI of A to B.
"""
d = int(round(_np.sqrt(A.shape[0])))
PF = entanglement_fidelity(A,B,mxBasis=mxBasis)
AGF = (d*PF + 1)/(1+d)
return float(AGF)
def average_gate_infidelity(A ,B, mxBasis="gm"):
"""
Computes the average gate infidelity (AGI) between two gates.
Average gate infidelity is related to entanglement infidelity
(EI) via:
AGI = (d * (1-EI) + 1)/(1 + d),
where d is the Hilbert space dimension. This formula, and the
definition of AGI, can be found in Phys. Lett. A 303 249-252 (2002).
Parameters
----------
A : array or gate
The gate to compute the AGI to B of. E.g., an imperfect
implementation of B.
B : array or gate
The gate to compute the AGI to A of. E.g., the target gate
corresponding to A.
mxBasis : {"std","gm","pp"} or Basis object, optional
The basis of the matrices.
Returns
----------
AGI : float
The AGI of A to B.
"""
return 1 - average_gate_fidelity(A ,B, mxBasis)
def entanglement_infidelity(A, B, mxBasis='pp'):
"""
Returns the entanglement infidelity (EI) between gate
matrices A and B given by :
EI = 1 - Tr( sqrt{ sqrt(J(A)) * J(B) * sqrt(J(A)) } )^2
where J(.) is the Jamiolkowski isomorphism map that maps a operation matrix
to it's corresponding Choi Matrix.
Parameters
----------
A, B : numpy array
The matrices to compute the fidelity between.
mxBasis : {'std', 'gm', 'pp', 'qt'} or Basis object
The basis of the matrices. Allowed values are Matrix-unit (std),
Gell-Mann (gm), Pauli-product (pp), and Qutrit (qt)
(or a custom basis object).
Returns
-------
EI : float
The EI of A to B.
"""
return 1 - float(entanglement_fidelity(A, B, mxBasis))
def unitarity(A, mxBasis="gm"):
"""
Returns the "unitarity" of a channel, as defined in Wallman et al,
``Estimating the Coherence of noise'' NJP 17 113020 (2015). The
unitarity is given by (Prop 1 in Wallman et al):
u(A) = Tr( A_u^{\dagger} A_u ) / (d^2 - 1),
where A_u is the unital submatrix of A, and d is the dimension of
the Hilbert space. When A is written in any basis for which the
first element is the normalized identity (e.g., the pp or gm
bases), The unital submatrix of A is the matrix obtained when the
top row and left hand column is removed from A.
Parameters
----------
A : array or gate
The gate for which the unitarity is to be computed.
mxBasis : {"std","gm","pp"} or a Basis object, optional
The basis of the matrix.
d : int, optional
The dimension of the Hilbert space.
Returns
----------
u : float
The unitarity of the gate A.
"""
d = int(round(_np.sqrt(A.shape[0])))
basisMxs = _basis_matrices(mxBasis, A.shape[0])
if _np.allclose( basisMxs[0], _np.identity(d,'d') ):
B = A
else:
B = _bt.change_basis(A, mxBasis, "gm") #everything should be able to be put in the "gm" basis
unital = B[1:d**2,1:d**2]
u = _np.trace(_np.dot(_np.conj(_np.transpose(unital)),unital)) / (d**2-1)
return u
def fidelity_upper_bound(operationMx):
"""
Get an upper bound on the fidelity of the given
operation matrix with any unitary operation matrix.
The closeness of the result to one tells
how "unitary" the action of operationMx is.
Parameters
----------
operationMx : numpy array
The operation matrix to act on.
Returns
-------
float
The resulting upper bound on fidelity(operationMx, anyUnitaryGateMx)
"""
choi = _jam.jamiolkowski_iso(operationMx, choiMxBasis="std")
choi_evals,choi_evecs = _np.linalg.eig(choi)
maxF_direct = max([_np.sqrt(max(ev.real,0.0)) for ev in choi_evals]) ** 2
iMax = _np.argmax([ev.real for ev in choi_evals]) #index of maximum eigenval
closestVec = choi_evecs[:, iMax:(iMax+1)]
##print "DEBUG: closest evec = ", closestUnitaryVec
#new_evals = _np.zeros( len(closestUnitaryVec) ); new_evals[iClosestU] = 1.0
#closestUnitaryJmx = _np.dot(choi_evecs, _np.dot( _np.diag(new_evals), _np.linalg.inv(choi_evecs) ) ) #gives same result
closestJmx = _np.kron( closestVec, _np.transpose(_np.conjugate(closestVec))) # closest rank-1 Jmx
closestJmx /= _mt.trace(closestJmx) #normalize so trace of Jmx == 1.0
maxF = fidelity(choi, closestJmx)
if not _np.isnan(maxF):
#Uncomment for debugging
#if abs(maxF - maxF_direct) >= 1e-6:
# print "DEBUG: operationMx:\n",operationMx
# print "DEBUG: choiMx:\n",choi
# print "DEBUG choi_evals = ",choi_evals, " iMax = ",iMax
# #print "DEBUG: J = \n", closestUnitaryJmx
# print "DEBUG: eigvals(J) = ", _np.linalg.eigvals(closestJmx)
# print "DEBUG: trace(J) = ", _mt.trace(closestJmx)
# print "DEBUG: maxF = %f, maxF_direct = %f" % (maxF, maxF_direct)
# raise ValueError("ERROR: maxF - maxF_direct = %f" % (maxF -maxF_direct))
assert(abs(maxF - maxF_direct) < 1e-6)
else:
maxF = maxF_direct # case when maxF is nan, due to scipy sqrtm function being buggy - just use direct F
closestOpMx = _jam.jamiolkowski_iso_inv( closestJmx, choiMxBasis="std" )
return maxF, closestOpMx
#closestU_evals, closestU_evecs = _np.linalg.eig(closestUnitaryGateMx)
#print "DEBUG: U = \n", closestUnitaryGateMx
#print "DEBUG: closest U evals = ",closestU_evals
#print "DEBUG: evecs = \n",closestU_evecs
def get_povm_map(model, povmlbl):
"""
Constructs a gate-like quantity for the POVM within `model`.
This is done by embedding the `k`-outcome classical output space of the POVM
in the Hilbert-Schmidt space of `k` by `k` density matrices by placing the
classical probability distribution along the diagonal of the density matrix.
Currently, this is only implemented for the case when `k` equals `d`, the
dimension of the POVM's Hilbert space.
Parameters
----------
model : Model
The model supplying the POVM effect vectors and the basis those
vectors are in.
povmlbl : str
The POVM label
Returns
-------
numpy.ndarray
The matrix of the "POVM map" in the `model.basis` basis.
"""
povmVectors = [v.todense()[:,None] for v in model.povms[povmlbl].values()]
if isinstance(model.basis,_DirectSumBasis): #HACK - need to get this to work with general bases
blkDims = [int(_np.sqrt(comp.dim)) for comp in model.basis.component_bases]
else:
blkDims = [ int(round(_np.sqrt(model.dim))) ] # [d] where density matrix is dxd
nV = len(povmVectors)
#assert(d**2 == model.dim), "Model dimension (%d) is not a perfect square!" % model.dim
#assert( nV**2 == d ), "Can only compute POVM metrics when num of effects == H space dimension"
# I don't think above assert is needed - should work in general (Robin?)
povm_mx = _np.concatenate( povmVectors, axis=1 ).T # "povm map" ( B(H) -> S_k ) (shape= nV,model.dim)
Sk_embedding_in_std = _np.zeros( (model.dim, nV) )
for i in range(nV):
Sk_embedding_in_std[:,i] = _flat_mut_blks(i,i,blkDims)
std_to_basis = model.basis.reverse_transform_matrix("std") #_bt.transform_matrix("std", model.basis, blkDims)
assert(std_to_basis.shape == (model.dim,model.dim))
return _np.dot(std_to_basis, _np.dot(Sk_embedding_in_std, povm_mx))
def povm_fidelity(model, targetModel, povmlbl):
"""
Computes the process (entanglement) fidelity between POVM maps.
Parameters
----------
model, targetModel : Model
Models containing the two POVMs to compare.
povmlbl : str
The POVM label
Returns
-------
float
"""
povm_mx = get_povm_map(model, povmlbl)
target_povm_mx = get_povm_map(targetModel, povmlbl)
return entanglement_fidelity(povm_mx, target_povm_mx, targetModel.basis)
def povm_jtracedist(model, targetModel, povmlbl):
"""
Computes the Jamiolkowski trace distance between POVM maps using :func:`jtracedist`.
Parameters
----------
model, targetModel : Model
Models containing the two POVMs to compare.
povmlbl : str
The POVM label
Returns
-------
float
"""
povm_mx = get_povm_map(model, povmlbl)
target_povm_mx = get_povm_map(targetModel, povmlbl)
return jtracedist(povm_mx, target_povm_mx, targetModel.basis)
def povm_diamonddist(model, targetModel, povmlbl):
"""
Computes the diamond distance between POVM maps using :func:`diamonddist`.
Parameters
----------
model, targetModel : Model
Models containing the two POVMs to compare.
povmlbl : str
The POVM label
Returns
-------
float
"""
povm_mx = get_povm_map(model, povmlbl)
target_povm_mx = get_povm_map(targetModel, povmlbl)
return diamonddist(povm_mx, target_povm_mx, targetModel.basis)
#decompose operation matrix into axis of rotation, etc
def decompose_gate_matrix(operationMx):
"""
Compute how the action of a operation matrix can be
is decomposed into fixed points, axes of rotation,
angles of rotation, and decays. Also determines
whether a gate appears to be valid and/or unitary.
Parameters
----------
operationMx : numpy array
The operation matrix to act on.
Returns
-------
dict
A dictionary describing the decomposed action. Keys are:
'isValid' : bool
whether decomposition succeeded
'isUnitary' : bool
whether operationMx describes unitary action
'fixed point' : numpy array
the fixed point of the action
'axis of rotation' : numpy array or nan
the axis of rotation
'decay of diagonal rotation terms' : float
decay of diagonal terms
'rotating axis 1' : numpy array or nan
1st axis orthogonal to axis of rotation
'rotating axis 2' : numpy array or nan
2nd axis orthogonal to axis of rotation
'decay of off diagonal rotation terms' : float
decay of off-diagonal terms
'pi rotations' : float
angle of rotation in units of pi radians
"""
op_evals,op_evecs = _np.linalg.eig(_np.asarray(operationMx))
# fp_eigenvec = None
# aor_eval = None; aor_eigenvec = None
# ra_eval = None; ra1_eigenvec = None; ra2_eigenvec = None
TOL = 1e-4 #1e-7
unit_eval_indices = [ i for (i,ev) in enumerate(op_evals) if abs(ev - 1.0) < TOL ]
#unit_eval_indices = [ i for (i,ev) in enumerate(op_evals) if ev > (1.0-TOL) ]
conjpair_eval_indices = [ ]
for (i,ev) in enumerate(op_evals):
if i in unit_eval_indices: continue #don't include the unit eigenvalues in the conjugate pair count
if any( [ (i in conjpair) for conjpair in conjpair_eval_indices] ): continue #don't include existing conjugate pairs
for (j,ev2) in enumerate(op_evals[i+1:]):
if abs(ev - _np.conjugate(ev2)) < TOL:
conjpair_eval_indices.append( (i,j+(i+1)) )
break #don't pair i-th eigenvalue with any other (pairs should be disjoint)
real_eval_indices = [] #indices of real eigenvalues that are not units or a part of any conjugate pair
complex_eval_indices = [] #indices of complex eigenvalues that are not units or a part of any conjugate pair
for (i,ev) in enumerate(op_evals):
if i in unit_eval_indices: continue #don't include the unit eigenvalues
if any( [ (i in conjpair) for conjpair in conjpair_eval_indices] ): continue #don't include the conjugate pairs
if abs(ev.imag) < TOL: real_eval_indices.append(i)
else: complex_eval_indices.append(i)
#if len(real_eval_indices + unit_eval_indices) > 0:
# max_real_eval = max([ op_evals[i] for i in real_eval_indices + unit_eval_indices])
# min_real_eval = min([ op_evals[i] for i in real_eval_indices + unit_eval_indices])
#else:
# max_real_eval = _np.nan
# min_real_eval = _np.nan
#
#fixed_points = [ op_evecs[:,i] for i in unit_eval_indices ]
#real_eval_axes = [ op_evecs[:,i] for i in real_eval_indices ]
#conjpair_eval_axes = [ (op_evecs[:,i],op_evecs[:,j]) for (i,j) in conjpair_eval_indices ]
#
#ret = { }
nQubits = _np.log2(operationMx.shape[0]) / 2
if nQubits == 1:
#print "DEBUG: 1 qubit decomp --------------------------"
#print " --> evals = ", op_evals
#print " --> unit eval indices = ", unit_eval_indices
#print " --> conj eval indices = ", conjpair_eval_indices
#print " --> unpaired real eval indices = ", real_eval_indices
#Special case: if have two conjugate pairs, check if one (or both) are real
# and break the one with the largest (real) value into two unpaired real evals.
if len(conjpair_eval_indices) == 2:
iToBreak = None
if abs(_np.imag( op_evals[ conjpair_eval_indices[0][0] ] )) < TOL and \
abs(_np.imag( op_evals[ conjpair_eval_indices[1][0] ] )) < TOL:
iToBreak = _np.argmax( [_np.real(conjpair_eval_indices[0][0]), _np.real(conjpair_eval_indices[1][0])] )
elif abs(_np.imag( op_evals[ conjpair_eval_indices[0][0] ] )) < TOL: iToBreak = 0
elif abs(_np.imag( op_evals[ conjpair_eval_indices[1][0] ] )) < TOL: iToBreak = 1
if iToBreak is not None:
real_eval_indices.append( conjpair_eval_indices[iToBreak][0])
real_eval_indices.append( conjpair_eval_indices[iToBreak][1])
del conjpair_eval_indices[iToBreak]
#Find eigenvector corresponding to fixed point (or closest we can get). This
# should be a unit eigenvalue with identity eigenvector.
if len(unit_eval_indices) > 0:
#Find linear least squares solution within possibly degenerate unit-eigenvalue eigenspace
# of eigenvector closest to identity density mx (the desired fixed point), then orthogonalize
# the remaining eigenvectors w.r.t this one.
A = _np.take(op_evecs, unit_eval_indices, axis=1)
b = _np.array( [[1],[0],[0],[0]], 'd') #identity density mx
x = _np.dot( _np.linalg.pinv( _np.dot(A.T,A) ), _np.dot(A.T, b))
fixedPtVec = _np.dot(A,x); #fixedPtVec / _np.linalg.norm(fixedPtVec)
fixedPtVec = fixedPtVec[:,0]
iLargestContrib = _np.argmax(_np.abs(x)) #index of gate eigenvector which contributed the most
for ii,i in enumerate(unit_eval_indices):
if ii == iLargestContrib:
op_evecs[:,i] = fixedPtVec
iFixedPt = i
else:
op_evecs[:,i] = op_evecs[:,i] - _np.vdot(fixedPtVec,op_evecs[:,i])*fixedPtVec
for jj,j in enumerate(unit_eval_indices[:ii]):
if jj == iLargestContrib: continue
op_evecs[:,i] = op_evecs[:,i] - _np.vdot(op_evecs[:,j],op_evecs[:,i])*op_evecs[:,j]
op_evecs[:,i] /= _np.linalg.norm(op_evecs[:,i])
elif len(real_eval_indices) > 0:
# just take eigenvector corresponding to the largest real eigenvalue?
#iFixedPt = real_eval_indices[ _np.argmax( [ op_evals[i] for i in real_eval_indices ] ) ]
# ...OR take eigenvector corresponding to a real unpaired eigenvalue closest to identity:
idmx = _np.array( [[1],[0],[0],[0]], 'd') #identity density mx
iFixedPt = real_eval_indices[ _np.argmin( [ _np.linalg.norm(op_evecs[i]-idmx) for i in real_eval_indices ] ) ]
else:
#No unit or real eigenvalues => two complex conjugate pairs or unpaired complex evals --> bail out
return { 'isValid': False, 'isUnitary': False, 'msg': "All evals are complex." }
#Find eigenvector corresponding to axis of rotation: find the *largest* unpaired real/unit eval
indsToConsider = (unit_eval_indices + real_eval_indices)[:]
del indsToConsider[ indsToConsider.index(iFixedPt) ] #don't consider fixed pt evec
if len(indsToConsider) > 0:
iRotAxis = indsToConsider[ _np.argmax( [ op_evals[i] for i in indsToConsider ] ) ]
else:
#No unit or real eigenvalues => an unpaired complex eval --> bail out
return { 'isValid': False, 'isUnitary': False, 'msg': "Unpaired complex eval." }
#There are only 2 eigenvalues left -- hopefully a conjugate pair giving rotation
inds = list(range(4))
del inds[ inds.index( iFixedPt ) ]
del inds[ inds.index( iRotAxis ) ]
if abs( op_evals[inds[0]] - _np.conjugate(op_evals[inds[1]])) < TOL:
iConjPair1,iConjPair2 = inds
else:
return { 'isValid': False, 'isUnitary': False, 'msg': "No conjugate pair for rotn." }
return { 'isValid': True,
'isUnitary': bool(len(unit_eval_indices) >= 2),
'fixed point': op_evecs[:,iFixedPt],
'axis of rotation': op_evecs[:,iRotAxis],
'rotating axis 1': op_evecs[:,iConjPair1],
'rotating axis 2': op_evecs[:,iConjPair2],
'decay of diagonal rotation terms': 1.0 - abs(op_evals[iRotAxis]),
'decay of off diagonal rotation terms': 1.0 - abs(op_evals[iConjPair1]),
'pi rotations': _np.angle(op_evals[iConjPair1])/_np.pi,
'msg': "Success" }
else:
return { 'isValid': False,
'isUnitary': False,
'msg': "Unsupported number of qubits: %d" % nQubits }
def state_to_dmvec(psi):
"""
Compute the vectorized density matrix which acts as the state `psi`.
This is just the outer product map |psi> => |psi><psi| with the
output flattened, i.e. `dot(psi, conjugate(psi).T)`.
Parameters
----------
psi : numpy array
The state vector.
Returns
-------
numpy array
The vectorized density matrix.
"""
psi = psi.reshape((psi.size,1)) # convert to (N,1) shape if necessary
dm = _np.dot(psi, _np.conjugate(psi.T))
return dm.flatten()
def dmvec_to_state(dmvec, tol=1e-6):
"""
Compute the pure state describing the action of density matrix vector `dmvec`.
If `dmvec` represents a mixed state, ValueError is raised.
Parameters
----------
dmvec : numpy array
The vectorized density matrix, assumed to be in the standard (matrix
unit) basis.
tol : float, optional
tolerance for determining whether an eigenvalue is zero.
Returns
-------
numpy array
The pure state, as a column vector of shape = (N,1)
"""
d2 = dmvec.size; d = int(round(_np.sqrt(d2)))
dm = dmvec.reshape((d,d))
evals,evecs = _np.linalg.eig(dm)
k = None
for i,ev in enumerate(evals):
if abs(ev) > tol:
if k is None: k = i
else: raise ValueError("Cannot convert mixed dmvec to pure state!")
if k is None: raise ValueError("Cannot convert zero dmvec to puse state!")
psi = evecs[:,k] * _np.sqrt(evals[k])
psi.shape = (d,1)
return psi
def unitary_to_process_mx(U):
"""
Compute the super-operator which acts on (row)-vectorized
density matrices from a unitary operator (matrix) U which
acts on state vectors. This super-operator is given by
the tensor product of U and conjugate(U), i.e. kron(U,U.conj).
Parameters
----------
U : numpy array
The unitary matrix which acts on state vectors.
Returns
-------
numpy array
The super-operator process matrix.
"""
# U -> kron(U,Uc) since U rho U_dag -> kron(U,Uc)
# since AXB --row-vectorize--> kron(A,B.T)*vec(X)
return _np.kron(U,_np.conjugate(U))
def process_mx_to_unitary(superop):
"""
Compute the unitary corresponding to the (unitary-action!)
super-operator `superop` which acts on (row)-vectorized
density matrices. The super-operator must be of the form
`kron(U,U.conj)` or an error will be thrown.
Parameters
----------
superop : numpy array
The superoperator matrix which acts on vectorized
density matrices (in the 'std' matrix-unit basis).
Returns