Skip to content

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

Open
rosspeili wants to merge 4 commits into
quantumlib: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 4 commits into
quantumlib:mainfrom
rosspeili:fix/issue-1105-low-rank-spin-exchange

Conversation

@rosspeili
Copy link
Copy Markdown

@rosspeili rosspeili commented May 2, 2026

Fixes #1105

simulate_trotter(..., algorithm=LOW_RANK) on spin-exchange Hamiltonians used to fail with a vague TypeError. LOW_RANK with spin_basis=True only 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 try spin_basis=False), updates docs/tests, and rebases on current main.

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
@rosspeili
Copy link
Copy Markdown
Author

Hey @mhucka any feedback on this? Let me know how it looks or if I am missing something. Thanks in advance. <3

@mhucka mhucka self-assigned this May 30, 2026
rosspeili and others added 4 commits May 30, 2026 14:25
…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
@rosspeili rosspeili force-pushed the fix/issue-1105-low-rank-spin-exchange branch from 813fdec to b5d8d98 Compare May 30, 2026 11:26
@rosspeili
Copy link
Copy Markdown
Author

@mhucka rebased also this, kept same 4-file fix. Thanks for taking a look when you have time.

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

2 participants