Functor#
- class discopy.tensor.Functor(ob_map, ar_map, dom=None, dtype=<class 'float'>, optimize='greedy', **params)[source]#
Bases:
discopy.frobenius.FunctorA tensor functor is a frobenius functor with a domain category
domandTensor[dtype]as codomain for a givendtype.Calling it on a diagram converts it to a
CMapand contracts the network in a singleeinsumcall under the activebackend(), passing any optional einsum parameters through.- Parameters:
ar_map (dict[cat.Box, list]) – The arrow mapping.
dom (type) – The domain of the functor, i.e. the class of diagrams it evaluates, the class attribute
domby default.dtype (type) – The datatype for the codomain
Tensor[dtype].optimize – The contraction path, passed verbatim to the backend
einsum, e.g."greedy","optimal"or an explicit path.params – Any other optional parameter of the backend
einsummethod, passed verbatim.
Example
>>> n, s = map(rigid.Ty, "ns") >>> Alice = rigid.Box('Alice', rigid.Ty(), n) >>> loves = rigid.Box('loves', rigid.Ty(), n.r @ s @ n.l) >>> Bob = rigid.Box('Bob', rigid.Ty(), n) >>> diagram = Alice @ loves @ Bob\ ... >> rigid.Cup(n, n.r) @ s @ rigid.Cup(n.l, n)
>>> F = Functor( ... ob_map={s: 1, n: 2}, ... ar_map={Alice: [0, 1], loves: [0, 1, 1, 0], Bob: [1, 0]}, ... dom=rigid.Diagram, dtype=bool) >>> F(diagram) Tensor[bool]([True], dom=Dim(1), cod=Dim(1))
>>> rewrite = diagram\ ... .transpose_box(2).transpose_box(0, left=True).normal_form() >>> Equation(diagram, rewrite).draw( ... figsize=(8, 3), path='docs/_static/tensor/rewrite.svg')
>>> assert F(diagram) == F(rewrite)
- contract(other)[source]#
Contract the image of a combinatorial map in a single
einsumcall under the activebackend().The map is Einstein notation: the 2-cycles of its
edgesinvolution are the summed indices, boxes are the tensors and the boundary ports are the free indices, with integer labels. A wire is one index of the size of its object’s image. Networks with more thanconfig.MAX_EINSUM_INDICESindices are contracted with the optionalopt_einsumpackage instead.