Skip to content

Spins

Spin realizations shared by the physics packages: Ising spins and the Monte Carlo sampling of Ising spins.

IsingSamplingInteraction(label, *, target, state='spin', rule='metropolis', schedule='checkerboard', neighborhood=None)

Bases: SpinSamplingInteraction

Monte Carlo sweeps of Ising spins s = ±1 of local energy -J s sum_j s_j - h s, by flips.

The coupling J, the field h, and the temperature are parameters of the spins.

Parameters:

Name Type Description Default
label str

The label of the interaction, unique among the parts of its system.

required
target Annotated[str, RoleSpec()]

The path of the group of spins.

required
state str

The name of the state that holds the spin.

'spin'
rule str

The acceptance, 'metropolis', 'glauber', or 'heat-bath'.

'metropolis'
schedule str

'checkerboard' on a square lattice, 'colored' on a network, or 'random-sequential'.

'checkerboard'
neighborhood BaseNeighborhood | None

The neighbours of a spin, or None for the von Neumann neighbourhood. A network takes AdjacencyNeighborhood().

None

Examples:

>>> from types import SimpleNamespace
>>> from sage.all__sagemath_symbolics import var
>>> from skcomplex.systems.physics.common.spins import IsingSamplingInteraction
>>> ising = IsingSamplingInteraction('ising', target='spins')
>>> other = SimpleNamespace(spin=var('t'))
>>> ising.energy(SimpleNamespace(target=SimpleNamespace(spin=var('s'), J=1, h=var('h'), other=other)))
-h*s - s*neighbors(t, reduce='sum')
Source code in src/skcomplex/systems/physics/common/spins/_ising_sampling.py
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def __init__(
    self,
    label: str,
    *,
    target: Annotated[str, RoleSpec()],
    state: str = 'spin',
    rule: str = 'metropolis',
    schedule: str = 'checkerboard',
    neighborhood: BaseNeighborhood | None = None,
) -> None:
    super().__init__(
        label,
        target=target,
        state=state,
        energy=self._energy,
        rule=rule,
        schedule=schedule,
        neighborhood=neighborhood,
    )

IsingSpinElement(label, visual=None, *, spin=1.0, J=1.0, h=0.0, temperature=2.0)

Bases: Element

Ising spin, up at 1 or down at -1, with its coupling J, its field h, and its temperature.

An IsingSamplingInteraction flips the spins by Monte Carlo sweeps at the temperature of the spins.

Parameters:

Name Type Description Default
label str

The label of the spin or the lattice group.

required
visual VisualSpec | None

How the spin is drawn, or None for default_visual_.

None
spin Annotated[float, StateSpec(domain=ZZ)]

The initial spin, 1 or -1.

1.0
J Annotated[float, ParameterSpec()]

The coupling between neighbouring spins.

1.0
h Annotated[float, ParameterSpec()]

The external field.

0.0
temperature Annotated[float, ParameterSpec(domain=open(0, oo))]

The temperature in units of the coupling.

2.0

Attributes:

Name Type Description
default_visual_

The visual of the spin when it sets none.

Examples:

>>> from skcomplex.systems.physics.common.spins import IsingSpinElement
>>> IsingSpinElement('spin').temperature
2.0
Source code in src/skcomplex/systems/physics/common/spins/_ising_spin.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def __init__(
    self,
    label: str,
    visual: VisualSpec | None = None,
    *,
    spin: Annotated[float, StateSpec(domain=ZZ)] = 1.0,
    J: Annotated[float, ParameterSpec()] = 1.0,
    h: Annotated[float, ParameterSpec()] = 0.0,
    temperature: Annotated[float, ParameterSpec(domain=RealSet.open(0, oo))] = 2.0,
) -> None:
    super().__init__(label, visual=visual)
    self.spin = spin
    self.J = J
    self.h = h
    self.temperature = temperature

SpinSamplingInteraction(label, *, target, state, energy, move='flip', rule='metropolis', schedule='checkerboard', neighborhood=None, values=(), width=1.0)

Bases: ThermalSamplingInteraction

Monte Carlo sweeps of the spins of a lattice or network group at the temperature of the spins.

The temperature is the parameter temperature of the spins, so it lives with the couplings and fields of the spins, and a schedule of <group>.temperature changes it during a run. The local energy reads the states and parameters of the spin as namespace.target.

Parameters:

Name Type Description Default
label str

The label of the interaction, unique among the parts of its system.

required
target Annotated[str, RoleSpec()]

The path of the group of spins.

required
state str

The name of the state the moves change.

required
energy Callable[[SimpleNamespace], object]

The function energy(namespace) of the local energy of a spin.

