Skip to content

factorized

factorized ¤

FullyFactorized(num_variables, *, num_repetitions=1) ¤

Construct a region graph with fully factorized partitions.

Parameters:

Name Type Description Default
num_variables int

The number of variables in the RG.

required
num_repetitions int

The number of fully factorized partitions. Defaults to 1.

1

Returns:

Name Type Description
RegionGraph RegionGraph

The fully-factorized region graph.

Raises:

Type Description
ValueError

If either the number of variables or number of reptitions are not positive.

Source code in cirkit/templates/region_graph/algorithms/factorized.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def FullyFactorized(num_variables: int, *, num_repetitions: int = 1) -> RegionGraph:
    """Construct a region graph with fully factorized partitions.

    Args:
        num_variables: The number of variables in the RG.
        num_repetitions: The number of fully factorized partitions. Defaults to 1.

    Returns:
        RegionGraph: The fully-factorized region graph.

    Raises:
        ValueError: If either the number of variables or number of reptitions are not positive.
    """
    if num_variables <= 0:
        raise ValueError("The number of variables must be positive")
    if num_repetitions <= 0:
        raise ValueError("The number of repetitions must be positive")

    root = RegionNode(range(num_variables))
    nodes: list[RegionGraphNode] = [root]
    in_nodes: dict[RegionGraphNode, list[RegionGraphNode]] = {root: []}
    if num_variables == 1:
        return RegionGraph(nodes, in_nodes, [root])

    for _ in range(num_repetitions):
        partition_node = PartitionNode(range(num_variables))
        leaf_nodes = [RegionNode([vid]) for vid in range(num_variables)]
        in_nodes[partition_node] = leaf_nodes
        in_nodes[root].append(partition_node)
        nodes.extend(leaf_nodes)
        nodes.append(partition_node)

    return RegionGraph(nodes, in_nodes, [root])