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 ofArrow
with anOb
subclassQubit
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 subclassGate
as boxes.>>> class Gate(Box, Circuit): ... pass
The identity and composition of
Circuit
is again aCircuit
.>>> X = Gate('X', Qubit(), Qubit()) >>> assert isinstance(X >> X, Circuit) >>> assert isinstance(Circuit.id(), Circuit) >>> assert isinstance(Circuit.id().dom, Qubit)