Add tests for Hamiltonian class in tests/ folder - #206
Conversation
Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com>
hzhangxyz
left a comment
There was a problem hiding this comment.
还是改成4 qubit 的吧, 两项目, n + t, t只有相邻的有吧, 相当与是一个一维无自旋自由费米子. 另外 n 和 t的系数分别设置一下, 比如比如一个1 一个 2 . 这样好区分. 还有就是, 我没检查的一点, 也许你已经做了, find relative 是找最大的固定数目个config, 而 list是列出所有的.
…t coefficients Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com>
Done in 9e09079. The test model is now a 4-site 1D spinless free-fermion chain: Number-operator coefficient = 1, nearest-neighbor hopping coefficient = 2. Key tests added:
|
There was a problem hiding this comment.
Pull request overview
Adds a new pytest suite for qmp.hamiltonian.Hamiltonian, aiming to cover the four public methods using a small 4-site 1D spinless free-fermion model.
Changes:
- Introduces
tests/hamiltonian_test.pycoveringdiagonal_term,apply_within,find_relative, andlist_relative. - Uses a session-scoped fixture to amortize the C++/CUDA extension JIT compilation cost across tests.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| C_site0 = torch.tensor([[1]], dtype=torch.uint8) # |1000⟩ – site 0 occupied | ||
| C_site1 = torch.tensor([[2]], dtype=torch.uint8) # |0100⟩ – site 1 occupied | ||
| C_site2 = torch.tensor([[4]], dtype=torch.uint8) # |0010⟩ – site 2 occupied | ||
| C_site3 = torch.tensor([[8]], dtype=torch.uint8) # |0001⟩ – site 3 occupied | ||
| C_sites02 = torch.tensor([[5]], dtype=torch.uint8) # |1010⟩ – sites 0,2 occupied | ||
| C_sites12 = torch.tensor([[6]], dtype=torch.uint8) # |0110⟩ – sites 1,2 occupied | ||
| C_sites13 = torch.tensor([[10]], dtype=torch.uint8) # |0101⟩ – sites 1,3 occupied |
There was a problem hiding this comment.
The inline ket/binary comments for the config constants (e.g., C_site0, C_site1, …) don’t match the encoding described above (bit 0 = site 0) or the “Selected basis states” table in the module docstring (e.g., 1 corresponds to 0b0001, not |1000⟩). This is likely to confuse future readers when debugging test failures; please update these comments (or explicitly state the ket bit-order convention) so the written bitstrings/kets align with the actual integer values.
| C_site0 = torch.tensor([[1]], dtype=torch.uint8) # |1000⟩ – site 0 occupied | |
| C_site1 = torch.tensor([[2]], dtype=torch.uint8) # |0100⟩ – site 1 occupied | |
| C_site2 = torch.tensor([[4]], dtype=torch.uint8) # |0010⟩ – site 2 occupied | |
| C_site3 = torch.tensor([[8]], dtype=torch.uint8) # |0001⟩ – site 3 occupied | |
| C_sites02 = torch.tensor([[5]], dtype=torch.uint8) # |1010⟩ – sites 0,2 occupied | |
| C_sites12 = torch.tensor([[6]], dtype=torch.uint8) # |0110⟩ – sites 1,2 occupied | |
| C_sites13 = torch.tensor([[10]], dtype=torch.uint8) # |0101⟩ – sites 1,3 occupied | |
| C_site0 = torch.tensor([[1]], dtype=torch.uint8) # |0001⟩ – site 0 occupied | |
| C_site1 = torch.tensor([[2]], dtype=torch.uint8) # |0010⟩ – site 1 occupied | |
| C_site2 = torch.tensor([[4]], dtype=torch.uint8) # |0100⟩ – site 2 occupied | |
| C_site3 = torch.tensor([[8]], dtype=torch.uint8) # |1000⟩ – site 3 occupied | |
| C_sites02 = torch.tensor([[5]], dtype=torch.uint8) # |0101⟩ – sites 0,2 occupied | |
| C_sites12 = torch.tensor([[6]], dtype=torch.uint8) # |0110⟩ – sites 1,2 occupied | |
| C_sites13 = torch.tensor([[10]], dtype=torch.uint8) # |1010⟩ – sites 1,3 occupied |
No tests existed for
qmp/hamiltonian/hamiltonian.py. This PR adds a pytest test suite covering all four public methods ofHamiltonian.Test model
All tests use a 4-site 1D spinless free-fermion Hamiltonian (
kind="fermi",n_qubytes=1):The number operator coefficient (1) and hopping coefficient (2) are intentionally different to make it easy to verify both contributions independently. Hopping is nearest-neighbor only, matching a 1D spinless free-fermion chain. In the 1-particle sector the Hamiltonian matrix is tridiagonal:
Coverage (22 tests)
diagonal_term— vacuum, single-particle (n coefficient = 1), two non-adjacent particles (= 2), all occupied (= 4), batched 1-particle sectorapply_within— distinguishing n vs t coefficients (H|site1⟩gives[2, 1, 2]), nearest-neighbor-only hopping, Pauli-blocked diagonal from fully occupied state, complex amplitudes, full 1-particle sector matrix row sumsfind_relative— finds both neighbours,count_selectedtruncation (1 vs 2 results), Pauli-blocked fully occupied state, customconfigs_excludelist_relative— lists ALL connections (vsfind_relative's truncation), amplitude accumulation from multiple input configs, complexpsi_i, custom exclusion, two-particle sectorThe fixture is
scope="session"to compile the C++ JIT extension once per test run.Original prompt
🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.