cotengra.hypergraph

Simple hypergraph (and also linegraph) representations for simulating contractions.

Attributes

Classes

HyperGraph

Simple hypergraph builder and writer.

LineGraph

Very simple line-graph builder and file writer.

Functions

plot_hypergraph(H, *[, edge_color, node_color, ...])

compute_size_by_dict(indices, size_dict)

Computes the product of sizes of indices based on size_dict.

prod(it)

Compute the product of sequence of numbers it.

get_hypergraph(inputs[, output, size_dict, accel])

Single entry-point for creating a, possibly accelerated, HyperGraph.

calc_edge_weight(ix, size_dict[, scale])

calc_edge_weight_float(ix, size_dict[, scale])

calc_node_weight(term, size_dict[, scale])

calc_node_weight_float(term, size_dict[, scale])

popcount(x)

dict_affine_renorm(d)

Module Contents

cotengra.hypergraph.plot_hypergraph(H, *, edge_color=True, node_color=True, highlight=(), centrality='simple', colormap='plasma', pos=None, dim=2, layout='auto', initial_layout='auto', iterations='auto', k=None, use_forceatlas2=False, flatten=False, node_size=None, node_scale=1.0, edge_alpha=1 / 3, edge_style='solid', hyperedge_style='dashed', draw_edge_labels=None, fontcolor=(0.5, 0.5, 0.5), edge_labels_font_size=8, edge_labels_font_family='monospace', node_labels_font_size=10, node_labels_font_family='monospace', info=None, ax=None, figsize=(5, 5))[source]
cotengra.hypergraph.compute_size_by_dict(indices, size_dict)[source]

Computes the product of sizes of indices based on size_dict.

Parameters:
  • indices (iterable[str] or iterable[int]) – The indices of the term.

  • size_dict (dict or list) – Mapping (or list/tuple if the indices are indexing integers, which can be slightly faster) of indices to sizes.

Returns:

d – The resulting product.

Return type:

int

Examples

>>> compute_size_by_dict('abbc', {'a': 2, 'b':3, 'c':5})
90
cotengra.hypergraph.prod(it)[source]

Compute the product of sequence of numbers it.

cotengra.hypergraph.HyperGraphRust = None
class cotengra.hypergraph.HyperGraph(inputs, output=None, size_dict=None)[source]

Simple hypergraph builder and writer.

Parameters:
  • inputs (sequence of list[str] or dict[int, list[str]]) – The nodes. If given as a dict, the keys will be taken as the node enumeration rather than range(len(inputs)).

  • output (str, optional) – Output indices.

  • size_dict (dict[str, int], optional) – Size of each index.

nodes

Mapping of node to the list of edges incident to it.

Type:

dict[int, list[str]]

edges

Mapping of hyper edges to list of nodes it is incident to.

Type:

dict[str, list[int]]

num_nodes

The number of nodes.

Type:

int

num_edges

The number of hyper-edges.

Type:

int

__slots__ = ('inputs', 'output', 'size_dict', 'nodes', 'edges', 'node_counter')
copy()[source]

Copy this HyperGraph.

classmethod from_edges(edges, output=(), size_dict=())[source]
get_num_nodes()[source]
property num_nodes
get_num_edges()[source]
property num_edges
__len__()[source]
edges_size(es)[source]

Get the combined, i.e. product, size of all edges in es.

bond_size(i, j)[source]

Get the combined, i.e. product, size of edges shared by nodes i and j.

node_size(i)[source]

Get the size of the term represented by node i.

neighborhood_size(nodes)[source]

Get the size of nodes in the immediate neighborhood of nodes.

contract_pair_cost(i, j)[source]

Get the cost of contracting nodes i and j - the product of the dimensions of the indices involved.

neighborhood_compress_cost(chi, nodes)[source]
total_node_size()[source]

Get the total size of all nodes.

output_nodes()[source]

Get the nodes with output indices.

neighbors(i)[source]

Get the neighbors of node i.

neighbor_edges(i)[source]

Get the edges incident to all neighbors of node i, (including its own edges).

has_node(i)[source]

Does this hypergraph have node i?

get_node(i)[source]

Get the edges node i is incident to.

