Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions tests/test_apply_pybind.py → pytests/apply_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,30 @@
# ---------------------------------------------------------------------------

def _are_nearly_eq(a, b, tol=1e-12):
"""Return True if two UniTensors have the same labels and nearly equal values."""
"""Return True if two block UniTensors have nearly equal raw block data.

Blocks are matched by their quantum-number indices, not by position:
e.g. permute().contiguous() keeps the source tensor's block enumeration
order, which differs from that of a freshly constructed tensor (see the
C++ AreNearlyEqUniTensor in tests/test_tools.cpp). Pending signflips must
agree so that raw block data can be compared directly -- this keeps the
comparison sensitive to whether apply() actually folded the signs in.
"""
a = a.permute(b.labels())
blocks_a = a.get_blocks_()
blocks_b = b.get_blocks_()
if len(blocks_a) != len(blocks_b):
return False
for ba, bb in zip(blocks_a, blocks_b):
if (ba - bb).Norm().item() > tol:
flips_a = a.signflip()
flips_b = b.signflip()
idx_b = {tuple(b.get_qindices(j)): j for j in range(len(blocks_b))}
for i, ba in enumerate(blocks_a):
j = idx_b.get(tuple(a.get_qindices(i)))
if j is None:
return False
if flips_a[i] != flips_b[j]:
return False
if (ba - blocks_b[j]).Norm().item() > tol:
return False
return True

Expand Down
Loading