pygsti.protocols.confidenceregionfactory
¶
Classes for constructing confidence regions
Module Contents¶
Classes¶
An object which is capable of generating confidence intervals/regions. 

Encapsulates a lightweight "view" of a ConfidenceRegionFactory. 
Functions¶

Get finite difference derivative grad_f that is shape (num_params, <shape of val>) 

 class pygsti.protocols.confidenceregionfactory.ConfidenceRegionFactory(parent, model_lbl, circuit_list_lbl, hessian=None, non_mark_radius_sq=None)¶
Bases:
pygsti.baseobjs.nicelyserializable.NicelySerializable
An object which is capable of generating confidence intervals/regions.
Often times, it does so by holding the Hessian of a fit function with respect to a Model’s parameters and related projections of it onto the nongauge space.
Alternative (nonHessianbased) means of computing confidence intervals are also available, such as by using socalled “linear reponse error bars”.
 Parameters
parent (Estimate) – the parent estimate object, needed to resolve model and gate string list labels.
model_lbl (str) – The key into the parent Estimate’s .models dictionary that gives the Model about which confidence regions will be constructed.
circuit_list_lbl (str) – The key into the parent Results’s .circuit_lists dictionary that specifies which circuits should be or were included when computing fit functions (the loglikelihood or chi2).
hessian (numpy array, optional) – A precomputed num_params x num_params Hessian matrix, where num_params is the number of dimensions of model space, i.e. model.num_params.
non_mark_radius_sq (float, optional) – The nonMarkovian radius associated with the goodness of fit found at the point where hessian was computed. This must be specified whenver hessian is, and should be left as None when hessian is not specified.
 __getstate__(self)¶
 __setstate__(self, state_dict)¶
 _to_nice_serialization(self)¶
 classmethod _from_nice_serialization(cls, state)¶
 set_parent(self, parent)¶
Sets the parent Estimate object of this ConfidenceRegionFactory.
This function is usually only needed internally to relink a ConfidenceRegionFactory with its parent after be unserialized from disk.
 Parameters
parent (Estimate) – The parent of this object.
 Returns
None
 property has_hessian(self)¶
Returns whether or not the Hessian has already been computed.
When True,
project_hessian()
can be used to project the Hessian for use in creating confidence intervals. When False, eithercompute_hessian()
can be called to compute the Hessian or slower methods must be used to estimate the necessary portion of the Hessian. The result of this function is often used to decide whether or not to proceed with an errorbar computation. Returns
bool
 can_construct_views(self)¶
Checks whether this factory has enough information to construct ‘views’ of itself.
ConfidenceRegionFactoryView view objects are created using the :method:`view` method, which can in turn be used to construct confidence intervals.
 Returns
bool
 property model(self)¶
Retrieve the associated model.
 Returns
Model – the model marking the center location of this confidence region.
 compute_hessian(self, comm=None, mem_limit=None, approximate=False)¶
Computes the Hessian for this factory.
 Parameters
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.
approximate (bool, optional) – Whether to compute the true Hessian or just an approximation of it. See :function:`logl_approximate_hessian`. Setting to True can significantly reduce the run time.
 Returns
numpy.ndarray – The Hessian matrix (also stored internally)
 project_hessian(self, projection_type, label=None, tol=1e07, maxiter=10000)¶
Projects the Hessian onto the nongauge space.
This is a necessary step before confidence regions/intervals can be computed via Hessianbased methods.
 Parameters
projection_type (string) –
Specifies how (and whether) to project the given hessian matrix onto a nongauge space. Allowed values are:
’std’ – standard projection onto the space perpendicular to the gauge space.
’none’ – no projection is performed. Useful if the supplied hessian has already been projected.
’optimal gate CIs’ – a lengthier projection process in which a numerical optimization is performed to find the nongauge space which minimizes the (average) size of the confidence intervals corresponding to gate (as opposed to SPAM vector) parameters.
’intrinsic error’ – compute separately the intrinsic error in the gate and spam Model parameters and set weighting metric based on their ratio.
label (str, optional) – The internal label to use for this projection. If None, then projection_type is used, which is usually fine.
tol (float, optional) – Tolerance for optimal Hessian projection. Only used when projection_type == ‘optimal gate CIs’.
maxiter (int, optional) – Maximum iterations for optimal Hessian projection. Only used when projection_type == ‘optimal gate CIs’.
 Returns
numpy.ndarray – The inverse of the projected Hessian matrix (also stored internally)
 enable_linear_response_errorbars(self, resource_alloc=None)¶
Stores the parameters needed to compute (ondemand) linear response error bars.
In particular, this function sets up the parameters needed to perform the model optimizations needed to compute error bars on quantities.
‘linear response’ mode obtains elements of the Hessian via the linear response of a “forcing term”. This requres a likelihood optimization for every computed error bar, but avoids pre computation of the entire Hessian matrix, which can be prohibitively costly on large parameter spaces.
 Parameters
resoure_alloc (ResourceAllocation) – Allocation for running linearresponse GST fits.
 Returns
None
 view(self, confidence_level, region_type='normal', hessian_projection_label=None)¶
