yarrow.diagram

Diagrams are the main datastructure of yarrow, and represent string diagrams.

The AbstractDiagram is the main datastructure of yarrow. It represents a string diagram as a cospan of bipartite multigraphs. For example, the diagram below left is represented internally below right:

a string diagram and its representation as a cospan

This representation (the AbstractDiagram class) consists of three things:

  1. An AbstractBipartiteMultigraph G (center grey box)

  2. The source map s: the dotted arrows from the left blue box to the center box

  3. The target map t: the dotted arrows from the right blue box to the center box

The center box G encodes the internal wiring of the diagram, while s and t encode the “dangling wires” on the left and right.

The AbstractDiagram class is a backend-agnostic implementation. Concrete implementations choose a backend, which is an implementation of the classes AbstractFiniteFunction and AbstractBipartiteMultigraph.

For example, numpy-backed diagrams are implemented by the Diagram class, which inherits AbstractDiagram and sets two class members:

  • _Fun = yarrow.array.numpy

  • _Graph = yarrow.bipartite_multigraph.BipartiteMultigraph

For more information on backends, see Backends.

Summary

AbstractDiagram(s, t, G)

Implements diagrams parametrised by an underlying choice of backend.

class yarrow.diagram.AbstractDiagram(s: AbstractFiniteFunction, t: AbstractFiniteFunction, G: AbstractBipartiteMultigraph)

Implements diagrams parametrised by an underlying choice of backend. To use this class, inherit from it and set class members:

  • _Fun (finite functions)

  • _Graph (bipartite multigraphs)

See for example the Diagram class, which uses numpy-backed arrays.

__init__(s: AbstractFiniteFunction, t: AbstractFiniteFunction, G: AbstractBipartiteMultigraph)

Construct a AbstractDiagram from a triple (s, t, G).

Description

Parameters:
  • s – Finite function of type A → G.W

  • t – Finite function of type B → G.W

  • G – An AbstractBipartiteMultigraph

property wires

Return the number of ‘wires’ in the diagram. A wire is a node in the graph corresponding to a wire of the string diagram.

property operations

Return the number of generating operations in the diagram.

property shape

Return the arity and coarity of the diagram.

property type

Return a pair of finite functions representing the type of the morphism.

Returns:

tuple of:

source(AbstractFiniteFunction): typed self.s.domain → Σ₀ target(AbstractFiniteFunction): typed self.t.domain → Σ₀

Return type:

(tuple)

classmethod empty(wn: AbstractFiniteFunction, xn: AbstractFiniteFunction)
Parameters:
  • wn – A FiniteFunction typed 0 → Σ₀: giving the generating objects

  • xn – A FiniteFunction typed 0 → Σ₁: giving the generating operations

Returns:

The empty diagram for the monoidal signature (Σ₀, Σ₁)

Note that for a non-finite signature, we allow the targets of wn and xn to be None.

classmethod identity(wn: AbstractFiniteFunction, xn: AbstractFiniteFunction)
Parameters:
  • wn – A FiniteFunction typed W → Σ₀: giving the generating objects

  • xn – A FiniteFunction typed 0 → Σ₁: giving the generating operations

Returns:

The identity diagram with W wires labeled wn : W → Σ₀ whose empty set of generators is labeled in Σ₁

Return type:

AbstractDiagram

classmethod twist(wn_A: AbstractFiniteFunction, wn_B: AbstractFiniteFunction, xn: AbstractFiniteFunction)
Parameters:
  • wn_A – typed A → Σ₀

  • wn_B – typed B → Σ₀

  • xn – typed 0 → Σ₁

Returns:

The symmetry diagram σ : A ● B → B ● A.

Return type:

AbstractDiagram

classmethod spider(s: AbstractFiniteFunction, t: AbstractFiniteFunction, w: AbstractFiniteFunction, x: AbstractFiniteFunction)

Create a Frobenius Spider (see Definition 2.8, Proposition 4.7 of [WZ23]).

Parameters:
  • s – source map typed S → W

  • t – target map typed T → W

  • w – wires typed W → Σ₀

  • x – empty set of operations 0 → Σ₁

Returns:

A frobenius spider with S inputs and T outputs.

Return type:

AbstractDiagram

classmethod half_spider(s: AbstractFiniteFunction, w: AbstractFiniteFunction, x: AbstractFiniteFunction)

Create a Frobenius Half-Spider, which is a spider whose target map is the identity

dagger()

Swap the source and target maps of the diagram.

Returns:

The dagger functor applied to this diagram.

Return type:

AbstractDiagram

classmethod singleton(a: AbstractFiniteFunction, b: AbstractFiniteFunction, xn: AbstractFiniteFunction)

Construct a diagram consisting of a single operation (Definition 4.9, [WZ23]).

Parameters:
  • x – A single operation represented as an AbstractFiniteFunction of type 1 → Σ₁

  • a – The input type of x as a finite function A → Σ₀

  • b – The output type of x as a finite function B → Σ₀

Returns:

a diagram with a single generating operation.

Return type:

AbstractDiagram

tensor(g: AbstractDiagram)

Stack one diagram atop another, so f.tensor(g) is the diagram depicted by

a depiction of the tensor product of diagrams
Parameters:

g (AbstractDiagram) – An arbitrary diagram

Returns:

The tensor product of this diagram with g.

Return type:

AbstractDiagram

compose(g: AbstractDiagram)

Compose this diagram with g, so f.compose(g) is the diagram

a depiction of the tensor product of diagrams
Parameters:

g (AbstractDiagram) – A diagram with g.type[0] == self.type[1]

Returns:

The tensor product of this diagram with g.

Return type:

AbstractDiagram

Raises:

AssertionError – If g.type[0] != f.type[1]

classmethod tensor_list(ds: List[AbstractDiagram], wn=None, xn=None)

Compute the tensor product of a list of diagrams. O(n) time in the size of the result.

Warning

Does not speed up to O(log n) in the parallel case

classmethod tensor_operations(ops: Operations)