# Drawing wiring diagrams in Compose.jl¶

Catlab can draw wiring diagrams using the Julia package Compose.jl.

For best results, it is recommended to load the packages Convex.j and SCS.jl. When available they are used to optimize the layout of the outer ports.

In [1]:
using Catlab.WiringDiagrams, Catlab.Graphics

import Convex, SCS


## Examples¶

### Symmetric monoidal category¶

In [2]:
using Catlab.Theories

A, B, C, D = Ob(FreeSymmetricMonoidalCategory, :A, :B, :C, :D)
f, g = Hom(:f, A, B), Hom(:g, B, A);


To start, here are a few very simple examples.

In [3]:
to_composejl(f)

Out[3]:
In [4]:
to_composejl(f⋅g)

Out[4]:
In [5]:
to_composejl(f⊗g)

Out[5]:

Here is a more complex example, involving generators with compound domains and codomains.

In [6]:
h, k = Hom(:h, C, D),  Hom(:k, D, C)
m, n = Hom(:m, B⊗A, A⊗B), Hom(:n, D⊗C, C⊗D)
q = Hom(:l, A⊗B⊗C⊗D, D⊗C⊗B⊗A)

to_composejl((f⊗g⊗h⊗k)⋅(m⊗n)⋅q⋅(n⊗m)⋅(h⊗k⊗f⊗g))

Out[6]:

Identities and braidings appear as wires.

In [7]:
to_composejl(id(A))

Out[7]:
In [8]:
to_composejl(braid(A,B))

Out[8]:
In [9]:
to_composejl(braid(A,B) ⋅ (g⊗f) ⋅ braid(A,B))

Out[9]:

The isomorphism $A \otimes B \otimes C \to C \otimes B \otimes A$ induced by the permutation $(3\ 2\ 1)$ is a composite of braidings and identities.

In [10]:
σ = (braid(A,B) ⊗ id(C)) ⋅ (id(B) ⊗ braid(A,C) ⋅ (braid(B,C) ⊗ id(A)))

to_composejl(σ)

Out[10]:

By default, anchor points are added along identity and braiding wires to reproduce the expression structure in the layout. The anchors can be disabled to get a more "unbiased" layout.

In [11]:
to_composejl(σ, anchor_wires=false)

Out[11]:

### Biproduct category¶

In [12]:
A, B, C = Ob(FreeBiproductCategory, :A, :B, :C)
f = Hom(:f, A, B)

to_composejl(mcopy(A))

Out[12]:
In [13]:
to_composejl(delete(A))

Out[13]:
In [14]:
to_composejl(mcopy(A)⋅(f⊗f)⋅mmerge(B))

Out[14]:
In [15]:
to_composejl(mcopy(A⊗B), orientation=TopToBottom)

Out[15]:
In [16]:
to_composejl(mcopy(A⊗B⊗C), orientation=TopToBottom)

Out[16]: