-
Notifications
You must be signed in to change notification settings - Fork 55
/
likelihoodfns.py
955 lines (778 loc) · 45.1 KB
/
likelihoodfns.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
"""
Functions related to computation of the log-likelihood.
"""
#***************************************************************************************************
# 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.
#***************************************************************************************************
import warnings as _warnings
import numpy as _np
import scipy.stats as _stats
from pygsti.tools import basistools as _bt
from pygsti.tools import jamiolkowski as _jam
from pygsti.tools import listtools as _lt
#from ..baseobjs.smartcache import smart_cached
TOL = 1e-20
# The log(Likelihood) within the standard (non-Poisson) picture is:
#
# L = prod_{i,sl} p_{i,sl}^N_{i,sl}
#
# Where i indexes the circuit, and sl indexes the spam label. N[i] is the total counts
# for the i-th circuit, and so sum_{sl} N_{i,sl} == N[i]. We can take the log:
#
# log L = sum_{i,sl} N_{i,sl} log(p_{i,sl})
#
# after patching (linear extrapolation below min_p and ignore f == 0 terms ( 0*log(0) == 0 ) ):
#
# logl = sum_{i,sl} N_{i,sl} log(p_{i,sl}) if p_{i,sl} >= min_p and N_{i,sl} > 0 # noqa
# N_{i,sl} log(min_p) + S * (p_{i,sl} - min_p) + S2 * (p_{i,sl} - min_p)**2 if p_{i,sl} < p_min and N_{i,sl} > 0 # noqa
# 0 if N_{i,sl} == 0 # noqa
#
# dlogL = sum_{i,sl} N_{i,sl} / p_{i,sl} * dp if p_{i,sl} >= min_p and N_{i,sl} > 0 # noqa
# (S + 2*S2*(p_{i,sl} - min_p)) * dp if p_{i,sl} < p_min and N_{i,sl} > 0 # noqa
# 0 if N_{i,sl} == 0 # noqa
#
# hlogL = sum_{i,sl} -N_{i,sl} / p_{i,sl}**2 * dp1 * dp2 + N_{i,sl} / p_{i,sl} *hp if p_{i,sl} >= min_p and N_{i,sl} > 0 # noqa
# 2*S2* dp1 * dp2 + (S + 2*S2*(p_{i,sl} - min_p)) * hp if p_{i,sl} < p_min and N_{i,sl} > 0 # noqa
# 0 if N_{i,sl} == 0 # noqa
#
# where S = N_{i,sl} / min_p is the slope of the line tangent to logl at min_p
# and S2 = 0.5*( -N_{i,sl} / min_p**2 ) is 1/2 the 2nd derivative of the logl term at min_p
# and hlogL == d/d1 ( d/d2 ( logl ) ) -- i.e. dp2 is the *first* derivative performed...
#Note: Poisson picture entered use when we allowed an EVec which was 1-{other EVecs} -- a
# (0,-1) spam index -- instead of assuming all probabilities of a given gat string summed
# to one -- a (-1,-1) spam index. The poisson picture gives a correct log-likelihood
# description when the probabilities (for a given circuit) may not sum to one, by
# interpreting them each as rates. In the standard picture, large circuit probabilities
# are not penalized (each standard logL term increases monotonically with each probability,
# and the reason this is ok when the probabilities sum to one is that for a probabilility
# that gets close to 1, there's another that is close to zero, and logL is very negative
# near zero.
# The log(Likelihood) within the Poisson picture is:
#
# L = prod_{i,sl} lambda_{i,sl}^N_{i,sl} e^{-lambda_{i,sl}} / N_{i,sl}!
#
# Where lamba_{i,sl} := p_{i,sl}*N[i] is a rate, i indexes the circuit,
# and sl indexes the spam label. N[i] is the total counts for the i-th circuit, and
# so sum_{sl} N_{i,sl} == N[i]. We can ignore the p-independent N_j! and take the log:
#
# log L = sum_{i,sl} N_{i,sl} log(N[i]*p_{i,sl}) - N[i]*p_{i,sl}
# = sum_{i,sl} N_{i,sl} log(p_{i,sl}) - N[i]*p_{i,sl} (where we ignore the p-independent log(N[i]) terms)
#
# after patching (linear extrapolation below min_p and "softening" f == 0 terms w/cubic below radius "a"):
#
# logl = sum_{i,sl} N_{i,sl} log(p_{i,sl}) - N[i]*p_{i,sl} if p_{i,sl} >= min_p and N_{i,sl} > 0 # noqa
# N_{i,sl} log(min_p) - N[i]*min_p + S * (p_{i,sl} - min_p) + S2 * (p_{i,sl} - min_p)**2 if p_{i,sl} < p_min and N_{i,sl} > 0 # noqa
# 0 - N[i]*p_{i,sl} if N_{i,sl} == 0 and p_{i,sl} >= a # noqa
# 0 - N[i]*( -(1/(3a**2))p_{i,sl}**3 + p_{i,sl}**2/a + (1/3)*a ) if N_{i,sl} == 0 and p_{i,sl} < a # noqa
# - N[i]*Y(1-sum(p_omitted)) added to "first" N_{i,sl} > 0 entry for omitted probabilities, where
# Y(p) = p if p >= a else ( -(1/(3a**2))p**3 + p**2/a + (1/3)*a )
#
# dlogL = sum_{i,sl} [ N_{i,sl} / p_{i,sl} - N[i] ] * dp if p_{i,sl} >= min_p and N_{i,sl} > 0 # noqa
# (S + 2*S2*(p_{i,sl} - min_p)) * dp if p_{i,sl} < p_min and N_{i,sl} > 0 # noqa
# -N[i] * dp if N_{i,sl} == 0 and p_{i,sl} >= a # noqa
# -N[i] * ( (-1/a**2)p_{i,sl}**2 + 2*p_{i,sl}/a ) * dp if N_{i,sl} == 0 and p_{i,sl} < a
# +N[i]*sum(dY/dp_omitted * dp_omitted) added to "first" N_{i,sl} > 0 entry for omitted probabilities
#
# hlogL = sum_{i,sl} -N_{i,sl} / p_{i,sl}**2 * dp1 * dp2 + [ N_{i,sl} / p_{i,sl} - N[i] ]*hp if p_{i,sl} >= min_p and N_{i,sl} > 0 # noqa
# 2*S2* dp1 * dp2 + (S + 2*S2*(p_{i,sl} - min_p)) * hp if p_{i,sl} < p_min and N_{i,sl} > 0 # noqa
# -N[i] * hp if N_{i,sl} == 0 and p_{i,sl} >= a # noqa
# -N[i]*( (-2/a**2)p_{i,sl} + 2/a ) * dp1 * dp2 # noqa
# - N[i]*( (-1/a**2)p_{i,sl}**2 + 2*p_{i,sl}/a ) * hp if N_{i,sl} == 0 and p_{i,sl} < a # noqa
# +N[i]*sum(d2Y/dp_omitted2 * dp_omitted1 * dp_omitted2 +
# dY/dp_omitted * hp_omitted) added to "first" N_{i,sl} > 0 entry for omitted probabilities # noqa
#
# where S = N_{i,sl} / min_p - N[i] is the slope of the line tangent to logl at min_p
# and S2 = 0.5*( -N_{i,sl} / min_p**2 ) is 1/2 the 2nd derivative of the logl term at min_p so
# logL_term = logL_term(min_p) + S * (p-min_p) + S2 * (p-min_p)**2
# and hlogL == d/d1 ( d/d2 ( logl ) ) -- i.e. dp2 is the *first* derivative performed...
#
# For cubic interpolation, use function F(p) (derived by Robin: match value, 1st-deriv, 2nd-deriv at p == r, and require
# min at p == 0):
# Given a radius r << 1 (but r>0):
# F(p) = piecewise{ if( p>r ) then p; else -(1/3)*p^3/r^2 + p^2/r + (1/3)*r } y = p/r
# Note p < r portion: F(p) = p/3 * (-p^2/r^2 + 3*p/r + r) -- zero at p=0, p at p=r
# 1st deriv = -p^2/r^2 + 2*p/r = -p/r (p/r - 2) -- matches slope=1 at p=r
# 2nd deriv = -2*p/r^2 + 2/r = -2/r * (p/r - 1) -- curvature=0 at p=r
# OLD: quadratic that doesn't match 2nd-deriv:
# F(p) = piecewise{ if( p>r ) then p; else (r-p)^2/(2*r) + p }
def logl(model, dataset, circuits=None,
min_prob_clip=1e-6, prob_clip_interval=(-1e6, 1e6), radius=1e-4,
poisson_picture=True, op_label_aliases=None, wildcard=None,
mdc_store=None, comm=None, mem_limit=None):
"""
The log-likelihood function.
Parameters
----------
model : Model
Model of parameterized gates
dataset : DataSet
Probability data
circuits : list of (tuples or Circuits), optional
Each element specifies a circuit to include in the log-likelihood
sum. Default value of None implies all the circuits in dataset
should be used.
min_prob_clip : float, optional
The minimum probability treated normally in the evaluation of the log-likelihood.
A penalty function replaces the true log-likelihood for probabilities that lie
below this threshold so that the log-likelihood never becomes undefined (which improves
optimizer performance).
prob_clip_interval : 2-tuple or None, optional
(min,max) values used to clip the probabilities predicted by models during MLEGST's
search for an optimal model (if not None). if None, no clipping is performed.
radius : float, optional
Specifies the severity of rounding used to "patch" the zero-frequency
terms of the log-likelihood.
poisson_picture : boolean, optional
Whether the log-likelihood-in-the-Poisson-picture terms should be included
in the returned logl value.
op_label_aliases : dictionary, optional
Dictionary whose keys are operation label "aliases" and whose values are tuples
corresponding to what that operation label should be expanded into before querying
the dataset. Defaults to the empty dictionary (no aliases defined)
e.g. op_label_aliases['Gx^3'] = ('Gx','Gx','Gx')
wildcard : WildcardBudget
A wildcard budget to apply to this log-likelihood computation.
This increases the returned log-likelihood value by adjusting
(by a maximal amount measured in TVD, given by the budget) the
probabilities produced by `model` to optimially match the data
(within the bugetary constraints) evaluating the log-likelihood.
mdc_store : ModelDatasetCircuitsStore, optional
An object that bundles cached quantities along with a given model, dataset, and circuit
list. If given, `model` and `dataset` and `circuits` should be set to None.
comm : mpi4py.MPI.Comm, optional
When not None, an MPI communicator for distributing the computation
across multiple processors.
mem_limit : int, optional
A rough memory limit in bytes which restricts the amount of intermediate
values that are computed and stored.
Returns
-------
float
The log likelihood
"""
v = logl_per_circuit(model, dataset, circuits,
min_prob_clip, prob_clip_interval, radius,
poisson_picture, op_label_aliases, wildcard,
mdc_store, comm, mem_limit)
return _np.sum(v) # sum over *all* dimensions
def logl_per_circuit(model, dataset, circuits=None,
min_prob_clip=1e-6, prob_clip_interval=(-1e6, 1e6), radius=1e-4,
poisson_picture=True, op_label_aliases=None, wildcard=None,
mdc_store=None, comm=None, mem_limit=None):
"""
Computes the per-circuit log-likelihood contribution for a set of circuits.
Parameters
----------
model : Model
Model of parameterized gates
dataset : DataSet
Probability data
circuits : list of (tuples or Circuits), optional
Each element specifies a circuit to include in the log-likelihood
sum. Default value of None implies all the circuits in dataset
should be used.
min_prob_clip : float, optional
The minimum probability treated normally in the evaluation of the log-likelihood.
A penalty function replaces the true log-likelihood for probabilities that lie
below this threshold so that the log-likelihood never becomes undefined (which improves
optimizer performance).
prob_clip_interval : 2-tuple or None, optional
(min,max) values used to clip the probabilities predicted by models during MLEGST's
search for an optimal model (if not None). if None, no clipping is performed.
radius : float, optional
Specifies the severity of rounding used to "patch" the zero-frequency
terms of the log-likelihood.
poisson_picture : boolean, optional
Whether the log-likelihood-in-the-Poisson-picture terms should be included
in the returned logl value.
op_label_aliases : dictionary, optional
Dictionary whose keys are operation label "aliases" and whose values are tuples
corresponding to what that operation label should be expanded into before querying
the dataset. Defaults to the empty dictionary (no aliases defined)
e.g. op_label_aliases['Gx^3'] = ('Gx','Gx','Gx')
wildcard : WildcardBudget
A wildcard budget to apply to this log-likelihood computation.
This increases the returned log-likelihood value by adjusting
(by a maximal amount measured in TVD, given by the budget) the
probabilities produced by `model` to optimially match the data
(within the bugetary constraints) evaluating the log-likelihood.
mdc_store : ModelDatasetCircuitsStore, optional
An object that bundles cached quantities along with a given model, dataset, and circuit
list. If given, `model` and `dataset` and `circuits` should be set to None.
comm : mpi4py.MPI.Comm, optional
When not None, an MPI communicator for distributing the computation
across multiple processors.
mem_limit : int, optional
A rough memory limit in bytes which restricts the amount of intermediate
values that are computed and stored.
Returns
-------
numpy.ndarray
Array of length either `len(circuits)` or `len(dataset.keys())`.
Values are the log-likelihood contributions of the corresponding
circuit aggregated over outcomes.
"""
from ..objectivefns import objectivefns as _objfns
regularization = {'min_prob_clip': min_prob_clip, 'radius': radius} if poisson_picture \
else {'min_prob_clip': min_prob_clip} # non-poisson-pic logl has no radius
obj_max = _objfns._objfn(_objfns.MaxLogLFunction, model, dataset, circuits,
mdc_store=mdc_store, method_names=('percircuit',),
op_label_aliases=op_label_aliases, poisson_picture=poisson_picture)
obj_cls = _objfns.PoissonPicDeltaLogLFunction if poisson_picture else _objfns.DeltaLogLFunction
obj = _objfns._objfn(obj_cls, model, dataset, circuits,
regularization, {'prob_clip_interval': prob_clip_interval},
op_label_aliases, comm, mem_limit, ('percircuit',), (), mdc_store)
if wildcard:
assert(poisson_picture), "Wildcard budgets can only be used with `poisson_picture=True`"
obj.terms() # objfn used within wildcard objective fn must be pre-evaluated
obj = _objfns.LogLWildcardFunction(obj, model.to_vector(), wildcard)
local = obj_max.percircuit() - obj.percircuit()
return obj.layout.allgather_local_array('c', local)
def logl_jacobian(model, dataset, circuits=None,
min_prob_clip=1e-6, prob_clip_interval=(-1e6, 1e6), radius=1e-4,
poisson_picture=True, op_label_aliases=None, mdc_store=None,
comm=None, mem_limit=None, verbosity=0):
"""
The jacobian of the log-likelihood function.
Parameters
----------
model : Model
Model of parameterized gates (including SPAM)
dataset : DataSet
Probability data
circuits : list of (tuples or Circuits), optional
Each element specifies a circuit to include in the log-likelihood
sum. Default value of None implies all the circuits in dataset
should be used.
min_prob_clip : float, optional
The minimum probability treated normally in the evaluation of the log-likelihood.
A penalty function replaces the true log-likelihood for probabilities that lie
below this threshold so that the log-likelihood never becomes undefined (which improves
optimizer performance).
prob_clip_interval : 2-tuple or None, optional
(min,max) values used to clip the probabilities predicted by models during MLEGST's
search for an optimal model (if not None). if None, no clipping is performed.
radius : float, optional
Specifies the severity of rounding used to "patch" the zero-frequency
terms of the log-likelihood.
poisson_picture : boolean, optional
Whether the Poisson-picutre log-likelihood should be differentiated.
op_label_aliases : dictionary, optional
Dictionary whose keys are operation label "aliases" and whose values are tuples
corresponding to what that operation label should be expanded into before querying
the dataset. Defaults to the empty dictionary (no aliases defined)
e.g. op_label_aliases['Gx^3'] = ('Gx','Gx','Gx')
mdc_store : ModelDatasetCircuitsStore, optional
An object that bundles cached quantities along with a given model, dataset, and circuit
list. If given, `model` and `dataset` and `circuits` should be set to None.
comm : mpi4py.MPI.Comm, optional
When not None, an MPI communicator for distributing the computation
across multiple processors.
mem_limit : int, optional
A rough memory limit in bytes which restricts the amount of intermediate
values that are computed and stored.
verbosity : int, optional
How much detail to print to stdout.
Returns
-------
numpy array
array of shape (M,), where M is the length of the vectorized model.
"""
from ..objectivefns import objectivefns as _objfns
regularization = {'min_prob_clip': min_prob_clip, 'radius': radius} if poisson_picture \
else {'min_prob_clip': min_prob_clip} # non-poisson-pic logl has no radius
obj_cls = _objfns.PoissonPicDeltaLogLFunction if poisson_picture else _objfns.DeltaLogLFunction
obj = _objfns._objfn(obj_cls, model, dataset, circuits,
regularization, {'prob_clip_interval': prob_clip_interval},
op_label_aliases, comm, mem_limit, ('jacobian',), (), mdc_store, verbosity)
local = -obj.jacobian() # negative b/c objective is deltaLogL = max_logl - logL
return obj.layout.allgather_local_array('ep', local)
def logl_hessian(model, dataset, circuits=None,
min_prob_clip=1e-6, prob_clip_interval=(-1e6, 1e6), radius=1e-4,
poisson_picture=True, op_label_aliases=None, mdc_store=None,
comm=None, mem_limit=None, verbosity=0):
"""
The hessian of the log-likelihood function.
Parameters
----------
model : Model
Model of parameterized gates (including SPAM)
dataset : DataSet
Probability data
circuits : list of (tuples or Circuits), optional
Each element specifies a circuit to include in the log-likelihood
sum. Default value of None implies all the circuits in dataset
should be used.
min_prob_clip : float, optional
The minimum probability treated normally in the evaluation of the log-likelihood.
A penalty function replaces the true log-likelihood for probabilities that lie
below this threshold so that the log-likelihood never becomes undefined (which improves
optimizer performance).
prob_clip_interval : 2-tuple or None, optional
(min,max) values used to clip the probabilities predicted by
models during MLEGST's search for an optimal model (if not None).
if None, no clipping is performed.
radius : float, optional
Specifies the severity of rounding used to "patch" the zero-frequency
terms of the log-likelihood.
poisson_picture : boolean, optional
Whether the Poisson-picutre log-likelihood should be differentiated.
op_label_aliases : dictionary, optional
Dictionary whose keys are operation label "aliases" and whose values are tuples
corresponding to what that operation label should be expanded into before querying
the dataset. Defaults to the empty dictionary (no aliases defined)
e.g. op_label_aliases['Gx^3'] = ('Gx','Gx','Gx')
mdc_store : ModelDatasetCircuitsStore, optional
An object that bundles cached quantities along with a given model, dataset, and circuit
list. If given, `model` and `dataset` and `circuits` should be set to None.
comm : mpi4py.MPI.Comm, optional
When not None, an MPI communicator for distributing the computation
across multiple processors.
mem_limit : int, optional
A rough memory limit in bytes which restricts the amount of intermediate
values that are computed and stored.
verbosity : int, optional
How much detail to print to stdout.
Returns
-------
numpy array or None
On the root processor, the Hessian matrix of shape (nModelParams, nModelParams),
where nModelParams = `model.num_params`. `None` on non-root processors.
"""
from ..objectivefns import objectivefns as _objfns
regularization = {'min_prob_clip': min_prob_clip, 'radius': radius} if poisson_picture \
else {'min_prob_clip': min_prob_clip} # non-poisson-pic logl has no radius
obj_cls = _objfns.PoissonPicDeltaLogLFunction if poisson_picture else _objfns.DeltaLogLFunction
obj = _objfns._objfn(obj_cls, model, dataset, circuits,
regularization, {'prob_clip_interval': prob_clip_interval},
op_label_aliases, comm, mem_limit, ('hessian',), (), mdc_store, verbosity)
hessian = obj.hessian() # Note: hessian is only assembled on root processor
return -hessian if (comm is None or comm.rank == 0) else None
# negative b/c objective is deltaLogL = max_logl - logL
def logl_approximate_hessian(model, dataset, circuits=None,
min_prob_clip=1e-6, prob_clip_interval=(-1e6, 1e6), radius=1e-4,
poisson_picture=True, op_label_aliases=None, mdc_store=None,
comm=None, mem_limit=None, verbosity=0):
"""
An approximate Hessian of the log-likelihood function.
An approximation to the true Hessian is computed using just the Jacobian
(and *not* the Hessian) of the probabilities w.r.t. the model
parameters. Let `J = d(probs)/d(params)` and denote the Hessian of the
log-likelihood w.r.t. the probabilities as `d2(logl)/dprobs2` (a *diagonal*
matrix indexed by the term, i.e. probability, of the log-likelihood). Then
this function computes:
`H = J * d2(logl)/dprobs2 * J.T`
Which simply neglects the `d2(probs)/d(params)2` terms of the true Hessian.
Since this curvature is expected to be small at the MLE point, this
approximation can be useful for computing approximate error bars.
Parameters
----------
model : Model
Model of parameterized gates (including SPAM)
dataset : DataSet
Probability data
circuits : list of (tuples or Circuits), optional
Each element specifies a circuit to include in the log-likelihood
sum. Default value of None implies all the circuits in dataset
should be used.
min_prob_clip : float, optional
The minimum probability treated normally in the evaluation of the log-likelihood.
A penalty function replaces the true log-likelihood for probabilities that lie
below this threshold so that the log-likelihood never becomes undefined (which improves
optimizer performance).
prob_clip_interval : 2-tuple or None, optional
(min,max) values used to clip the probabilities predicted by models during MLEGST's
search for an optimal model (if not None). if None, no clipping is performed.
radius : float, optional
Specifies the severity of rounding used to "patch" the zero-frequency
terms of the log-likelihood.
poisson_picture : boolean, optional
Whether the Poisson-picutre log-likelihood should be differentiated.
op_label_aliases : dictionary, optional
Dictionary whose keys are operation label "aliases" and whose values are tuples
corresponding to what that operation label should be expanded into before querying
the dataset. Defaults to the empty dictionary (no aliases defined)
e.g. op_label_aliases['Gx^3'] = ('Gx','Gx','Gx')
mdc_store : ModelDatasetCircuitsStore, optional
An object that bundles cached quantities along with a given model, dataset, and circuit
list. If given, `model` and `dataset` and `circuits` should be set to None.
comm : mpi4py.MPI.Comm, optional
When not None, an MPI communicator for distributing the computation
across multiple processors.
mem_limit : int, optional
A rough memory limit in bytes which restricts the amount of intermediate
values that are computed and stored.
verbosity : int, optional
How much detail to print to stdout.
Returns
-------
numpy array or None
On the root processor, the approximate Hessian matrix of shape (nModelParams, nModelParams),
where nModelParams = `model.num_params`. `None` on non-root processors.
"""
from ..objectivefns import objectivefns as _objfns
obj_cls = _objfns.PoissonPicDeltaLogLFunction if poisson_picture else _objfns.DeltaLogLFunction
obj = _objfns._objfn(obj_cls, model, dataset, circuits,
{'min_prob_clip': min_prob_clip,
'radius': radius},
{'prob_clip_interval': prob_clip_interval},
op_label_aliases, comm, mem_limit, ('approximate_hessian',), (), mdc_store, verbosity)
hessian = obj.approximate_hessian() # Note: hessian is only assembled on root processor
return -hessian if (comm is None or comm.rank == 0) else None
# negative b/c objective is deltaLogL = max_logl - logL
def logl_max(model, dataset, circuits=None, poisson_picture=True,
op_label_aliases=None, mdc_store=None):
"""
The maximum log-likelihood possible for a DataSet.
That is, the log-likelihood obtained by a maximal model that can
fit perfectly the probability of each circuit.
Parameters
----------
model : Model
the model, used only for circuit compilation
dataset : DataSet
the data set to use.
circuits : list of (tuples or Circuits), optional
Each element specifies a circuit to include in the max-log-likelihood
sum. Default value of None implies all the circuits in dataset should
be used.
poisson_picture : boolean, optional
Whether the Poisson-picture maximum log-likelihood should be returned.
op_label_aliases : dictionary, optional
Dictionary whose keys are operation label "aliases" and whose values are tuples
corresponding to what that operation label should be expanded into before querying
the dataset. Defaults to the empty dictionary (no aliases defined)
e.g. op_label_aliases['Gx^3'] = ('Gx','Gx','Gx')
mdc_store : ModelDatasetCircuitsStore, optional
An object that bundles cached quantities along with a given model, dataset, and circuit
list. If given, `model` and `dataset` and `circuits` should be set to None.
Returns
-------
float
"""
from ..objectivefns import objectivefns as _objfns
obj_max = _objfns._objfn(_objfns.MaxLogLFunction, model, dataset, circuits, mdc_store=mdc_store,
op_label_aliases=op_label_aliases, poisson_picture=poisson_picture, method_names=('fn',))
return obj_max.fn() # gathers internally
def logl_max_per_circuit(model, dataset, circuits=None,
poisson_picture=True, op_label_aliases=None, mdc_store=None):
"""
The vector of maximum log-likelihood contributions for each circuit, aggregated over outcomes.
Parameters
----------
model : Model
the model, used only for circuit compilation
dataset : DataSet
the data set to use.
circuits : list of (tuples or Circuits), optional
Each element specifies a circuit to include in the max-log-likelihood
sum. Default value of None implies all the circuits in dataset should
be used.
poisson_picture : boolean, optional
Whether the Poisson-picture maximum log-likelihood should be returned.
op_label_aliases : dictionary, optional
Dictionary whose keys are operation label "aliases" and whose values are tuples
corresponding to what that operation label should be expanded into before querying
the dataset. Defaults to the empty dictionary (no aliases defined)
e.g. op_label_aliases['Gx^3'] = ('Gx','Gx','Gx')
mdc_store : ModelDatasetCircuitsStore, optional
An object that bundles cached quantities along with a given model, dataset, and circuit
list. If given, `model` and `dataset` and `circuits` should be set to None.
Returns
-------
numpy.ndarray
Array of length either `len(circuits)` or `len(dataset.keys())`.
Values are the maximum log-likelihood contributions of the corresponding
circuit aggregated over outcomes.
"""
from ..objectivefns import objectivefns as _objfns
obj_max = _objfns._objfn(_objfns.MaxLogLFunction, model, dataset, circuits, mdc_store=mdc_store,
op_label_aliases=op_label_aliases, poisson_picture=poisson_picture,
method_names=('percircuit',))
local = obj_max.percircuit()
return obj_max.layout.allgather_local_array('c', local)
def two_delta_logl_nsigma(model, dataset, circuits=None,
min_prob_clip=1e-6, prob_clip_interval=(-1e6, 1e6), radius=1e-4,
poisson_picture=True, op_label_aliases=None,
dof_calc_method='modeltest', wildcard=None):
"""
See docstring for :function:`pygsti.tools.two_delta_logl`
Parameters
----------
model : Model
Model of parameterized gates
dataset : DataSet
Probability data
circuits : list of (tuples or Circuits), optional
Each element specifies a circuit to include in the log-likelihood
sum. Default value of None implies all the circuits in dataset
should be used.
min_prob_clip : float, optional
The minimum probability treated normally in the evaluation of the log-likelihood.
A penalty function replaces the true log-likelihood for probabilities that lie
below this threshold so that the log-likelihood never becomes undefined (which improves
optimizer performance).
prob_clip_interval : 2-tuple or None, optional
(min,max) values used to clip the probabilities predicted by models during MLEGST's
search for an optimal model (if not None). if None, no clipping is performed.
radius : float, optional
Specifies the severity of rounding used to "patch" the zero-frequency
terms of the log-likelihood.
poisson_picture : boolean, optional
Whether the log-likelihood-in-the-Poisson-picture terms should be included
in the returned logl value.
op_label_aliases : dictionary, optional
Dictionary whose keys are operation label "aliases" and whose values are tuples
corresponding to what that operation label should be expanded into before querying
the dataset. Defaults to the empty dictionary (no aliases defined)
e.g. op_label_aliases['Gx^3'] = ('Gx','Gx','Gx')
dof_calc_method : {"all", "modeltest"}
How `model`'s number of degrees of freedom (parameters) are obtained
when computing the number of standard deviations and p-value relative to
a chi2_k distribution, where `k` is additional degrees of freedom
possessed by the maximal model. `"all"` uses `model.num_params` whereas
`"modeltest"` uses `model.num_modeltest_params` (the number of non-gauge
parameters by default).
wildcard : WildcardBudget
A wildcard budget to apply to this log-likelihood computation.
This increases the returned log-likelihood value by adjusting
(by a maximal amount measured in TVD, given by the budget) the
probabilities produced by `model` to optimially match the data
(within the bugetary constraints) evaluating the log-likelihood.
Returns
-------
float
"""
assert(dof_calc_method is not None)
return two_delta_logl(model, dataset, circuits,
min_prob_clip, prob_clip_interval, radius,
poisson_picture, op_label_aliases,
dof_calc_method, wildcard, None, None)[1]
def two_delta_logl(model, dataset, circuits=None,
min_prob_clip=1e-6, prob_clip_interval=(-1e6, 1e6), radius=1e-4,
poisson_picture=True, op_label_aliases=None,
dof_calc_method=None, wildcard=None,
mdc_store=None, comm=None):
"""
Twice the difference between the maximum and actual log-likelihood.
Optionally also can return the Nsigma (# std deviations from mean) and
p-value relative to expected chi^2 distribution (when `dof_calc_method`
is not None).
This function's arguments are supersets of :function:`logl`, and
:function:`logl_max`. This is a convenience function, equivalent to
`2*(logl_max(...) - logl(...))`, whose value is what is often called
the *log-likelihood-ratio* between the "maximal model" (that which trivially
fits the data exactly) and the model given by `model`.
Parameters
----------
model : Model
Model of parameterized gates
dataset : DataSet
Probability data
circuits : list of (tuples or Circuits), optional
Each element specifies a circuit to include in the log-likelihood
sum. Default value of None implies all the circuits in dataset
should be used.
min_prob_clip : float, optional
The minimum probability treated normally in the evaluation of the log-likelihood.
A penalty function replaces the true log-likelihood for probabilities that lie
below this threshold so that the log-likelihood never becomes undefined (which improves
optimizer performance).
prob_clip_interval : 2-tuple or None, optional
(min,max) values used to clip the probabilities predicted by models during MLEGST's
search for an optimal model (if not None). if None, no clipping is performed.
radius : float, optional
Specifies the severity of rounding used to "patch" the zero-frequency
terms of the log-likelihood.
poisson_picture : boolean, optional
Whether the log-likelihood-in-the-Poisson-picture terms should be included
in the computed log-likelihood values.
op_label_aliases : dictionary, optional
Dictionary whose keys are operation label "aliases" and whose values are tuples
corresponding to what that operation label should be expanded into before querying
the dataset. Defaults to the empty dictionary (no aliases defined)
e.g. op_label_aliases['Gx^3'] = ('Gx','Gx','Gx')
dof_calc_method : {None, "all", "modeltest"}
How `model`'s number of degrees of freedom (parameters) are obtained
when computing the number of standard deviations and p-value relative to
a chi2_k distribution, where `k` is additional degrees of freedom
possessed by the maximal model. If None, then `Nsigma` and `pvalue` are
not returned (see below).
wildcard : WildcardBudget
A wildcard budget to apply to this log-likelihood computation.
This increases the returned log-likelihood value by adjusting
(by a maximal amount measured in TVD, given by the budget) the
probabilities produced by `model` to optimially match the data
(within the bugetary constraints) evaluating the log-likelihood.
mdc_store : ModelDatasetCircuitsStore, optional
An object that bundles cached quantities along with a given model, dataset, and circuit
list. If given, `model` and `dataset` and `circuits` should be set to None.
comm : mpi4py.MPI.Comm, optional
When not None, an MPI communicator for distributing the computation
across multiple processors.
Returns
-------
twoDeltaLogL : float
2*(loglikelihood(maximal_model,data) - loglikelihood(model,data))
Nsigma, pvalue : float
Only returned when `dof_calc_method` is not None.
"""
from ..objectivefns import objectivefns as _objfns
obj_cls = _objfns.PoissonPicDeltaLogLFunction if poisson_picture else _objfns.DeltaLogLFunction
obj = _objfns._objfn(obj_cls, model, dataset, circuits,
{'min_prob_clip': min_prob_clip,
'radius': radius},
{'prob_clip_interval': prob_clip_interval},
op_label_aliases, comm, None, ('terms',), (), mdc_store)
if wildcard:
assert(poisson_picture), "Wildcard budgets can only be used with `poisson_picture=True`"
obj.terms() # objfn used within wildcard objective fn must be pre-evaluated
obj = _objfns.LogLWildcardFunction(obj, model.to_vector(), wildcard)
two_delta_logl = 2 * obj.fn() # gathers internally
if dof_calc_method is None:
return two_delta_logl
elif dof_calc_method == "modeltest":
mdl_dof = model.num_modeltest_params
elif dof_calc_method == "all":
mdl_dof = model.num_params
else: raise ValueError("Invalid `dof_calc_method` arg: %s" % dof_calc_method)
if circuits is not None:
ds_strs = _lt.apply_aliases_to_circuits(circuits, op_label_aliases)
else: ds_strs = None
ds_dof = dataset.degrees_of_freedom(ds_strs)
k = max(ds_dof - mdl_dof, 1)
if ds_dof <= mdl_dof:
_warnings.warn("Max-model params (%d) <= model params (%d)! Using k == 1." % (ds_dof, mdl_dof))
nsigma = (two_delta_logl - k) / _np.sqrt(2 * k)
pvalue = 1.0 - _stats.chi2.cdf(two_delta_logl, k)
return two_delta_logl, nsigma, pvalue
def two_delta_logl_per_circuit(model, dataset, circuits=None,
min_prob_clip=1e-6, prob_clip_interval=(-1e6, 1e6), radius=1e-4,
poisson_picture=True, op_label_aliases=None,
dof_calc_method=None, wildcard=None,
mdc_store=None, comm=None):
"""
Twice the per-circuit difference between the maximum and actual log-likelihood.
Contributions are aggregated over each circuit's outcomes, but no further.
Optionally (when `dof_calc_method` is not None) returns parallel vectors
containing the Nsigma (# std deviations from mean) and the p-value relative
to expected chi^2 distribution for each sequence.
Parameters
----------
model : Model
Model of parameterized gates
dataset : DataSet
Probability data
circuits : list of (tuples or Circuits), optional
Each element specifies a circuit to include in the log-likelihood
sum. Default value of None implies all the circuits in dataset
should be used.
min_prob_clip : float, optional
The minimum probability treated normally in the evaluation of the log-likelihood.
A penalty function replaces the true log-likelihood for probabilities that lie
below this threshold so that the log-likelihood never becomes undefined (which improves
optimizer performance).
prob_clip_interval : 2-tuple or None, optional
(min,max) values used to clip the probabilities predicted by models during MLEGST's
search for an optimal model (if not None). if None, no clipping is performed.
radius : float, optional
Specifies the severity of rounding used to "patch" the zero-frequency
terms of the log-likelihood.
poisson_picture : boolean, optional
Whether the log-likelihood-in-the-Poisson-picture terms should be included
in the returned logl value.
op_label_aliases : dictionary, optional
Dictionary whose keys are operation label "aliases" and whose values are tuples
corresponding to what that operation label should be expanded into before querying
the dataset. Defaults to the empty dictionary (no aliases defined)
e.g. op_label_aliases['Gx^3'] = ('Gx','Gx','Gx')
dof_calc_method : {"all", "modeltest"}
How `model`'s number of degrees of freedom (parameters) are obtained
when computing the number of standard deviations and p-value relative to
a chi2_k distribution, where `k` is additional degrees of freedom
possessed by the maximal model.
wildcard : WildcardBudget
A wildcard budget to apply to this log-likelihood computation.
This increases the returned log-likelihood value by adjusting
(by a maximal amount measured in TVD, given by the budget) the
probabilities produced by `model` to optimially match the data
(within the bugetary constraints) evaluating the log-likelihood.
mdc_store : ModelDatasetCircuitsStore, optional
An object that bundles cached quantities along with a given model, dataset, and circuit
list. If given, `model` and `dataset` and `circuits` should be set to None.
comm : mpi4py.MPI.Comm, optional
When not None, an MPI communicator for distributing the computation
across multiple processors.
Returns
-------
twoDeltaLogL_terms : numpy.ndarray
Nsigma, pvalue : numpy.ndarray
Only returned when `dof_calc_method` is not None.
"""
from ..objectivefns import objectivefns as _objfns
obj_cls = _objfns.PoissonPicDeltaLogLFunction if poisson_picture else _objfns.DeltaLogLFunction
obj = _objfns._objfn(obj_cls, model, dataset, circuits,
{'min_prob_clip': min_prob_clip,
'radius': radius}, {'prob_clip_interval': prob_clip_interval},
op_label_aliases, comm, None, ('percircuit',), (), mdc_store)
if wildcard:
assert(poisson_picture), "Wildcard budgets can only be used with `poisson_picture=True`"
obj.percircuit() # objfn used within wildcard objective fn must be pre-evaluated
obj = _objfns.LogLWildcardFunction(obj, model.to_vector(), wildcard)
two_dlogl_percircuit = 2 * obj.layout.allgather_local_array('c', obj.percircuit())
if dof_calc_method is None: return two_dlogl_percircuit
elif dof_calc_method == "all": mdl_dof = model.num_params
elif dof_calc_method == "modeltest": mdl_dof = model.num_modeltest_params
else: raise ValueError("Invalid `dof_calc_method` arg: %s" % dof_calc_method)
if circuits is not None:
ds_strs = _lt.apply_aliases_to_circuits(circuits, op_label_aliases)
else: ds_strs = None
ds_dof = dataset.degrees_of_freedom(ds_strs)
k = max(ds_dof - mdl_dof, 1)
# HACK - just take a single average #dof per circuit to use as chi_k distribution!
k = int(_np.ceil(k / (1.0 * len(circuits))))
nsigma = (two_dlogl_percircuit - k) / _np.sqrt(2 * k)
pvalue = _np.array([1.0 - _stats.chi2.cdf(x, k) for x in two_dlogl_percircuit], 'd')
return two_dlogl_percircuit, nsigma, pvalue
def two_delta_logl_term(n, p, f, min_prob_clip=1e-6, poisson_picture=True):
"""
Term of the 2*[log(L)-upper-bound - log(L)] sum corresponding to a single circuit and spam label.
Parameters
----------
n : float or numpy array
Number of samples.
p : float or numpy array
Probability of 1st outcome (typically computed).
f : float or numpy array
Frequency of 1st outcome (typically observed).
min_prob_clip : float, optional
Minimum probability clip point to avoid evaluating
log(number <= zero)
poisson_picture : boolean, optional
Whether the log-likelihood-in-the-Poisson-picture terms should be included
in the returned logl value.
Returns
-------
float or numpy array
"""
from ..objectivefns import objectivefns as _objfns
#Allow this function to pass NaNs through silently, since
# fiducial pair reduction may pass inputs with nan's legitimately and the desired
# behavior is to just let the nan's pass through to nan's in the output.
nan_indices = _np.isnan(f) # get indices of invalid entries
if not _np.isscalar(f):
f = f.copy(); p = p.copy(); n = n.copy()
f[nan_indices] = p[nan_indices] = n[nan_indices] = 0.0 # so computation runs fine
if poisson_picture:
rawfn = _objfns.RawPoissonPicDeltaLogLFunction({'min_prob_clip': min_prob_clip})
else:
rawfn = _objfns.RawDeltaLogLFunction({'min_prob_clip': min_prob_clip})
ret = 2 * rawfn.terms(p, n * f, n, f)
if not _np.isscalar(f):
ret[nan_indices] = _np.nan
return ret