Simulation classes (quanguru.classes.QSimBase)#
Contains the _parameter class and the parent classes of the Simulation object.
|
_parameter instances can be bound to each other to implement a (value) dependency between them. |
|
Contains attributes and methods for |
|
Implements 3 basic time information, namely total time of simulation ( |
Function Name |
Docstrings |
Unit Tests |
Tutorials |
|---|---|---|---|
_parameter |
✅ |
✅ |
❌ |
stateBase |
✅ |
✅ |
❌ |
timeBase |
✅ |
✅ |
❌ |
- setAttr(obj: paramBoundBase, attrStr: str, val: Any) None[source]#
a customized setattr that changes the value (and _paramUpdated) if the new value is different than the old/existing. Especially useful for multi parameter sweeps (see
_sweep).
- setAttrParam(obj: paramBoundBase, attrStr: str, val: Any) None[source]#
a customized setattr that changes the value stored as the
valueattribute of a _parameter object. The value (and _paramUpdated) is changed, if the new value is different than the old/existing. In any case, it breaks existing timeBase bounds (bound betweenSimulationinstances). Especially useful for multi parameter sweeps (see_sweep).
- class _parameter(value: Any = None, _bound: _parameter = None)[source]#
Bases:
object_parameter instances can be bound to each other to implement a (value) dependency between them.
There are two types of parametric bounds/relations/dependencies in this library,
Value of an attribute in an instance is bound to the value of the same attribute in another instance, meaning it gets its value from its bound.
Not the value of the attribute itself, but any change in its value is important for another object.
This class wraps (composition) the value of a parameter (an attribute) to create a hierarchical dependency required for the first case. The second case is covered by
paramBoundBase.It is intended to be used with private attributes and the corresponding properties returns the
valueof that attribute (which is a _parameter object).If a
_parameteris given another_parameteras its_bound, it returns thevalueof its_bound, while keeping its_valueunchanged (which is mostly left to beNone). This is the same for the bound one, which means a chain of dependency is achieved by bounding each object to another.bounds can be broken by explicitly setting the
value.TODO update this list and add cross-references. Used in
stepSize,totalTime,stepCount,initialStateetc. The goal of having such dependency is that, for example, when simulating a quantum system simultaneously using more than 1 protocol, we can assign aninitialStatefor the quantum system, and all the protocolsinitialStatewill by default be_boundto quantum systemsinitialStateand get their value from it. If we explicitly assign aninitialStateto any protocol, the_boundwill break for that particular protocol, which will have its owninitialState, but not the others, unless they are also explicitly given aninitialState. These sort of dependencies are implemented in the library and are not meant for external modifications.This class can be replaced by a proxy class. However, this is intended to be used completely internally (private attributes + properties), this simple option should suffice.
- _value: Any#
the value to be wrapped
- _bound: _parameter#
bounded _parameter object, self is not bounded to anything when this is None or any other object that does not have a
valueattribute. Assigned to False when a bound is broken (by updating the value).
- property value: Any#
Property to get the
_valueof self, ifbounddoes not havevalueas an attribute, else returns thevalueof the_bound, which should be an instance of_parameterobject but can be any other object with an_value&/_boundattribute.Setter assigns the
_valueto a given input and_boundtoFalse(meaning the bound to any other object is broken, and value is different than the default, which is None)
- __firstlineno__ = 65#
- __static_attributes__ = ('_bound', '_value')#
- class stateBase(**kwargs)[source]#
Bases:
computeBaseContains attributes and methods for
initialStateand a boolean to determine whether to store or discard time-evolved states. Since, we can usecomputeto compute quantities of interest at the run-time of simulation, we don’t need to keep the states and can discard them to save memory.NOTE : This class branches the inheritance started by
paramBoundBaseand extends toSimulation.NOTE : All three attributes of this class (and all 4 of timeBase) are instances of
_parameter, so they have a corresponding property.- label: str = 'stateBase'#
(class attribute) class label used in default naming
- _internalInstances: int = 0#
(class attribute) number of instances created internally by the library
- _externalInstances: int = 0#
(class attribute) number of instances created explicitly by the user
- _instances: int = 0#
(class attribute) number of total instances = _internalInstances + _externalInstances
- __initialState#
This attribute is a
_parameterand value stores theinitialStatematrix
- __initialStateInput#
Input used in the creation of the
initialState, there are various ways to createinitialState, so its value changes depending on the case. This information is kept so that it can be used in cases where a new reconstruction ofinitialStateis required, e.g. when system dimension is swept or changed.
- __delStates#
This boolean determines if we delete/discard (
True) time-evolved states (to save memory) and keep only theinitialStateandcurrentStateof the system. By default, it isFalseand states will be stored in the corresponding protocolsqRes.states.
- _initialStateSystem#
This system will be used in the initial state creation, i.e. input will be passed to this systems createState function. If this is None, it fallbacks to self.superSys, and raises error if self.superSys is also None.
- _maxInput#
Stores the maximum of the basis numbers passed to initial state input.
- property initialStateSystem#
- property initialState#
The initialState property
returns _stateBase__initialState.valueif it is notNone. Otherwise, uses first element in itssubSysdictionary values to create and assign its value. This assumes that, to be able to assign a state to a Simulation, it needs at least one quantum system in itssubSysdictionary, and the subSys have method to create the state (_createState). This requirement, by default, is satisfied by the internally created Simulation objects that are attributes of quantum systems and protocols.Setter sets
_stateBase__initialState.valuematrix forselfby using the first element ofsubSysdictionary values.
- property _initialStateInput#
getter and setter of _stateBase__initialStateInput.value.
- getResultByNameOrAlias(name)[source]#
This method exists to enrich the terminology, it just
returns super().getByNameOrAlias(name), which returns the object with the given name. SeegetByNameOrAliasfor details.
- property delStates#
gets and sets
_stateBase__delStates.valueboolean
- delMatrices(_exclude=[])[source]#
This method extends
delMatricesto also set the_stateBase__initialState.valuetoNoneand del the old value.
- __firstlineno__ = 132#
- __static_attributes__ = ('__delStates', '__initialState', '__initialStateInput', '_initialStateInput', '_initialStateSystem', '_maxInput')#
- class timeBase(**kwargs)[source]#
Bases:
stateBaseImplements 3 basic time information, namely total time of simulation (
totalTime), step size for each unitary (stepSize), and number of steps (stepCount = totalTime/stepSize). Additionally, one more parameter, namely, number of samples (samples) is introduced to be used with time-dependent Hamiltonian, where a continuous parameter is discretely changed at everystepSizeand more than onesamplesare desired during thestepSize.These 4 attributes are all
_parameterinstances and protected attributes, meaning they are modified by the corresponding properties. One other functionality of property is to create flexible use of these attributes. For example, not all 3 ofstepSize,totalTime, andstepCountare need to be explicitly defined, any of these two would be sufficient, since the 3rd can be calculated using the two. So, property setters&getters also covers such cases. Another flexibility ensured by the properties is when_boundare broken.- label: str = 'timeBase'#
(class attribute) class label used in default naming
- _internalInstances: int = 0#
(class attribute) number of instances created internally by the library
- _externalInstances: int = 0#
(class attribute) number of instances created explicitly by the user
- _instances: int = 0#
(class attribute) number of total instances = _internalInstances + _externalInstances
- __totalTime#
_parameter storing the total time of simulation
- __stepSize#
_parameter storing the step size of simulation
- __samples#
_parameter storing number of samples in each step, by default 1.
- __stepCount#
_parameter storing the number of steps, i.e totalTime/stepSize.
- __updated#
_updated storing the two most recent time parameters which were updated (totalTime/stepSize/stepCount)
- __bound#
if bound to another object, meaning the _parameters of this gets their value from the others _parameters, this attribute is a reference to another. Else None.
- __firstlineno__ = 291#
- __static_attributes__ = ('__bound', '__samples', '__stepCount', '__stepSize', '__totalTime', '__updated', '_timeBase__bound', '_timeBase__updated')#
- property totalTime#
gets and sets
_timeBase__totalTime.value, and also sets the_timeBase__stepCount.valueconditioned on thatstepSizeis notNone. It also sets_paramUpdatedtoTrue. Additionally to these, it sets_timeBase__stepSize._valueto_timeBase__stepSize._bound._value, if_timeBase__stepSize._boundis notNone or False. This is introduced to provide a flexible use of these parameters, such as not forcing to define at least 2 of 3 timeBase parameters, if it already has a_boundand can obtain the second one from the_bound.
- property stepCount#
gets and sets
_timeBase__stepCount.value. getter also try seting thetotalTimeif it isNone. Setter also sets_timeBase__stepSize.valueconditioned on thattotalTimeis notNone. It also sets_paramUpdatedtoTrue. Additionally to these, it sets_timeBase__totalTime._valueto_timeBase__totalTime._bound._value, if_timeBase__totalTime._boundis notNone or False. This is introduced to provide a flexible use of these parameters, such as not forcing to define at least 2 of 3 timeBase parameters, if it already has a_boundand can obtain the second one from the_bound.
- property stepSize#
gets and sets
_timeBase__stepSize.value, and also_timeBase__stepCount.valueconditioned on thattotalTimeis notNone. It also sets_paramUpdatedtoTrue. Additionally to these, it sets_timeBase__totalTime._valueto_timeBase__totalTime._bound._value, if_timeBase__totalTime._boundis notNone or False. This is introduced to provide a flexible use of these parameters, such as not forcing to define at least 2 of 3 timeBase parameters, if it already has a_boundand can obtain the second one from the_bound.
- property samples#
gets and sets
_timeBase__samples.valueand also sets_paramUpdatedtoTrue.
- _copyVals(other, keys)[source]#
Method to copy the values for given attributes (as keys) from
othertoself.
- static _boundTree(osys, tree=None)[source]#
Creates a tree of bound instances starting from the given
osys. A primitive method used in debugging and to be improved.
- _bound(other, params=['_stateBase__delStates', '_stateBase__initialState', '_stateBase__initialStateInput'], re=False)[source]#
This method is used internally at appropriate places to create bound between different simulation instances in the intended hierarchical order. For example, when a
quantum systemis added tosubSysof explicitly createdSimulation, The parameters of anyprotocol.simulationfor that system will be bound to(quantum system).simulationwhich will be bound to explicitly created Simulation. This method creates such a bound between twoSimulationobjects, and it is used in appropriate places of the library. Such a bound is broken or not created at all, if a parameter is explicitly assigned for a protocol or system.if self _parameter attributes have bound False (meaning set a value before)
copies the value from other, if value in other is not None.
is any self _parameter bound is None, and the same value in other is not, sets it and breaks the loop
tries bounding if bound is None
NOTE : This method is intended purely for internal uses!
- Parameters:
other (Simulation) – The other Simulation (or timeBase) class to bound the parameters of self
re (bool, optional) – This boolean used (internally) to re-bound a simulation object to another one, by default False. So, re-calling this method to bound a simulation object to another will not work unless
re=True.param (List[str]) – This is a list of strings for attributes (which are also _parameter objects) other than time parameters, for which the same bound will be created. The difference between
paramandtimeBaseparameters is that the latter ones has a functional dependency to each other, meaning a break in one of them should appropriately be reflected to the others.