Fix TypeError on non-spin-symmetric hamiltonians in low_rank trotter decomposition#1296
Open
rosspeili wants to merge 3 commits intoquantumlib:mainfrom
Open
Fix TypeError on non-spin-symmetric hamiltonians in low_rank trotter decomposition#1296rosspeili wants to merge 3 commits intoquantumlib:mainfrom
rosspeili wants to merge 3 commits intoquantumlib:mainfrom
Conversation
Contributor
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.
…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.
2dab6e2 to
26a4332
Compare
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>
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.
Fixes #1105
Summary
simulate_trotterwithalgorithm=LOW_RANKraised aTypeError: Invalid two-body coefficient tensor specification.when given a non-spin-symmetric Hamiltonian (eg. spin-exchange operator). The error gave no indication of the actual requirement, making it a bit difficult to diagnose.Root cause
get_chemist_two_body_coefficientssilently assumed spin-symmetry whenspin_basis=True, extracting only the αα→ββ block of the two-body tensor without validating the assumption. For spin-exchange Hamiltonians this block is asymmetric when reshaped, causing the downstream symmetry check inlow_rank_two_body_decompositionto fail, but with aTypeErrorrather than aValueError, and with no detailed info.Changes
circuits/low_rank.py: adds an explicit spin-symmetry check inget_chemist_two_body_coefficientsbefore the spin downfolding. If the extracted spatial-orbital block is not symmetric when reshaped, aValueErroris raised with a clear message explaining the requirement and suggestingspin_basis=Falseas an alt. The downstream check inlow_rank_two_body_decompositionis also changed fromTypeErrortoValueErrorfor correctness.circuits/trotter/algorithms/low_rank.py: documents the spin-symmetry requirement in theLowRankTrotterAlgorithmclass docstring.circuits/low_rank_test.py: updates the existingassertRaises(TypeError)assertion toassertRaises(ValueError)and adds newSpinExchangeTestclass covering: spin-exchange input (must raiseValueErrorwith informative message), and spin-symmetric input (must succeed).circuits/trotter/simulate_trotter_test.py: adds an integration-level test that the fullsimulate_trotter+LOW_RANKpath raisesValueErrorfor a spin-exchange Hamiltonian.Testing
All existing tests pass and new tests verified against the exact reproducer from the original issue.