Diagram#

class discopy.tensor.Diagram(inside, dom, cod, _scan=True)[source]#

Bases: discopy.abc.Result, discopy.frobenius.Diagram

A tensor diagram is a frobenius diagram with tensor boxes.

Example

>>> vector = Box('vector', Dim(1), Dim(2), [0, 1])
>>> diagram = vector[::-1] >> vector @ vector
>>> print(diagram)
vector[::-1] >> vector >> Dim(2) @ vector
Parameters:
  • inside (tuple[Layer, ...]) –

  • dom (C0) –

  • cod (C0) –

ob#

alias of Dim

eval(dtype=None, optimize='greedy', **params)[source]#

Evaluate a tensor network as a Tensor: call the Functor that sends each box to its array.

Parameters:
  • dtype (type) – The datatype for spiders and the result, inferred from the boxes by default.

  • optimize – The contraction path, passed verbatim to the backend einsum.

  • params – Any other optional parameter of the backend einsum method, passed verbatim.

Return type:

Tensor

Examples

>>> vector = Box('vector', Dim(1), Dim(2), [0, 1])
>>> assert (vector >> vector[::-1]).eval().array == 1
>>> assert (vector >> vector[::-1]).eval(
...     optimize="optimal").array == 1
to_quimb(dtype=None)[source]#

Convert a tensor diagram to a quimb tensor.

Parameters:

dtype (type) – Used for spiders.

Return type:

quimb.tensor.Tensor

Examples

>>> vector = Box('vector', Dim(1), Dim(2), [0, 1])
>>> t_net = (vector >> vector[::-1]).to_quimb()  
>>> assert t_net.contract(preserve_tensor=True).data == 1
to_tn(dtype=None)[source]#

Convert a tensor diagram to tensornetwork.

Parameters:

dtype (type) – Used for spiders.

Return type:

tuple[list[‘tensornetwork.Node’], list[‘tensornetwork.Edge’]]

Examples

>>> import numpy as np
>>> from tensornetwork import Node, Edge  
>>> vector = Box('vector', Dim(1), Dim(2), [0, 1])
>>> nodes, output_edge_order = vector.to_tn()
>>> node, = nodes
>>> assert node.name == "vector" and np.all(node.tensor == [0, 1])
>>> assert output_edge_order == [node[0]]
grad(var, **params)[source]#

Gradient with respect to var.

jacobian(variables, **params)[source]#

Diagrammatic jacobian with respect to variables.

Parameters:

variables (List[sympy.Symbol]) – Differentiated variables.

Returns:

tensor – with tensor.dom == self.dom and tensor.cod == Dim(len(variables)) @ self.cod.

Return type:

Tensor

Examples

>>> from sympy import Expr  
>>> from sympy.abc import x, y, z
>>> vector = Box("v", Dim(1), Dim(2), [x ** 2, y * z])
>>> vector.jacobian([x, y, z]).eval(dtype=Expr)
Tensor[Expr]([2.0*x, 0, 0, 1.0*z, 0, 1.0*y], dom=Dim(1), cod=Dim(3, 2))
braid_factory#

alias of Swap

bubble_factory#

alias of Bubble

cap_factory#

alias of Cap

cup_factory#

alias of Cup

factory#

alias of Diagram

map_factory#

alias of CMap

spider_factory#

alias of Spider

sum_factory#

alias of Sum