Wiring diagrams as normal forms for computing in symmetric monoidal categories
DDavid I. Spivak and Jamie Vicary (Eds.):Applied Category Theory 2020 (ACT2020)EPTCS 333, 2021, pp. 49–64, doi:10.4204/EPTCS.333.4
Wiring diagrams as normal forms forcomputing in symmetric monoidal categories
Evan Patterson David I. Spivak Dmitry Vagner
Applications of category theory often involve symmetric monoidal categories (SMCs), in which ab-stract processes or operations can be composed in series and parallel. However, in 2020 there remainsa dearth of computational tools for working with SMCs. We present an “unbiased” approach to im-plementing symmetric monoidal categories, based on an operad of directed, acyclic wiring diagrams.Because the interchange law and other laws of a SMC hold identically in a wiring diagram, no rewriterules are needed to compare diagrams. We discuss the mathematics of the operad of wiring diagrams,as well as its implementation in the software package Catlab.
The syntax for an algebraic structure is often derived from its traditional axiomatization, without addi-tional thought. A symmetric monoidal category (SMC) is defined through operations of composition,identity, monoidal product, monoidal unit, and braiding, subject to various laws. Once it is decided howto assign symbols to these operations, such as (cid:35) for composition and ⊗ for the monoidal product, a sym-bolic syntax for constructing objects and morphisms follows immediately. So, given morphisms, say f : x → x ⊗ y and g : y ⊗ z → z , a new morphism can be constructed via such expressions as ( f ⊗ id z ) (cid:35) ( id x ⊗ g ) . Symbolic syntax has a long tradition in algebra. Its utility derives, on the one hand, from its ease inwriting and typesetting, and on the other, from its immediacy given an axiomatization of an algebraicstructure as a generalized algebraic theory.But these are not the only desiderata for mathematical syntax. In general, the mathematical ob-jects denoted by two different expressions may be equal under the axioms. A good syntax narrows thegap between a mathematical object and its representation by avoiding redundancy. For example, sincemonoidal products are associative in a strict SMC, the expressions f ⊗ ( g ⊗ h ) and ( f ⊗ g ) ⊗ h denotethe same morphism; thus, it is standard practice to eliminate parentheses around the monoidal product,writing simply f ⊗ g ⊗ h .Encoding algebraic equations into a simplified yet unambiguous syntax has important cognitive andcomputational benefits. For humans, it substitutes visual inspection for equational reasoning, playing toour cognitive strengths. For computers, it reduces possibly complex algorithms for checking equality toa simple test of identity on a suitable data structure. From this perspective, the ideal syntax provides a normal form , making two expressions identical if and only if they denote equal mathematical objects.What is the right syntax for symmetric monoidal categories? Beginning with the Penrose graphicalnotation for tensors [8], it was gradually understood that morphisms in a monoidal category are bestdepicted by a two-dimensional syntax, with one axis representing composition and the other representing0 Wiring diagrams as normal forms for computing in SMCs monoidal product. For example, the above expression ( f ⊗ id z ) (cid:35) ( id x ⊗ g ) becomes the string diagramf g x xyz z As string diagrams, both sides of the interchange law ( f (cid:35) g ) ⊗ ( h (cid:35) k ) = ( f ⊗ h ) (cid:35) ( g ⊗ k ) of a monoidalcategory have the same representation, namely: f gh k String diagrams were first put on a rigorous footing by Joyal and Street, who showed that the diagram-matic language is sound and complete for the equations between morphisms deducible from the axiomsof a strict symmetric monoidal category [5]. Diagrammatic languages are now known for other kinds ofmonoidal categories, such as traced monoidal categories and hypergraph categories [10, 3].For Joyal and Street, string diagrams are geometric figures in the plane or in a higher-dimensionalEuclidean space. This perspective, while intuitively appealing, is of little computational use, since geo-metric objects are not readily translated into data structures. For computational purposes, we would liketo extract the combinatorial data defining a string diagram, much like a graph does for a graph embeddingor graph drawing.
Wiring diagrams were introduced in [9]. Though they are often depicted graphically, wiring diagramsare combinatorial, rather than geometric, objects. They also differ from string diagrams in that they dealonly with the syntax of composition, and do not include explicit morphisms from a given SMC. Moreprecisely, wiring diagrams—say for representing compositions in symmetric monoidal categories—areorganized as the morphisms of a typed operad W . Composition of morphisms corresponds to nesting ofwiring diagrams, for example: (cid:35) = (1)Note that in the symbolic syntax of a symmetric monoidal category, there are infinitely-many ways torepresent any of the wiring diagrams in (1), for example the middle one could be represented by tensoringwith an arbitrary number of monoidal units, but the wiring diagram represents exactly one element of theset W ( X , X ; Y ) , where X and X are the inner boxes and Y is the outer box.A given model of W , which we can think of as a symmetric monoidal category C , is representedas a functor H : W → Set , which we refer to as a W -algebra. Each box (object in W ) is sent by H tothe set of all C -morphisms of that shape, and each wiring diagram (morphism in W ) is sent by H to thefunction that takes morphisms in C and composes them accordingly.Operads of wiring diagrams can be used to model various sorts of categories, such as traced or hyper-graph categories [12, 3]—whose string diagram languages we mentioned above—or even just ordinarycategories. Here we will focus on the operad W of wiring diagrams for symmetric monoidal categories. For now, we elide the fact that wires should be annotated by types corresponding to the objects of C . van Patterson, David I. Spivak, and Dmitry Vagner W can serve as a foundation for the computer algebra ofsymmetric monoidal categories, both practically and theoretically. Since the interchange law and otheraxioms of an SMC hold identically within W , wiring diagrams are nearly normal forms for morphismsin a free SMC, as will be explained. With this motivation, Section 2 describes an implementation ofthe operad of wiring diagrams in Catlab.jl, a Julia package for applied category theory [7]. The operadstructure is taken as fundamental in Catlab and used to implement a diagrammatic syntax for symmetricmonoidal categories. The wiring diagram operad is then extended to SMCs with extra structure, suchas cartesian monoidal categories, biproduct categories, and traced monoidal categories. In contrast tosystems like Quantomatic [6] and Cartographer [11], Catlab is not a graphical editor or a proof assistant;rather, it provides data structures and algorithms for computing with in symmetrical monoidal categoriesfor scientific and engineering applications.The remainder of the paper is about the mathematics of the operad of wiring diagrams. After somepreliminaries on biproducts and spans in Section 3, an operad of wiring diagrams is constructed in Sec-tion 4. The directed wiring diagrams are defined to satisfy an acyclicity condition, making them suitablefor symmetric monoidal categories. They are built using the categorical matrix calculus, which extendsthe notion of an adjacency matrix of a directed graph. Finally, Section 5 shows how algebras on the wiringdiagram operad give rise to symmetric monoidal categories and, conversely, how symmetric monoidalcategories yield algebras on this operad. These two constructions are not inverse equivalences; however,beginning with an SMC, the roundtrip does return an equivalent SMC. We will make the correspondencemore precise in Theorems 5.2 and 5.3. In the Julia package Catlab, wiring diagrams are implemented as combinatorial data structures. Wiringdiagrams are akin to directed graphs but possess extra structure; namely each box—which plays therole of a node—has an explicit set of input and output ports. Every wiring diagram has an underlyingdirected graph, obtained by forgetting this extra structure. For implementation purposes, Catlab exploitsthis hierarchy by building its data structure for wiring diagrams on top of graph data structures thatalready exist in the Julia ecosystem.The functionality for wiring diagrams is implemented in several layers. The bottom layer is thecore data structure and a low-level imperative interface for mutating it. Operadic composition—orsubstitution—of wiring diagrams is defined using this interface. Finally, the operadic interface is usedto define a syntax for morphisms in symmetric monoidal categories. The three layers are summarized inSections 2.1, 2.2, and 2.3 respectively.
The data structure for wiring diagrams is comprised of several data types. From the user’s perspective,these types are:1.
WiringDiagram : a quadruple consisting of (i) a list of input port types and (ii) a list of outputport types, both for the outer box; (iii) a list of boxes, where the first and second entries are specialvalues representing the input and output types of the outer box and the remaining entries are boxesinside the diagram, of type
Box ; and (iv) a set of wires, of type
Wire .2 Wiring diagrams as normal forms for computing in SMCs Box : a triple consisting of (i) a label or value for the box, (ii) a list of input port types, (iii) and a listof output port types. Thus, a morphism f : x ⊗ y → z would be represented as Box(:f, [:x,:y],[:z]) , where the Julia syntax :x denotes a symbol named “ x ”.3. Wire : a source-target pair, where both the source and target are pairs of numbers identifying a boxin the diagram and an input or output port on that box. So, a wire from output port p on box i toinput port q on box j is Wire((i,p) => (j,q)) .As a small but complete example, the wiring diagram corresponding to the composite f (cid:35) g : x → z ofmorphisms f : x → y and g : y → z is implemented as: WiringDiagram([:x], [:z],[ 1 => {inputs}, 2 => {outputs}, 3 => Box(:f, [:x], [:y]), 4 => Box(:g, [:y], [:z]) ],[ Wire((1,1) => (3,1)), Wire((3,1) => (4,1)), Wire((4,1) => (2,1)) ])
For performance reasons, the wires in a wiring diagram are not actually stored as a set. Instead,underlying each wiring diagram is a simple directed graph, as implemented by the Julia package Light-Graphs.jl [1]. The vertices in the graph are numbered consecutively from 1 to n +
2, where n is thenumber of boxes in the wiring diagram. Vertices 1 and 2, labelled { inputs } and { outputs } above,refer to the inputs and outputs of the outer box, respectively; the remaining vertices refer to boxes insidethe diagram. There is an edge between two vertices if and only if there is at least one wire between thecorresponding boxes.Traversals on the wiring diagram are then delegated to the underlying directed graph. For example, tofind the in-neighbors or out-neighbors of a box, one simply finds the in-neighbors or out-neighbors of thecorresponding vertex in the underlying graph. The graph data structure is designed to do this efficiently.Wiring diagrams can be accessed and mutated through a simple imperative interface. Boxes, ports, andwires can be retrieved individually or iterated over, and boxes and wires can be added and removed froman existing diagram. Operadic composition of wiring diagrams as in (1) is supported through the function ocompose , withtwo signatures: ocompose(f::WiringDiagram, gs::Vector{<:WiringDiagram})::WiringDiagramocompose(f::WiringDiagram, i::Int, g::WiringDiagram)::WiringDiagram
The first signature corresponds to full (May-style) operadic composition ◦ and the second corre-sponds to partial (Markl-style) operadic composition ◦ i . Both methods are one-line wrappers aroundthe procedure substitute , which substitutes wiring diagrams for one or more boxes in another wiringdiagram. Mathematically, substitute simultaneously performs one or more non-overlapping partialoperadic compositions.Substitution of wiring diagrams is implemented using the imperative interface. In outline, the algo-rithm proceeds as follows:1. Create a copy d of the original wiring diagram.2. Add to d all the boxes from all the diagrams to be substituted.3. Extend each wire in each substituted diagram to new a wire in d . This subroutine branches intofour different cases, shown in pseudo-Julia code in Listing 1. van Patterson, David I. Spivak, and Dmitry Vagner d all the boxes from the original diagram that were to be substituted, which in turnremoves any extraneous wires created during step 3. function substitute_wires!(d::WiringDiagram, v::Int, sub::WiringDiagram)for wire in wires(sub) if {wire source and target are on outer box}for in_wire in {wires in d incoming to source outer port}for out_wire in {wires in d outgoing from target outer port}{add wire to d fusing in_wire -> wire -> out_wire}endend elseif {wire source is on outer box}for in_wire in {wires in d incoming to source outer port}{add wire to d fusing in_wire -> wire}end elseif {wire target is on outer box}for out_wire in {wires in d outgoing from target outer port}{add wire to d fusing wire -> out_wire}end else{add wire to d}endendend Listing 1: Pseudo-Julia code for main subroutine in substitution algorithm. Each wire substituted intothe new diagram is either passing, incoming, outgoing, or fully internal. The inner loops under each ofthe cases are needed because a port may have many or no incident wires, representing copying, merging,deleting, or creating.
Having implemented the operad of wiring diagrams, it is now straightforward to define an alternatesyntax for symmetric monoidal categories using wiring diagrams. That is, we construct a symmetricmonoidal category whose morphisms are wiring diagrams in which each box has been filled with amorphism. To compute the series composition f (cid:35) g of morphisms f and g , one forms the followingwiring diagram f g ... ... ... and then performs an operadic composition. Apart from exception handling and formatting, the followingJulia code for the procedure compose is identical to the implementation in Catlab. function compose(f::WiringDiagram, g::WiringDiagram)::WiringDiagram@assert length(codom(f)) == length(dom(g)) Wiring diagrams as normal forms for computing in SMCs h = WiringDiagram(dom(f), codom(g))fv, gv = add_box!(h, f), add_box!(h, g)add_wires!(h, [[ (input_id(h),i) => (fv,i) for i in 1:length(dom(f)) ];[ (fv,i) => (gv,i) for i in 1:length(codom(f)) ];[ (gv,i) => (output_id(h),i) for i in 1:length(codom(g)) ]])substitute(h, [fv,gv])end
Similarly, to compute the parallel composition f ⊗ g of two morphisms f and g , form the generic diagramwith two boxes composed in parallel and then perform an operadic composition: fg ... ...... ... The wiring diagram syntax provides a normal form for morphisms in a free symmetric monoidalcategory, up to (simple directed) graph isomorphism. Specifically, two morphisms represented by wiringdiagrams are equal if and only if there is an isomorphism of the underlying graphs making the diagramsidentical as Julia data structures. Although the graph isomorphism problem is not known to be solvablein polynomial time, in practice it can usually be solved efficiently. Moreover, the labels on the boxesdrastically restrict the possible matchings. Wiring diagrams thus constitute an effective normal form formorphisms in a free SMC.
We will formally present a wiring diagram by encoding all the interconnections between its boxes as asingle span. Since the category of spans enjoys a biproduct structure, we can represent wiring diagramsas matrices and their compositions using matrix algebra. We begin with a brief detour into biproductcategories.
Let ( C , ⊕ , ) be a biproduct category, i.e., a monoidal category for which ⊕ is both a product and acoproduct. Such categories can equivalently be seen as monoidal categories with a homomorphic supplyof bimonoids [4]. Given a morphism t ⊕ · · · ⊕ t m f −→ v ⊕ · · · ⊕ v n in such a category, we can extract the component t i f v j corresponding to a choice of direct summand t i in the domain and v j in the codomain, by pre- and post-composing f with canonical inclusions andprojections: t i t ⊕ · · · ⊕ t n v ⊕ · · · ⊕ v m v j . ι t i f π v j (2)This procedure defines a map of type C (cid:0) (cid:76) i : I t i , (cid:76) j : J v j (cid:1) → C ( t i , v j ) for finite sets I and J , to whichwe can define a section by swapping the inclusion and projections in (2). We say that this section is the van Patterson, David I. Spivak, and Dmitry Vagner embedding of the component since it produces a morphism for which all other components are the zeromorphisms : t k → v l , i.e. the unique morphism of the form t k → → v l .Any biproduct category is enriched in commutative monoids, with the sum f + g of two morphismsof type t → v given by t t ⊕ t v ⊕ v v ∆ f ⊕ g ∇ . Here ∆ and ∇ are the diagonal and co-diagonal morphisms arising from the universal property of product and coproduct. By the naturalityof these maps, composition distributes over sums. This operation allows us to define the leftward mapin the bijection C (cid:0) (cid:76) i : I t i , (cid:76) j : J v j (cid:1) ∼ = ∏ ( i , j ) : I × J C ( t i , v j ) by simply summing across the embeddings ofeach component. By the universal property of the biproduct, a morphism is fully specified by its com-ponents. More formally, given a choice of direct sum decomposition on both domain and codomain, wecan represent f as the block matrix f = t f v · · · t f v n ... . . . ... t m f v · · · t m f v n meaning that we can reason about morphisms by simultaneously breaking up both domain and codomaininto cases. As an example of this reasoning, we recover matrix multiplication from the composition oftwo morphisms s ⊕ · · · ⊕ s k t ⊕ · · · ⊕ t m v ⊕ · · · ⊕ v n . f g The s i ( f g ) v j component is defined as the composite ( ι s i f )( g π v j ) , where ι s i f is given by a sum of mor-phisms of type s i → t ⊕ · · · ⊕ t m and g π v j is given by a sum of morphisms of type t ⊕ · · · ⊕ t m → v j : ι s i f = ( s i f t )( ι t ) + · · · + ( s i f t m )( ι t m ) and g π v j = ( π t )( t g v j ) + · · · + ( π t m )( t m g v j ) . Composing these sums, applying distributivity, and then noting that ι t k π t l = δ k , l (the Kronecker delta),we arrive at the familiar formula for the component of a matrix multiplication s i ( f g ) v j = ∑ i , j ( s i f t k )( ι t k π t l )( t l g v j ) = ∑ k ( s i f t k )( t k g v j ) . We can narrativize this expression as answering the question “how can we go from s i to v j ?” with theresponse “through any one of the t k ,” demonstrating that the sum can be interpreted as a disjunction. Thecapacity that biproduct categories possess for declaratively formalizing such reasoning motivates us tosituate wiring diagrams in a certain biproduct category, which we now proceed to define. Let C be an extensive category, i.e., one for which the coproduct functor C / x × C / y → C / ( x + y ) isan equivalence of categories. For example, C could be any elementary topos. Recall the bicategory of C -spans, whose objects are C -objects, morphisms X → Y are spans Y ← S → X in C , and 2-morphismsfrom Y ← S → X to Y ← S (cid:48) → X are C -arrows S → S (cid:48) commuting with the legs of the spans. Let Sp C bethe category whose objects are C -objects and whose morphisms are isomorphism classes of spans in thebicategory of C -spans. The coproduct in C is then the biproduct in Sp C , which we denote as ⊕ . Thismeans that we can compute with spans using the matrix calculus of the previous subsection.6 Wiring diagrams as normal forms for computing in SMCs
It is worthwhile to observe how the matrix calculus manifests itself in this concrete setting. Given aspan Φ : t ⊕ · · · ⊕ t m → v ⊕ · · · ⊕ v n and indices 1 ≤ i ≤ m and 1 ≤ j ≤ n , we can define a componentsub-span t i Φ v j : t i → v j via the following limit: t i Φ v j t i Φ v j t ⊕ · · · ⊕ t m v ⊕ · · · ⊕ v n The multiplication t ⊕ t → t and unit → t morphisms are given by the spans t ⊕ t t ⊕ t t id ∇ id ! and the comultiplication and counit are their transposes.Let τ be a set, which we interpret as a set of types. We now specialize to the slice category C : = Set / τ ,for which an object is a function x → τ , regarded as a τ -typed set ; when we speak of its elements, wemean elements of x . Note that C : = Set / τ is extensive, so the above discussion applies. In the setting of Sp C we will refer to elements of the domain and codomain of a map as ports , to elements of the apex as wires , and to the legs as attachment maps .The sum of two spans t α v f − f + and t β v g − g + is given by the span t α ⊕ β v f − (cid:79) g − f + (cid:79) g + where, given f : t → v and g : u → v , f (cid:79) g : t ⊕ u → v is the composition ι t f + ι u g . We hence interpretthis enrichment in commutative monoids as a disjunction: in α ⊕ β , the t ports attach to the v ports via either α -wires or β -wires. Of course, the zero span is t ← → v , also denoted .In linear algebra, block matrices of linear maps correspond to direct sum decompositions, whileordinary matrices of scalars correspond to maximal decompositions into one-dimensional subspaces.Similarly, in Sp ( Set / τ ) , we have maximal decompositions into singleton sets. What serves as a basis inthis context is simply a choice of ordering on a set, which we represent via enumeration as a tuple. Insuch a decomposition, every entry of the corresponding matrix represents a subspan whose domain andcodomain are both singletons. In this case, the attachment maps are trivial, and hence we can, withoutloss of information, represent such entries via the set of wires that connect the domain and codomainsingletons. Furthermore, for the sake of notational convenience, if a span Φ is given by a diagonal matrix,i.e. consists of a direct sum of singleton spans with apex sets S , . . . , S n , we will write S ⊕ · · · ⊕ S n forthe total span. Example . The ( r , s , t ) → ( x , y , z ) matrix { B }
00 0 0 { C } { A , D } represents the span { r , s , t } { A , B , C , D } { x , y , z } f − f + given by f − ( A ) = t f − ( B ) = r f − ( C ) = t f − ( D ) = t van Patterson, David I. Spivak, and Dmitry Vagner f + ( A ) = z f + ( B ) = y f + ( C ) = x f + ( D ) = z We note that the span matrix is like an adjacency matrix, except that rather than merely indicating thepresence of connection via a truth value, the entries indicate the set of all such connections.We note that the composite of the span { s } ← α → { t } and the span { t } ← β → { v } is the span { s } ← α × β → { v } . We can hence perform matrix multiplication as in the following example. Example . The composite of the span matrices ( j , k ) ( r , s , t ) ( x , y , z ) (cid:20) { N } { L , M }{ O , P } { Q } (cid:21) { F , G } { B }
00 0 { A , D }{ C } { E } is given by the following ( j , k ) → ( x , y , z ) matrix (cid:20) { L , M } × { C } { N } × { A , D } + { L , M } × { E }{ O , P } × { F , G } + { Q } × { C } { O , P } × { B } { Q } × { E } (cid:21) Given our interpretation of apexes as wires, allowing non-bijective attachment maps corresponds toallowing wires to split, merge, terminate, and initiate. For the purposes of defining wiring diagramsfor (strict) symmetric monoidal categories, we will (in Definition 4.1) restrict the legs of our spans tobijections; however, the more general definition can be readily used to define wiring diagrams for SMCsin which objects are supplied with monoids and/or comonoids. In particular, this bijectivity restriction islifted in Listing 1, where ports can attach to multiple other ports on either side.We denote the category of τ -typed finite sets and typed bijections between them as Bij τ and theassociated category of spans by Sp ( Bij τ ) . One might remark that spans of bijections are equivalentsimply to bijections and wonder why Definition 4.1 uses the former. Indeed, since Sp ( Bij τ ) is equivalentto Bij τ , we could have in principle simply defined such wiring diagrams in the latter category. However,our composition formula mirrors the case analysis of Listing 1 by leveraging the matrix calculus of thebiproduct category Sp ( Set / τ ) . We are now equipped to characterize unbiased compositions of morphisms in a strict symmetric monoidalcategory. We make the strictness assumption for a couple of reasons. First, any monoidal category ismonoidally equivalent to a strict one and hence no loss of generality is incurred. Second, the graphicallanguages of string and wiring diagrams prohibits bracketing of parallel wires and hides unit wires,corresponding to strict associativity and unitality.In addition to requiring that our symmetric monoidal category be strict, we define the monoidal prod-uct in an unbiased manner, as in [2]. Thus, we index the hom-sets by a pair of typed finite sets—ratherthan typed finite ordinals—whose monoidal product give the domain and codomain respectively. In thecontext of computer science, this choice corresponds to a dictionary-like representation of domain andcodomain rather than the more traditional list-like representation. We note that this does not imply thatwe enforce commutativity of the monoidal product. For instance, the typed finite sets f , g : { a , a } ⇒ τ given by f ( a ) = t , f ( a ) = t and g ( a ) = t , g ( a ) = t are distinct, though isomorphic.8 Wiring diagrams as normal forms for computing in SMCs
Fix a set τ , whose elements we think of as types. Next we will define the operad W τ of acyclicwiring diagrams . Roughly speaking, morphisms in W τ specify compositions (serial, parallel, etc.) ofmorphisms in an arbitrary strict unbiased SMC C that has been equipped with a function τ → Ob ( C ) ,i.e., for which an object of C has been chosen for each element of τ . Definition 4.1.
Let τ be a set, elements of which we call types . We define the τ -typed operad W τ ofacyclic wiring diagrams as follows.• an object, called a box , t is a τ -typed signed set; i.e. a pair ( t − , t + ) of τ -typed sets, where a τ -typedset is an object of Set / τ ; we call t − the inputs and t + the outputs .• a morphism, called a wiring diagram , Φ of type t , . . . , t n → v is a span in Bij τ v − ⊕ t + ω t − ⊕ v + Φ src Φ tgt where t ± : = (cid:76) ni = t i ± . We call ω -elements wires and enforce the following: (cid:63) progress condition : imposing t i ≺ t j whenever there is a wire A ∈ ω for which both Φ src ( A ) ∈ t i + and Φ tgt ( A ) ∈ t j − , the result must be a partial order on the t i .• the identity morphism inert t : t → t is given by the identity span t − ⊕ t + t − ⊕ t + t − ⊕ t + we note that the block matrix form in the summands is given by the identity matrix.• given wiring diagrams Ψ : s i , . . . , s i mi → t i and Φ : t , . . . , t n → v , we now define their i th partialcomposite (sometimes called “circle- i ” composition) Ψ (cid:35) i Φ : t , . . . , s i , . . . , s i mi , . . . t n → v Letting t ¬ i ± : = (cid:76) j (cid:54) = i t j ± , we make the following abbreviations u − : = v − ⊕ t ¬ i + u + : = t ¬ i − ⊕ v + The composite Ψ (cid:35) i Φ is then given by the block matrix of type u − ⊕ s + → s − ⊕ u + Ψ (cid:35) i Φ = (cid:34) ( u − Φ t i − )( t i − Ψ s − ) u − Φ u + + ( u − Φ t i − )( t i − Ψ t i + )( t i + Φ u + ) s + Ψ s − ( s + Ψ t i + )( t i + Φ u + ) (cid:35) (3)If ≺ Φ and ≺ Ψ are the orderings induced on the t i and s j by Φ and Ψ respectively, then one candefine an ordering ≺ on { t , . . . , s k , . . . , s k mk , . . . t n } as follows s k i ≺ s k j : = s k i ≺ Ψ s k j s k i ≺ t j : = t k ≺ Φ t j t j ≺ s k i : = t j ≺ Φ t k t i ≺ t j : = t i ≺ Φ t j Thus Ψ (cid:35) i Φ satisfies the progress condition.We remark that the composite formula (3) is a declarative version of the imperative substitutionalgorithm presented in Listing 1. In particular, the four cases in the algorithm correspond to the fourentries in the composite matrix: incoming wire, passing wire, fully internal wire, and outgoing wire.Furthermore, the products within each component, e.g. ( u − Φ t i − )( t i − Ψ t i + )( t i + Φ u + ) , correspond to the fusing van Patterson, David I. Spivak, and Dmitry Vagner u − Φ u + , that did not interact with input box t i .Each of the entries in the fully decomposed matrix representation of a bijective span as in Defini-tion 4.1 is either the empty set or a singleton; we will write in the former case and write A for thesingleton set { A } . Similarly, we will write ( A , B ) for { A } × { B } . Example . We now define wiring diagrams corresponding to core SMC operations. We call these W -representations for any set τ ; though we introduce them in an example, they will play an importantrole in the theory.• Symmetry.
Consider wires ω , no inner boxes, and outer box t = ( ω , ω ) . For any permutation σ : ω ∼ −→ ω , the W -representation of symmetry is given by the 0-ary wiring diagram sym σω : () → t represented by the permutation matrix of σ . We let unit ω : = sym id ω . For instance here is the wiringdiagram and matrix form in the case where σ is the transposition of two elements. AB (cid:20) AB (cid:21) • Sequential composition.
Consider wires ω = { A , B , C } , inner boxes t , t (cid:48) , and outer box v . The W representation of sequential composition (shown left) is given by the wiring diagram seq ( A , B , C ) : t , t (cid:48) → v with matrix of type v − ⊕ t + ⊕ t (cid:48) + → t − ⊕ t (cid:48)− ⊕ v + shown right: t t (cid:48) v A B C A B
00 0 C • Parallel composition.
Consider wires ω = { A , A (cid:48) , B , B (cid:48) } , inner boxes t , t (cid:48) , and outer box v . The W representation of parallel composition is given by the wiring diagram para [ A BA (cid:48) B (cid:48) ] : t , t (cid:48) → v withmatrix of type v − ⊕ t + ⊕ t (cid:48) + → t − ⊕ t (cid:48)− ⊕ v + , where both v − and v + are two-dimensional. tt (cid:48) v A BA (cid:48) B (cid:48) A A (cid:48) B
00 0 0 B (cid:48) Example . Non-example.
Consider the following wiring diagram Φ , which has a loop. t t (cid:48) AB This wiring diagram is of type t , t (cid:48) → , with wires { A , B } , given by the span Φ src ( A ) = t + Φ tgt ( A ) = t (cid:48)− Φ src ( B ) = t (cid:48) + Φ tgt ( B ) = t − The ordering ≺ from Definition 4.1 is not a partial order: both t ≺ t (cid:48) and t (cid:48) ≺ t . Therefore this diagramfails to satisfy the progress condition of Definition 4.1.0 Wiring diagrams as normal forms for computing in SMCs
Example . Interchange Law.
To demonstrate composition, we will prove that W -representationssatisfy the interchange law. t t t t vu • u • B B A + A − A + A − C − C + C − C + t t t t vu • u • B B − B + B B − B + A + A − A + A − C − C + C − C + =We wish to show the following diagram commutes: t , t , t , t u • , u • u • , u • v seq ω , seq ω para η , para η para η seq ω where t , t , t , t are inner boxes, u • , u • , u • , u • are intermediary boxes, v is the outer box, and thesubscripts on the morphisms correspond to the following wire tuples: ω = ( A + , B , C − ) ω = ( A + , B , C − ) ω = ( A − ⊕ A − , B ⊕ B , C + ⊕ C + ) η = (cid:20) A + B − A + B − (cid:21) η = (cid:20) B + C − B + C − (cid:21) η = (cid:20) A − C + A − C + (cid:21) The composite spans are represented as matrices of type v − ⊕ t + ⊕ t + ⊕ t + ⊕ t + → t − ⊕ t − ⊕ t − ⊕ t − ⊕ v + where v ± are two-dimensional; we now compute these composites to be as follows: seq ω , seq ω (cid:35) para η = ( A − , A + ) ( A − , A + ) B B ( C − , C + )
00 0 0 0 0 ( C − , C + ) para η , para η (cid:35) seq ω = ( A − , A + ) ( A − , A + ) ( B − , B , B + ) ( B − , B , B + ) ( C − , C + )
00 0 0 0 0 ( C − , C + ) These matrices of sets are isomorphic since they have isomorphic sets (singleton and empty sets) incorresponding entries. van Patterson, David I. Spivak, and Dmitry Vagner With the operad W τ of acyclic wiring diagrams (whose wires are labeled by a chosen set τ ) in hand, wenow formalize how it captures the compositional structure of symmetric monoidal categories. Recall thata W τ -algebra is an operad functor: H : W τ → Set , where Set is the operad corresponding to the symmetric monoidal category ( Set , × , ) . We will nowshow precisely how such algebras correspond to strict SMC’s. Before doing so, we will first characterize W τ in terms of generators. Lemma 5.1.
The operad W τ is generated by sym , seq , para defined in Example 4.2, and these satisfy theusual axioms (parallel and sequential unitality and associativity, interchange, and permutation).Sketch of proof. We proceed by induction on the number of inner boxes. Let Φ : t , . . . , t n → v be amorphism. If n = sym σ . For n ≥
0, the progress condition givesa partial ordering on boxes. Without loss of generality, suppose that t is minimal. Then we can rewritethe diagram in the form t (4)where all the wires shown can represent multiple wires (possibly none). The wiring diagram in (4) can bewritten as a sequential composite of parallel composites. An example axiom relating series and parallelcomposition was proved in Example 4.4.We now show how W -algebras give rise to SMC’s. Theorem 5.2.
Let τ be a set. There is a fully faithful functor from the category of W τ -algebras tothat of strict SMC’s whose objects are τ -typed finite sets and whose morphisms are identity-on-objectssymmetric monoidal functors.Sketch of proof. Let H : W τ → Set be a functor. We need to define an SMC ( C , ⊗ , I ) with objects τ -typed finite sets; in particular, we need to define the hom-set for a given pair of objects ( t − , t + ) ∈ Ob ( C ) .But since objects in W τ are exactly such pairs, we may simply use H : C ( t − , t + ) : = H ( t − , t + ) . (5)We will obtain the identities, composition, and monoidal structure of C , by applying H to various wiringdiagrams.For any object A ∈ Ob ( C ) , consider the box t = ( A , A ) and the 0-ary wiring diagram unit A : () → t given by A A . We obtain a function H ( unit A ) : 1 → H ( A , A ) , and we define the identity on A to bethe image of the unique element.Given objects A , B , C ∈ Ob ( C ) , we need a function Hom ( A , B ) × Hom ( B , C ) → Hom ( A , C ) . We obtainit by applying H to the wiring diagram seq ( A , B , C ) : ( A , B ) , ( B , C ) → ( A , C ) , giving (cid:35) A , B , C : = H ( seq ( A , B , C ) ) : H ( A , B ) × H ( B , C ) → H ( A , C ) . Wiring diagrams as normal forms for computing in SMCs
Similarly, given objects A , A (cid:48) , B , B (cid:48) ∈ Ob ( C ) , parallel composition is defined by ⊗ A , A (cid:48) , B , B (cid:48) : = H (cid:16) para [ A BA (cid:48) B (cid:48) ] (cid:17) : H ( A , B ) × H ( A (cid:48) , B (cid:48) ) → H ( A ⊗ A (cid:48) , B ⊗ B (cid:48) ) . These two compositions are respectively depicted below.
A B C A BA (cid:48) B (cid:48) The interchange law was established in Example 4.4, and the others, unitality and associativity ofserial composition, and unitality, associativity, and symmetry of parallel composition, are similar.It remains to show that for any two W τ -algebras H , H (cid:48) with associated SMCs C , C (cid:48) , we have abijection between the set of natural transformations H → H (cid:48) and that of identity-on-objects symmet-ric monoidal functors C → C (cid:48) . A natural transformation α : H → H (cid:48) defines a function H ( t − , t + ) → H (cid:48) ( t − , t + ) , which by (5) defines the required functor on hom-sets. This functor respects identity, com-position, and the symmetric monoidal structure by the naturality of α with respect to the morphisms in W τ that correspond to these structures.The SMC resulting from the procedure in Theorem 5.2 is free-on-objects. In the next theorem we willprove that the functor from Theorem 5.2 is 2-essentially surjective: every symmetric monoidal categoryis equivalent to one coming from an W -algebra. Theorem 5.3.
To every SMC ( C , ⊗ , I ) there is an associated W Ob ( C ) -algebra H C . If we then apply theconstruction of Theorem 5.2 to H C to obtain an SMC C (cid:48) , there is an induced equivalence of symmetricmonoidal categories C (cid:48) → C .Sketch of proof. Let ( C , ⊗ , I ) be a symmetric monoidal category. We define a functor H C : W Ob ( C ) → Set as follows. Suppose given a box, i.e. an object ( t − , t + ) in W Ob ( C ) , where t − : S − → Ob ( C ) and t + : S + → Ob ( C ) are the typed finite sets. To it we assign the hom-set H C ( t − , t + ) : = C (cid:32) (cid:79) s ∈ S − t − ( s ) , (cid:79) s ∈ S + t + ( s ) (cid:33) . (6)By Lemma 5.1, W Ob ( C ) is generated by the morphisms para , seq , and sym corresponding to parallel com-position, series composition, and permutation, and that these morphisms satisfy well-known relations.Thus to give the action of H C on morphisms, it suffices to say how it acts on these generators, and showthat relations hold. For the morphism seq ( A , B , C ) in W Ob ( C ) representing series composition t t (cid:48) v A B C we need to give a function H C ( A , B ) × H C ( B , C ) → H C ( A , C ) . By definition this is just a function C ( A , B ) × C ( B , C ) → C ( A , C ) , and of course we use the composition function (cid:35) A , B , C from C as a cate-gory. The case for parallel composition (resp. symmetry) is similar: one uses the monoidal product (resp.symmetry) from C . These satisfy the required diagrammatic relations because by definition C satisfiesthe laws of monoidal categories. van Patterson, David I. Spivak, and Dmitry Vagner C , constructing H C as above, and then applying theconstruction of Theorem 5.2 to obtain a new SMC C (cid:48) . The objects of C (cid:48) are the Ob ( C ) -typed finite sets,and there is a surjection Ob ( C (cid:48) ) → Ob ( C ) given by sending t : S → Ob ( C ) to (cid:78) s ∈ S t ( s ) . At this point,(5) and (6) provide a bijection C (cid:48) ( t − , t + ) → C ( (cid:78) s ∈ S − t − ( s ) , (cid:78) s ∈ S + t + ( s )) , which one can check is partof a surjective-on-objects fully faithful symmetric monoidal functor. Acknowledgments
David Spivak acknowledges support from AFOSR grants FA9550-19-1-0113 and FA9550-17-1-0058.The authors also appreciate the helpful reviews of the ACT2020 program committee, which improvedthe quality of this paper.
References [1] Seth Bromberger, James Fairbanks, and other contributors.
LightGraphs.jl: an optimized graphspackage for the Julia programming language . 2017.
DOI :
10 . 5281 / zenodo . 889971 (cit. onp. 52).[2] Pierre Deligne and James S. Milne. “Tannakian categories”. In:
Hodge cycles, motives, and Shimuravarieties . 1982, pp. 101–228.
URL : (cit. on p. 57).[3] Brendan Fong and David I. Spivak. “Hypergraph categories”. In: Journal of Pure and AppliedAlgebra
DOI : (cit. on p. 50).[4] Brendan Fong and David I. Spivak. “Supplying bells and whistles in symmetric monoidal cate-gories”. In: (2019). arXiv: (cit. on p. 54).[5] Andr´e Joyal and Ross Street. “The geometry of tensor calculus, I”. In: Advances in mathematics
International Conference on Automated Deduction . 2015, pp. 326–336.
DOI :
10 .1007/978-3-319-21401-6_22 (cit. on p. 51).[7] Evan Patterson and other contributors.
Catlab.jl: a framework for applied and computational cat-egory theory . 2020.
DOI : (cit. on p. 51).[8] Roger Penrose. “Applications of negative dimensional tensors”. In: Combinatorial mathematicsand its applications (cit. on p. 50).[10] Peter Selinger. “A survey of graphical languages for monoidal categories”. In:
New structures forphysics . 2010, pp. 289–355.
DOI : . arXiv: (cit. onp. 50).[11] Pawel Sobocinski, Paul W Wilson, and Fabio Zanasi. “CARTOGRAPHER: A Tool for StringDiagrammatic Reasoning (Tool Paper)”. In: . Schloss Dagstuhl-Leibniz-Zentrum fuer Informatik. 2019. DOI : (cit. on p. 51).4 Wiring diagrams as normal forms for computing in SMCs [12] David I. Spivak, Patrick Schultz, and Dylan Rupel. “String diagrams for traced and compactcategories are oriented 1-cobordisms”. In:
Journal of Pure and Applied Algebra
DOI : . arXiv:1508.01069