Skip to content

Fix TypeError on non-spin-symmetric hamiltonians in low_rank trotter decomposition#1296

Open
rosspeili wants to merge 3 commits intoquantumlib:mainfrom
rosspeili:fix/issue-1105-low-rank-spin-exchange
Open

Fix TypeError on non-spin-symmetric hamiltonians in low_rank trotter decomposition#1296
rosspeili wants to merge 3 commits intoquantumlib:mainfrom
rosspeili:fix/issue-1105-low-rank-spin-exchange

Conversation

@rosspeili
Copy link
Copy Markdown

Fixes #1105

Summary

simulate_trotter with algorithm=LOW_RANK raised a TypeError: 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_coefficients silently assumed spin-symmetry when spin_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 in low_rank_two_body_decomposition to fail, but with a TypeError rather than a ValueError, and with no detailed info.

Changes

  • circuits/low_rank.py: adds an explicit spin-symmetry check in get_chemist_two_body_coefficients before the spin downfolding. If the extracted spatial-orbital block is not symmetric when reshaped, a ValueError is raised with a clear message explaining the requirement and suggesting spin_basis=False as an alt. The downstream check in low_rank_two_body_decomposition is also changed from TypeError to ValueError for correctness.

  • circuits/trotter/algorithms/low_rank.py: documents the spin-symmetry requirement in the LowRankTrotterAlgorithm class docstring.

  • circuits/low_rank_test.py: updates the existing assertRaises(TypeError) assertion to assertRaises(ValueError) and adds new SpinExchangeTest class covering: spin-exchange input (must raise ValueError with informative message), and spin-symmetric input (must succeed).

  • circuits/trotter/simulate_trotter_test.py: adds an integration-level test that the full simulate_trotter + LOW_RANK path raises ValueError for a spin-exchange Hamiltonian.

Testing

All existing tests pass and new tests verified against the exact reproducer from the original issue.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

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.

Comment thread src/openfermion/circuits/low_rank.py Outdated
Comment thread src/openfermion/circuits/low_rank.py Outdated
…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.
@rosspeili rosspeili force-pushed the fix/issue-1105-low-rank-spin-exchange branch from 2dab6e2 to 26a4332 Compare May 2, 2026 18:18
rosspeili and others added 2 commits May 2, 2026 21:20
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>
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.

simulate_trotter throws error on spin exchange Hamiltonian

1 participant