Skip to content

Fredkin's self-replicating parity automaton

Fredkin's parity automaton.

  • Class: FredkinReplicatorSystem
  • Import: from skcomplex.systems.computer_science.automata import FredkinReplicatorSystem
  • Field: computer_science/automata
  • Topics: lattice, replicator, linear, parity
  • References:
    • Fredkin 1990
    • Dilão 1993
    • Dilão 2023

Use this system

The block imports the class and simulates the system with its default parameters and its tuned run settings.

from skcomplex.systems.computer_science.automata import FredkinReplicatorSystem

trajectory = FredkinReplicatorSystem().prepare().simulate()

Parameters

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

Parameter Default
shape (128, 128)
initial None
radius 4.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 unknown
Domains boolean
Structure fixed

Class

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

class FredkinReplicatorSystem(BaseSystem):
    """Fredkin's parity automaton.

    Its local law copies any initial pattern into translated replicas.

    Every cell takes the sum modulo 2 of its four nearest neighbours, `x(i, j, n + 1) = x(i - 1, j, n) + x(i + 1, j, n)
    + x(i, j - 1, n) + x(i, j + 1, n) (mod 2)`. The law is linear over the integers modulo 2, and raising it to the
    power `2^k` gives the sum of the pattern translated by `2^k` cells in each of the four directions, so at the times
    `2^k` the initial pattern reappears four times, far enough apart not to overlap once `2^k` exceeds its width, and at
    other times the copies multiply in more complex arrangements. By default the pattern is a circumference, as in
    Dilão's example. The system has the cells `cells.alive`.

    Args:
        shape:
            The number of rows and columns of the torus.

        initial:
            The initial cells, an array of the shape, or `None` for a circumference of the radius in the middle.

        radius:
            The radius of the default circumference.

        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.computer_science.automata import FredkinReplicatorSystem
        >>> int(FredkinReplicatorSystem((32, 32)).prepare().system_.elements[0].alive.sum())
        32
    """

    field = 'computer_science/automata'
    topics = ('lattice', 'replicator', 'linear', 'parity')
    references = ('Fredkin 1990', 'Dilão 1993', 'Dilão 2023')
    until = 32
    dt = 1

    def __init__(
        self,
        shape: Sequence[int] = (128, 128),
        *,
        initial: npt.ArrayLike | None = None,
        radius: float = 4.0,
        label: str = 'system',
        solver: BaseSolver | str = 'auto',
        backend: BaseBackend | str = 'auto',
    ) -> None:
        super().__init__(label, solver=solver, backend=backend)
        self.shape = shape
        self.initial = initial
        self.radius = radius

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set the system of the cells, a circumference by default, and their parity rule."""
        super()._prepare(context)
        if self.initial is None:
            rows, columns = np.indices(tuple(self.shape))
            distance = np.hypot(rows - self.shape[0] // 2, columns - self.shape[1] // 2)
            values = (np.abs(distance - self.radius) < HALF_CELL).astype(float)
        else:
            values = np.asarray(self.initial, dtype=float)
        parity = LifeRuleInteraction(
            'rule',
            target='cells',
            born=(1, 3),
            survive=(1, 3),
            neighborhood=RadiusNeighborhood(1, metric='manhattan'),
        )
        self.system_ = System(
            self.label,
            elements=[BinaryCellElement('cells', alive=values.ravel(), space=LatticeSpace(tuple(self.shape)))],
            interactions=[parity],
        )

Components

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

Elements:

Interactions:

  • LifeRuleInteraction, from Interaction
class BinaryCellElement(Element):
    """Cell of an automaton whose value `alive` is 0 or 1.

    The local rule of an interaction changes the value. With `space`, the element holds one cell on every site.

    Args:
        label:
            The label of the cells.

        alive:
            The initial value, 0 or 1, or one value per cell.

        space:
            The space with one cell on every site, such as a lattice, or `None` for one cell.

    Examples:
        >>> from skcomplex.systems.computer_science.automata import BinaryCellElement
        >>> BinaryCellElement('cell', alive=1).alive
        1
    """

    def __init__(
        self,
        label: str,
        *,
        alive: Annotated[float | Array, {0, 1}] = 0,
        space: BaseSpace | None = None,
    ) -> None:
        super().__init__(label, space=space)
        self.alive = alive

class LifeRuleInteraction(Interaction):
    """Life-like rule of cells, such as `B3/S23` of Conway's Game of Life.

    A dead cell comes to life when its number of live neighbours is one of `born`, and a live cell stays alive when it
    is one of `survive`. Every other cell is dead at the next step.

    Args:
        label:
            The label of the interaction.

        target:
            The cells whose state `alive` the rule updates.

        born:
            The numbers of live neighbours that bring a dead cell to life.

        survive:
            The numbers of live neighbours that keep a live cell alive.

        neighborhood:
            The neighbours every cell counts, the eight cells around it by default.

    Examples:
        >>> from skcomplex.systems.computer_science.automata import LifeRuleInteraction
        >>> LifeRuleInteraction('rule', target='cells', born=(3, 6)).born
        (3, 6)
    """

    def __init__(
        self,
        label: str,
        *,
        target: Role = 'cells',
        born: Sequence[int] = (3,),
        survive: Sequence[int] = (2, 3),
        neighborhood: BaseNeighborhood = MOORE,
    ) -> None:
        super().__init__(label)
        self.target = target
        self.born = born
        self.survive = survive
        self.neighborhood = neighborhood

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the local rule of the cells as the law.

        Args:
            context:
                The namespace of the elements a binding of the roles names.
        """
        super()._prepare(context)
        rule = life(self.born, self.survive)
        alive = context.target.alive
        self.laws_: list[BaseLaw] = [UpdateLaw(alive, rule, neighborhood=self.neighborhood, label=self.label)]