# String diagram

String diagrams are a formal graphical language for representing morphisms in monoidal categories, or more generally 2-cells in 2-categories. They are a prominent tool in applied category theory. When interpreted in the monoidal category of vector spaces and linear maps with the tensor product, string diagrams are called tensor networks or Penrose graphical notation. This has led to the development of categorical quantum mechanics where the axioms of quantum theory are expressed in the language of monoidal categories.

## History

Günter Hotz gave the first mathematical definition of string diagrams in order to formalise electronic circuits. However, the invention of string diagrams is usually credited to Roger Penrose, with Feynman diagrams also described as a precursor. They were later characterised as the arrows of free monoidal categories in a seminal article by André Joyal and Ross Street. While the diagrams in these first articles were hand-drawn, the advent of typesetting software such as LaTeX and PGF/TikZ made the publication of string diagrams more wide-spread.
The existential graphs and diagrammatic reasoning of Charles Sanders Peirce are arguably the oldest form of string diagrams, they are interpreted in the monoidal category of finite sets and relations with the Cartesian product. The lines of identity of Peirce's existential graphs can be axiomatised as a Frobenius algebra, the cuts are unary operators on homsets that axiomatise logical negation. This makes string diagrams a sound and complete two-dimensional deduction system for first-order logic, invented independently from the one-dimensional syntax of Gottlob Frege's Begriffsschrift.

## Intuition

String diagrams are made of boxes $f:x\to y$, which represent processes, with a list of wires $x$ coming in at the top and $y$ at the bottom, which represent the input and output systems being processed by the box $f$. Starting from a collection of wires and boxes, called a signature, one may generate the set of all string diagrams by induction:
* each box $f:x\to y$ is a string diagram,
* for each list of wires $x$, the identity ${\text{id}}(x):x\to x$ is a string diagram representing the process which does nothing to its input system, it is drawn as a bunch of parallel wires,
* for each pair of string diagrams $f:x\to y$ and $f':x'\to y'$, their tensor $f\otimes f':xx'\to yy'$ is a string diagram representing the parallel composition of processes, it is drawn as the horizontal concatenation of the two diagrams,
* for each pair of string diagrams $f:x\to y$ and $g:y\to z$, their composition $g\circ f:x\to z$ is a string diagram representing the sequential composition of processes, it is drawn as the vertical concatenation of the two diagrams.

## Definition
### Algebraic

Let the Kleene star $X^{\star }$ denote the free monoid, i.e. the set of lists with elements in a set $X$.
A monoidal signature $\Sigma$ is given by:
* a set $\Sigma _{0}$ of generating objects, the lists of generating objects in Σ$_0$$^⋆${\textstyle \Sigma _{0}^{\star }} are also called types,
* a set $\Sigma _{1}$ of generating arrows, also called boxes,
* a pair of functions ${\text{dom}},{\text{cod}}:\Sigma _{1}\to \Sigma _{0}^{\star }$ which assign a domain and codomain to each box, i.e. the input and output types.
A morphism of monoidal signature $F:\Sigma \to \Sigma '$ is a pair of functions $F_{0}:\Sigma _{0}\to \Sigma '_{0}$ and $F_{1}:\Sigma _{1}\to \Sigma '_{1}$ which is compatible with the domain and codomain, i.e. such that ${\text{dom}}\circ F_{1}\ =\ F_{0}\circ {\text{dom}}$ and ${\text{cod}}\circ F_{1}\ =\ F_{0}\circ {\text{cod}}$. Thus we get the category $\mathbf {MonSig}$ of monoidal signatures and their morphisms.
There is a forgetful functor $U:\mathbf {MonCat} \to \mathbf {MonSig}$ which sends a monoidal category to its underlying signature and a monoidal functor to its underlying morphism of signatures, i.e. it forgets the identity, composition and tensor. The free functor $C_{-}:\mathbf {MonSig} \to \mathbf {MonCat}$, i.e. the left adjoint to the forgetful functor, sends a monoidal signature $\Sigma$ to the free monoidal category $C_{\Sigma }$ it generates.
String diagrams (with generators from $\Sigma$) are arrows in the free monoidal category $C_{\Sigma }$. The interpretation in a monoidal category $D$ is a defined by a monoidal functor $F:C_{\Sigma }\to D$, which by freeness is uniquely determined by a morphism of monoidal signatures $F:\Sigma \to U(D)$. Intuitively, once the image of generating objects and arrows are given, the image of every diagram they generate is fixed.

