oxford-quantum-group / Discopy

Licence: bsd-3-clause
a toolbox for computing with monoidal categories

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Discopy

Iqx User Guide
The users guide for the IBM Q experience
Stars: ✭ 115 (-22.3%)
Mutual labels:  quantum-computing
Naive functional programming
A naive approach to functional programming using TypeScript
Stars: ✭ 129 (-12.84%)
Mutual labels:  category-theory
Solutionqcqinielsenchuang
Solution for Quantum Computation and Quantum Information by Nielsen and Chuang
Stars: ✭ 141 (-4.73%)
Mutual labels:  quantum-computing
Qiskit Tutorials
A collection of Jupyter notebooks showing how to use the Qiskit SDK
Stars: ✭ 1,777 (+1100.68%)
Mutual labels:  quantum-computing
Quiver
A modern commutative diagram editor for the web.
Stars: ✭ 1,799 (+1115.54%)
Mutual labels:  category-theory
Cql
CQL: Categorical Query Language implementation in Haskell
Stars: ✭ 132 (-10.81%)
Mutual labels:  category-theory
Foundational Knowledge For Programmers
List of resources about foundational knowledge for programmers (supposed to last a few decades)
Stars: ✭ 115 (-22.3%)
Mutual labels:  category-theory
Lawvere
A categorical programming language with effects
Stars: ✭ 142 (-4.05%)
Mutual labels:  category-theory
Quantum Nc
Microsoft Quantum Computing Libraries for noncommercial use
Stars: ✭ 126 (-14.86%)
Mutual labels:  quantum-computing
Monthly Challenges
Repository containing monthly challenges about quantum computing.
Stars: ✭ 126 (-14.86%)
Mutual labels:  quantum-computing
Blazor.diagrams
A fully customizable and extensible all-purpose diagrams library for Blazor
Stars: ✭ 119 (-19.59%)
Mutual labels:  diagrams
Domains
A computational algebra system in Smalltalk.
Stars: ✭ 124 (-16.22%)
Mutual labels:  category-theory
Scaffcc
Compilation, analysis and optimization framework for the Scaffold quantum programming language.
Stars: ✭ 133 (-10.14%)
Mutual labels:  quantum-computing
Teach Me Quantum
⚛ 10 week Practical Course on Quantum Information Science and Quantum Computing - with Qiskit and IBMQX
Stars: ✭ 118 (-20.27%)
Mutual labels:  quantum-computing
Categories
Categories parametrized by morphism equality, in Agda
Stars: ✭ 141 (-4.73%)
Mutual labels:  category-theory
Quantum Algorithms Tutorials
Tutorials for Quantum Algorithms with Qiskit implementations.
Stars: ✭ 115 (-22.3%)
Mutual labels:  quantum-computing
Qpanda 2
QPanda 2 is an open source quantum computing framework developed by OriginQC that can be used to build, run, and optimize quantum algorithms.
Stars: ✭ 128 (-13.51%)
Mutual labels:  quantum-computing
Learnquantum
Repo of resources to help learn about quantum computing.
Stars: ✭ 143 (-3.38%)
Mutual labels:  quantum-computing
Awesome Quantum Machine Learning
Here you can get all the Quantum Machine learning Basics, Algorithms ,Study Materials ,Projects and the descriptions of the projects around the web
Stars: ✭ 1,940 (+1210.81%)
Mutual labels:  quantum-computing
Ssss
"Deep Learning and Quantum Programming" Spring School @ Song Shan Lake
Stars: ✭ 135 (-8.78%)
Mutual labels:  quantum-computing

snake equation

Distributional Compositional Python

DisCoPy is a tool box for computing with monoidal categories.

Features

Diagrams & Recipes

Diagrams are the core data structure of DisCoPy, they are generated by the following grammar:

diagram ::= Box(name, dom=type, cod=type)
    | diagram @ diagram
    | diagram >> diagram
    | Id(type)

type ::= Ty(name) | type.l | type.r | type @ type | Ty()

String diagrams (also known as tensor networks or Penrose notation) are a graphical calculus for computing with monoidal categories. For example, if we take ingredients as types and cooking steps as boxes then a diagram is a recipe:

from discopy import Ty, Box, Id, Swap

egg, white, yolk = Ty('egg'), Ty('white'), Ty('yolk')
crack = Box('crack', egg, white @ yolk)
merge = lambda x: Box('merge', x @ x, x)

crack_two_eggs = crack @ crack\
    >> Id(white) @ Swap(yolk, white) @ Id(yolk)\
    >> merge(white) @ merge(yolk)
crack_two_eggs.draw(path='docs/_static/imgs/crack-eggs.png')

crack two eggs

Snakes & Sentences

Wires can be bended using two special kinds of boxes: cups and caps, which satisfy the snake equations, also called triangle identities.

from discopy import Cup, Cap

x = Ty('x')
left_snake = Id(x) @ Cap(x.r, x) >> Cup(x, x.r) @ Id(x)
right_snake =  Cap(x, x.l) @ Id(x) >> Id(x) @ Cup(x.l, x)
assert left_snake.normal_form() == Id(x) == right_snake.normal_form()

snake equations, with types

In particular, DisCoPy can draw the grammatical structure of natural language sentences encoded as reductions in a pregroup grammar (see Lambek, From Word To Sentence (2008) for an introduction).

from discopy import pregroup, Word

s, n = Ty('s'), Ty('n')
Alice, Bob = Word('Alice', n), Word('Bob', n)
loves = Word('loves', n.r @ s @ n.l)

sentence = Alice @ loves @ Bob >> Cup(n, n.r) @ Id(s) @ Cup(n.l, n)
pregroup.draw(sentence, path='docs/_static/imgs/alice-loves-bob.png')

Alice loves Bob

Functors & Rewrites

Monoidal functors compute the meaning of a diagram, given an interpretation for each wire and for each box. In particular, tensor functors evaluate a diagram as a tensor network using numpy. Applied to pregroup diagrams, DisCoPy implements the distributional compositional (DisCo) models of Clark, Coecke, Sadrzadeh (2008).

from discopy import TensorFunctor

F = TensorFunctor(
    ob={s: 1, n: 2},
    ar={Alice: [1, 0], loves: [[0, 1], [1, 0]], Bob: [0, 1]})

assert F(sentence) == 1

Free functors (i.e. from diagrams to diagrams) can fill each box with a complex diagram. The result can then be simplified using diagram.normalize() to remove the snakes.

from discopy import Functor

def wiring(word):
    if word.cod == n:  # word is a noun
        return word
    if word.cod == n.r @ s @ n.l:  # word is a transitive verb
        return Cap(n.r, n) @ Cap(n, n.l)\
            >> Id(n.r) @ Box(word.name, n @ n, s) @ Id(n.l)

W = Functor(ob={s: s, n: n}, ar=wiring)


rewrite_steps = W(sentence).normalize()
sentence.to_gif(*rewrite_steps, path='autonomisation.gif', timestep=1000)

autonomisation

Getting Started

pip install discopy

Contributing

Contributions are welcome, please drop one of us an email or open an issue.

Tests

If you want the bleeding edge, you can install DisCoPy locally:

git clone https://github.com/oxford-quantum-group/discopy.git
cd discopy
pip install .

You should check you haven't broken anything by running the test suite:

pip install ".[test]" .
pip install pytest coverage pycodestyle
coverage run -m pytest --doctest-modules --pycodestyle
coverage report -m discopy/*.py discopy/*/*.py

The documentation is built automatically from the source code using sphinx. If you need to build it locally, just run:

(cd docs && (make clean; make html))

Documentation

The tool paper is now available on arXiv:2005.02975, it was presented at ACT2020.

The documentation is hosted at readthedocs.io, you can also checkout the notebooks for a demo!

readthedocs Build Status codecov PyPI version arXiv:2005.02975

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].