Skip to content

Q2R reversible Ising automaton

The Q2R automaton.

  • Class: Q2rSystem
  • Import: from skcomplex.systems.physics.statistical_mechanics import Q2rSystem
  • Field: physics/statistical_mechanics
  • Topics: lattice, cellular automaton, reversible, spins
  • References:
    • Vichniac 1984
    • Pomeau 1984
    • Lindgren 2024

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 Q2rSystem

trajectory = Q2rSystem().prepare().simulate()

Parameters

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

Parameter Default
shape (64, 64)
up 0.3
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 update
Randomness deterministic
Domains real
Structure fixed

Class

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

class Q2rSystem(BaseSystem):
    """The Q2R automaton.

    It is a reversible, energy-conserving Ising dynamics on a periodic lattice of even sides.

    The spins start uncorrelated with a fraction `up` of spins up, and the sites of even coordinate sum update first.
    The system has the spins `spins.spin` and phases `spins.phase`.

    Args:
        shape:
            The number of sites along each axis, all even.

        up:
            The probability that an initial spin is up.

        random_state:
            The seed or generator of the 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 Q2rSystem
        >>> trajectory = Q2rSystem((8,)).prepare().simulate(until=1)
        >>> trajectory.get_values('spins.spin').shape
        (2, 8)
    """

    field = 'physics/statistical_mechanics'
    topics = ('lattice', 'cellular automaton', 'reversible', 'spins')
    references = ('Vichniac 1984', 'Pomeau 1984', 'Lindgren 2024')
    until = 100
    dt = 1

    def __init__(
        self,
        shape: Sequence[int] = (64, 64),
        *,
        up: float = 0.3,
        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.shape = shape
        self.up = up
        self.random_state = random_state

    @staticmethod
    def _get_checkerboard(shape: tuple[int, ...]) -> Array:
        """Return 1 on the sites of even coordinate sum and 0 on the others."""
        return (np.indices(shape).sum(axis=0) % 2 == 0).astype(float)

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set the spins and phases of the lattice and their flips.

        Args:
            context:
                The observables to compile with the problem, or `None` for none.
        """
        super()._prepare(context)
        shape = tuple(self.shape)
        spins = np.where(np.random.default_rng(self.random_state).random(shape) < self.up, 1.0, -1.0)
        sites = Q2RSpinElement(
            'spins',
            spin=spins.ravel(),
            phase=self._get_checkerboard(shape).ravel(),
            space=LatticeSpace(shape),
        )
        self.system_ = System(self.label, elements=[sites], interactions=[Q2RFlipInteraction('flip', target='spins')])

Components

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

Elements:

Interactions:

  • Q2RFlipInteraction, from Interaction
class Q2RSpinElement(Element):
    """Sites of the Q2R automaton.

    Every site holds a spin and a phase that says whether the site updates in the current step.

    Args:
        label:
            The label of the site or the lattice of sites.

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

        phase:
            The initial phase, 1 for updating and 0 for quiescent, or one phase per site.

        space:
            The lattice of the sites, or `None` for one site.

    Examples:
        >>> from skcomplex.systems.physics.statistical_mechanics import Q2RSpinElement
        >>> Q2RSpinElement('site').phase
        0.0
    """

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

class Q2RFlipInteraction(Interaction):
    """Flips of the Q2R automaton.

    The local rule of Q2R flips the spins of the updating sites whose neighbours sum to zero, which leaves the Ising
    energy unchanged, and swaps the phase of every site, so the two sublattices update in turn and the dynamics is
    reversible.

    Args:
        label:
            The label of the interaction.

        target:
            The path of the sites, whose values are the spins `spin` and the phases `phase`.

    Examples:
        >>> from skcomplex.systems.physics.statistical_mechanics import Q2RFlipInteraction
        >>> Q2RFlipInteraction('flip', target='spins').target
        'spins'
    """

    law_classes = (UpdateLaw,)

    def __init__(self, label: str, *, target: Role) -> None:
        super().__init__(label)
        self.target = target

    @staticmethod
    def _flip_without_cost(*columns: Array, **kwargs: object) -> tuple[Array, Array]:
        """Return the spins and phases after one Q2R step.

        The step reads the spins and the phases of a centred neighbourhood.
        """
        half = len(columns) // 2
        spin, around, phase = columns[0], columns[1:half], columns[half]
        flips = (phase == 1) & (sum(around) == 0)
        return np.where(flips, -spin, spin), 1 - phase

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the flips of the spins and the swap of the phases.

        Args:
            context:
                The namespace of the target, the time `t`, and the parameters.
        """
        super()._prepare(context)
        sites = context.target
        spin_and_phase = (sites.spin, sites.phase)
        self.laws_ = [UpdateLaw(spin_and_phase, self._flip_without_cost, neighborhood=NEAREST, label=self.label)]