Diagram#

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

Bases: Diagram

ZX Diagram.

draw(**params)[source]#

ZX diagrams don’t have labels on wires.

grad(var, **params)[source]#

Gradient with respect to var.

Parameters:

var (sympy.Symbol) – Differentiated variable.

Returns:

diagrams

Return type:

discopy.monoidal.Sum

Examples

>>> from sympy.abc import phi
>>> assert Z(1, 1, phi).grad(phi) == scalar(pi) @ Z(1, 1, phi + .5)
to_pyzx()[source]#

Returns a pyzx.Graph.

>>> bialgebra = Z(1, 2, .25) @ Z(1, 2, .75)\
...     >> Id(1) @ SWAP @ Id(1) >> X(2, 1, .5) @ X(2, 1, .5)
>>> graph = bialgebra.to_pyzx()
>>> assert len(graph.vertices()) == 8
>>> assert (graph.inputs(), graph.outputs()) == ((0, 1), (6, 7))
>>> from pyzx import VertexType
>>> assert graph.type(2) == graph.type(3) == VertexType.Z
>>> assert graph.phase(2) == 2 * .25 and graph.phase(3) == 2 * .75
>>> assert graph.type(4) == graph.type(5) == VertexType.X
>>> assert graph.phase(4) == graph.phase(5) == 2 * .5
>>> assert graph.graph == {
...     0: {2: 1},
...     1: {3: 1},
...     2: {0: 1, 4: 1, 5: 1},
...     3: {1: 1, 4: 1, 5: 1},
...     4: {2: 1, 3: 1, 6: 1},
...     5: {2: 1, 3: 1, 7: 1},
...     6: {4: 1},
...     7: {5: 1}}
static from_pyzx(graph)[source]#

Takes a pyzx.Graph returns a zx.Diagram.

Examples

>>> bialgebra = Z(1, 2, .25) @ Z(1, 2, .75)\
...     >> Id(1) @ SWAP @ Id(1) >> X(2, 1, .5) @ X(2, 1, .5)
>>> graph = bialgebra.to_pyzx()
>>> assert Diagram.from_pyzx(graph) == bialgebra

Note

Raises ValueError if either: * a boundary node is not in graph.inputs() + graph.outputs(), * or set(graph.inputs()).intersection(graph.outputs())

is non-empty.

id#

alias of Id