Skip to content

Sherrington-Kirkpatrick spin glass

The Sherrington-Kirkpatrick spin glass with Glauber dynamics.

  • Class: SherringtonKirkpatrickSystem
  • Import: from skcomplex.systems.physics.statistical_mechanics import SherringtonKirkpatrickSystem
  • Field: physics/statistical_mechanics
  • Topics: spin glass, disorder, mean field, spins, jump process
  • References:
    • Sherrington and Kirkpatrick 1975
    • Bertin 2021
    • Mezard, Parisi, and Virasoro 1987

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 SherringtonKirkpatrickSystem

trajectory = SherringtonKirkpatrickSystem().prepare().simulate()

Parameters

The model parameters take these defaults, and set_params changes them.

Parameter Default
n_spins 200
temperature 0.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 continuous
Space lattice
Laws jump
Randomness stochastic
Domains real
Structure fixed

Class

The source of the class shows how it builds its components from its model parameters.

class SherringtonKirkpatrickSystem(BaseSystem):
    """The Sherrington-Kirkpatrick spin glass with Glauber dynamics.

    The glass freezes below the temperature `T_c = J`.

    The energy is `-sum_{i<j} J_ij s_i s_j - h sum_i s_i` with quenched Gaussian couplings `J_ij` of mean 0 and variance
    `J^2 / N`, drawn once. Above `T_c` the energy per spin is `-J^2 / (2 T)`, and below it the spins freeze in random
    directions, with no magnetization. The spins are drawn in a row, which orders the drawing only. The system has the
    spins `spins.spin`, the coupling matrix of unit scale in the interaction `flips`, and the observable
    `magnetization`.

    Args:
        n_spins:
            The number of spins.

        temperature:
            The temperature, which can change during a run.

        J:
            The standard deviation of the couplings times the square root of the number of spins.

        h:
            The external field.

        random_state:
            The seed or generator of the couplings 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:
        >>> from skcomplex.systems.physics.statistical_mechanics import SherringtonKirkpatrickSystem
        >>> couplings = SherringtonKirkpatrickSystem(10).prepare().system_.interactions[0].couplings
        >>> bool((couplings == couplings.T).all()), float(couplings.trace())
        (True, 0.0)
    """

    field = 'physics/statistical_mechanics'
    topics = ('spin glass', 'disorder', 'mean field', 'spins', 'jump process')
    references = ('Sherrington and Kirkpatrick 1975', 'Bertin 2021', 'Mezard, Parisi, and Virasoro 1987')
    until = 50.0
    dt = 0.5
    observables = (_get_magnetization(),)

    def __init__(
        self,
        n_spins: int = 200,
        *,
        temperature: float = 0.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.n_spins = n_spins
        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 spins and their Glauber flips on quenched Gaussian couplings.

        Args:
            context:
                The observables to compile with the problem, or `None` for none.
        """
        super()._prepare(context)
        rng = np.random.default_rng(self.random_state)
        upper = np.triu(rng.normal(0.0, 1.0 / np.sqrt(self.n_spins), (self.n_spins, self.n_spins)), 1)
        spins = CurieWeissSystem._get_initial_spins(self.n_spins, 'random', rng)
        flips = GlauberFlipInteraction(
            'flips',
            target='spins',
            J=self.J,
            h=self.h,
            temperature=self.temperature,
            couplings=upper + upper.T,
        )
        self.system_ = System(
            self.label,
            elements=[FullyConnectedSpinElement('spins', spin=spins, space=LatticeSpace(self.n_spins))],
            interactions=[flips],
        )

Components

The system uses these element and interaction classes, each with its nearest class of the core.

Elements:

  • FullyConnectedSpinElement, from Element

Interactions:

  • GlauberFlipInteraction, from Interaction
class FullyConnectedSpinElement(Element):
    """Ising spins of 1 or -1, each coupled to every other spin.

    Args:
        label:
            The label of the spin or the group.

        spin:
            The initial spin, 1 or -1, or one spin per site.

        space:
            The row of sites of the spins, or `None` for one spin.

    Examples:
        >>> from skcomplex.systems.physics.statistical_mechanics import FullyConnectedSpinElement
        >>> FullyConnectedSpinElement('spin').spin
        1.0
    """

    visual = VisualSpec('image', {'states': ('spin',), 'low': -1.0, 'high': 1.0})

    def __init__(self, label: str, *, spin: object = 1.0, space: BaseSpace | None = None) -> None:
        super().__init__(label, space=space)
        self.spin = spin

class GlauberFlipInteraction(Interaction):
    """Glauber flips of fully connected Ising spins.

    A spin flips at the rate `1 / (1 + exp(2 s h / T))`. The local field is `h = J (sum_{j != i} s_j) / N + h_ext` of
    the Curie-Weiss energy `-(J / N) sum_{i<j} s_i s_j`, or `h = J sum_j C_ij s_j + h_ext` with a symmetric coupling
    matrix `C` of zero diagonal. The rate satisfies detailed balance with the Boltzmann distribution at the temperature
    `T`.

    Args:
        label:
            The label of the interaction.

        target:
            The path of the spins.

        J:
            The scale of the couplings.

        h:
            The external field.

        temperature:
            The temperature.

        couplings:
            The coupling matrix `C`, one row and one column per spin, or `None` for the uniform couplings `1 / N`.

    Examples:
        >>> from skcomplex.systems.physics.statistical_mechanics import GlauberFlipInteraction
        >>> GlauberFlipInteraction('flips', target='spins').temperature
        0.5
    """

    law_classes = (JumpLaw,)

    def __init__(
        self,
        label: str,
        *,
        target: Role,
        J: float = 1.0,
        h: float = 0.0,
        temperature: Annotated[float, RealSet.open(0, oo)] = 0.5,
        couplings: npt.ArrayLike | None = None,
    ) -> None:
        super().__init__(label)
        self.target = target
        self.J = J
        self.h = h
        self.temperature = temperature
        self.couplings = couplings

    def _get_field(self, s: SimpleNamespace) -> Expression:
        """Return the local field of a spin, without its own contribution."""
        spin = s.target.spin
        if self.couplings is None:
            return s.J * (mean(spin) - spin / count()) + s.h
        return s.J * total(spin, weights=np.asarray(self.couplings, dtype=float)) + s.h

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the flips of up spins and of down spins at the Glauber rate.

        Args:
            context:
                The namespace of the target, the time `t`, and the parameters.
        """
        super()._prepare(context)
        spin = context.target.spin
        rate = 1 / (1 + exp(2 * spin * self._get_field(context) / context.temperature))
        self.laws_ = [JumpLaw(where(spin > 0, rate, 0), {spin: -2}), JumpLaw(where(spin < 0, rate, 0), {spin: 2})]