Use NDTensors.with_auto_fermion in ITensorsExtensions and tests#340
Merged
Use NDTensors.with_auto_fermion in ITensorsExtensions and tests#340
NDTensors.with_auto_fermion in ITensorsExtensions and tests#340Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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>
NDTensors.with_auto_fermion in map_eigvalsNDTensors.with_auto_fermion in map_eigvals
…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>
NDTensors.with_auto_fermion in map_eigvalsNDTensors.with_auto_fermion in ITensorsExtensions and tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adopt the new exception-safe scope wrapper
NDTensors.with_auto_fermion(added inNDTensors v0.4.25), replacing the manualITensors.enable_auto_fermion()/disable_auto_fermion()save-restore patterns inITensorsExtensions.map_eigvalsand 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 singleNDTensors.with_auto_fermion(!fermionic_itensor) do … enddo-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 withwith_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 withwith_auto_fermion().test/test_opsum_to_ttn_mpo_cross_check.jl: wrap "OpSum to TTN Fermions" withwith_auto_fermion(). Remove a vestigial save/restore from "OpSum to TTN" (the testset never actually toggled the flag, so the capturedauto_fermion_enabledand the conditional restore were dead code).test/test_ttn_position.jl("ProjTTN position"): fold theif use_qns; enable; else; disable; endconditional intowith_auto_fermion(use_qns) do … end.Version
0.19.0→0.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.