### Geometric

A topological graph, also called a one-dimensional cell complex, is a tuple $(\Gamma ,\Gamma _{0},\Gamma _{1})$ of a Hausdorff space $\Gamma$, a closed discrete subset $\Gamma _{0}\subseteq \Gamma$ of nodes and a set of connected components $\Gamma _{1}$ called edges, each homeomorphic to an open interval with boundary in $\Gamma _{0}$ and such that Γ−Γ$_0$=∐Γ$_1${\textstyle \Gamma -\Gamma _{0}=\coprod \Gamma _{1}}.
A plane graph between two real numbers $a,b\in \mathbb {R}$ with $a<b$ is a finite topological graph embedded in $\mathbb {R} \times [a,b]$ such that every point $x\in \Gamma \ \cap \ \mathbb {R} \times \{a,b\}$ is also a node $x\in \Gamma _{0}$ and belongs to the closure of exactly one edge in $\Gamma _{1}$. Such points are called outer nodes, they define the domain and codomain ${\text{dom}}(\Gamma ),{\text{cod}}(\Gamma )\in \Gamma _{1}^{\star }$ of the string diagram, i.e. the list of edges that are connected to the top and bottom boundary. The other nodes $f\in \Gamma _{0}\ -\ \{a,b\}\times \mathbb {R}$ are called inner nodes.
A plane graph is progressive, also called recumbent, when the vertical projection $e\to [a,b]$ is injective for every edge $e\in \Gamma _{1}$. Intuitively, the edges in a progressive plane graph go from top to bottom without bending backward. In that case, each edge can be given a top-to-bottom orientation with designated nodes as source and target. One can then define the domain and codomain ${\text{dom}}(f),{\text{cod}}(f)\in \Gamma _{1}^{\star }$ of each inner node $f$, given by the list of edges that have source and target.
A plane graph is generic when the vertical projection $\Gamma _{0}-\{a,b\}\times \mathbb {R} \to [a,b]$ is injective, i.e. no two inner nodes are at the same height. In that case, one can define a list ${\text{boxes}}(\Gamma )\in \Gamma _{0}^{\star }$ of the inner nodes ordered from top to bottom.
A progressive plane graph is labeled by a monoidal signature $\Sigma$ if it comes equipped with a pair of functions $v_{0}:\Gamma _{1}\to \Sigma _{0}$ from edges to generating objects and $v_{1}:\Gamma _{0}-\{a,b\}\times \mathbb {R} \to \Sigma _{1}$ from inner nodes to generating arrows, in a way compatible with domain and codomain.
A deformation of plane graphs is a continuous map $h:\Gamma \times [0,1]\to [a,b]\times \mathbb {R}$ such that
* the image of $h(-,t)$ defines a plane graph for all $t\in [0,1]$,
* for all $x\in \Gamma _{0}$, if $h(x,t)$ is an inner node for some $t$ it is inner for all $t\in [0,1]$.
A deformation is progressive (generic, labeled) if $h(-,t)$ is progressive (generic, labeled) for all $t\in [0,1]$. Deformations induce an equivalence relation with $\Gamma \sim \Gamma '$ if and only if there is some $h$ with $h(-,0)=\Gamma$ and $h(-,1)=\Gamma '$. String diagrams are equivalence classes of labeled progressive plane graphs. Indeed, one can define:
* the identity diagram ${\text{id}}(x)$ as a set of parallel edges labeled by some type $x\in \Sigma _{0}^{\star }$,
* the composition of two diagrams as their vertical concatenation with the codomain of the first identified with the domain of the second,
* the tensor of two diagrams as their horizontal concatenation.

