Backend#

class discopy.drawing.backend.Backend(linewidth=1)[source]#

Bases: abc.ABC

Abstract drawing backend.

draw_text(text, i, j, **params)[source]#

Draws a piece of text at a given position.

draw_node(i, j, **params)[source]#

Draws a node for a given position, color and shape.

draw_polygon(*points, facecolor=None, edgecolor=None)[source]#

Draws a polygon given a list of points.

draw_curved_polygon(*points, facecolor=None, edgecolor=None, bend_out=False)[source]#

Draws a polygon whose first edge is a quadratic Bezier curve rather than a straight line, e.g. for the coloured regions of draw_regions().

The first two points are the start and end of the curved edge, the remaining points are connected by straight lines back to the start, as in draw_polygon(). The Bezier control point is the corner of the start and end points, i.e. (points[1].x, points[0].y), or (points[0].x, points[1].y) when bend_out is set, so that the curve hugs a wire bending out of or into a box.

static readable_foreground(colour, threshold=0.5)[source]#

Pick a readable foreground text colour, i.e. "white" or "black", for text drawn on top of a given background colour, using the standard luma formula for perceived brightness.

>>> Backend.readable_foreground("white")
'black'
>>> Backend.readable_foreground("black")
'white'
draw_wire(source, target, bend_out=False, bend_in=False, style=None, linewidth=None)[source]#

Draws a wire from source to target, possibly with a Bezier.

draw_bezier(points)[source]#

Draws a cubic Bezier curve from a list of four control points.

draw_braid_strand(source, target, middle, gap=0)[source]#

Draws a single strand of a braid crossing the horizontal line at height middle. The strand is vertical at both ends and diagonal in between, so that two strands cross at a right angle rather than meeting flat. If gap is non-zero the strand is broken around the crossing, i.e. it goes under the other strand.

draw_spiders(graph, draw_box_labels=True, **params)[source]#

Draws a list of boxes depicted as spiders.

abstract output(path=None, show=True, **params)[source]#

Output the drawing.

abstract draw_regions(graph, **params)[source]#

Draw coloured 0-cell regions, e.g. the background of a Drawing.frame() or the wire-typed regions of a diagram.

This has no default drawing logic: backends that do not want to support coloured regions can simply inherit this no-op.

draw_legend(graph, **params)[source]#

Draw a legend of region colours when supported by the backend.

static region_colours(graph)[source]#

The distinct non-white region colours of a diagram, keyed by colour.

Returns an order-preserving mapping from each colour’s name to its monoidal.Colour, suitable for a drawing legend. White is omitted as it is the neutral background.

static visible_edges(graph)[source]#

Yield the edges of a graph that are not inside a box.

static has_boundary_sides(typ)[source]#

Whether the first or last object of a type is a frame side, i.e. it carries the frame_boundary flag set by Drawing.bubble() with draw_as_square.

static is_frame_boundary(node)[source]#

Whether a node belongs to the sides of a frame, i.e. the box drawn around the terms of an Equation with coloured boundaries.

A box carrying the frame_boundary flag is a bubble opening or closing squashed to half height, whose horizontal boundary is drawn as a wire through the box node. That boundary is hidden only for a square frame, i.e. when the left and right side types carry the flag too; a plain bubble keeps its top and bottom boundaries visible.

draw_dual_rail_cup(positions, node, **params)[source]#

Draws a discopy.ribbon.DualRailCup (or cap) as a single constant-width fold, i.e. two concentric half circles joining the outer and inner rails of two ribbons.

draw_braid(positions, node)[source]#

Draws a braid or a swap as its two wires crossing diagonally, so that they meet at a right angle. A braid (over/under) breaks the wire that goes under; a symmetric swap simply crosses both wires.

draw_box(positions, node, **params)[source]#

Draws a box node on a given backend.

draw_discard(positions, node, **params)[source]#

Draws a discopy.quantum.circuit.Discard box.

draw_measure(positions, node, **params)[source]#

Draws a discopy.quantum.circuit.Measure box.

draw_dual_rail_braid(positions, node, **params)[source]#

Draws a discopy.balanced.DualRailBraid, i.e. the two ribbons (0, 1) and (2, 3) crossing as a whole rather than wire by wire.

draw_dual_rail_twist(positions, node, **params)[source]#

Draws a discopy.balanced.DualRailTwist, i.e. the two rails of a ribbon crossing each other twice in quick succession.

draw_brakets(positions, node, **params)[source]#

Draws a discopy.quantum.gates.Ket box.

draw_controlled_gate(positions, node, **params)[source]#

Draws a discopy.quantum.gates.Controlled gate.