-
Notifications
You must be signed in to change notification settings - Fork 32
Implement AlternativeSpinHamiltonian for Issue #179 #186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
smandal21-pixel
wants to merge
10
commits into
theochem:main
Choose a base branch
from
smandal21-pixel:issue-179-spin-hamiltonians
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2c03c80
Add AlternativeSpinHamiltonian class skeleton
smandal21-pixel ead5d5d
Implement get_sz_operator and verify with 2-spin Heisenberg test
smandal21-pixel f9759b7
Implement AlternativeSpinHamiltonian with Sz, S+, and S- operators. A…
smandal21-pixel 9fdea40
Finalized spin Hamiltonian logic and added pytest suite
smandal21-pixel 79d1fa6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8019f58
Merge branch 'main' of https://github.com/smandal21-pixel/ModelHamilt…
smandal21-pixel 5f2aa39
Refactor AlternativeSpinHamiltonian to inherit fermion mapping from H…
smandal21-pixel fe2d3dc
Resolve merge conflicts in hamiltonians.py and test_alternative_spin.py
smandal21-pixel 29128e5
Clean up file and remove leftover merge markers
smandal21-pixel 955a430
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import numpy as np | ||
| from moha.hamiltonians import AlternativeSpinHamiltonian | ||
|
|
||
| # Initialize 2-spin system with homogeneous coupling | ||
| n_spins = 2 | ||
| couplings = [1.0] | ||
| model = AlternativeSpinHamiltonian(n_spins, couplings) | ||
|
|
||
| print(f"Testing AlternativeSpinHamiltonian (Spins: {n_spins})") | ||
| H_matrix = model.generate_integrals() | ||
|
|
||
| print("\nHamiltonian Matrix:") | ||
| print(H_matrix) | ||
|
|
||
| # Verify eigenvalues against analytical solutions for 2-spin Heisenberg model | ||
| energies = np.linalg.eigvals(H_matrix) | ||
| sorted_energies = np.sort(energies.real) | ||
|
|
||
| print("\nCalculated Eigenvalues:") | ||
| print(sorted_energies) | ||
|
|
||
| expected = np.array([-0.75, 0.25, 0.25, 0.25]) | ||
| if np.allclose(sorted_energies, expected): | ||
| print("\nStatus: Passed ") | ||
| else: | ||
| print("\nStatus: Failed - Eigenvalues deviate from analytical expectations.") |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import numpy as np | ||
| import pytest | ||
| from moha.hamiltonians import AlternativeSpinHamiltonian | ||
|
|
||
| def test_heisenberg_2spin_energies(): | ||
| """Verify the 2-spin Heisenberg model produces exact analytical eigenvalues.""" | ||
| n_spins = 2 | ||
| couplings = [1.0] | ||
| model = AlternativeSpinHamiltonian(n_spins, couplings) | ||
|
|
||
| H = model.generate_integrals() | ||
| energies = np.linalg.eigvals(H) | ||
| sorted_energies = np.sort(energies.real) | ||
|
|
||
| # Expected eigenvalues for J=1: [-0.75, 0.25, 0.25, 0.25] | ||
| expected = np.array([-0.75, 0.25, 0.25, 0.25]) | ||
| assert np.allclose(sorted_energies, expected), f"Expected {expected}, got {sorted_energies}" | ||
|
|
||
| def test_hermiticity(): | ||
| """The Hamiltonian must be Hermitian (H = H.T).""" | ||
| model = AlternativeSpinHamiltonian(3, [1.0, 1.0]) | ||
| H = model.generate_integrals() | ||
| assert np.allclose(H, H.T), "Hamiltonian is not Hermitian!" |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need to manually code spin operators, as we have introduced mapping from spin-operators to fermion creation/annihilation operators
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the review and the guidance, @RichRick1! I completely understand. Since I was working from a ground-up approach, I missed that the spin-to-fermion mapping was already introduced.
I see the existing HamHeisenberg class in moha/hamiltonians.py. I will scrap my manual dense matrix generation and pivot to utilizing the built-in fermion creation/annihilation operators instead. Could you point me to the specific file/module where the spin-to-fermion mapping functions are stored so I can review them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Following up on my previous message—I’ve just reviewed the newly updated docs/spin.rst and the HamHeisenberg / HamIsing classes. I see that the library is already set up to handle these as one-body and two-body integrals. I am currently refactoring my implementation to remove the manual dense matrix Kronecker products and instead utilize the alpha/beta fermion mapping to generate the integral equivalents. This will ensure full compatibility with the existing moha solvers.