Skip to content

fix(tests): match blocks by qindices in apply() pybind test helper#985

Open
yingjerkao wants to merge 1 commit into
masterfrom
claude/upbeat-kepler-72c026
Open

fix(tests): match blocks by qindices in apply() pybind test helper#985
yingjerkao wants to merge 1 commit into
masterfrom
claude/upbeat-kepler-72c026

Conversation

@yingjerkao

Copy link
Copy Markdown
Collaborator

Summary

Fixes the two pre-existing failures in tests/test_apply_pybind.py:

  • TestApplyBlockFermionicUniTensor::test_apply_applies_signflips
  • TestApplyBlockFermionicUniTensor::test_apply_inplace_applies_signflips

The backend is correct — the test helper was the bug. T.apply() output was verified element-wise against the hand-built BFUT3PERM reference over the full tensor shape: 0 mismatches, including the expected sign-flipped entries (−5, −6, −7, −8), matching the C++ LinAlgElementwise expectation in tests/BlockFermionicUniTensor_test.cpp.

Root cause

_are_nearly_eq zipped get_blocks_() by list position, but block order is not canonical: permute().contiguous() keeps the source tensor's block enumeration order, while a freshly constructed reference tensor enumerates blocks in canonical qnum order. Positional pairing therefore compared unrelated blocks. The C++ helper AreNearlyEqUniTensor (tests/test_tools.cpp) explicitly matches blocks by quantum numbers ("they might be not in the same order!"), which is why the C++ test passes.

Changes

  • Rewrite _are_nearly_eq to match blocks by get_qindices() and require signflip() flags to agree before comparing raw block data. Deliberately no sign-folding, so the tests stay sensitive to whether apply() actually baked the pending signs in (verified: unapplied T vs reference → False, applied → True).
  • Use the public Bond.redirect_() instead of the c_redirect_ shadow binding (equivalent on master via the Bond_conti.py glue wrapper; avoids a conflict with the in-progress shadow-API removal branch).

Test plan

  • tests/test_apply_pybind.py: 9 passed (was 7 passed / 2 failed)
  • Full pytests/ suite: 28 passed (matches baseline)
  • Test-only change; no C++ touched

🤖 Generated with Claude Code

The two BlockFermionicUniTensor apply()/apply_() signflip tests failed
because _are_nearly_eq compared get_blocks_() by list position, but
block order is not canonical: permute().contiguous() keeps the source
tensor's block enumeration order, while a freshly constructed reference
enumerates blocks in canonical qnum order. The backend apply() output is
element-wise identical to the C++ LinAlgElementwise expectation; only
the comparison was wrong.

Match blocks by get_qindices() and require signflip() flags to agree
before comparing raw block data, mirroring the C++ AreNearlyEqUniTensor
(tests/test_tools.cpp) while staying sensitive to whether apply()
actually folded the pending signs in. Also use the public Bond
redirect_() instead of the c_redirect_ shadow binding.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the _are_nearly_eq helper function in tests/test_apply_pybind.py to match block UniTensors by their quantum-number indices instead of their positions, and to verify that pending signflips agree. Additionally, it replaces the call to c_redirect_() with redirect_() in _make_bfut3(). There are no review comments, so no further feedback is provided.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@codecov

codecov Bot commented Jul 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 30.87%. Comparing base (32079a4) to head (c68cff3).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #985   +/-   ##
=======================================
  Coverage   30.87%   30.87%           
=======================================
  Files         229      229           
  Lines       34757    34757           
  Branches    14409    14409           
=======================================
  Hits        10730    10730           
  Misses      16720    16720           
  Partials     7307     7307           
Flag Coverage Δ
cpp 30.38% <ø> (ø)
python 59.41% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
C++ backend 32.19% <ø> (ø)
Python bindings 17.28% <ø> (ø)
Python package 59.41% <ø> (ø)

Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 32079a4...c68cff3. Read the comment docs.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pcchen

pcchen commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

Code Review

Rewrites _are_nearly_eq in tests/test_apply_pybind.py to match blocks by quantum-number sector (get_qindices) instead of list position, requiring signflip() agreement before comparing block data; plus c_redirect_redirect_.

Correct as far as it goes

  • The _are_nearly_eq sector-matching + signflip-agreement logic is right — it matches the C++ AreNearlyEqUniTensor (blocks are not in canonical order between permute().contiguous() and a freshly-built reference), and deliberately doesn't sign-fold, so it stays sensitive to whether apply() actually baked the pending signs in. ✅
  • redirect_() is correct on current master (post-refactor(pybind): bind in-place methods directly, drop the c__* shadow API #986, which binds it directly). ✅

🟠 Finding 1 — the fix isn't actually enforced by CI

CI runs pytest pytests/ (ci-cmake_tests.yml:140), and this file lives under tests/, which pytest does not discover there. So tests/test_apply_pybind.py has never been run by CI — that's why the "2 failures" only appear in manual runs, and why #985's green checks don't include them. This PR fixes the helper but leaves the file where CI won't run it, so the now-passing tests remain silently skipped. To make it a real fix, also git mv tests/test_apply_pybind.py pytests/apply_test.py so CI enforces it.

🟠 Finding 2 — duplicates #1001

#1001 (still open, CONFLICTING) does the identical _are_nearly_eq rewrite and the pytests/ file move and the redirect_ change, as part of its larger Bond-immutable work. #985 and #1001 modify the same lines, so they'll conflict — whichever merges second must drop this. Given #1001 is currently stuck (changes-requested + conflicts + gateway-narrowing follow-up), landing #985made complete by adding the file move — is a pragmatic way to fix these tests now, after which #1001 drops its copy.

Recommendation

Good, correct fix — but please add the pytests/ relocation (otherwise it's a no-op in CI), and coordinate with #1001 so the duplicate _are_nearly_eq change doesn't collide.

Posted by Claude Code on behalf of @pcchen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants