utils
utils
¤
HyperCube = tuple[tuple[int, ...], tuple[int, ...]]
module-attribute
¤
A hypercube represented by "top-left" and "bottom-right" coordinates (cut points).
HypercubeToScope
¤
Helper class to map sub-hypercubes to scopes with caching for variables arranged in a hypercube.
This is implemented as a dict subclass with customized missing, so that: - If a hypercube is already queried, the corresponding scope is retrieved the dict; - If it's not in the dict yet, the scope is calculated and cached to the dict.
Source code in cirkit/templates/region_graph/algorithms/utils.py
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | |
hypercube = np.arange(np.prod(shape), dtype=(np.int64)).reshape(shape)
instance-attribute
¤
ndims = len(shape)
instance-attribute
¤
shape = shape
instance-attribute
¤
__init__(shape)
¤
Initialize a hypercube to scope object. Note that this does not accept initial elements and is initialized empty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
tuple[int, int, int]
|
The image shape \((C, H, W)\), where \(H\) is the height, \(W\) is the width, and \(C\) is the number of channels. |
required |
Source code in cirkit/templates/region_graph/algorithms/utils.py
27 28 29 30 31 32 33 34 35 36 37 38 39 | |
__missing__(key)
¤
Construct the item when not exist in the dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
HyperCube
|
The key that is missing from the dict, i.e., a hypercube that is visited for the first time. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Scope |
Scope
|
The value for the key, i.e., the corresponding scope. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the hyper-cube key has incorrect shape, or if it's empty. |
Source code in cirkit/templates/region_graph/algorithms/utils.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | |
tree2rg(tree)
¤
Convert a tree structure into a region graph. Useful to convert CLTs into HCLT region graphs. More details in https://arxiv.org/abs/2409.07953.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tree
|
ndarray
|
A tree in form of list of predecessors, i.e. tree[i] is the parent of i, and is equal to -1 when i is root. |
required |
Returns:
| Type | Description |
|---|---|
RegionGraph
|
A region graph. |
Source code in cirkit/templates/region_graph/algorithms/utils.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | |