factory

Contents

factory#

discopy.cat.factory(cls)[source]#

Allows the identity and composition of an Arrow subclass to remain within the subclass.

Parameters:

cls (Type[Composable]) – Some subclass of Arrow.

Return type:

Type[Composable]

Note

The factory method pattern (FMP) is used all over DisCoPy.

Example

Let’s create Circuit as a subclass of Arrow with an Ob subclass Qubit as domain and codomain.

>>> from discopy.cat import Ob, Arrow, Box
>>> class Qubit(Ob):
...     pass
>>> @factory
... class Circuit(Arrow):
...     ty_factory = Qubit

The Circuit subclass itself has a subclass Gate as boxes.

>>> class Gate(Box, Circuit):
...     pass

The identity and composition of Circuit is again a Circuit.

>>> X = Gate('X', Qubit(), Qubit())
>>> assert isinstance(X >> X, Circuit)
>>> assert isinstance(Circuit.id(), Circuit)
>>> assert isinstance(Circuit.id().dom, Qubit)