diff --git a/tests/test_apply_pybind.py b/pytests/apply_test.py similarity index 81% rename from tests/test_apply_pybind.py rename to pytests/apply_test.py index 79ac3df7d..82d965a9f 100644 --- a/tests/test_apply_pybind.py +++ b/pytests/apply_test.py @@ -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