Constructs a “view” of this ConfidenceRegionFactory for a particular type and confidence level.
The returned view object can then be used to construct confidence intervals/regions.
 Parameters
confidence_level (float) – The confidence level as a percentage, i.e. between 0 and 100.
region_type ({'normal', 'nonmarkovian'}) – The type of confidence regions. ‘normal’ constructs standard intervals based on the inverted Hessian matrix or linearresponse optimizations. ‘nonmarkovian’ attempts to enlarge the intervals to account for the badnessoffit at the current location.
hessian_projection_label (str, optional) – A label specifying which Hessian projection to use (only useful when there are multiple). These labels are either the projection_type values of
project_hessian()
or the custom label argument provided to that function. If None, then the most recent (perhaps the only) projection is used.
 Returns
ConfidenceRegionFactoryView
 _opt_projection_for_operation_cis(self, method='LBFGSB', maxiter=10000, maxfev=10000, tol=1e06, verbosity=0)¶
 _opt_projection_from_split(self, verbosity=0)¶
 class pygsti.protocols.confidenceregionfactory.ConfidenceRegionFactoryView(model, inv_projected_hessian, mlgst_params, confidence_level, non_mark_radius_sq, n_non_gauge_params, n_gauge_params)¶
Bases:
object
Encapsulates a lightweight “view” of a ConfidenceRegionFactory.
A view object is principally defined by it’s having a fixed confidencelevel. Thus, a “view” is like a factory that generates confidence intervals for just a single confidence level. As such, it is a useful object to pass around to routines which compute and display error bars, as these routines typically don’t depend on what confidencelevel is being used.
 Parameters
model (Model) – The model at the center of this confidence region.
inv_projected_hessian (numpy.ndarray) – The computed inverse of the nongaugeprojected Hessian.
mlgst_params (dict) – A dictionary of MLGST parameters only used for linearresponse error bars.
confidence_level (float) – the confidence level (between 0 and 100) used in the computation of confidence regions/intervals.
non_mark_radius_sq (float, optional) – When nonzero, “a nonMarkovian error region” is constructed using this value as the squared “nonmarkovian radius”. This specifies the portion of 2*(maxloglikelihood  modelloglikelihood) that we attribute to nonMarkovian errors (typically the previous difference minus it’s expected value, the difference in number of parameters between the maximal and model models). If set to zero (the default), a standard and thereby statistically rigorous conficence region is created. Nonzero values should only be supplied if you really know what you’re doing.
n_non_gauge_params (int) – The numbers of nongauge parameters. This could be computed from model but can be passed in to save compuational time.
n_gauge_params (int) – The numbers of gauge parameters. This could be computed from model but can be passed in to save compuational time.
 __getstate__(self)¶
 property errorbar_type(self)¶
Return the type of error bars this view will generate, either “standard” or “nonmarkovian”.
 Returns
str
 retrieve_profile_likelihood_confidence_intervals(self, label=None, component_label=None)¶
Retrieve the profilelikelihood confidence intervals for a specified model object (or all such intervals).
 Parameters
label (Label, optional) – If not None, can be either a gate or SPAM vector label to specify the confidence intervals corresponding to gate, rhoVec, or EVec parameters respectively. If None, then intervals corresponding to all of the model’s parameters are returned.
component_label (Label, optional) – Labels an effect within a POVM or a member within an instrument.
 Returns
numpy array – Onedimensional array of (positive) interval halfwidths which specify a symmetric confidence interval.
 compute_confidence_interval(self, fn_obj, eps=1e07, return_fn_val=False, verbosity=0)¶
Compute the confidence interval for an arbitrary function.
This “function”, however, must be encapsulated as a ModelFunction object, which allows it to neatly specify what its dependencies are and allows it to compaute finite different derivatives more efficiently.
 Parameters
fn_obj (ModelFunction) – An object representing the function to evaluate. The returned confidence interval is based on linearizing this function and propagating the modelspace confidence region.
eps (float, optional) – Step size used when taking finitedifference derivatives of fnOfOp.
return_fn_val (bool, optional) – If True, return the value of fnOfOp along with it’s confidence region halfwidths.
verbosity (int, optional) – Specifies level of detail in standard output.
 Returns
df (float or numpy array) – Halfwidths of confidence intervals for each of the elements in the float or array returned by fnOfOp. Thus, shape of df matches that returned by fnOfOp.
f0 (float or numpy array) – Only returned when return_fn_val == True. Value of fnOfOp at the gate specified by op_label.
 _compute_return_from_grad_f(self, grad_f, f0, return_fn_val, verbosity)¶
Just adds logic for special behavior when f0 is a dict
 _compute_df_from_grad_f(self, grad_f, f0, return_fn_val, verbosity)¶
 _compute_df_from_grad_f_linresponse(self, grad_f, f0, verbosity)¶
 _compute_df_from_grad_f_hessian(self, grad_f, f0, verbosity)¶
Internal function which computes error bars given an function value and gradient (using linear approx. to function)
 pygsti.protocols.confidenceregionfactory._create_empty_grad(val, num_params)¶
Get finite difference derivative grad_f that is shape (num_params, <shape of val>)
 pygsti.protocols.confidenceregionfactory._create_empty_grad_f(f0, num_params)¶