Various aspects of the contraction process can be visualized using the following functions.

%config InlineBackend.figure_formats = ['svg']
import cotengra as ctg

# generate an equation representing a 2D lattice
inputs, output, shapes, size_dict = ctg.utils.lattice_equation([5, 6])

Hypergraph visualization#

The first visualization we can do is to visualize the hypergraph corresponding to the geometry of the tensor network or equation with HyperGraph.plot:

hg = ctg.get_hypergraph(inputs, output, size_dict)

The default coloring of the nodes shows a simple centrality measure.


Hyper edges are shown as a zero size vertex connecting the tensors they appear on, like a COPY-tensor.

ctg.get_hypergraph(['ax', 'bx', 'cx']).plot(draw_edge_labels=True)

Optimizer trials visualization#

If we run an hyper optimizer, we can visualize how the scores progress with trials using the HyperOptimizer.plot_trials method:

opt = ctg.HyperOptimizer(methods=['greedy', 'kahypar', 'labels'], progbar=True)

# run it and generate a tree
tree =, output, size_dict)
log2[SIZE]: 6.00 log10[FLOPs]: 3.80: 100%|██████████| 128/128 [00:02<00:00, 50.78it/s]

By default the y-axis is the objective score, but you can specify e.g. 'flops'’:


We can also plot the distribution of contraction costs against contraction widths using the HyperOptimizer.plot_scatter method:

opt.plot_scatter(x='size', y='flops')

Tree visualizations#

The following visualization functions are available for inspecting a single, complete ContractionTree once generated. They mostly wrap plot_tree, where you can see most of the extra options.


The most general purpose visualization for a ContractionTree is the ContractionTree.plot_tent method. This plots the input network (in grey) at the bottom, and the contraction tree intermediates laid out above. The width and color of the tree edges denote the intermediate tensor widths, and the size and color of the tree nodes denote the FLOPs required to contract each intermediate tensor:


If you supply order=True then the intermediate nodes will be in the exact vertical order than they would be performed:



If you have sliced indices, these will appear as dashed lines in the input graph.


Another option is the ContractionTree.plot_ring method which lays out the input network on a ring, with the contraction tree intermediates laid out towards the center. The more arcs cross between branches the more expensive that contraction. This can be useful for inspecting how many ‘spines’ a contraction has or how balanced it is:



For small and close to planar graphs, an alternative visualization is the ContractionTree.plot_rubberband method. method from the hypernetx package. Here, nodes of the input graph are hierarchically grouped into bands according to the contraction tree. The order of contraction is represented by the colors:


All of the above methods can be pretty extensively customized, including by supplying custom colormaps. Most also take a return_fig kwarg which can be used to return the matplotlib figure for more customization.

inputs, output, shapes, size_dict = ctg.utils.lattice_equation([5, 5, 5])
opt = ctg.HyperOptimizer(progbar=True, reconf_opts={}, minimize='combo-256')
tree =, output, size_dict)
log2[SIZE]: 30.00 log10[FLOPs]: 12.53: 100%|██████████| 128/128 [02:13<00:00,  1.04s/it]
    figsize=(10, 10),