Skip to content

Use NDTensors.with_auto_fermion in ITensorsExtensions and tests#340

Merged
mtfishman merged 3 commits intomainfrom
mf/use_NDTensors_with_auto_fermion
Apr 26, 2026
Merged

Use NDTensors.with_auto_fermion in ITensorsExtensions and tests#340
mtfishman merged 3 commits intomainfrom
mf/use_NDTensors_with_auto_fermion

Conversation

@mtfishman
Copy link
Copy Markdown
Member

@mtfishman mtfishman commented Apr 25, 2026

Summary

Adopt the new exception-safe scope wrapper NDTensors.with_auto_fermion (added in NDTensors v0.4.25), replacing the manual ITensors.enable_auto_fermion() / disable_auto_fermion() save-restore patterns in ITensorsExtensions.map_eigvals and across four test files.

The wrapper restores the auto-fermion state via try / finally, fixing exception-safety bugs at callsites where a thrown exception would have left the flag flipped, and also fixing one logic bug in a test that always re-enabled the flag instead of restoring its prior state.

Changes

Source

  • src/lib/ITensorsExtensions/src/itensor.jl::map_eigvals: replace the manual disable/enable around the eigendecomposition with a single NDTensors.with_auto_fermion(!fermionic_itensor) do … end do-block. Numerical output is bit-identical across all four (UAF × has-fermionic-subspaces) cases on the existing test suite plus a Case-3 differential test.

Tests

  • test/test_opsum_to_ttn.jl ("Multiple onsite terms"): wrap with with_auto_fermion(). Also fixes a real bug in the previous save-restore: the conditional only re-enabled the flag if it was already enabled at entry, so a run with the flag originally off would leave it flipped on.
  • test/test_itensorsextensions.jl ("Fermionic eigendecomp", "Fermionic map eigvals tests"): wrap each fermionic testset with with_auto_fermion().
  • test/test_opsum_to_ttn_mpo_cross_check.jl: wrap "OpSum to TTN Fermions" with with_auto_fermion(). Remove a vestigial save/restore from "OpSum to TTN" (the testset never actually toggled the flag, so the captured auto_fermion_enabled and the conditional restore were dead code).
  • test/test_ttn_position.jl ("ProjTTN position"): fold the if use_qns; enable; else; disable; end conditional into with_auto_fermion(use_qns) do … end.

Version

  • Patch bump 0.19.00.19.1.

Out of scope

The four read-only using_auto_fermion() queries that gate logic in package source (ITensorsExtensions/itensor.jl:63, opsum_to_ttn.jl:225/382/581) are state queries, not state changes; converting them would require larger algorithm-level changes (threading a fermion-mode parameter through call signatures). Left unchanged here.

Replace the manual `ITensors.disable_auto_fermion()` /
`enable_auto_fermion()` save-restore pair in `map_eigvals` with the
new exception-safe scope wrapper `NDTensors.with_auto_fermion(_, false)`.
The previous structure left the auto-fermion system in the disabled
state if the eigendecomposition threw; the wrapper restores it via
`try`/`finally`.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 25, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 72.48%. Comparing base (6e3c3b3) to head (7a68a03).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #340      +/-   ##
==========================================
- Coverage   72.50%   72.48%   -0.02%     
==========================================
  Files          67       67              
  Lines        3179     3177       -2     
==========================================
- Hits         2305     2303       -2     
  Misses        874      874              
Flag Coverage Δ
docs 46.24% <0.00%> (-0.01%) ⬇️

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Replace the `body = function () ... end` closure plus ternary dispatch with
a single `with_auto_fermion(!fermionic_itensor) do ... end`. When the
tensor is fermionic, this disables the auto-fermion system during the
eigendecomposition (matching the prior conditional behavior); when it
isn't, this requests the same auto-fermion state already in effect, which
is a no-op. Numerical output is bit-identical across all four cases (UAF
× has-fermionic-subspaces) on the existing test suite plus a Case-3
differential test.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@mtfishman mtfishman changed the title [ITensorsExtensions] Use NDTensors.with_auto_fermion in map_eigvals Use NDTensors.with_auto_fermion in map_eigvals Apr 26, 2026
…terns in tests with `with_auto_fermion`

Apply the same scope-wrapper pattern in the four test files that touch
the auto-fermion flag, replacing each manual save/enable/disable
sequence with `with_auto_fermion(...) do ... end`. Where the scope
should always enable auto-fermion, use the no-arg form
`with_auto_fermion() do ... end` (the wrapper defaults to `enable=true`).

This also fixes a pre-existing bug in the "Multiple onsite terms"
testset of `test_opsum_to_ttn.jl`, where the manual restore re-enabled
auto-fermion only when it was already enabled at entry — leaving the
flag flipped on if it was originally off. The wrapper restores the
prior state in either direction.

`test_opsum_to_ttn_mpo_cross_check.jl`'s "OpSum to TTN" testset had a
vestigial save/restore (the testset never toggled the flag, so the
captured `auto_fermion_enabled` and the conditional restore were dead);
removed.

In `test_ttn_position.jl`, fold the `if use_qns; enable; else; disable;
end` conditional into `with_auto_fermion(use_qns) do ... end`.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@mtfishman mtfishman changed the title Use NDTensors.with_auto_fermion in map_eigvals Use NDTensors.with_auto_fermion in ITensorsExtensions and tests Apr 26, 2026
@mtfishman mtfishman enabled auto-merge (squash) April 26, 2026 15:53
@mtfishman mtfishman merged commit 629b79c into main Apr 26, 2026
16 of 17 checks passed
@mtfishman mtfishman deleted the mf/use_NDTensors_with_auto_fermion branch April 26, 2026 16:07
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.

1 participant