%load_ext autoreload
%autoreload 2
Simulation State in pylattica¶
At the core of every pylattica simulation is a state object. This object is implemented by the SimulationState
class. The easiest way to think about it is as a dictionary mapping site IDs to state values. These site IDs refer to sites inside the lattice that you've defined for your simulation. Technically, the sites don't have to refer to physically arranged sites, but for the usual application of pylattica, they do.
NOTE: The following examples are to show how the state of the simulation works. In application, the simulation runner performs most of the updates, and you will not explicitly run state update commands
Here's the most basic way to set up a simulation state:
from pylattica.core import SimulationState
state = SimulationState()
Now, you can specify the state value for a given site. By default, the site state is None
for an unknown site.
print("State for site 1 before setting state: ", state.get_site_state(1))
state.set_site_state(1, { "my state key": "my state value"})
print("State for site 1 after setting state: ", state.get_site_state(1))
State for site 1 before setting state: None State for site 1 after setting state: {'_site_id': 1, 'my state key': 'my state value'}
Note that the simulation state will also return the ID of the site inside the state. This just simplifies calculations during simulations.
Setting site state does not completely overwrite the previous state. It instead only overwrites the values that you specify.
state.set_site_state(1, { "another key": "another value" })
state.get_site_state(1)
{'_site_id': 1, 'my state key': 'my state value', 'another key': 'another value'}
Here's how you partially update existing state:
state.set_site_state(1, { "my state key": "a brand new value" })
state.get_site_state(1)
{'_site_id': 1, 'my state key': 'a brand new value', 'another key': 'another value'}
Note that only the state value for "my state key"
was updated.
General State¶
Some simulations require state at each simulation frame that isn't specific to any one single site, but instead applies to the whole simulation frame. The SimulationState
class supports that with the concept of General State. This state is accessed as follows:
state.get_general_state()
{}
It's a dictionary, just like the state for each site. You can update it like this:
state.set_general_state({"key1": "value1"})
state.get_general_state()
{'key1': 'value1'}