Skip to content

Compare CTMRG gradients against a converged reference - #438

Merged
leburgel merged 2 commits into
mainfrom
lb/gradient_tests
Oct 14, 2026
Merged

leburgel merged 2 commits into
mainfrom
lb/gradient_tests

Conversation

@leburgel

Copy link
Copy Markdown
Member

The CTMRG gradient tests checked every gradient algorithm with OptimKit.optimtest, comparing directional derivatives with finite differences at atol = 1e-2. That is too loose to catch actual gradient errors: the wrong ImplicitGradient for C4vQRProjector fixed in #435, for instance, passes it.

This PR restructures ctmrg_gradients.jl and c4v_ctmrg_gradients.jl around a single reference:

  • For each state and forward CTMRG algorithm, a tightly converged reference gradient (FixedPointGradient with GMRES) is computed once and checked against a finite difference of the energy.
  • Every other gradient algorithm is compared with that reference by its relative error.
  • Each option is covered at least once instead of running the full product of all options: the decomposition rrules on every forward algorithm, the linear solvers on one, and the implicit gradient wherever it is defined, plus a larger state for the implicit gradient where truncation effects show up.
  • Naive AD is differentiated through enough CTMRG iterations (miniter) to be compared at the same accuracy as the other algorithms.
  • Shared helpers move to a new test/testsuite/gradients/gradient_utility.jl.

Both test files also run faster than before, since far fewer cases are run.

Depends on #435: without it, the C4vQRProjector implicit-gradient case fails.

@codecov

codecov Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
see 1 file with indirect coverage changes

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

@pbrehmer pbrehmer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Only have a very minor comment but overall this is a really nice simplification.

Comment thread test/testsuite/gradients/ctmrg_gradients.jl Outdated
@leburgel
leburgel marked this pull request as ready for review October 13, 2026 04:00
@leburgel
leburgel force-pushed the lb/gradient_tests branch 2 times, most recently from 8950ce0 to da40c3c Compare October 13, 2026 05:49
leburgel and others added 2 commits October 14, 2026 10:06
Replace the optimtest directional-derivative checks at atol = 1e-2 by
comparing every gradient algorithm against one tightly converged
fixed-point reference gradient per state and forward algorithm, which
is itself checked against a finite difference of the energy. Cover each
option at least once instead of the full product of all options.
Co-authored-by: Paul Brehmer <paul.brehmer@univie.ac.at>
@leburgel
leburgel merged commit 9623a41 into main Oct 14, 2026
76 of 77 checks passed
@leburgel
leburgel deleted the lb/gradient_tests branch October 14, 2026 13:40
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