Mattis spin glass
The Mattis spin glass on a periodic square lattice.
- Class:
MattisSpinGlassSystem - Import:
from skcomplex.systems.physics.statistical_mechanics import MattisSpinGlassSystem - Field:
physics/statistical_mechanics - Topics:
spin glass,disorder,lattice,Monte Carlo,spins - References:
- Mattis 1976
- Bertin 2021
Use this system
The block imports the class and simulates the system with its default parameters and its tuned run settings.
from skcomplex.systems.physics.statistical_mechanics import MattisSpinGlassSystem
trajectory = MattisSpinGlassSystem().prepare().simulate()
Parameters
The model parameters take these defaults, and set_params changes them.
| Parameter | Default |
|---|---|
size |
48 |
temperature |
1.5 |
J |
1.0 |
h |
0.0 |
random_state |
0 |
solver |
'auto' |
backend |
'auto' |
Tags
The system with its default parameters has these capability tags, which solvers and analyses check.
| Tag | Values |
|---|---|
| Time | discrete |
| Space | lattice |
| Laws | sampling |
| Randomness | stochastic |
| Domains | real |
| Structure | fixed |
Class
The source of the class shows how it builds its components from its model parameters.
class MattisSpinGlassSystem(systems.BaseSystem):
"""The Mattis spin glass on a periodic square lattice.
The couplings are `J_ij = J xi_i xi_j` of a pattern `xi`.
The gauge change `sigma_i = xi_i s_i` maps the system at `h = 0` to the Ising system, so the overlap `m_xi = sum_i
xi_i s_i / N` orders below the Ising critical temperature `2 J / ln(1 + sqrt 2)` while the magnetization averages to
zero over the disorder. One unit of time is one sweep of Metropolis updates. The system has the spins `spins.spin`,
the pattern `spins.pattern`, and the observables `magnetization` and `overlap`.
Args:
size:
The side of the square lattice, or its two sides.
temperature:
The temperature, which can change during a run.
J:
The coupling.
h:
The external field.
random_state:
The seed or generator of the pattern and of the random initial spins.
label:
The label of the system, which names it when it is nested in another one.
solver:
The solver of the problem, or `'auto'` for the one the system chooses.
backend:
The backend that evaluates the compiled functions, or `'auto'` for the one the system chooses.
Examples:
>>> import numpy as np
>>> from skcomplex.systems.physics.statistical_mechanics import MattisSpinGlassSystem
>>> spins = MattisSpinGlassSystem(8).prepare().system_.elements[0]
>>> pattern = spins.pattern.reshape(8, 8)
>>> bool(np.array_equal(spins.J_right.reshape(8, 8), pattern * np.roll(pattern, -1, axis=1)))
True
"""
field = 'physics/statistical_mechanics'
topics = ('spin glass', 'disorder', 'lattice', 'Monte Carlo', 'spins')
references = ('Mattis 1976', 'Bertin 2021')
until = 300
dt = 2
observables = (
Observable('magnetization', group='spins', variable='spin'),
Observable('overlap', _get_overlap, group='spins'),
)
def __init__(
self,
size: int | Sequence[int] = 48,
*,
temperature: float = 1.5,
J: float = 1.0,
h: float = 0.0,
random_state: int | np.random.Generator | None = 0,
label: str = 'system',
solver: BaseSolver | str = 'auto',
backend: BaseBackend | str = 'auto',
) -> None:
super().__init__(label, solver=solver, backend=backend)
self.size = size
self.temperature = temperature
self.J = J
self.h = h
self.random_state = random_state
def _prepare(self, context: Sequence[Observable] | None = None) -> None:
"""Set the Mattis spin glass, whose bonds follow a quenched pattern.
Args:
context:
The observables to compile with the problem, or `None` for none.
"""
super()._prepare(context)
shape = EdwardsAndersonSystem._get_square(self.size)
rng = np.random.default_rng(self.random_state)
pattern = rng.choice([-1.0, 1.0], size=shape)
right = self.J * pattern * np.roll(pattern, -1, axis=1)
down = self.J * pattern * np.roll(pattern, -1, axis=0)
spins = rng.choice([-1.0, 1.0], size=shape)
bonds = {name: np.ravel(value) for name, value in EdwardsAndersonSystem._get_bond_states(right, down).items()}
group = MattisSpinElement(
'spins',
spin=spins.ravel(),
pattern=pattern.ravel(),
h=self.h,
temperature=self.temperature,
space=LatticeSpace(shape),
**bonds,
)
self.system_ = systems.System(
self.label,
elements=[group],
interactions=[BondSamplingInteraction('sampling', target='spins')],
solver=MetropolisSolver(moves={'spins.spin': 'flip'}, neighborhood=NEAREST),
)
Components
The system uses these element and interaction classes, each with its nearest class of the core.
Elements:
MattisSpinElement, fromElement
Interactions:
BondSamplingInteraction, fromInteraction
class MattisSpinElement(BondSpinElement):
"""Spin of the Mattis system.
The bond spin also holds its quenched pattern `xi`, with couplings `J xi_i xi_j`.
Args:
label:
The label of the spin or the lattice group.
spin:
The initial spin, 1 or -1.
J_up:
The coupling to the upper neighbour.
J_left:
The coupling to the left neighbour.
J_right:
The coupling to the right neighbour.
J_down:
The coupling to the lower neighbour.
h:
The external field.
temperature:
The temperature.
pattern:
The quenched pattern `xi`, 1 or -1.
space:
The lattice of the spins, or `None` for one spin.
Examples:
>>> from skcomplex.systems.physics.statistical_mechanics import MattisSpinElement
>>> MattisSpinElement('spin', pattern=-1.0).pattern
-1.0
"""
visual = VisualSpec('image', {'states': ()})
def __init__(
self,
label: str,
*,
spin: object = 1.0,
J_up: object = 1.0,
J_left: object = 1.0,
J_right: object = 1.0,
J_down: object = 1.0,
h: float = 0.0,
temperature: float = 1.0,
pattern: object = 1.0,
space: BaseSpace | None = None,
) -> None:
super().__init__(
label,
spin=spin,
J_up=J_up,
J_left=J_left,
J_right=J_right,
J_down=J_down,
h=h,
temperature=temperature,
space=space,
)
self.pattern = pattern
class BondSamplingInteraction(SpinSamplingInteraction):
"""Metropolis sweeps of Ising spins with quenched couplings to their neighbours.
The local energy is `-s (sum J_k s_k + h)`.
The couplings `J_up`, `J_left`, `J_right`, and `J_down` are constant states of every spin, in the order of the
offsets of the von Neumann neighbourhood, and the field `h` and the temperature are its parameters.
Args:
label:
The label of the interaction, unique among the components of its system.
target:
The path of the lattice group of spins.
Examples:
>>> from skcomplex.systems.physics.statistical_mechanics import BondSamplingInteraction
>>> BondSamplingInteraction('sampling', target='spins').state
'spin'
"""
def __init__(self, label: str, *, target: Role) -> None:
super().__init__(label, target=target, state='spin', energy=self._get_energy)
def _get_energy(self, namespace: SimpleNamespace) -> Expression:
"""Return the local energy `-s (sum_k J_k s_k + h)` of a spin."""
s = namespace.target
couplings = (s.J_up, s.J_left, s.J_right, s.J_down)
neighbours = neighbors(s.spin, NEAREST, reduce='each')
return -s.spin * (sum(coupling * other for coupling, other in zip(couplings, neighbours, strict=True)) + s.h)