discopy
v1.1.7
DisCoPy is a Python toolkit for computing with string diagrams.
DisCoPy began as an implementation of DisCoCat and QNLP. This has now become its own library: lambeq.
Arrow data structure for free dagger categories with formal sums, unary operators and symbolic variables from SymPyDiagram data structure for planar string diagrams in any (pre)monoidal category in the hierarchy of graphical languages (with braids, twists, spiders, etc.)Hypergraph data structure for string diagrams in hypergraph categories and its restrictions to symmetric, traced, compact and Markov categoriesFunctor evaluation into:
This example is inspired from Pawel Sobocinski's blog post Crema di Mascarpone and Diagrammatic Reasoning.
from discopy.symmetric import Ty as Ingredient, Box as Step, Diagram as Recipe
egg, white, yolk = Ingredient("egg"), Ingredient("white"), Ingredient("yolk")
crack = Step("crack", egg, white @ yolk)
merge = lambda x: Step("merge", x @ x, x)
# DisCoPy allows string diagrams to be defined as Python functions
@Recipe.from_callable(egg @ egg, white @ yolk)
def crack_two_eggs(left_egg, right_egg):
left_white, left_yolk = crack(left_egg)
right_white, right_yolk = crack(right_egg)
return (merge(white)(left_white, right_white),
merge(yolk)(left_yolk, right_yolk))
# ... or in point-free style using parallel (@) and sequential (>>) composition
assert crack_two_eggs == crack @ crack
>> white @ Recipe.swap(yolk, white) @ yolk
>> merge(white) @ merge(yolk)
crack_two_eggs.draw()
pip install discopyIf you want to see DisCoPy in action, check out the QNLP tutorial!
We're keen to welcome new contributors!
First, read the contributing guidelines. Then get in touch on Discord or open an issue.
If you used DisCoPy in the context of an academic publication, we suggest you cite:
If furthermore your work is related to quantum computing, you can also cite:
If you use any of the recent features (e.g. Hypergraph) you should also mention: