State with remember¶
A device runs the aggregate program from the top at every round, so ordinary Python local variables are lost when the round ends. remember keeps a local value on a device from one round to the next (the rep construct of field calculus).
from phyelds.calculus import aggregate, remember
@aggregate
def counter():
count, set_count = remember(0) # (1)!
return set_count(count + 1) # (2)!
- On the very first round,
countstarts at the initial value0. On every subsequent round,remember(0)ignores0and loads the value stored at the end of the previous round. set_count(...)stores the value to keep for the next round and returns it, socounter()returns1in round 1,2in round 2,3in round 3, and so on.
remember(initial) returns a pair (current_state, update_fn):
- the current state: the plain Python value held in this round;
- an update function: call it with the value to keep for the next round (it also returns that new value).
Updating state vs. sharing state with neighbors
Because count is a plain Python value, calling set_count(count + 1) does not mutate the local variable count in place; rebind count = set_count(count + 1) or return set_count(...) if you need the updated value later in the round.
When a state evolves by exchanging values with neighbors (such as in gradients, gossip, or collections), use exchange instead of combining remember and neighbors.
How state evolves across rounds¶

remember_and_evolve¶
When the next value only depends on the current one, remember_and_evolve(initial, evolution) performs both steps and returns the updated value:
from phyelds.calculus import aggregate, remember_and_evolve
@aggregate
def rounds_since_start():
return remember_and_evolve(0, lambda count: count + 1)
How state is identified¶
Each remember call is identified by where it is called: the chain of @aggregate calls that leads to it, and its position among sibling calls (see Alignment and @aggregate).
Because of this:
- two
remembercalls in the same function have independent states; - calling the same
@aggregatehelper twice in a round gives each call its own independent state.
How long state lives¶
- State not read in a round is automatically dropped at the end of that round. If a
rememberlives inside anifbranch that is not taken during a round, its state is discarded; re-entering that branch in a later round restarts frominitial. This matches the branching semantics of field calculus (see Alignment and@aggregate). - Storing
Noneis equivalent to having no state: in the next roundrememberwill return itsinitialvalue again. Use a dedicated sentinel (such asfloat("inf")or an empty tuple()) if you need to represent "no value".
In short: rules for local state
- Store per-device persistent values in
remember(...), never in module-level Python globals. - If a random number should be chosen once per device (e.g., a tie-breaking UUID), wrap it in
remember(random.random()). - Prefer immutable values (
tuple,frozenset, frozendataclass) insiderememberso neighbors cannot accidentally mutate your state in the simulator. - See Common pitfalls for examples.