required
move str | Callable[..., object]

'flip', 'random-value', 'rotate', 'gaussian', or a function that proposes values.

'flip'
rule str

The acceptance, 'metropolis', 'glauber', or 'heat-bath'.

'metropolis'
schedule str

'checkerboard', 'colored', or 'random-sequential'.

'checkerboard'
neighborhood BaseNeighborhood | None

The neighbourhood the energy reduces over, or None for the von Neumann neighbourhood.

None
values Sequence[float]

The values a random-value move draws from.

()
width float

The largest change of a rotation, or the standard deviation of a Gaussian move.

1.0

Examples:

>>> from skcomplex.core.components.laws.utils import neighbors
>>> from skcomplex.systems.physics.common.spins import SpinSamplingInteraction
>>> energy = lambda n: -n.target.spin * neighbors(n.target.other.spin)
>>> SpinSamplingInteraction('ising', target='spins', state='spin', energy=energy).rule
'metropolis'
Source code in src/skcomplex/systems/physics/common/spins/_spin_sampling.py
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def __init__(
    self,
    label: str,
    *,
    target: Annotated[str, RoleSpec()],
    state: str,
    energy: Callable[[SimpleNamespace], object],
    move: str | Callable[..., object] = 'flip',
    rule: str = 'metropolis',
    schedule: str = 'checkerboard',
    neighborhood: BaseNeighborhood | None = None,
    values: Sequence[float] = (),
    width: float = 1.0,
) -> None:
    super().__init__(
        label,
        target=target,
        state=state,
        energy=energy,
        move=move,
        rule=rule,
        schedule=schedule,
        neighborhood=neighborhood,
        values=values,
        width=width,
    )

ThermalSamplingInteraction(label, *, target, state, energy, move='flip', temperature=1.0, rule='metropolis', schedule='checkerboard', neighborhood=None, values=(), width=1.0, cooling=None)

Bases: SamplingInteraction

Monte Carlo sweeps of a state in an energy at a temperature, the intensity one over the temperature.

Parameters:

Name Type Description Default
label str

The label of the interaction, unique among the parts of its system.

required
target Annotated[str, RoleSpec()]

The path of the group on the sites of a lattice or the nodes of a network.

required
state str

The name of the state the moves change.

required
energy Callable[[SimpleNamespace], object]

The function energy(namespace) of the local energy of a member.

required
move str | Callable[..., object]

'flip', 'random-value', 'rotate', 'gaussian', 'cluster', or a function that proposes values.

'flip'
temperature Annotated[float, ParameterSpec(domain=closed_open(0, oo))]

The temperature, where 0 accepts only moves that do not raise the energy.

1.0
rule str

The acceptance, 'metropolis', 'glauber', or 'heat-bath', which draws among all values of a discrete move by their Boltzmann weights.

'metropolis'
schedule str

'checkerboard', 'colored', or 'random-sequential'.

'checkerboard'
neighborhood BaseNeighborhood | None

The neighbourhood the energy reduces over, or None for the von Neumann neighbourhood.

None
values Sequence[float]

The values a random-value move draws from.

()
width float

The largest change of a rotation, or the standard deviation of a Gaussian move.

1.0
cooling Callable[[Expression], object] | None

The function of the time symbol that multiplies the temperature, or None for a constant temperature.

None

Examples:

>>> from skcomplex.core.components.laws.utils import neighbors
>>> from skcomplex.systems.physics.common.spins import ThermalSamplingInteraction
>>> energy = lambda n: -n.target.s * neighbors(n.target.other.s)
>>> ThermalSamplingInteraction('ising', target='spins', state='s', energy=energy, temperature=2.0).temperature
2.0
Source code in src/skcomplex/systems/physics/common/spins/_thermal_sampling.py
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
def __init__(
    self,
    label: str,
    *,
    target: Annotated[str, RoleSpec()],
    state: str,
    energy: Callable[[SimpleNamespace], object],
    move: str | Callable[..., object] = 'flip',
    temperature: Annotated[float, ParameterSpec(domain=RealSet.closed_open(0, oo))] = 1.0,
    rule: str = 'metropolis',
    schedule: str = 'checkerboard',
    neighborhood: BaseNeighborhood | None = None,
    values: Sequence[float] = (),
    width: float = 1.0,
    cooling: Callable[[Expression], object] | None = None,
) -> None:
    super().__init__(
        label,
        target=target,
        state=state,
        objective=energy,
        move=move,
        rule=rule,
        schedule=schedule,
        neighborhood=neighborhood,
        values=values,
        width=width,
        cooling=cooling,
    )
    self.energy = energy
    self.temperature = temperature