The Python toolkit for computing with string diagrams.
Project description
DisCoPy
DisCoPy is a Python toolkit for computing with string diagrams.
- Organisation: https://discopy.org
- Documentation: https://docs.discopy.org
- Source code: https://github.com/discopy/discopy
- Paper (for applied category theorists): https://doi.org/10.4204/EPTCS.333.13
- Paper (for quantum computer scientists): https://arxiv.org/abs/2205.05190
DisCoPy began as an implementation of DisCoCat and QNLP. This has now become its own library: lambeq.
Features
- an
Arrow
data structure for free dagger categories with formal sums, unary operators and symbolic variables from SymPy - a
Diagram
data structure for planar string diagrams in any (pre)monoidal category in the hierarchy of graphical languages (with braids, twists, spiders, etc.) - a
Hypergraph
data structure for string diagrams in hypergraph categories and its restrictions to symmetric, traced, compact and Markov categories - methods for diagram composition, drawing, rewriting and
Functor
evaluation into:- Python code, i.e. wires as types and boxes as functions
- tensor networks, i.e. wires as dimensions and boxes as arrays from NumPy, PyTorch, TensorFlow, TensorNetwork and JAX
- an implementation of categorical quantum mechanics interfacing with:
- tket for circuit compilation
- PyZX for optimisation with the ZX calculus
- PennyLane for automatic differentiation
- an implementation of formal grammars (context-free, categorial, pregroup or dependency) with interfaces to lambeq, spaCy and NLTK
Example: Cooking
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()
Quickstart
pip install discopy
If you want to see DisCoPy in action, check out the QNLP tutorial!
Contribute
We're keen to welcome new contributors!
First, read the contributing guidelines. Then get in touch on Discord or open an issue.
How to cite
If you used DisCoPy in the context of an academic publication, we suggest you cite:
- G. de Felice, A. Toumi & B. Coecke, DisCoPy: Monoidal Categories in Python, EPTCS 333, 2021, pp. 183-197, DOI: 10.4204/EPTCS.333.13
If furthermore your work is related to quantum computing, you can also cite:
- A. Toumi, G. de Felice & R. Yeung, DisCoPy for the quantum computer scientist, arXiv:2205.05190
If you use any of the recent features (e.g. Hypergraph
) you should also mention:
- A. Toumi, R. Yeung, B. Poór & G. de Felice, DisCoPy: the Hierarchy of Graphical Languages in Python arXiv:2311.10608
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file discopy-1.1.7.tar.gz
.
File metadata
- Download URL: discopy-1.1.7.tar.gz
- Upload date:
- Size: 3.7 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fe85080704a06994c2c25fea8da8da237d0b966fafedf01b264a1a859cfd1bc2 |
|
MD5 | 69ddb1472496b111d761dc434e508cf1 |
|
BLAKE2b-256 | 632a2f1ac02e8d389ffebbcf83b757084d39c866434b56190b51e337e633ffb3 |
File details
Details for the file discopy-1.1.7-py3-none-any.whl
.
File metadata
- Download URL: discopy-1.1.7-py3-none-any.whl
- Upload date:
- Size: 149.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1fe0027f6d48e1894cdc2fbc4beb2e0601c136cccc8561a30561f3b2c69b58c9 |
|
MD5 | 564e54235677837dfe530414cedcbe88 |
|
BLAKE2b-256 | 549801f757e159ee17d8606081ab6113f32489bdcd541892502b4d8eeb24d15d |