Permutation#

class discopy.symmetric.Permutation(dom, perm)[source]#

Bases: Box

A permutation box, i.e. a Box that reorders its input wires.

A permutation holds a discopy.python.finset.Permutation perm as attribute, with the convention that output wire i comes from input wire perm[i], i.e. cod[i] == dom[perm[i]].

A Layer stores it as routing rather than as a generator, and the identity permutation is the identity diagram. It draws as a single band of crossing wires rather than a staircase of swaps.

Parameters:
  • dom (C0) – The domain, i.e. the wires to permute.

  • perm (Sequence[int]) – The permutation as a finset.Permutation or a list.

Examples

>>> x, y, z, w = map(Ty, "xyzw")
>>> perm = Permutation(x @ y @ z, [1, 2, 0])
>>> assert perm.cod == y @ z @ x
>>> assert perm.dagger() == Permutation(y @ z @ x, [2, 0, 1])
>>> assert Equation(perm >> perm.dagger(), Id(x @ y @ z))
>>> assert perm @ Id(w) == Permutation(x @ y @ z @ w, [1, 2, 0, 3])
>>> assert Permutation(x @ y, [1, 0]) != Swap(x, y)
>>> assert Equation(Permutation(x @ y, [1, 0]), Swap(x, y))
>>> assert Permutation(x @ y, [0, 1]) == Id(x @ y)

Writing permutations by hand keeps swap-heavy diagrams compact: a whole permutation occupies a single layer rather than a quadratic staircase of swaps. Reversing four wires before a single layer of boxes is a permutation layer followed by a box layer.

>>> f0, f1 = Box("f0", w, x), Box("f1", z, y)
>>> g0, g1 = Box("g0", y, z), Box("g1", x, w)
>>> reverse = Permutation(x @ y @ z @ w, [3, 2, 1, 0])
>>> diagram = reverse >> f0 @ f1 @ g0 @ g1
>>> diagram.depth()
1
>>> diagram.draw(
...     doctest='docs/_static/symmetric/foliation.svg', figsize=(4, 4))
../_images/foliation.svg
property is_identity: bool#

Whether the underlying permutation is the identity.

>>> assert Permutation(Ty('x', 'y'), [0, 1]).is_identity
property size: int#

Structural permutations are not generator boxes in a layer.

to_drawing()[source]#

Draw as a compact band, or as wires for the identity.

to_swaps()[source]#

The same permutation built as a composition of swaps.

>>> x, y, z = Ty('x'), Ty('y'), Ty('z')
>>> perm = Permutation(x @ y @ z, [1, 2, 0])
>>> assert Equation(perm.to_swaps(), perm)
Return type:

Diagram

to_tree()[source]#

Serialise a permutation, see discopy.utils.dumps().

>>> from discopy.utils import dumps, loads
>>> x, y = Ty('x'), Ty('y')
>>> assert loads(dumps(Permutation(x @ y, [1, 0])))\
...     == Permutation(x @ y, [1, 0])
Return type:

dict