Skip to content

Commit 4326fda

Browse files
committed
Improved times for geometry determination (before the dangling edges time was absurd)
1 parent f9d829e commit 4326fda

5 files changed

Lines changed: 1279 additions & 359 deletions

File tree

examples/tensorkrowch_demo.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
"weird": "Irregular topology for layout fallback.",
4747
}
4848

49-
5049
def _build_blueprint(example: str, args: ExampleCliArgs) -> GraphBlueprint:
5150
if example == "mps":
5251
return build_mps_blueprint(args.n_sites)

src/tensor_network_viz/_core/layout/direction_common.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from __future__ import annotations
44

5+
import math
6+
57
import numpy as np
68

79
from ..axis_directions import _AXIS_DIR_2D, _AXIS_DIR_3D
@@ -22,6 +24,54 @@
2224
np.array([-1.0, -1.0], dtype=float) / np.sqrt(2.0),
2325
np.array([1.0, -1.0], dtype=float) / np.sqrt(2.0),
2426
)
27+
_SEMIDIAGONAL_DIRECTIONS_2D: tuple[np.ndarray, ...] = tuple(
28+
np.array(
29+
[math.cos(math.radians(angle_degrees)), math.sin(math.radians(angle_degrees))],
30+
dtype=float,
31+
)
32+
for angle_degrees in (67.5, 112.5, 157.5, 202.5, 247.5, 292.5, 337.5, 22.5)
33+
)
34+
_NORTH_BEHAVIOR_ORDER_2D: tuple[np.ndarray, ...] = (
35+
np.array([0.0, 1.0], dtype=float),
36+
np.array([0.0, -1.0], dtype=float),
37+
np.array([1.0, 0.0], dtype=float),
38+
np.array([-1.0, 0.0], dtype=float),
39+
*_DIAGONAL_DIRECTIONS_2D,
40+
*_SEMIDIAGONAL_DIRECTIONS_2D,
41+
)
42+
43+
44+
def _rotate_direction_2d(
45+
direction: np.ndarray,
46+
*,
47+
quarter_turns_clockwise: int,
48+
) -> np.ndarray:
49+
x_coord, y_coord = np.asarray(direction, dtype=float).reshape(-1)[:2]
50+
turns = int(quarter_turns_clockwise) % 4
51+
if turns == 0:
52+
return np.array([x_coord, y_coord], dtype=float)
53+
if turns == 1:
54+
return np.array([y_coord, -x_coord], dtype=float)
55+
if turns == 2:
56+
return np.array([-x_coord, -y_coord], dtype=float)
57+
return np.array([-y_coord, x_coord], dtype=float)
58+
59+
60+
def _behavior_direction_order_2d(behavior: str) -> tuple[np.ndarray, ...]:
61+
turns_by_behavior = {
62+
"north": 0,
63+
"east": 1,
64+
"south": 2,
65+
"west": 3,
66+
}
67+
turns = turns_by_behavior.get(behavior.lower().strip(), 0)
68+
return tuple(
69+
_normalize_direction(
70+
_rotate_direction_2d(direction, quarter_turns_clockwise=turns),
71+
dimensions=2,
72+
)
73+
for direction in _NORTH_BEHAVIOR_ORDER_2D
74+
)
2575

2676

2777
def _is_dangling_leg_axis(graph: _GraphData, node_id: int, axis_index: int) -> bool:
@@ -465,6 +515,7 @@ def _preferred_component_directions_3d(
465515

466516

467517
__all__ = [
518+
"_behavior_direction_order_2d",
468519
"_dedupe_candidate_directions",
469520
"_direction_from_axis_name",
470521
"_direction_has_space",

0 commit comments

Comments
 (0)