lfox 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
lfox-0.0.1/LICENSE.txt ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023-present Ethan Neil <ethan.neil@colorado.edu>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
lfox-0.0.1/PKG-INFO ADDED
@@ -0,0 +1,167 @@
1
+ Metadata-Version: 2.4
2
+ Name: lfox
3
+ Version: 0.0.1
4
+ Summary: Lattice Fields Over jaX
5
+ Keywords: lattice field theory,lattice qcd,hybrid monte carlo,jax,physics
6
+ Author: Ethan Neil
7
+ Author-email: Ethan Neil <ethan.neil@colorado.edu>
8
+ License-Expression: MIT
9
+ License-File: LICENSE.txt
10
+ Classifier: Development Status :: 3 - Alpha
11
+ Classifier: Intended Audience :: Science/Research
12
+ Classifier: Programming Language :: Python :: 3.12
13
+ Classifier: Programming Language :: Python :: 3.13
14
+ Classifier: Topic :: Scientific/Engineering :: Physics
15
+ Requires-Dist: equinox>=0.13.7
16
+ Requires-Dist: jax>=0.10.0
17
+ Requires-Dist: numpy>=1.26
18
+ Requires-Dist: jax-mps>=0.10.10 ; extra == 'mps'
19
+ Requires-Python: >=3.12
20
+ Project-URL: Homepage, https://github.com/etneil/lfox
21
+ Project-URL: Repository, https://github.com/etneil/lfox
22
+ Project-URL: Issues, https://github.com/etneil/lfox/issues
23
+ Provides-Extra: mps
24
+ Description-Content-Type: text/markdown
25
+
26
+ # lfox
27
+
28
+ **L**attice **F**ields **O**ver ja**X** — a JAX-based library for lattice field theory
29
+ simulations: Markov-chain field evolution (HMC), lattice operators, and observable
30
+ measurement.
31
+
32
+ _Why?_ JAX provides two very useful things for lattice simulations:
33
+
34
+ 1. __Autodiff__. Implement the action as a Python function, JAX computes the
35
+ necessary gradients for HMC or other algorithms automatically.
36
+ 2. __Flexible backend__. JAX can target GPUs and its backend for sharding can
37
+ handle communications and scale up beyond a single machine.
38
+
39
+ Since JAX is focused on supporting machine learning applications, `lfox` is also
40
+ naturally interoperable with machine-learning workflows implemented in JAX.
41
+
42
+ The code being fully Python-based also means that it should be easy to work with and to
43
+ modify; this will probably never be the most efficient lattice code out there, but
44
+ the goal is "good enough for prototyping" or working with toy or exotic theory targets.
45
+
46
+ The core abstraction is an [Equinox](https://github.com/patrick-kidger/equinox)-backed
47
+ `Lattice` + `LatticeField` pair that participates in JAX pytrees, so everything composes
48
+ with `jax.jit` and `jax.grad`.
49
+
50
+ > **Status: early alpha.** This is a research codebase under active development. The API is not
51
+ > stable, and `lfox.fermions` is incomplete (see [Roadmap](#roadmap)). Pin an exact
52
+ > version if you depend on it.
53
+
54
+ ## Installation
55
+
56
+ ```console
57
+ pip install lfox
58
+ ```
59
+
60
+ or, with [uv](https://docs.astral.sh/uv/):
61
+
62
+ ```console
63
+ uv add lfox
64
+ ```
65
+
66
+ On Apple Silicon you can optionally pull in the experimental Metal backend:
67
+
68
+ ```console
69
+ pip install "lfox[mps]"
70
+ ```
71
+
72
+ JAX is installed as a plain CPU build by default. For CUDA or TPU, install the
73
+ appropriate [JAX wheel](https://docs.jax.dev/en/latest/installation.html) for your
74
+ platform alongside `lfox`.
75
+
76
+ ## Quickstart
77
+
78
+ A phi^4 scalar field on a 4³ lattice, evolved with hybrid Monte Carlo:
79
+
80
+ ```python
81
+ import jax
82
+ import jax.numpy as jnp
83
+
84
+ import lfox.lattice as lat
85
+ from lfox.action import Action
86
+ from lfox.evolution import HMC, Chain, LeapfrogIntegrator
87
+
88
+ jax.config.update("jax_enable_x64", True)
89
+
90
+
91
+ class ScalarAction(Action):
92
+ # NOTE: @staticmethod must sit *above* @jax.jit, or tracing breaks.
93
+ @staticmethod
94
+ @jax.jit
95
+ def _S(fields, params):
96
+ phi = fields[0]
97
+ S = phi**2
98
+ for ax in range(phi.d()):
99
+ S -= 2 * params["kappa"] * phi * phi.nn_field(ax)
100
+ S += params["lambda"] * (phi**2 - 1) ** 2
101
+ return S
102
+
103
+
104
+ lattice = lat.SquareLattice(st_dims=(4, 4, 4))
105
+ phi = lat.LatticeField(lattice=lattice, F=jnp.ones((4, 4, 4)))
106
+
107
+ action = ScalarAction(field_names=["phi"], params={"kappa": 0.18169, "lambda": 1.3282})
108
+ hmc = HMC(action=action, integrator=LeapfrogIntegrator(eps=0.1, Nstep=10))
109
+
110
+ chain = Chain(
111
+ evolver=hmc,
112
+ seed=1,
113
+ init_fields={"phi": phi},
114
+ observables={"magn": lambda fields, params: float(jnp.mean(fields["phi"].F))},
115
+ )
116
+ chain.warmup(100)
117
+ chain.run(1000)
118
+
119
+ print(f"acceptance: {chain.acceptance():.3f}")
120
+ print(f"<phi>: {sum(chain.obs_chain['magn']) / len(chain.obs_chain['magn']):.5f}")
121
+ ```
122
+
123
+ Note that only `_S` was written by hand. The force `dS` used by the integrator is
124
+ obtained by differentiating it.
125
+
126
+ ## What's in the box
127
+
128
+ - **`lfox.lattice`** — `Lattice` and `LatticeField`, with overloaded arithmetic
129
+ and `nn_field(axis, shift)` for nearest-neighbor hops with (anti-)periodic boundary conditions.
130
+ - **`lfox.action`** — `Action`, the action functional. Subclass it and implement `_S`;
131
+ you get `S`, `S_field`, and autodiff `dS` for free. Actions compose with `+`.
132
+ - **`lfox.evolution`** — `HMC` (a pure, jittable Markov transition kernel),
133
+ `LeapfrogIntegrator` / `OmelyanIntegrator`, and `Chain`, the host-side driver that owns
134
+ the RNG key, saved configurations, diagnostics, and observable measurement.
135
+ - **`lfox.fermions`** — Dirac spin structure, the Wilson operator, and a minimal-residual
136
+ solver. **Experimental and currently non-functional** — this subpackage has not yet been
137
+ migrated to the current `LatticeField`, and importing it raises.
138
+
139
+ ## Roadmap
140
+
141
+ - Fermions!
142
+ - Gauge fields!
143
+ - Group/representation machinery
144
+ - Other update algorithms
145
+ - Proper testing of scaling and GPU backend
146
+ - A richer measurement / analysis layer
147
+
148
+ ## Development
149
+
150
+ ```console
151
+ git clone https://github.com/etneil/lfox
152
+ cd lfox
153
+ uv sync # installs the package plus test and notebook dependencies
154
+ uv run pytest
155
+ ```
156
+
157
+ The test suite pins physics invariants rather than implementation details: action
158
+ normalization, autodiff force against the analytic force, `<exp(-ΔH)> = 1`, RNG key
159
+ threading, and `Chain` bookkeeping.
160
+
161
+ Exploratory work lives in Jupyter notebooks at the repo root, paired to `py:percent`
162
+ scripts via [jupytext](https://jupytext.readthedocs.io/). Launch with `uv run jupyter lab`.
163
+
164
+ ## License
165
+
166
+ `lfox` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html)
167
+ license.
lfox-0.0.1/README.md ADDED
@@ -0,0 +1,142 @@
1
+ # lfox
2
+
3
+ **L**attice **F**ields **O**ver ja**X** — a JAX-based library for lattice field theory
4
+ simulations: Markov-chain field evolution (HMC), lattice operators, and observable
5
+ measurement.
6
+
7
+ _Why?_ JAX provides two very useful things for lattice simulations:
8
+
9
+ 1. __Autodiff__. Implement the action as a Python function, JAX computes the
10
+ necessary gradients for HMC or other algorithms automatically.
11
+ 2. __Flexible backend__. JAX can target GPUs and its backend for sharding can
12
+ handle communications and scale up beyond a single machine.
13
+
14
+ Since JAX is focused on supporting machine learning applications, `lfox` is also
15
+ naturally interoperable with machine-learning workflows implemented in JAX.
16
+
17
+ The code being fully Python-based also means that it should be easy to work with and to
18
+ modify; this will probably never be the most efficient lattice code out there, but
19
+ the goal is "good enough for prototyping" or working with toy or exotic theory targets.
20
+
21
+ The core abstraction is an [Equinox](https://github.com/patrick-kidger/equinox)-backed
22
+ `Lattice` + `LatticeField` pair that participates in JAX pytrees, so everything composes
23
+ with `jax.jit` and `jax.grad`.
24
+
25
+ > **Status: early alpha.** This is a research codebase under active development. The API is not
26
+ > stable, and `lfox.fermions` is incomplete (see [Roadmap](#roadmap)). Pin an exact
27
+ > version if you depend on it.
28
+
29
+ ## Installation
30
+
31
+ ```console
32
+ pip install lfox
33
+ ```
34
+
35
+ or, with [uv](https://docs.astral.sh/uv/):
36
+
37
+ ```console
38
+ uv add lfox
39
+ ```
40
+
41
+ On Apple Silicon you can optionally pull in the experimental Metal backend:
42
+
43
+ ```console
44
+ pip install "lfox[mps]"
45
+ ```
46
+
47
+ JAX is installed as a plain CPU build by default. For CUDA or TPU, install the
48
+ appropriate [JAX wheel](https://docs.jax.dev/en/latest/installation.html) for your
49
+ platform alongside `lfox`.
50
+
51
+ ## Quickstart
52
+
53
+ A phi^4 scalar field on a 4³ lattice, evolved with hybrid Monte Carlo:
54
+
55
+ ```python
56
+ import jax
57
+ import jax.numpy as jnp
58
+
59
+ import lfox.lattice as lat
60
+ from lfox.action import Action
61
+ from lfox.evolution import HMC, Chain, LeapfrogIntegrator
62
+
63
+ jax.config.update("jax_enable_x64", True)
64
+
65
+
66
+ class ScalarAction(Action):
67
+ # NOTE: @staticmethod must sit *above* @jax.jit, or tracing breaks.
68
+ @staticmethod
69
+ @jax.jit
70
+ def _S(fields, params):
71
+ phi = fields[0]
72
+ S = phi**2
73
+ for ax in range(phi.d()):
74
+ S -= 2 * params["kappa"] * phi * phi.nn_field(ax)
75
+ S += params["lambda"] * (phi**2 - 1) ** 2
76
+ return S
77
+
78
+
79
+ lattice = lat.SquareLattice(st_dims=(4, 4, 4))
80
+ phi = lat.LatticeField(lattice=lattice, F=jnp.ones((4, 4, 4)))
81
+
82
+ action = ScalarAction(field_names=["phi"], params={"kappa": 0.18169, "lambda": 1.3282})
83
+ hmc = HMC(action=action, integrator=LeapfrogIntegrator(eps=0.1, Nstep=10))
84
+
85
+ chain = Chain(
86
+ evolver=hmc,
87
+ seed=1,
88
+ init_fields={"phi": phi},
89
+ observables={"magn": lambda fields, params: float(jnp.mean(fields["phi"].F))},
90
+ )
91
+ chain.warmup(100)
92
+ chain.run(1000)
93
+
94
+ print(f"acceptance: {chain.acceptance():.3f}")
95
+ print(f"<phi>: {sum(chain.obs_chain['magn']) / len(chain.obs_chain['magn']):.5f}")
96
+ ```
97
+
98
+ Note that only `_S` was written by hand. The force `dS` used by the integrator is
99
+ obtained by differentiating it.
100
+
101
+ ## What's in the box
102
+
103
+ - **`lfox.lattice`** — `Lattice` and `LatticeField`, with overloaded arithmetic
104
+ and `nn_field(axis, shift)` for nearest-neighbor hops with (anti-)periodic boundary conditions.
105
+ - **`lfox.action`** — `Action`, the action functional. Subclass it and implement `_S`;
106
+ you get `S`, `S_field`, and autodiff `dS` for free. Actions compose with `+`.
107
+ - **`lfox.evolution`** — `HMC` (a pure, jittable Markov transition kernel),
108
+ `LeapfrogIntegrator` / `OmelyanIntegrator`, and `Chain`, the host-side driver that owns
109
+ the RNG key, saved configurations, diagnostics, and observable measurement.
110
+ - **`lfox.fermions`** — Dirac spin structure, the Wilson operator, and a minimal-residual
111
+ solver. **Experimental and currently non-functional** — this subpackage has not yet been
112
+ migrated to the current `LatticeField`, and importing it raises.
113
+
114
+ ## Roadmap
115
+
116
+ - Fermions!
117
+ - Gauge fields!
118
+ - Group/representation machinery
119
+ - Other update algorithms
120
+ - Proper testing of scaling and GPU backend
121
+ - A richer measurement / analysis layer
122
+
123
+ ## Development
124
+
125
+ ```console
126
+ git clone https://github.com/etneil/lfox
127
+ cd lfox
128
+ uv sync # installs the package plus test and notebook dependencies
129
+ uv run pytest
130
+ ```
131
+
132
+ The test suite pins physics invariants rather than implementation details: action
133
+ normalization, autodiff force against the analytic force, `<exp(-ΔH)> = 1`, RNG key
134
+ threading, and `Chain` bookkeeping.
135
+
136
+ Exploratory work lives in Jupyter notebooks at the repo root, paired to `py:percent`
137
+ scripts via [jupytext](https://jupytext.readthedocs.io/). Launch with `uv run jupyter lab`.
138
+
139
+ ## License
140
+
141
+ `lfox` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html)
142
+ license.
@@ -0,0 +1,69 @@
1
+ [project]
2
+ name = "lfox"
3
+ version = "0.0.1"
4
+ description = "Lattice Fields Over jaX"
5
+ readme = "README.md"
6
+ license = "MIT"
7
+ license-files = ["LICENSE.txt"]
8
+ keywords = [
9
+ "lattice field theory",
10
+ "lattice qcd",
11
+ "hybrid monte carlo",
12
+ "jax",
13
+ "physics",
14
+ ]
15
+ classifiers = [
16
+ "Development Status :: 3 - Alpha",
17
+ "Intended Audience :: Science/Research",
18
+ "Programming Language :: Python :: 3.12",
19
+ "Programming Language :: Python :: 3.13",
20
+ "Topic :: Scientific/Engineering :: Physics",
21
+ ]
22
+ requires-python = ">=3.12"
23
+ dependencies = [
24
+ "equinox>=0.13.7",
25
+ "jax>=0.10.0",
26
+ "numpy>=1.26",
27
+ ]
28
+
29
+ [[project.authors]]
30
+ name = "Ethan Neil"
31
+ email = "ethan.neil@colorado.edu"
32
+
33
+ [project.urls]
34
+ Homepage = "https://github.com/etneil/lfox"
35
+ Repository = "https://github.com/etneil/lfox"
36
+ Issues = "https://github.com/etneil/lfox/issues"
37
+
38
+ [project.optional-dependencies]
39
+ mps = ["jax-mps>=0.10.10"]
40
+
41
+ [build-system]
42
+ requires = ["uv_build>=0.11.8,<0.12"]
43
+ build-backend = "uv_build"
44
+
45
+ [dependency-groups]
46
+ test = ["pytest>=8.0"]
47
+ notebooks = [
48
+ "gvar>=13.1",
49
+ "jupyter>=1.1.1",
50
+ "jupytext>=1.19.4",
51
+ "lsqfit>=13.3",
52
+ "matplotlib>=3.10.3",
53
+ "tqdm>=4.67.1",
54
+ ]
55
+
56
+ [[dependency-groups.dev]]
57
+ include-group = "test"
58
+
59
+ [[dependency-groups.dev]]
60
+ include-group = "notebooks"
61
+
62
+ [[tool.uv.index]]
63
+ name = "testpypi"
64
+ url = "https://test.pypi.org/simple/"
65
+ publish-url = "https://test.pypi.org/legacy/"
66
+ explicit = true
67
+
68
+ [tool.jupytext]
69
+ formats = "ipynb,py:percent"
@@ -0,0 +1,76 @@
1
+ [project]
2
+ name = "lfox"
3
+ version = "0.0.1"
4
+ description = 'Lattice Fields Over jaX'
5
+ readme = "README.md"
6
+ license = "MIT"
7
+ license-files = ["LICENSE.txt"]
8
+ authors = [
9
+ { name = "Ethan Neil", email = "ethan.neil@colorado.edu" },
10
+ ]
11
+ keywords = [
12
+ "lattice field theory",
13
+ "lattice qcd",
14
+ "hybrid monte carlo",
15
+ "jax",
16
+ "physics",
17
+ ]
18
+ classifiers = [
19
+ "Development Status :: 3 - Alpha",
20
+ "Intended Audience :: Science/Research",
21
+ "Programming Language :: Python :: 3.12",
22
+ "Programming Language :: Python :: 3.13",
23
+ "Topic :: Scientific/Engineering :: Physics",
24
+ ]
25
+ requires-python = ">=3.12"
26
+ dependencies = [
27
+ "equinox>=0.13.7",
28
+ "jax>=0.10.0",
29
+ "numpy>=1.26",
30
+ ]
31
+
32
+ [project.urls]
33
+ Homepage = "https://github.com/etneil/lfox"
34
+ Repository = "https://github.com/etneil/lfox"
35
+ Issues = "https://github.com/etneil/lfox/issues"
36
+
37
+ [project.optional-dependencies]
38
+ mps = [
39
+ "jax-mps>=0.10.10",
40
+ ]
41
+
42
+ [build-system]
43
+ requires = ["uv_build>=0.11.8,<0.12"]
44
+ build-backend = "uv_build"
45
+
46
+ # Development-only dependencies. These are *not* published as part of the
47
+ # distribution -- they support the tests and the exploratory notebooks at the
48
+ # repo root, neither of which ship to PyPI.
49
+ [dependency-groups]
50
+ dev = [
51
+ { include-group = "test" },
52
+ { include-group = "notebooks" },
53
+ ]
54
+ test = [
55
+ "pytest>=8.0",
56
+ ]
57
+ notebooks = [
58
+ "gvar>=13.1",
59
+ "jupyter>=1.1.1",
60
+ "jupytext>=1.19.4",
61
+ "lsqfit>=13.3",
62
+ "matplotlib>=3.10.3",
63
+ "tqdm>=4.67.1",
64
+ ]
65
+
66
+ # Named publish target for *local* release dry runs: `uv publish --index testpypi`.
67
+ # CI does not use this -- the publish job passes --publish-url explicitly, since it
68
+ # runs without a checkout. `explicit` keeps this out of dependency resolution.
69
+ [[tool.uv.index]]
70
+ name = "testpypi"
71
+ url = "https://test.pypi.org/simple/"
72
+ publish-url = "https://test.pypi.org/legacy/"
73
+ explicit = true
74
+
75
+ [tool.jupytext]
76
+ formats = "ipynb,py:percent"
@@ -0,0 +1,12 @@
1
+ # SPDX-FileCopyrightText: 2023-present Ethan Neil <ethan.neil@colorado.edu>
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+
5
+ # The version lives in pyproject.toml and is read back out of the installed
6
+ # distribution metadata, so there is exactly one place to bump it.
7
+ from importlib.metadata import PackageNotFoundError, version
8
+
9
+ try:
10
+ __version__ = version("lfox")
11
+ except PackageNotFoundError: # running from a source tree without an install
12
+ __version__ = "0.0.0+unknown"
@@ -0,0 +1,6 @@
1
+ # SPDX-FileCopyrightText: 2023-present Ethan Neil <ethan.neil@colorado.edu>
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ from lfox.__about__ import __version__
5
+
6
+ __all__ = ["__version__"]
@@ -0,0 +1,131 @@
1
+ import copy
2
+ import dataclasses
3
+ from abc import abstractmethod
4
+
5
+ import equinox as eqx
6
+ import jax
7
+ import jax.numpy as jnp
8
+
9
+
10
+ class Action(eqx.Module):
11
+ # An action needs the following to be created:
12
+ # - An ordered list of lattice field names
13
+ # - [optional] A dictionary of non-field parameters the action depends on (e.g. couplings)
14
+ # - [optional] A list of "sub_actions", which will be added in whenever S or S_field is computed
15
+
16
+ # Although most functions of fields use dictionaries in lfox, we use lists in action definitions.
17
+ # This is to make "aliasing" easy - defining copies of the action with different field names,
18
+ # for example to create a many-flavor fermion action.
19
+
20
+ # There is a single abstract method in this class, the action functional _S. This
21
+ # functional should depend on the fields and the parameters, and must be implemented
22
+ # by any inheriting subclass as a STATIC method. It should have signature:
23
+ # @staticmethod
24
+ # def _S(fields, params):
25
+ # (...)
26
+ #
27
+ # where `fields` is a list of fields. The functions S(self, fields) and S_field(self, fields)
28
+ # take dictionaries of fields, mapping them to lists for _S(fields, params) using
29
+ # Action.field_names.
30
+
31
+ # JIT compilation of the action is HIGHLY RECOMMENDED! This can be done simply
32
+ # by adding the jax.jit decorator, i.e.
33
+ #
34
+ # @staticmethod
35
+ # @jax.jit
36
+ # def _S(fields, params):
37
+ # (...)
38
+ #
39
+ # (Note that the order of decorators is important, don't swap them!)
40
+
41
+ # Actions can be created by combining two actions together using += or +.
42
+ # This uses the sub_actions parameter.
43
+
44
+ field_names: list = eqx.field(static=True)
45
+ params: dict = eqx.field(static=True)
46
+ sub_actions: list
47
+
48
+ def __init__(self, field_names, params=None, sub_actions=None):
49
+ self.field_names = field_names
50
+
51
+ if params is None:
52
+ self.params = {}
53
+ else:
54
+ self.params = params
55
+
56
+ if sub_actions is None:
57
+ self.sub_actions = []
58
+ else:
59
+ self.sub_actions = sub_actions
60
+
61
+ # Action density functional; returns S as a LatticeField, i.e. not summed.
62
+ @jax.jit
63
+ def S_field(self, fields):
64
+ # Evaluate main action functional
65
+ field_call = [fields[fname] for fname in self.field_names]
66
+
67
+ S_tot = self._S(fields=field_call, params=self.params)
68
+
69
+ # Add action functionals for any subclasses
70
+ for action in self.sub_actions:
71
+ field_call = [fields[fname] for fname in action.field_names]
72
+ S_tot += action._S(fields=field_call, params=self.params)
73
+
74
+ return S_tot
75
+
76
+ # Total action
77
+ # Note that we DON'T have to be careful about extra indices here;
78
+ # the action density must already be a scalar per-site, so a simple
79
+ # jnp.sum is guaranteed to be a sum over the lattice sites.
80
+ @jax.jit
81
+ def S(self, fields):
82
+ S_density = self.S_field(fields)
83
+ return jnp.sum(S_density.F)
84
+
85
+ @jax.jit
86
+ def dS(self, fields):
87
+ return jax.grad(self.S)(fields)
88
+
89
+ @staticmethod
90
+ @abstractmethod
91
+ def _S(fields, params):
92
+ # This method interfaces to a function which can be JIT compiled and which is friendly
93
+ # to computing JAX gradients.
94
+ # Call signature (fields and params) MUST match the names
95
+ # in the dictionaries.
96
+ # This is meant to be maximally flexible; can always be overridden to be more efficient
97
+ # by a subclass.
98
+ pass
99
+
100
+ # Overload addition with composition
101
+ def __iadd__(self, other):
102
+ self.add_subaction(other)
103
+ return self
104
+
105
+ def __add__(self, other):
106
+ newAct = self.__copy__()
107
+ newAct.add_subaction(other)
108
+
109
+ return newAct
110
+
111
+ def __copy__(self):
112
+ new_subact = copy.deepcopy(self.sub_actions)
113
+ return dataclasses.replace(self, sub_actions=new_subact)
114
+
115
+ def copy(self):
116
+ return self.__copy__()
117
+
118
+ def add_subaction(self, other):
119
+ # Combine the parameters
120
+ # TODO: warn in case of collision, which will overwrite...
121
+ self.params.update(other.params)
122
+
123
+ # If there are subactions within the other field, promote them up to the current subaction list
124
+ if len(other.sub_actions) > 0:
125
+ for subact in other.sub_actions:
126
+ self.sub_actions.append(subact)
127
+
128
+ other.sub_actions = []
129
+
130
+ # Register the subaction
131
+ self.sub_actions.append(other)
@@ -0,0 +1,18 @@
1
+ from lfox.action import Action
2
+ from lfox.evolution.base import Chain, Evolver
3
+ from lfox.evolution.hmc import HMC
4
+ from lfox.evolution.integrators import (
5
+ LeapfrogIntegrator,
6
+ MDIntegrator,
7
+ OmelyanIntegrator,
8
+ )
9
+
10
+ __all__ = [
11
+ "Action",
12
+ "Chain",
13
+ "Evolver",
14
+ "HMC",
15
+ "LeapfrogIntegrator",
16
+ "MDIntegrator",
17
+ "OmelyanIntegrator",
18
+ ]