kups.core.patch
¶
Patch system for simulation state modifications.
This module provides a comprehensive system for managing composable state modifications in simulations. The key components are:
- Patch: Protocol for modifications to simulation state
- Probe: Protocol for extracting information from state and patches
- IdPatch: Identity patch that returns state unchanged
- ExplicitPatch: Patch with explicitly stored proposed state
- IndexLensPatch: Patch that modifies state at specific indices via a lens
- ComposedPatch: Composition of multiple patches applied sequentially
The patch system allows for composable, type-safe state modifications that integrate with JAX transformations while maintaining runtime validation.
Addable
¶
ComposedPatch
¶
Bases: Patch[State]
A patch that composes multiple patches together by applying them in sequence.
Source code in src/kups/core/patch.py
ExplicitPatch
¶
Bases: Patch[State]
A patch that applies a custom function with payload data.
This patch type provides maximum flexibility by accepting an arbitrary function that defines how the state should be modified based on the payload.
Attributes:
| Name | Type | Description |
|---|---|---|
payload |
T
|
Data to pass to the apply function |
apply_fn |
Callable[[State, T, Accept], State]
|
Function that applies the patch given state, payload, and acceptance |
Source code in src/kups/core/patch.py
IdPatch
¶
Bases: Patch[State]
A patch that does nothing, i.e., returns the state unchanged.
Source code in src/kups/core/patch.py
IndexLensPatch
¶
Bases: Patch[State]
A patch that uses a lens to update indexed elements in the state.
This patch combines lens-based state access with indexed updates and acceptance masking. It's particularly useful for updating specific elements in arrays or nested structures based on particle indices.
Attributes:
| Name | Type | Description |
|---|---|---|
data |
T
|
New data values to apply |
mask_idx |
Any
|
A prefix pytree to match against Index leaves in the state; determines which indices to update. |
lens |
Lens[State, T]
|
Lens that focuses on the part of state to modify |
Source code in src/kups/core/patch.py
Patch
¶
Bases: Protocol
A patch represents a modification to simulation state.
Patches are composable transformations that accept a state and an acceptance array, returning a modified state. The acceptance array controls which modifications are applied (useful for Monte Carlo acceptance/rejection).
When called, takes (state, accept) and returns the modified state with updates applied according to the acceptance mask.
Source code in src/kups/core/patch.py
Probe
¶
Bases: Protocol
Protocol for querying state together with a proposal or patch.
The payload can be raw changes inspected before constructing a patch, or
a patch queried for observables, energy calculations and other diagnostics.
Consumers that require an applicable patch constrain P to Patch.
When called, takes (state, payload) and returns information of type R.
Source code in src/kups/core/patch.py
WithPatch
¶
Bases: Generic[T_Data, T_Patch]
Generic wrapper pairing data with a patch.
This class provides a unified pattern for operations that return both
a result (data) and a side-effect (patch). It is parameterized by
T_Data (the wrapped data type, e.g. Energy, PotentialOut,
Array) and T_Patch (the patch type, which must satisfy the
Patch protocol).
Attributes:
| Name | Type | Description |
|---|---|---|
data |
T_Data
|
The primary data result |
patch |
T_Patch
|
The patch to apply |
Example
Source code in src/kups/core/patch.py
__add__(other)
¶
Add two WithPatch instances by adding data and composing patches.
Requires T_Data to support add. Returns WithPatch with summed data and patches composed in sequence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
WithPatch[D, P2]
|
Another WithPatch with compatible data type |
required |
Returns:
| Type | Description |
|---|---|
WithPatch[D, ComposedPatch[Any]]
|
New WithPatch with summed data and composed patches |
Source code in src/kups/core/patch.py
compose_patch(other)
¶
Compose another patch after this one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Patch[Any]
|
Patch to compose after this one |
required |
Returns:
| Type | Description |
|---|---|
WithPatch[T_Data, ComposedPatch[Any]]
|
New WithPatch with composed patch |