get_edge(e)[source]

Get the nodes edge e is incident to.

has_edge(e)[source]

Does this hypergraph have edge e?

next_node()[source]

Get the next available node identifier.

add_node(inds, node=None)[source]

Add a node with inds, and optional identifier node. The identifier will be generated if not given and returned.

remove_node(i)[source]

Remove node i from this hypergraph.

remove_edge(e)[source]

Remove edge e from this hypergraph.

contract(i, j, node=None)[source]

Combine node i and node j.

compress(chi, edges=None)[source]

‘Compress’ multiedges, combining their size up to a maximum of chi.

compute_contracted_inds(nodes)[source]

Generate the output indices if one were to contract nodes.

candidate_contraction_size(i, j, chi=None)[source]

Get the size of the node created if i and j were contracted, optionally including the effect of first compressing bonds to size chi.

all_shortest_distances(nodes=None)[source]
all_shortest_distances_condensed(nodes=None)[source]
simple_distance(region, p=2)[source]

Compute a simple distance metric from nodes in region to all others. Unlike graph distance, relative connectedness is taken into account.

simple_closeness(p=0.75, mu=0.5)[source]

Compute a rough hypergraph ‘closeness’.

Parameters:
  • p (float, optional) – Once any node has had H.num_nodes**p visitors terminate. Set greater than 1.0 for no limit (slower).

  • mu (float, optional) – Let the visitor score decay with this power. The higher this is, the more local connectivity is favored.

Returns:

scores – The simple hypergraph closenesses - higher being more central.

Return type:

dict[int, float]

simple_centrality(r=None, smoothness=2, **closeness_opts)[source]

A simple algorithm for large hypergraph centrality. First we find a rough closeness centrality, then relax / smooth this by nodes iteratively radiating their centrality to their neighbors.

Parameters:
  • r (None or int, optional) – Number of iterations. Defaults to max(10, int(self.num_nodes**0.5)).

  • smoothness (float, optional) – The smoothness. In conjunction with a high value of r this will create a smooth gradient from one of the hypergraph to the other.

  • closeness_opts – Supplied to HyperGraph.simple_closeness as the starting point.

Return type:

dict[int, float]

compute_loops(start=None, max_loop_length=None)[source]

Generate all loops up to a certain length in this hypergraph.

Parameters:
  • start (sequence of int, optional) – Only generate loops including these nodes, defaults to all.

  • max_loop_length (None or int, optional) – The maximum loop length to search for. If None, then this is set automatically by the length of the first loop found.

Yields:

loop (tuple[int]) – A set of nodes that form a loop.

get_laplacian()[source]

Get the graph Laplacian.

get_resistance_distances()[source]

Get the resistance distance between all nodes of the raw graph.

resistance_centrality(rescale=True)[source]

Compute the centrality in terms of the total resistance distance to all other nodes.

to_networkx(as_tree_leaves=False)[source]

Convert to a networkx Graph, with hyperedges represented as nodes.

Parameters:

as_tree_leaves (bool, optional) – If true, then the nodes are converted to ‘tree leaf’ form, i.e. map node i to frozenset([i]), to match the nodes in a ContractionTree.

compute_weights(weight_edges='const', weight_nodes='const')[source]
plot[source]
__repr__()[source]

Return repr(self).

cotengra.hypergraph.get_hypergraph(inputs, output=None, size_dict=None, accel=False)[source]

Single entry-point for creating a, possibly accelerated, HyperGraph.

cotengra.hypergraph.calc_edge_weight(ix, size_dict, scale='log')[source]
cotengra.hypergraph.calc_edge_weight_float(ix, size_dict, scale='log')[source]
cotengra.hypergraph.calc_node_weight(term, size_dict, scale='linear')[source]
cotengra.hypergraph.calc_node_weight_float(term, size_dict, scale='linear')[source]
class cotengra.hypergraph.LineGraph(inputs, output)[source]

Very simple line-graph builder and file writer.

to_gr_str()[source]
to_gr_file(fname)[source]
to_cnf_str()[source]
to_cnf_file(fname)[source]
cotengra.hypergraph.popcount(x)[source]
cotengra.hypergraph.dict_affine_renorm(d)[source]