11import pytest
22import numpy as np
3+ import tensorcircuit as tc
4+
35
46from tensorcircuit .templates .lattice import (
57 ChainLattice ,
68 SquareLattice ,
79 CustomizeLattice ,
810)
911from tensorcircuit .templates .hamiltonians import (
10- generate_heisenberg_hamiltonian ,
11- generate_rydberg_hamiltonian ,
12+ heisenberg_hamiltonian ,
13+ rydberg_hamiltonian ,
1214)
1315
1416PAULI_X = np .array ([[0 , 1 ], [1 , 0 ]], dtype = complex )
1921
2022class TestHeisenbergHamiltonian :
2123 """
22- Test suite for the generate_heisenberg_hamiltonian function.
24+ Test suite for the heisenberg_hamiltonian function.
2325 """
2426
2527 def test_empty_lattice (self ):
@@ -29,16 +31,16 @@ def test_empty_lattice(self):
2931 empty_lattice = CustomizeLattice (
3032 dimensionality = 2 , identifiers = [], coordinates = []
3133 )
32- h = generate_heisenberg_hamiltonian (empty_lattice )
33- assert h .shape == (0 , 0 )
34+ h = heisenberg_hamiltonian (empty_lattice )
35+ assert h .shape == (1 , 1 )
3436 assert h .nnz == 0
3537
3638 def test_single_site (self ):
3739 """
3840 Test that a single-site lattice (no bonds) produces a 2x2 zero matrix.
3941 """
4042 single_site_lattice = ChainLattice (size = (1 ,), pbc = False )
41- h = generate_heisenberg_hamiltonian (single_site_lattice )
43+ h = heisenberg_hamiltonian (single_site_lattice )
4244 assert h .shape == (2 , 2 )
4345 assert h .nnz == 0
4446
@@ -49,7 +51,7 @@ def test_two_sites_chain(self):
4951 """
5052 lattice = ChainLattice (size = (2 ,), pbc = False )
5153 j_coupling = - 1.5 # Test with a non-trivial coupling constant
52- h_generated = generate_heisenberg_hamiltonian (lattice , j_coupling = j_coupling )
54+ h_generated = heisenberg_hamiltonian (lattice , j_coupling = j_coupling )
5355
5456 # Manually construct the expected Hamiltonian: H = J * (X_0X_1 + Y_0Y_1 + Z_0Z_1)
5557 xx = np .kron (PAULI_X , PAULI_X )
@@ -58,24 +60,24 @@ def test_two_sites_chain(self):
5860 h_expected = j_coupling * (xx + yy + zz )
5961
6062 assert h_generated .shape == (4 , 4 )
61- assert np .allclose (h_generated . toarray ( ), h_expected )
63+ assert np .allclose (tc . backend . to_dense ( h_generated ), h_expected )
6264
6365 def test_square_lattice_properties (self ):
6466 """
6567 Test properties of a larger lattice (2x2 square) without full matrix comparison.
6668 """
6769 lattice = SquareLattice (size = (2 , 2 ), pbc = True ) # 4 sites, 8 bonds with PBC
68- h = generate_heisenberg_hamiltonian (lattice , j_coupling = 1.0 )
70+ h = heisenberg_hamiltonian (lattice , j_coupling = 1.0 )
6971
7072 assert h .shape == (16 , 16 )
7173 assert h .nnz > 0
72- h_dense = h . toarray ( )
74+ h_dense = tc . backend . to_dense ( h )
7375 assert np .allclose (h_dense , h_dense .conj ().T )
7476
7577
7678class TestRydbergHamiltonian :
7779 """
78- Test suite for the generate_rydberg_hamiltonian function.
80+ Test suite for the rydberg_hamiltonian function.
7981 """
8082
8183 def test_single_site_rydberg (self ):
@@ -84,20 +86,20 @@ def test_single_site_rydberg(self):
8486 """
8587 lattice = ChainLattice (size = (1 ,), pbc = False )
8688 omega , delta , c6 = 2.0 , 0.5 , 100.0
87- h_generated = generate_rydberg_hamiltonian (lattice , omega , delta , c6 )
89+ h_generated = rydberg_hamiltonian (lattice , omega , delta , c6 )
8890
8991 h_expected = (omega / 2.0 ) * PAULI_X + (delta / 2.0 ) * PAULI_Z
9092
9193 assert h_generated .shape == (2 , 2 )
92- assert np .allclose (h_generated . toarray ( ), h_expected )
94+ assert np .allclose (tc . backend . to_dense ( h_generated ), h_expected )
9395
9496 def test_two_sites_rydberg (self ):
9597 """
9698 Test a two-site chain for Rydberg Hamiltonian, including interaction.
9799 """
98100 lattice = ChainLattice (size = (2 ,), pbc = False , lattice_constant = 1.5 )
99101 omega , delta , c6 = 1.0 , - 0.5 , 10.0
100- h_generated = generate_rydberg_hamiltonian (lattice , omega , delta , c6 )
102+ h_generated = rydberg_hamiltonian (lattice , omega , delta , c6 )
101103
102104 v_ij = c6 / (1.5 ** 6 )
103105
@@ -110,7 +112,9 @@ def test_two_sites_rydberg(self):
110112 h_expected = h1 + h2 + h3
111113
112114 assert h_generated .shape == (4 , 4 )
113- assert np .allclose (h_generated .toarray (), h_expected )
115+ h_generated_dense = tc .backend .to_dense (h_generated )
116+
117+ assert np .allclose (h_generated_dense , h_expected )
114118
115119 def test_zero_distance_robustness (self ):
116120 """
@@ -123,10 +127,29 @@ def test_zero_distance_robustness(self):
123127 )
124128
125129 try :
126- h = generate_rydberg_hamiltonian (lattice , omega = 1.0 , delta = 1.0 , c6 = 1.0 )
130+ h = rydberg_hamiltonian (lattice , omega = 1.0 , delta = 1.0 , c6 = 1.0 )
127131 # The X terms contribute 8 non-zero elements.
128132 # The Z terms (Z0+Z1) have diagonal elements that cancel out,
129133 # resulting in only 2 non-zero elements. Total nnz = 8 + 2 = 10.
130134 assert h .nnz == 10
131135 except ZeroDivisionError :
132136 pytest .fail ("The function failed to handle zero distance between sites." )
137+
138+ def test_anisotropic_heisenberg (self ):
139+ """
140+ Test the anisotropic Heisenberg model with different Jx, Jy, Jz.
141+ """
142+ lattice = ChainLattice (size = (2 ,), pbc = False )
143+ j_coupling = [- 1.0 , 0.5 , 2.0 ] # Jx, Jy, Jz
144+ h_generated = heisenberg_hamiltonian (lattice , j_coupling = j_coupling )
145+
146+ # Manually construct the expected Hamiltonian
147+ jx , jy , jz = j_coupling
148+ xx = np .kron (PAULI_X , PAULI_X )
149+ yy = np .kron (PAULI_Y , PAULI_Y )
150+ zz = np .kron (PAULI_Z , PAULI_Z )
151+ h_expected = jx * xx + jy * yy + jz * zz
152+
153+ h_generated_dense = tc .backend .to_dense (h_generated )
154+ assert h_generated_dense .shape == (4 , 4 )
155+ assert np .allclose (h_generated_dense , h_expected )
0 commit comments