# Diagram#

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

Bases: `NamedGeneric['dtype']`, `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 (T) –

• cod (T) –

ty_factory#

alias of `Dim`

eval(contractor=None, dtype=None)[source]#

Evaluate a tensor diagram as a `Tensor`.

Parameters:
• contractor (Callable | None) – Use `tensornetwork` or `Functor` by default.

• dtype (type | None) – Used for spiders.

Return type:

Tensor

Examples

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

Convert a tensor diagram to `tensornetwork`.

Parameters:

dtype (type | None) – 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]]
```

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*x, 0, 0, z, 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`

spider_factory#

alias of `Spider`

sum_factory#

alias of `Sum`