/
reportables.py
2443 lines (1849 loc) · 70.2 KB
/
reportables.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
"""
Functions which compute named quantities for Models and Datasets.
"""
#***************************************************************************************************
# Copyright 2015, 2019 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
# Under the terms of Contract DE-NA0003525 with NTESS, the U.S. Government retains certain rights
# in this software.
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
# in compliance with the License. You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0 or in the LICENSE file in the root pyGSTi directory.
#***************************************************************************************************
"""
Named quantities as well as their confidence-region error bars are
computed by the functions in this module. These quantities are
used primarily in reports, so we refer to these quantities as
"reportables".
"""
import numpy as _np
import scipy.linalg as _spl
import warnings as _warnings
from .. import tools as _tools
from .. import algorithms as _alg
from ..objects.basis import Basis as _Basis, DirectSumBasis as _DirectSumBasis
from ..objects.label import Label as _Lbl
from ..objects.reportableqty import ReportableQty as _ReportableQty
from ..objects import modelfunction as _modf
import pkgutil
_CVXPY_AVAILABLE = pkgutil.find_loader('cvxpy') is not None
FINITE_DIFF_EPS = 1e-7
def _null_fn(*arg):
return None
def _project_to_valid_prob(p, tol=1e-9):
if p < tol: return tol
if p > 1 - tol: return 1 - tol
return p
def _make_reportable_qty_or_dict(f0, df=None, non_markovian_ebs=False):
""" Just adds special processing with f0 is a dict, where we
return a dict or ReportableQtys rather than a single
ReportableQty of the dict.
"""
if isinstance(f0, dict):
#special processing for dict -> df is dict of error bars
# and we return a dict of ReportableQtys
if df:
return {ky: _ReportableQty(f0[ky], df[ky], non_markovian_ebs) for ky in f0}
else:
return {ky: _ReportableQty(f0[ky], None, False) for ky in f0}
else:
return _ReportableQty(f0, df, non_markovian_ebs)
def evaluate(model_fn, cri=None, verbosity=0):
"""
Evaluate a ModelFunction object using confidence region information
Parameters
----------
model_fn : ModelFunction
The function to evaluate
cri : ConfidenceRegionFactoryView, optional
View for computing confidence intervals.
verbosity : int, optional
Amount of detail to print to stdout.
Returns
-------
ReportableQty or dict
If `model_fn` does returns a dict of ReportableQty objects, otherwise
a single ReportableQty.
"""
if model_fn is None: # so you can set fn to None when they're missing (e.g. diamond norm)
return _ReportableQty(_np.nan)
if cri:
nmEBs = bool(cri.errorbar_type == "non-markovian")
df, f0 = cri.compute_confidence_interval(
model_fn, return_fn_val=True,
verbosity=verbosity)
return _make_reportable_qty_or_dict(f0, df, nmEBs)
else:
return _make_reportable_qty_or_dict(model_fn.evaluate(model_fn.base_model))
def spam_dotprods(rho_vecs, povms):
"""
SPAM dot products (concatenates POVMS)
Parameters
----------
rho_vecs : list
A list of state-preparation :class:`SPAMVec` objects.
povms : list
A list of :class:`POVM` objects.
Returns
-------
numpy.ndarray
A 2D array of shape `(len(rho_vecs), num_evecs)` where `num_evecs`
is the total number of effect vectors in all of `povms`.
"""
nEVecs = sum(len(povm) for povm in povms)
ret = _np.empty((len(rho_vecs), nEVecs), 'd')
for i, rhoVec in enumerate(rho_vecs):
j = 0
for povm in povms:
for EVec in povm.values():
ret[i, j] = _np.vdot(EVec.to_dense(), rhoVec.to_dense()); j += 1
# to_dense() gives a 1D array, so no need to transpose EVec
return ret
Spam_dotprods = _modf.spamfn_factory(spam_dotprods) # init args == (model)
def choi_matrix(gate, mx_basis):
"""
Choi matrix
Parameters
----------
gate : numpy.ndarray
the transfer-matrix specifying a gate's action.
mx_basis : Basis or {'pp', 'gm', 'std'}
the basis that `gate` is in.
Returns
-------
numpy.ndarray
"""
return _tools.jamiolkowski_iso(gate, mx_basis, mx_basis)
Choi_matrix = _modf.opfn_factory(choi_matrix) # init args == (model, op_label)
def choi_eigenvalues(gate, mx_basis):
"""
Choi matrix eigenvalues
Parameters
----------
gate : numpy.ndarray
the transfer-matrix specifying a gate's action.
mx_basis : Basis or {'pp', 'gm', 'std'}
the basis that `gate` is in.
Returns
-------
numpy.ndarray
"""
choi = _tools.jamiolkowski_iso(gate, mx_basis, mx_basis)
choi_eigvals = _np.linalg.eigvals(choi)
return _np.array(sorted(choi_eigvals))
Choi_evals = _modf.opfn_factory(choi_eigenvalues) # init args == (model, op_label)
def choi_trace(gate, mx_basis):
"""
Trace of the Choi matrix
Parameters
----------
gate : numpy.ndarray
the transfer-matrix specifying a gate's action.
mx_basis : Basis or {'pp', 'gm', 'std'}
the basis that `gate` is in.
Returns
-------
float
"""
choi = _tools.jamiolkowski_iso(gate, mx_basis, mx_basis)
return _np.trace(choi)
Choi_trace = _modf.opfn_factory(choi_trace) # init args == (model, op_label)
class GateEigenvalues(_modf.ModelFunction):
"""
Gate eigenvalues
Parameters
----------
model : Model
Model gate is contained within.
oplabel : Label
The gate's label within `model`.
"""
def __init__(self, model, oplabel):
self.oplabel = oplabel
_modf.ModelFunction.__init__(self, model, [("gate", oplabel)])
def evaluate(self, model):
"""
Evaluate at `model`
Parameters
----------
model : Model
A model nearby in parameter space.
Returns
-------
numpy.ndarray
"""
evals, evecs = _np.linalg.eig(model.operations[self.oplabel].to_dense())
ev_list = list(enumerate(evals))
ev_list.sort(key=lambda tup: abs(tup[1]), reverse=True)
indx, evals = zip(*ev_list)
evecs = evecs[:, indx] # sort evecs according to evals
self.G0 = model.operations[self.oplabel]
self.evals = _np.array(evals)
self.evecs = evecs
self.inv_evecs = _np.linalg.inv(evecs)
return self.evals
def evaluate_nearby(self, nearby_model):
"""
Evaluate at a nearby model
Parameters
----------
nearby_model : Model
A model nearby in parameter space.
Returns
-------
numpy.ndarray
"""
#avoid calling minweight_match again
dMx = nearby_model.operations[self.oplabel] - self.G0
#evalsM = evals0 + Uinv * (M-M0) * U
return _np.array([self.evals[k] + _np.dot(self.inv_evecs[k, :], _np.dot(dMx, self.evecs[:, k]))
for k in range(dMx.shape[0])])
# ref for eigenvalue derivatives: https://www.win.tue.nl/casa/meetings/seminar/previous/_abstract051019_files/Presentation.pdf # noqa
class CircuitEigenvalues(_modf.ModelFunction):
"""
Circuit eigenvalues
Parameters
----------
model : Model
Model used to evaluate `circuit`.
circuit : Circuit
The circuit whose process matrix we want the eigenvalues of.
"""
def __init__(self, model, circuit):
self.circuit = circuit
_modf.ModelFunction.__init__(self, model, ["all"])
def evaluate(self, model):
"""
Evaluate at `model`
Parameters
----------
model : Model
Model to evaluate at.
Returns
-------
numpy.ndarray
"""
Mx = model.sim.product(self.circuit)
evals, evecs = _np.linalg.eig(Mx)
ev_list = list(enumerate(evals))
ev_list.sort(key=lambda tup: abs(tup[1]), reverse=True)
indx, evals = zip(*ev_list)
evecs = evecs[:, indx] # sort evecs according to evals
self.Mx = Mx
self.evals = _np.array(evals)
self.evecs = evecs
self.inv_evecs = _np.linalg.inv(evecs)
return self.evals
def evaluate_nearby(self, nearby_model):
"""
Evaluate at nearby model
Parameters
----------
nearby_model : Model
A model nearby in parameter space.
Returns
-------
numpy.ndarray
"""
#avoid calling minweight_match again
Mx = nearby_model.sim.product(self.circuit)
dMx = Mx - self.Mx
#evalsM = evals0 + Uinv * (M-M0) * U
return _np.array([self.evals[k] + _np.dot(self.inv_evecs[k, :], _np.dot(dMx, self.evecs[:, k]))
for k in range(dMx.shape[0])])
# ref for eigenvalue derivatives: https://www.win.tue.nl/casa/meetings/seminar/previous/_abstract051019_files/Presentation.pdf # noqa
#def circuit_eigenvalues(model, circuit):
# return _np.array(sorted(_np.linalg.eigvals(model.sim.product(circuit)),
# key=lambda ev: abs(ev), reverse=True))
#CircuitEigenvalues = _modf.modelfn_factory(circuit_eigenvalues)
## init args == (model, circuit)
def rel_circuit_eigenvalues(model_a, model_b, circuit):
"""
Eigenvalues of dot(productB(circuit)^-1, productA(circuit))
Parameters
----------
model_a : Model
The first model (to evaluate productA)
model_b : Model
The second model (to evaluate productB)
circuit : Circuit
The circuit.
Returns
-------
numpy.ndarray
"""
A = model_a.sim.product(circuit) # "gate"
B = model_b.sim.product(circuit) # "target gate"
rel_op = _np.dot(_np.linalg.inv(B), A) # "relative gate" == target^{-1} * gate
return _np.linalg.eigvals(rel_op)
Rel_circuit_eigenvalues = _modf.modelfn_factory(rel_circuit_eigenvalues)
# init args == (model_a, model_b, circuit)
def circuit_frobenius_diff(model_a, model_b, circuit):
"""
Frobenius distance btwn productA(circuit) and productB(circuit)
Parameters
----------
model_a : Model
The first model (to evaluate productA)
model_b : Model
The second model (to evaluate productB)
circuit : Circuit
The circuit.
Returns
-------
float
"""
A = model_a.sim.product(circuit) # "gate"
B = model_b.sim.product(circuit) # "target gate"
return frobenius_diff(A, B, model_b.basis)
Circuit_fro_diff = _modf.modelfn_factory(circuit_frobenius_diff)
# init args == (model_a, model_b, circuit)
def circuit_entanglement_infidelity(model_a, model_b, circuit):
"""
Entanglement infidelity btwn productA(circuit) and productB(circuit)
Parameters
----------
model_a : Model
The first model (to evaluate productA)
model_b : Model
The second model (to evaluate productB)
circuit : Circuit
The circuit.
Returns
-------
float
"""
A = model_a.sim.product(circuit) # "gate"
B = model_b.sim.product(circuit) # "target gate"
return entanglement_infidelity(A, B, model_b.basis)
Circuit_entanglement_infidelity = _modf.modelfn_factory(circuit_entanglement_infidelity)
# init args == (model_a, model_b, circuit)
def circuit_avg_gate_infidelity(model_a, model_b, circuit):
"""
Average gate infidelity between productA(circuit) and productB(circuit).
Parameters
----------
model_a : Model
The first model (to evaluate productA)
model_b : Model
The second model (to evaluate productB)
circuit : Circuit
The circuit.
Returns
-------
float
"""
A = model_a.sim.product(circuit) # "gate"
B = model_b.sim.product(circuit) # "target gate"
return avg_gate_infidelity(A, B, model_b.basis)
Circuit_avg_gate_infidelity = _modf.modelfn_factory(circuit_avg_gate_infidelity)
# init args == (model_a, model_b, circuit)
def circuit_jtrace_diff(model_a, model_b, circuit):
"""
Jamiolkowski trace distance between productA(circuit) and productB(circuit)
Parameters
----------
model_a : Model
The first model (to evaluate productA)
model_b : Model
The second model (to evaluate productB)
circuit : Circuit
The circuit.
Returns
-------
float
"""
A = model_a.sim.product(circuit) # "gate"
B = model_b.sim.product(circuit) # "target gate"
return jtrace_diff(A, B, model_b.basis)
Circuit_jt_diff = _modf.modelfn_factory(circuit_jtrace_diff)
# init args == (model_a, model_b, circuit)
if _CVXPY_AVAILABLE:
class CircuitHalfDiamondNorm(_modf.ModelFunction):
"""
1/2 diamond norm of difference between productA(circuit) and productB(circuit)
Parameters
----------
model_a : Model
The first model (to evaluate productA)
model_b : Model
The second model (to evaluate productB)
circuit : Circuit
The circuit.
"""
def __init__(self, model_a, model_b, circuit):
self.circuit = circuit
self.B = model_b.sim.product(circuit)
self.d = int(round(_np.sqrt(model_a.dim)))
_modf.ModelFunction.__init__(self, model_a, ["all"])
def evaluate(self, model):
"""
Evaluate this function at `model`
Parameters
----------
model : Model
Model to evaluate at.
Returns
-------
float
"""
A = model.sim.product(self.circuit)
dm, W = _tools.diamonddist(A, self.B, model.basis,
return_x=True)
self.W = W
return 0.5 * dm
def evaluate_nearby(self, nearby_model):
"""
Evaluate at a nearby model
Parameters
----------
nearby_model : Model
A model nearby in parameter space.
Returns
-------
float
"""
mxBasis = nearby_model.basis
JAstd = self.d * _tools.fast_jamiolkowski_iso_std(
nearby_model.sim.product(self.circuit), mxBasis)
JBstd = self.d * _tools.fast_jamiolkowski_iso_std(self.B, mxBasis)
Jt = (JBstd - JAstd).T
return 0.5 * _np.trace(_np.dot(Jt.real, self.W.real) + _np.dot(Jt.imag, self.W.imag))
#def circuit_half_diamond_norm(model_a, model_b, circuit):
# A = model_a.sim.product(circuit) # "gate"
# B = model_b.sim.product(circuit) # "target gate"
# return half_diamond_norm(A, B, model_b.basis)
#CircuitHalfDiamondNorm = _modf.modelfn_factory(circuit_half_diamond_norm)
# # init args == (model_a, model_b, circuit)
else:
circuit_half_diamond_norm = None
CircuitHalfDiamondNorm = _null_fn
def circuit_nonunitary_entanglement_infidelity(model_a, model_b, circuit):
"""
Nonunitary entanglement infidelity between productA(circuit) and productB(circuit)
Parameters
----------
model_a : Model
The first model (to evaluate productA)
model_b : Model
The second model (to evaluate productB)
circuit : Circuit
The circuit.
Returns
-------
float
"""
A = model_a.sim.product(circuit) # "gate"
B = model_b.sim.product(circuit) # "target gate"
return nonunitary_entanglement_infidelity(A, B, model_b.basis)
Circuit_nonunitary_entanglement_infidelity = _modf.modelfn_factory(circuit_nonunitary_entanglement_infidelity)
# init args == (model_a, model_b, circuit)
def circuit_nonunitary_avg_gate_infidelity(model_a, model_b, circuit):
"""
Nonunitary average gate infidelity between productA(circuit) and productB(circuit).
Parameters
----------
model_a : Model
The first model (to evaluate productA)
model_b : Model
The second model (to evaluate productB)
circuit : Circuit
The circuit.
Returns
-------
float
"""
A = model_a.sim.product(circuit) # "gate"
B = model_b.sim.product(circuit) # "target gate"
return nonunitary_avg_gate_infidelity(A, B, model_b.basis)
Circuit_nonunitary_avg_gate_infidelity = _modf.modelfn_factory(circuit_nonunitary_avg_gate_infidelity)
# init args == (model_a, model_b, circuit)
def circuit_eigenvalue_entanglement_infidelity(model_a, model_b, circuit):
"""
Eigenvalue entanglement infidelity between productA(circuit) and productB(circuit).
Parameters
----------
model_a : Model
The first model (to evaluate productA)
model_b : Model
The second model (to evaluate productB)
circuit : Circuit
The circuit.
Returns
-------
float
"""
A = model_a.sim.product(circuit) # "gate"
B = model_b.sim.product(circuit) # "target gate"
return eigenvalue_entanglement_infidelity(A, B, model_b.basis)
Circuit_eigenvalue_entanglement_infidelity = _modf.modelfn_factory(circuit_eigenvalue_entanglement_infidelity)
# init args == (model_a, model_b, circuit)
def circuit_eigenvalue_avg_gate_infidelity(model_a, model_b, circuit):
"""
Eigenvalue average gate infidelity between productA(circuit) and productB(circuit).
Parameters
----------
model_a : Model
The first model (to evaluate productA)
model_b : Model
The second model (to evaluate productB)
circuit : Circuit
The circuit.
Returns
-------
float
"""
A = model_a.sim.product(circuit) # "gate"
B = model_b.sim.product(circuit) # "target gate"
return eigenvalue_avg_gate_infidelity(A, B, model_b.basis)
Circuit_eigenvalue_avg_gate_infidelity = _modf.modelfn_factory(circuit_eigenvalue_avg_gate_infidelity)
# init args == (model_a, model_b, circuit)
def circuit_eigenvalue_nonunitary_entanglement_infidelity(model_a, model_b, circuit):
"""
Eigenvalue nonunitary entanglement infidelity between productA(circuit) and productB(circuit).
Parameters
----------
model_a : Model
The first model (to evaluate productA)
model_b : Model
The second model (to evaluate productB)
circuit : Circuit
The circuit.
Returns
-------
float
"""
A = model_a.sim.product(circuit) # "gate"
B = model_b.sim.product(circuit) # "target gate"
return eigenvalue_nonunitary_entanglement_infidelity(A, B, model_b.basis)
Circuit_eigenvalue_nonunitary_entanglement_infidelity = _modf.modelfn_factory(
circuit_eigenvalue_nonunitary_entanglement_infidelity)
# init args == (model_a, model_b, circuit)
def circuit_eigenvalue_nonunitary_avg_gate_infidelity(model_a, model_b, circuit):
"""
Eigenvalue nonunitary average gate infidelity between productA(circuit) and productB(circuit).
Parameters
----------
model_a : Model
The first model (to evaluate productA)
model_b : Model
The second model (to evaluate productB)
circuit : Circuit
The circuit.
Returns
-------
float
"""
A = model_a.sim.product(circuit) # "gate"
B = model_b.sim.product(circuit) # "target gate"
return eigenvalue_nonunitary_avg_gate_infidelity(A, B, model_b.basis)
Circuit_eigenvalue_nonunitary_avg_gate_infidelity = _modf.modelfn_factory(
circuit_eigenvalue_nonunitary_avg_gate_infidelity)
# init args == (model_a, model_b, circuit)
def circuit_eigenvalue_diamondnorm(model_a, model_b, circuit):
"""
Eigenvalue diamond distance between productA(circuit) and productB(circuit).
Parameters
----------
model_a : Model
The first model (to evaluate productA)
model_b : Model
The second model (to evaluate productB)
circuit : Circuit
The circuit.
Returns
-------
float
"""
A = model_a.sim.product(circuit) # "gate"
B = model_b.sim.product(circuit) # "target gate"
return eigenvalue_diamondnorm(A, B, model_b.basis)
Circuit_eigenvalue_diamondnorm = _modf.modelfn_factory(circuit_eigenvalue_diamondnorm)
# init args == (model_a, model_b, circuit)
def circuit_eigenvalue_nonunitary_diamondnorm(model_a, model_b, circuit):
"""
Eigenvalue nonunitary diamond distance between productA(circuit) and productB(circuit).
Parameters
----------
model_a : Model
The first model (to evaluate productA)
model_b : Model
The second model (to evaluate productB)
circuit : Circuit
The circuit.
Returns
-------
float
"""
A = model_a.sim.product(circuit) # "gate"
B = model_b.sim.product(circuit) # "target gate"
return eigenvalue_nonunitary_diamondnorm(A, B, model_b.basis)
Circuit_eigenvalue_nonunitary_diamondnorm = _modf.modelfn_factory(circuit_eigenvalue_nonunitary_diamondnorm)
# init args == (model_a, model_b, circuit)
def povm_entanglement_infidelity(model_a, model_b, povmlbl):
"""
POVM entanglement infidelity between `model_a` and `model_b`.
Equal to `1 - entanglement_fidelity(POVM_MAP)` where `POVM_MAP` is
the extension of the POVM from the classical space of k-outcomes
to the space of (diagonal) k by k density matrices.
Parameters
----------
model_a : Model
The first model.
model_b : Model
The second model.
povmlbl : Label
The POVM label (must be present in both models).
Returns
-------
float
"""
return 1.0 - _tools.povm_fidelity(model_a, model_b, povmlbl)
POVM_entanglement_infidelity = _modf.povmfn_factory(povm_entanglement_infidelity)
# init args == (model1, model_b, povmlbl)
def povm_jtrace_diff(model_a, model_b, povmlbl):
"""
POVM Jamiolkowski trace distance between `model_a` and `model_b`
Equal to `Jamiolkowski_trace_distance(POVM_MAP)` where `POVM_MAP` is the
extension of the POVM from the classical space of k-outcomes to the space of
(diagonal) k by k density matrices.
Parameters
----------
model_a : Model
The first model.
model_b : Model
The second model.
povmlbl : Label
The POVM label (must be present in both models).
Returns
-------
float
"""
return _tools.povm_jtracedist(model_a, model_b, povmlbl)
POVM_jt_diff = _modf.povmfn_factory(povm_jtrace_diff)
# init args == (model1, model_b, povmlbl)
if _CVXPY_AVAILABLE:
def povm_half_diamond_norm(model_a, model_b, povmlbl):
"""
Half the POVM diamond distance between `model_a` and `model_b`.
Equal to `half_diamond_dist(POVM_MAP)` where `POVM_MAP` is the extension
of the POVM from the classical space of k-outcomes to the space of
(diagonal) k by k density matrices.
Parameters
----------
model_a : Model
The first model.
model_b : Model
The second model.
povmlbl : Label
The POVM label (must be present in both models).
Returns
-------
float
"""
return 0.5 * _tools.povm_diamonddist(model_a, model_b, povmlbl)
POVM_half_diamond_norm = _modf.povmfn_factory(povm_half_diamond_norm)
else:
povm_half_diamond_norm = None
POVM_half_diamond_norm = _null_fn
def decomposition(gate):
"""
DEPRECATED: Decompose a 1Q `gate` into rotations about axes.
Parameters
----------
gate : numpy.ndarray
the transfer-matrix specifying a gate's action.
Returns
-------
ReportableQty
"""
decompDict = _tools.decompose_gate_matrix(gate)
if decompDict['isValid']:
#angleQty = decompDict.get('pi rotations',0)
#diagQty = decompDict.get('decay of diagonal rotation terms',0)
#offdiagQty = decompDict.get('decay of off diagonal rotation terms',0)
errBarDict = {'pi rotations': None,
'decay of diagonal rotation terms': None,
'decay of off diagonal rotation terms': None}
return _ReportableQty(decompDict, errBarDict)
else:
return _ReportableQty({})
def upper_bound_fidelity(gate, mx_basis):
"""
Upper bound on entanglement fidelity
Parameters
----------
gate : numpy.ndarray
the transfer-matrix specifying a gate's action.
mx_basis : Basis or {'pp', 'gm', 'std'}
the basis that `gate` is in.
Returns
-------
float
"""
return _tools.fidelity_upper_bound(gate)[0]
Upper_bound_fidelity = _modf.opfn_factory(upper_bound_fidelity)
# init args == (model, op_label)
def closest_ujmx(gate, mx_basis):
"""
Jamiolkowski state of closest unitary to `gate`
Parameters
----------
gate : numpy.ndarray
the transfer-matrix specifying a gate's action.
mx_basis : Basis or {'pp', 'gm', 'std'}
the basis that `gate` is in.
Returns
-------
float
"""
closestUOpMx = _alg.find_closest_unitary_opmx(gate)
return _tools.jamiolkowski_iso(closestUOpMx, mx_basis, mx_basis)
Closest_ujmx = _modf.opfn_factory(closest_ujmx)
# init args == (model, op_label)
def maximum_fidelity(gate, mx_basis):
"""
Fidelity between `gate` and its closest unitary
Parameters
----------
gate : numpy.ndarray
the transfer-matrix specifying a gate's action.
mx_basis : Basis or {'pp', 'gm', 'std'}
the basis that `gate` is in.
Returns
-------
float
"""
closestUOpMx = _alg.find_closest_unitary_opmx(gate)
closestUJMx = _tools.jamiolkowski_iso(closestUOpMx, mx_basis, mx_basis)
choi = _tools.jamiolkowski_iso(gate, mx_basis, mx_basis)
return _tools.fidelity(closestUJMx, choi)
Maximum_fidelity = _modf.opfn_factory(maximum_fidelity)
# init args == (model, op_label)
def maximum_trace_dist(gate, mx_basis):
"""
Jamiolkowski trace distance between `gate` and its closest unitary
Parameters
----------
gate : numpy.ndarray
the transfer-matrix specifying a gate's action.
mx_basis : Basis or {'pp', 'gm', 'std'}
the basis that `gate` is in.
Returns
-------
float
"""
closestUOpMx = _alg.find_closest_unitary_opmx(gate)
#closestUJMx = _tools.jamiolkowski_iso(closestUOpMx, mx_basis, mx_basis)
_tools.jamiolkowski_iso(closestUOpMx, mx_basis, mx_basis)
return _tools.jtracedist(gate, closestUOpMx)
Maximum_trace_dist = _modf.opfn_factory(maximum_trace_dist)
# init args == (model, op_label)
def angles_btwn_rotn_axes(model):
"""
Array of angles between the rotation axes of the gates of `model`.
Parameters
----------
model : Model
The model to process.