Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix Model Parameter Desync #482

Merged
merged 5 commits into from
Sep 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 59 additions & 9 deletions pygsti/models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ def set_parameter_bounds(self, index, lower_bound=-_np.inf, upper_bound=_np.inf)
if lower_bound == -_np.inf and upper_bound == _np.inf:
return # do nothing

if self._param_bounds is None:
#Note, this property call will also invoke a param vector rebuild if needed.
if self.parameter_bounds is None:
self._param_bounds = _default_param_bounds(self.num_params)
self._param_bounds[index, :] = (lower_bound, upper_bound)

Expand Down Expand Up @@ -489,13 +490,6 @@ def __setstate__(self, state_dict):
## Get/Set methods
##########################################

@property
def parameter_labels(self):
"""
A list of labels, usually of the form `(op_label, string_description)` describing this model's parameters.
"""
return self._ops_paramlbls_to_model_paramlbls(self._paramlbls)

@property
def sim(self):
""" Forward simulator for this model """
Expand Down Expand Up @@ -611,6 +605,56 @@ def num_params(self):
self._clean_paramvec()
return len(self._paramvec)

@property
def parameter_labels(self):
"""
A list of labels, usually of the form `(op_label, string_description)` describing this model's parameters.
"""
self._clean_paramvec()
return self._ops_paramlbls_to_model_paramlbls(self._paramlbls)

def set_parameter_label(self, index, label):
"""
Set the label of a single model parameter.

Parameters
----------
index : int
The index of the paramter whose label should be set.

label : object
An object that serves to label this parameter. Often a string.

Returns
-------
None
"""
self._clean_paramvec()
self._paramlbls[index] = label

@property
def parameter_bounds(self):
""" Upper and lower bounds on the values of each parameter, utilized by optimization routines """
self._clean_paramvec()
return self._param_bounds

@property
def num_modeltest_params(self):
"""
The parameter count to use when testing this model against data.

Often times, this is the same as :meth:`num_params`, but there are times
when it can convenient or necessary to use a parameter count different than
the actual number of parameters in this model.

Returns
-------
int
the number of model parameters.
"""
self._clean_paramvec()
return Model.num_modeltest_params.fget(self)

def _iter_parameterized_objs(self):
raise NotImplementedError("Derived Model classes should implement _iter_parameterized_objs")
#return # default is to have no parameterized objects
Expand Down Expand Up @@ -905,6 +949,13 @@ def _rebuild_paramvec(self):
w = self._model_paramvec_to_ops_paramvec(self._paramvec)
Np = len(w) # NOT self.num_params since the latter calls us!
wl = self._paramlbls

if self._param_bounds is not None:
msg = 'Internal Model attributes are being rebuilt. This is likely because a modelmember has been '\
+ 'either added or removed. If you have manually set parameter bounds values at the Model level '\
+ '(not the model member level), for example using the `set_parameter_bounds` method, these values '\
+ 'will be overwritten by the parameter bounds found in each of the modelmembers.'
_warnings.warn(msg)
wb = self._param_bounds if (self._param_bounds is not None) else _default_param_bounds(Np)
#NOTE: interposer doesn't quite work with parameter bounds yet, as we need to convert "model"
# bounds to "ops" bounds like we do the parameter vector. Need something like:
Expand Down Expand Up @@ -1022,7 +1073,6 @@ def _rebuild_paramvec(self):
Np = len(w) # reset Np from possible new params (NOT self.num_params since the latter calls us!)
indices_to_remove = sorted(set(range(Np)) - used_gpindices)
if debug: print("Indices to remove = ", indices_to_remove, " of ", Np)

if len(indices_to_remove) > 0:
#if debug: print("DEBUG: Removing %d params:" % len(indices_to_remove), indices_to_remove)
w = _np.delete(w, indices_to_remove)
Expand Down
Loading