Skip to content

literal_tree_energy

from tensorsig.offset_coded_signatures.graph_queries import literal_tree_energy

Return sum_T mu_q(T)^2 exactly, for a chordal occurrence graph.

This is LE6's coefficient circuit, with two independent coefficient replicas on one common forest. peo may supply any valid occurrence PEO; otherwise deterministic MCS finds one. Disconnected graphs give zero. Nonchordal inputs are rejected. ResourceLimit is an inconclusive refusal. max_entries bounds all query-live dynamic sparse state entries together, including pending messages and simultaneous input/output tables. Immutable attachment/Bernoulli caches and Python object bytes are outside that budget.

Source code in src/tensorsig/offset_coded_signatures/_graph/literal_energy.py
def literal_tree_energy(
    composition,
    occurrence_channels,
    graph,
    *,
    peo=None,
    max_entries=100_000,
    max_terms=1_000_000,
):
    """Return sum_T mu_q(T)^2 exactly, for a chordal occurrence graph.

    This is LE6's coefficient circuit, with two independent coefficient
    replicas on one common forest. ``peo`` may supply any valid occurrence
    PEO; otherwise deterministic MCS finds one. Disconnected graphs give zero.
    Nonchordal inputs are rejected. ResourceLimit is an inconclusive refusal.
    ``max_entries`` bounds all query-live dynamic sparse state entries together,
    including pending messages and simultaneous input/output tables. Immutable
    attachment/Bernoulli caches and Python object bytes are outside that budget.
    """
    parts, channels = validate_cell(composition, occurrence_channels, graph)
    work = ExactWork(max_entries, max_terms)
    adjacency = occurrence_adjacency(channels, graph)
    if peo is not None:
        peo = tuple(peo)
        if not valid_peo(adjacency, peo):
            raise ValueError(
                "peo must be a valid occurrence perfect elimination order."
            )
    else:
        peo = perfect_elimination_order(adjacency)
        if peo is None:
            raise ValueError("the occurrence dependence graph must be chordal.")
    if not is_connected(adjacency):
        return Fraction()
    degree = len(channels)
    root = peo[-1]
    colours = tuple(i for i, count in enumerate(parts) for _ in range(count))
    positions = {v: i for i, v in enumerate(peo)}
    children = {v: [] for v in peo}
    later = {
        v: tuple(
            sorted(
                (w for w in adjacency[v] if positions[w] > positions[v]),
                key=positions.get,
            )
        )
        for v in peo
    }
    for v in peo[:-1]:
        children[later[v][0]].append(v)
    messages = {}
    for vertex in peo:
        bag = tuple(sorted((vertex, *later[vertex])))
        table = {}
        _add(table, (0, (0,) * (4 * len(bag)), 0, 0), Fraction(1), work)
        for child in children[vertex]:
            child_bag, child_table = messages.pop(child)
            extended = _extend(child_table, child_bag, bag, work)
            work.discard(child_table)
            joined = _join(table, extended, degree - 1, work)
            work.discard(table)
            work.discard(extended)
            table = joined
        for i, neighbor in enumerate(later[vertex]):
            updated = _edge(
                table,
                bag,
                vertex,
                neighbor,
                colours,
                root,
                degree,
                work,
                final_vertex=vertex if i + 1 == len(later[vertex]) else None,
            )
            work.discard(table)
            table = updated
        if vertex != root:
            forgotten = _forget(table, bag, vertex, work)
            work.discard(table)
            messages[vertex] = (
                tuple(v for v in bag if v != vertex),
                forgotten,
            )
    bernoulli = bernoulli_at_one(degree - 1)
    energy = Fraction()
    for (bits, balances, left, right), coefficient in table.items():
        a, b, c, d = balances
        if bits or left or right or min(balances) < 0 or max(a + b, c + d) >= degree:
            continue
        energy += coefficient * bernoulli[a] * bernoulli[c] / ((b + 1) * (d + 1))
    work.discard(table)
    return (4 ** (degree - 1)) * energy