Fix TypeError on non-spin-symmetric hamiltonians in low_rank trotter decomposition#1296
Fix TypeError on non-spin-symmetric hamiltonians in low_rank trotter decomposition#1296rosspeili wants to merge 4 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces explicit spin-symmetry validation for low-rank decompositions. Key changes include updating get_chemist_two_body_coefficients and low_rank_two_body_decomposition to validate that input tensors are spin-symmetric and real, raising descriptive ValueError exceptions when these conditions are not met. The PR also updates relevant docstrings and adds comprehensive test cases for spin-exchange Hamiltonians. Feedback focuses on improving the numerical stability of symmetry checks by using numpy.amax instead of numpy.sum to avoid false positives in large systems.
2dab6e2 to
26a4332
Compare
|
Hey @mhucka any feedback on this? Let me know how it looks or if I am missing something. Thanks in advance. <3 |
…position When spin_basis=True, get_chemist_two_body_coefficients silently assumed a spin-symmetric two-body tensor, then raised a cryptic TypeError if the assumption failed. This change: - Adds an explicit spin-symmetry validation in get_chemist_two_body_coefficients: the extracted alpha-alpha-beta-beta block is checked for matrix symmetry before the spin downfolding proceeds. Asymmetric tensors (e.g. spin-exchange Hamiltonians) now raise a ValueError with an informative message. - Changes the downstream check in low_rank_two_body_decomposition from TypeError to ValueError, which is the correct exception type for a data-value violation. - Documents the spin-symmetry requirement in LowRankTrotterAlgorithm. - Adds tests covering spin-exchange input (must raise ValueError), spin-symmetric input (must succeed), and complex tensor input.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Replace rST-syntax double-backtick inline code (`code`) and cross-reference roles (:func:, :class:) with plain text, per OpenFermion's Google-style (TensorFlow) docstring convention. Affected sections: - get_chemist_two_body_coefficients Raises block - low_rank_two_body_decomposition Raises block - LowRankTrotterAlgorithm Note block
813fdec to
b5d8d98
Compare
|
@mhucka rebased also this, kept same 4-file fix. Thanks for taking a look when you have time. |
Fixes #1105
simulate_trotter(..., algorithm=LOW_RANK)on spin-exchange Hamiltonians used to fail with a vagueTypeError. LOW_RANK withspin_basis=Trueonly supports interactions that are symmetric in the α/β channel sense used for spatial downfolding; spin-exchange is not.This PR validates that earlier and raises a clear
ValueError(with a hint to tryspin_basis=False), updates docs/tests, and rebases on currentmain.