cotengra.hypergraph

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

Module Contents

Classes

HyperGraph

Simple hypergraph builder and writer.

LineGraph

Very simple line-graph builder and file writer.

Functions

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)

Attributes

cotengra.hypergraph.HyperGraphRust
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

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

Copy this HyperGraph.

classmethod from_edges(edges, output=(), size_dict=())[source]
get_num_nodes()[source]
get_num_edges()[source]
__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]
__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]