### Combinatorial

While the geometric definition makes explicit the link between category theory and low-dimensional topology, a combinatorial definition is necessary to formalise string diagrams in computer algebra systems and use them to define computational problems. One such definition is to define string diagrams as equivalence classes of well-typed formulae generated by the signature, identity, composition and tensor. In practice, it is more convenient to encode string diagrams as formulae in generic form, which are in bijection with the labeled generic progressive plane graphs defined above.
Fix a monoidal signature $\Sigma$. A layer is defined as a triple $(x,f,y)\in \Sigma _{0}^{\star }\times \Sigma _{1}\times \Sigma _{0}^{\star }=:L(\Sigma )$ of a type $x$ on the left, a box $f$ in the middle and a type $y$ on the right. Layers have a domain and codomain ${\text{dom}},{\text{cod}}:L(\Sigma )\to \Sigma _{0}^{\star }$ defined in the obvious way. This forms a directed multigraph, also known as a quiver, with the types as vertices and the layers as edges. A string diagram $d$ is encoded as a path in this multigraph, i.e. it is given by:
* a domain ${\text{dom}}(d)\in \Sigma _{0}^{\star }$ as starting point
* a length ${\text{len}}(d)=n\geq 0$,
* a list of ${\text{layers}}(d)=d_{1}\dots d_{n}\in L(\Sigma )$
such that ${\text{dom}}(d_{1})={\text{dom}}(d)$ and ${\text{cod}}(d_{i})={\text{dom}}(d_{i+1})$ for all $i<n$. In fact, the explicit list of layers is redundant, it is enough to specify the length of the type to the left of each layer, known as the offset. The whiskering $d\otimes z$ of a diagram $d=(x_{1},f_{1},y_{1})\dots (x_{n},f_{n},y_{n})$ by a type $z$ is defined as the concatenation to the right of each layer $d\otimes z=(x_{1},f_{1},y_{1}z)\dots (x_{n},f_{n},y_{n}z)$ and symmetrically for the whiskering $z\otimes d$ on the left. One can then define:
* the identity diagram ${\text{id}}(x)$ with ${\text{len}}({\text{id}}(x))=0$ and ${\text{dom}}({\text{id}}(x))=x$,
* the composition of two diagrams as the concatenation of their list of layers,
* the tensor of two diagrams as the composition of whiskerings $d\otimes d'=d\otimes {\text{dom}}(d')\ \circ \ {\text{cod}}(d)\otimes d'$.
Note that because the diagram is in generic form (i.e. each layer contains exactly one box) the definition of tensor is necessarily biased: the diagram on the left hand-side comes above the one on the right-hand side. One could have chosen the opposite definition d⊗d$^′$=dom(d)⊗d$^′$ ∘ d⊗cod(d$^′$){\textstyle d\otimes d'={\text{dom}}(d)\otimes d'\ \circ \ d\otimes {\text{cod}}(d')}.
Two diagrams are equal (up to the axioms of monoidal categories) whenever they are in the same equivalence class of the congruence relation generated by the interchanger:$d\otimes {\text{dom}}(d')\ \circ \ {\text{cod}}(d)\otimes d'\quad =\quad {\text{dom}}(d)\otimes d'\ \circ \ d\otimes {\text{cod}}(d')$That is, if the boxes in two consecutive layers are not connected then their order can be swapped. Intuitively, if there is no communication between two parallel processes then the order in which they happen is irrelevant.
The word problem for free monoidal categories, i.e. deciding whether two given diagrams are equal, can be solved in polynomial time. The interchanger is a confluent rewriting system on the subset of boundary connected diagrams, i.e. whenever the plane graphs have no more than one connected component which is not connected to the domain or codomain and the Eckmann–Hilton argument does not apply.

## Extension to 2-categories

The idea is to represent structures of dimension d by structures of dimension 2-d, using Poincaré duality. Thus,
* an object is represented by a portion of plane,
* a 1-cell $f:A\to B$ is represented by a vertical segment—called a string—separating the plane in two (the right part corresponding to A and the left one to B),
* a 2-cell $\alpha :f\Rightarrow g:A\to B$ is represented by an intersection of strings (the strings corresponding to f above the link, the strings corresponding to g below the link).
The parallel composition of 2-cells corresponds to the horizontal juxtaposition of diagrams and the sequential composition to the vertical juxtaposition of diagrams.
A monoidal category is equivalent to a 2-category with a single 0-cell. Intuitively, going from monoidal categories to 2-categories amounts to adding colours to the background of string diagrams.

## Examples
### The snake equation

Consider an adjunction $(F,G,\eta ,\varepsilon )$ between two categories ${\mathcal {C}}$ and ${\mathcal {D}}$ where $F:{\mathcal {C}}\leftarrow {\mathcal {D}}$ is left adjoint of $G:{\mathcal {C}}\rightarrow {\mathcal {D}}$ and the natural transformations $\eta :I\rightarrow GF$ and $\varepsilon :FG\rightarrow I$ are respectively the unit and the counit. The string diagrams corresponding to these natural transformations are:
The string corresponding to the identity functor is drawn as a dotted line and can be omitted. The definition of an adjunction requires the following equalities:
    ${\begin{aligned}(\varepsilon F)\circ F(\eta )&=1_{F}\\G(\varepsilon )\circ (\eta G)&=1_{G}\end{aligned}}$
The first one is depicted as
$(\varepsilon F)\circ F(\eta )=1_{F}$
A monoidal category where every object has a left and right adjoint is called a rigid category. String diagrams for rigid categories can be defined as non-progressive plane graphs, i.e. the edges can bend backward.
In the context of categorical quantum mechanics, this is known as the snake equation.
The category of Hilbert spaces is rigid, this fact underlies the proof of correctness for the quantum teleportation protocol. The unit and counit of the adjunction are an abstraction of the Bell state and the Bell measurement respectively. If Alice and Bob share two qubits Y and Z in an entangled state and Alice performs a (post-selected) entangled measurement between Y and another qubit X, then this qubit X will be teleported from Alice to Bob: quantum teleportation is an identity morphism.
The same equation appears in the definition of pregroup grammars where it captures the notion of information flow in natural language semantics. This observation has led to the development of the DisCoCat framework and quantum natural language processing.

## Hierarchy of graphical languages

Many extensions of string diagrams have been introduced to represent arrows in monoidal categories with extra structure, forming a hierarchy of graphical languages which is classified in Selinger's Survey of graphical languages for monoidal categories.
* Braided monoidal categories with 3-dimensional diagrams, a generalisation of braid groups.
* Symmetric monoidal categories with 4-dimensional diagrams where edges can cross, a generalisation of the symmetric group.
* Ribbon categories with 3-dimensional diagrams where the edges are undirected, a generalisation of knot diagrams.
* Compact closed categories with 4-dimensional diagrams where the edges are undirected, a generalisation of Penrose graphical notation.
* Dagger categories where every diagram has a horizontal reflection.

## List of applications

String diagrams have been used to formalise the following objects of study.
* Concurrency theory
* Artificial neural networks
* Game theory
* Bayesian probability
* Consciousness
* Markov kernels
* Signal-flow graphs
* Conjunctive queries
* Bidirectional transformations
* Categorical quantum mechanics
* Quantum circuits, measurement-based quantum computing and quantum error correction, see ZX-calculus
* Natural language processing, see DisCoCat
* Quantum natural language processing