Diagram#

class discopy.tensor.Diagram(dom, cod, boxes, offsets, layers=None)[source]#

Bases: Diagram

Diagram with Tensor boxes.

Examples

>>> vector = Box('vector', Dim(1), Dim(2), [0, 1])
>>> diagram = vector[::-1] >> vector @ vector
>>> print(diagram)
vector[::-1] >> vector >> Id(Dim(2)) @ vector
eval(contractor=None, dtype=None)[source]#

Diagram evaluation.

Parameters:
  • contractor (callable, optional) – Use tensornetwork contraction instead of tensor.Functor.

  • dtype (type of np.Number, optional) – dtype to be used for spiders.

Returns:

tensor – With the same domain and codomain as self.

Return type:

Tensor

Examples

>>> vector = Box('vector', Dim(1), Dim(2), [0, 1])
>>> assert (vector >> vector[::-1]).eval() == 1
>>> import tensornetwork as tn
>>> assert (vector >> vector[::-1]).eval(tn.contractors.auto) == 1
to_tn(dtype=None)[source]#

Sends a diagram to tensornetwork.

Parameters:

dtype (type of np.Number, optional) – dtype to be used for spiders.

Returns:

  • nodes (tensornetwork.Node) – Nodes of the network.

  • output_edge_order (list of tensornetwork.Edge) – Output edges of the network.

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.abc import x, y, z
>>> vector = Box("v", Dim(1), Dim(2), [x ** 2, y * z])
>>> vector.jacobian([x, y, z]).eval()
Tensor(dom=Dim(1), cod=Dim(3, 2), array=[2.0*x, 0, 0, 1.0*z, 0, 1.0*y])
static spiders(n_legs_in, n_legs_out, dim)[source]#

Spider diagram.

Parameters:
  • n_legs_in (int) – Number of legs in and out.

  • n_legs_out (int) – Number of legs in and out.

  • dim (Dim) – Dimension for each leg.

Examples

>>> assert Diagram.spiders(3, 2, Dim(1)) == Id(Dim(1))
>>> assert Diagram.spiders(1, 2, Dim(2)) == Spider(1, 2, Dim(2))
>>> Diagram.spiders(1, 2, Dim(2, 3))  
Traceback (most recent call last):
...
NotImplementedError
bubble_factory#

alias of Bubble

id#

alias of Id

sum#

alias of Sum