:py:mod:`cotengra.hypergraph` ============================= .. py:module:: cotengra.hypergraph .. autoapi-nested-parse:: Simple hypergraph (and also linegraph) representations for simulating contractions. Module Contents --------------- Classes ~~~~~~~ .. autoapisummary:: cotengra.hypergraph.HyperGraph cotengra.hypergraph.LineGraph Functions ~~~~~~~~~ .. autoapisummary:: cotengra.hypergraph.get_hypergraph cotengra.hypergraph.calc_edge_weight cotengra.hypergraph.calc_edge_weight_float cotengra.hypergraph.calc_node_weight cotengra.hypergraph.calc_node_weight_float cotengra.hypergraph.popcount cotengra.hypergraph.dict_affine_renorm Attributes ~~~~~~~~~~ .. autoapisummary:: cotengra.hypergraph.HyperGraphRust .. py:data:: HyperGraphRust .. py:class:: HyperGraph(inputs, output=None, size_dict=None) Simple hypergraph builder and writer. :param inputs: The nodes. If given as a dict, the keys will be taken as the node enumeration rather than ``range(len(inputs))``. :type inputs: sequence of list[str] or dict[int, list[str]] :param output: Output indices. :type output: str, optional :param size_dict: Size of each index. :type size_dict: dict[str, int], optional .. attribute:: nodes Mapping of node to the list of edges incident to it. :type: dict[int, list[str]] .. attribute:: edges Mapping of hyper edges to list of nodes it is incident to. :type: dict[str, list[int]] .. attribute:: num_nodes The number of nodes. :type: int .. attribute:: num_edges The number of hyper-edges. :type: int .. py:property:: num_nodes .. py:property:: num_edges .. py:attribute:: __slots__ :value: ('inputs', 'output', 'size_dict', 'nodes', 'edges', 'node_counter') .. py:attribute:: plot .. py:method:: copy() Copy this ``HyperGraph``. .. py:method:: from_edges(edges, output=(), size_dict=()) :classmethod: .. py:method:: get_num_nodes() .. py:method:: get_num_edges() .. py:method:: __len__() .. py:method:: edges_size(es) Get the combined, i.e. product, size of all edges in ``es``. .. py:method:: bond_size(i, j) Get the combined, i.e. product, size of edges shared by nodes ``i`` and ``j``. .. py:method:: node_size(i) Get the size of the term represented by node ``i``. .. py:method:: neighborhood_size(nodes) Get the size of nodes in the immediate neighborhood of ``nodes``. .. py:method:: contract_pair_cost(i, j) Get the cost of contracting nodes ``i`` and ``j`` - the product of the dimensions of the indices involved. .. py:method:: neighborhood_compress_cost(chi, nodes) .. py:method:: total_node_size() Get the total size of all nodes. .. py:method:: output_nodes() Get the nodes with output indices. .. py:method:: neighbors(i) Get the neighbors of node ``i``. .. py:method:: neighbor_edges(i) Get the edges incident to all neighbors of node ``i``, (including its own edges). .. py:method:: has_node(i) Does this hypergraph have node ``i``? .. py:method:: get_node(i) Get the edges node ``i`` is incident to. .. py:method:: get_edge(e) Get the nodes edge ``e`` is incident to. .. py:method:: has_edge(e) Does this hypergraph have edge ``e``? .. py:method:: next_node() Get the next available node identifier. .. py:method:: add_node(inds, node=None) Add a node with ``inds``, and optional identifier ``node``. The identifier will be generated if not given and returned. .. py:method:: remove_node(i) Remove node ``i`` from this hypergraph. .. py:method:: remove_edge(e) Remove edge ``e`` from this hypergraph. .. py:method:: contract(i, j, node=None) Combine node ``i`` and node ``j``. .. py:method:: compress(chi, edges=None) 'Compress' multiedges, combining their size up to a maximum of ``chi``. .. py:method:: compute_contracted_inds(nodes) Generate the output indices if one were to contract ``nodes``. .. py:method:: candidate_contraction_size(i, j, chi=None) Get the size of the node created if ``i`` and ``j`` were contracted, optionally including the effect of first compressing bonds to size ``chi``. .. py:method:: all_shortest_distances(nodes=None) .. py:method:: all_shortest_distances_condensed(nodes=None) .. py:method:: simple_distance(region, p=2) Compute a simple distance metric from nodes in ``region`` to all others. Unlike graph distance, relative connectedness is taken into account. .. py:method:: simple_closeness(p=0.75, mu=0.5) Compute a rough hypergraph 'closeness'. :param p: Once any node has had ``H.num_nodes**p`` visitors terminate. Set greater than 1.0 for no limit (slower). :type p: float, optional :param mu: Let the visitor score decay with this power. The higher this is, the more local connectivity is favored. :type mu: float, optional :returns: **scores** -- The simple hypergraph closenesses - higher being more central. :rtype: dict[int, float] .. py:method:: simple_centrality(r=None, smoothness=2, **closeness_opts) 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. :param r: Number of iterations. Defaults to ``max(10, int(self.num_nodes**0.5))``. :type r: None or int, optional :param smoothness: The smoothness. In conjunction with a high value of ``r`` this will create a smooth gradient from one of the hypergraph to the other. :type smoothness: float, optional :param closeness_opts: Supplied to ``HyperGraph.simple_closeness`` as the starting point. :rtype: dict[int, float] .. py:method:: compute_loops(start=None, max_loop_length=None) Generate all loops up to a certain length in this hypergraph. :param start: Only generate loops including these nodes, defaults to all. :type start: sequence of int, optional :param max_loop_length: The maximum loop length to search for. If ``None``, then this is set automatically by the length of the first loop found. :type max_loop_length: None or int, optional :Yields: **loop** (*tuple[int]*) -- A set of nodes that form a loop. .. py:method:: get_laplacian() Get the graph Laplacian. .. py:method:: get_resistance_distances() Get the resistance distance between all nodes of the raw graph. .. py:method:: resistance_centrality(rescale=True) Compute the centrality in terms of the total resistance distance to all other nodes. .. py:method:: to_networkx(as_tree_leaves=False) Convert to a networkx Graph, with hyperedges represented as nodes. :param as_tree_leaves: 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``. :type as_tree_leaves: bool, optional .. py:method:: compute_weights(weight_edges='const', weight_nodes='const') .. py:method:: __repr__() Return repr(self). .. py:function:: get_hypergraph(inputs, output=None, size_dict=None, accel=False) Single entry-point for creating a, possibly accelerated, HyperGraph. .. py:function:: calc_edge_weight(ix, size_dict, scale='log') .. py:function:: calc_edge_weight_float(ix, size_dict, scale='log') .. py:function:: calc_node_weight(term, size_dict, scale='linear') .. py:function:: calc_node_weight_float(term, size_dict, scale='linear') .. py:class:: LineGraph(inputs, output) Very simple line-graph builder and file writer. .. py:method:: to_gr_str() .. py:method:: to_gr_file(fname) .. py:method:: to_cnf_str() .. py:method:: to_cnf_file(fname) .. py:function:: popcount(x) .. py:function:: dict_affine_renorm(d)