pygsti.modelmembers.modelmember
Defines the ModelChild and ModelMember classes, which represent Model members
Module Contents
Classes
Base class for all objects contained in a Model that hold a parent reference to their parent Model. |
|
Base class for Model member objects that possess a definite state space, parameters count, and evolution type. |
- class pygsti.modelmembers.modelmember.ModelChild(parent=None)
Bases:
object
Base class for all objects contained in a Model that hold a parent reference to their parent Model.
Parameters
- parentModel, optional
The parent model.
Attributes
- parentModel
The parent of this object.
- class pygsti.modelmembers.modelmember.ModelMember(state_space, evotype, gpindices=None, parent=None)
Bases:
ModelChild
,pygsti.baseobjs.nicelyserializable.NicelySerializable
Base class for Model member objects that possess a definite state space, parameters count, and evolution type.
A ModelMember can be vectorized into/onto a portion of their parent Model’s (or other ModelMember’s) parameter vector. They therefore contain a gpindices reference to the global Model indices “owned” by this member. Note that GateSetMembers may contain other GateSetMembers (may be nested).
Parameters
- state_spaceStateSpace
The state space, which should match the parent model if/when one exists.
- evotypeEvoType or str
The evolution type, which should match the parent model if/when one exists.
- gpindicesslice or numpy.ndarray, optional
The indices of this member’s local parameters into the parent Model’s parameter vector.
- parentModel, optional
The parent model.
Attributes
- dirtybool
Whether this member’s local parameters may have been updated without its parent’s knowledge. The parent model can check this flag and perform re-synchronization of it’s parameter vector when needed.
- gpindicesslice or numpy.ndarray
The indices of this member’s local parameters into the parent Model’s parameter vector.
- parentModel, optional
The parent model.
Initialize a new ModelMember
- property state_space
- property evotype
- property dirty
Flag indicating whether this member’s local parameters may have been updated without its parent’s knowledge.
- property gpindices
The indices of this member’s local parameters into the parent Model’s parameter vector.
Returns
slice or numpy.ndarray
- property parameter_labels
An array of labels (usually strings) describing this model member’s parameters.
- property parameter_bounds
Upper and lower bounds on the values of each parameter, utilized by optimization routines
- property num_params
Get the number of independent parameters which specify this object.
Returns
int
- submembers()
Returns a sequence of any sub-ModelMember objects contained in this one.
Sub-members are processed by other
ModelMember
methods (e.g. unlink_parent and set_gpindices) as though the parent object is just a container for these sub-members and has no parameters of its own. Member objects that contain other members and possess their own independent parameters should implement the appropriate ModelMember functions (usually just allocate_gpindices, using the base implementation as a reference).Returns
list or tuple
- relink_parent(parent)
Sets the parent of this object without altering its gpindices.
This operation is appropriate to do when “re-linking” a parent with its children after the parent and child have been serialized. (the parent is not saved in serialization - see ModelChild.__getstate__ – and so must be manually re-linked upon de-serialization).
In addition to setting the parent of this object, this method sets the parent of any objects this object contains (i.e. depends upon) - much like allocate_gpindices. To ensure a valid parent is not overwritten, the existing parent must be None prior to this call.
Parameters
- parentModel
The model to (re-)set as the parent of this member.
Returns
None
- unlink_parent(force=False)
Remove the parent-link of this member.
Called when at least one reference (via key) to this object is being disassociated with parent. If all references are to this object are now gone, set parent to None, invalidating any gpindices.
Parameters
- forcebool, optional
If True, then resets parent to None, effectively de-allocating the model member’s parameters from the parent model, even if the parent still contains references to it. If False, the parent is only set to None when its parent contains no reference to it.
Returns
None
- set_gpindices(gpindices, parent, memo=None)
Set the parent and indices into the parent’s parameter vector that are used by this ModelMember object.
Parameters
- gpindicesslice or integer ndarray
The indices of this objects parameters in its parent’s array.
- parentModel or ModelMember
The parent whose parameter array gpindices references.
- memoset, optional
A set keeping track of the object ids that have had their indices set in a root set_gpindices call. Used to prevent duplicate calls and self-referencing loops. If memo contains an object’s id (id(self)) then this routine will exit immediately.
Returns
None
- shift_gpindices(above, amount, parent_filter=None, memo=None)
Shifts this member’s gpindices by the given amount.
Usually called by the parent model when it shifts parameter indices around in its parameter vector.
Parameters
- aboveint
The “insertion point” within the range of indices. All indices greater than or equal to this index are shifted.
- amountint
The amount to shift indices greater than or equal to above by.
- parent_filterModel or None
If a
Model
object, then only those members with indices allocated to this model will be shifted. It usually makes sense to specify this argument, supplying the parent model whose parameter vector is being shifted.- memoset, optional
A set keeping track of the object ids that have had their indices set in a root set_gpindices call. Used to prevent duplicate calls and self-referencing loops. If memo contains an object’s id (id(self)) then this routine will exit immediately.
Returns
None
- gpindices_are_allocated(model, memo=None)
Whether or not this model member’s parameter indices are allocated within a potential parent model.
This is used to infer when the model member needs to have its parameter indices reallocated (by its parent model).
Parameters
- modelModel
Test for parameter allocation with respect to this model.
- memodict, optional
Used to prevent duplicate calls and self-referencing loops. If memo contains an object’s id (id(self)) then this routine will exit immediately with a cached value.
Returns
- bool
True if this member’s .gpindices and those of its sub-members are not None and refer to parameter indices of model.
- preallocate_gpindices(parent, memo=None)
Computes two key pieces of information for a model preparing to allocate this member.
These pieces of information are:
the total number of new parameters that would need to be allocated to parent in order to have all this model member’s parameters allocated to parent.
the largest parameter index from all the parameters already allocated to parent. This is useful for providing an ideal insertion point for the new parameters, once the model has made space for them.
Note that this function does not update this model member’s .gpindces or other attributes at all - it just serves as a pre-allocation probe so that the allocating model knows how much space in its parameter vector is needed/ requested by this perhaps-not-fully-allocated member.
Parameters
- parentModel
The model that parameter allocation is being considered with respect to.
- memoset, optional
Used to prevent duplicate calls and self-referencing loops. If memo contains an object’s id (id(self)) then this routine will exit immediately..
Returns
- num_new_paramsint
the number of parameters that aren’t currently allocated to parent
- max_indexint
the maximum index of the parameters currently allocated to parent
- allocate_gpindices(starting_index, parent, memo=None, submembers_already_allocated=False)
Sets gpindices array for this object or any objects it contains (i.e. depends upon).
Indices may be obtained from contained objects which have already been initialized (e.g. if a contained object is shared with other top-level objects), or given new indices starting with starting_index.
Parameters
- starting_indexint
The starting index for un-allocated parameters.
- parentModel or ModelMember
The parent whose parameter array gpindices references.
- memoset, optional
Used to prevent duplicate calls and self-referencing loops. If memo contains an object’s id (id(self)) then this routine will exit immediately.
- submembers_already_allocatedbool, optional
Whether submembers of this object are known to already have their parameter indices allocated to parent. Leave this as False unless you know what you’re doing.
Returns
- num_newint
The number of new allocated parameters (so the parent should mark as allocated parameter indices starting_index to starting_index + new_new).
- init_gpindices(allocated_to_parent=None)
Initializes this model member’s parameter indices by allocating them to an “anticipated” parent model.
Objects with submembers often rely on having valid .gpindices and .subm_rpindices attributes, but these aren’t set until the object is allocated to a parent model. This method initializes these attributes in the best way possible before receiving the actual parent model. Typically model members (containing sub-members) are build in two ways:
The sub-members are all un-allocated, i.e. their .parent model is None
The sub-members are all allocated to the same parent model.
This method computes an “anticipated parent” model as the common parent of all the submembers (if one exists) or None, and calls
allocate_gpindices()
using this parent model and a starting index of 0. This has the desired behavior in the two cases above. In case 1, parameter indices are set (allocated) but the parent is set to None, so that the to-be parent model will see this member as being unallocated. In case 2, the parent model, if it is the anticipated one, will see that this member’s indices are already allocated to it, and won’t need to re-allocate them.
- gpindices_as_array()
Returns gpindices as a numpy.ndarray of integers.
The underlying .gpindices attribute itself can be None, a slice, or an integer array. If gpindices is None, an empty array is returned.
Returns
numpy.ndarray
- from_vector(v, close=False, dirty_value=True)
Initialize this object using a vector of parameters.
Parameters
- vnumpy array
The 1D vector of parameters. Length must == num_params()
- closebool, optional
Whether v is close to the current parameter vector.
- dirty_valuebool, optional
The value to set this object’s “dirty flag” to before exiting this call. This is passed as an argument so it can be updated recursively. Leave this set to True unless you know what you’re doing.
Returns
None
- copy(parent=None, memo=None)
Copy this object.
Parameters
- parentModel, optional
The parent of the returned copy.
Returns
- LinearOperator
A copy of this object.
- is_similar(other, rtol=1e-05, atol=1e-08)
Comparator returning whether two ModelMembers are the same type and parameterization.
ModelMembers with internal parameterization information (e.g. LindbladErrorgen) should overload this function to account for that.
Parameters
- other: ModelMember
ModelMember to compare to
- rtol: float
Relative tolerance for floating poing comparisons (passed to numpy.isclose)
- atol: float
Absolute tolerance for floating point comparisons (passed to numpy.isclose)
Returns
- bool
True if structure (all but parameter values) matches
- is_equivalent(other, rtol=1e-05, atol=1e-08)
Comparator returning whether two ModelMembers are equivalent.
This uses is_similar for type checking and NumPy allclose for parameter checking, so is unlikely to be needed to overload.
Note that this only checks for NUMERICAL equivalence, not whether the objects are the same.
Parameters
- other: ModelMember
ModelMember to compare to
- rtol: float
Relative tolerance for floating point comparisons (passed to numpy.isclose)
- atol: float
Absolute tolerance for floating point comparisons (passed to numpy.isclose)
Returns
- bool
True if structure AND parameter vectors match
- to_memoized_dict(mmg_memo)
Create a serializable dict with references to other objects in the memo.
Parameters
- mmg_memo: dict
Memo dict from a ModelMemberGraph, i.e. keys are object ids and values are ModelMemberGraphNodes (which contain the serialize_id). This is NOT the same as other memos in ModelMember (e.g. copy, allocate_gpindices, etc.).
Returns
- mm_dict: dict
A dict representation of this ModelMember ready for serialization This must have at least the following fields: module, class, submembers, params, state_space, evotype Additional fields may be added by derived classes.
- classmethod from_memoized_dict(mm_dict, serial_memo, parent_model)
Deserialize a ModelMember object and relink submembers from a memo.
Parameters
- mm_dict: dict
A dict representation of this ModelMember ready for deserialization This must have at least the following fields: module, class, submembers, state_space, evotype
- serial_memo: dict
Keys are serialize_ids and values are ModelMembers. This is NOT the same as other memos in ModelMember, (e.g. copy(), allocate_gpindices(), etc.). This is similar but not the same as mmg_memo in to_memoized_dict(), as we do not need to build a ModelMemberGraph for deserialization.
- parent_model: Model
The parent model that being build that will eventually hold this ModelMember object. It’s important to set this so that the Model considers the set gpindices valid and doesn’t just wipe them out when cleaning its parameter vector.
Returns
- ModelMember
An initialized object