22from typing import Any , List , Tuple , Union
33import numpy as np
44from tensorcircuit .cons import dtypestr , backend
5- import tensorcircuit as tc
5+ from .. quantum import PauliStringSum2COO
66from .lattice import AbstractLattice
77
88
99def _create_empty_sparse_matrix (shape : Tuple [int , int ]) -> Any :
1010 """
1111 Helper function to create a backend-agnostic empty sparse matrix.
1212 """
13- indices = tc . backend .convert_to_tensor (backend .zeros ((0 , 2 ), dtype = "int32" ))
14- values = tc . backend .convert_to_tensor (backend .zeros ((0 ,), dtype = dtypestr )) # type: ignore
15- return tc . backend .coo_sparse_matrix (indices = indices , values = values , shape = shape ) # type: ignore
13+ indices = backend .convert_to_tensor (backend .zeros ((0 , 2 ), dtype = "int32" ))
14+ values = backend .convert_to_tensor (backend .zeros ((0 ,), dtype = dtypestr )) # type: ignore
15+ return backend .coo_sparse_matrix (indices = indices , values = values , shape = shape ) # type: ignore
1616
1717
1818def heisenberg_hamiltonian (
@@ -46,8 +46,10 @@ def heisenberg_hamiltonian(
4646 raise ValueError ("j_coupling must be a float or a list/tuple of 3 floats." )
4747 js = [float (j ) for j in j_coupling ]
4848
49- if num_sites == 0 or not neighbor_pairs :
49+ if not neighbor_pairs :
5050 return _create_empty_sparse_matrix (shape = (2 ** num_sites , 2 ** num_sites ))
51+ if num_sites == 0 :
52+ raise ValueError ("Cannot generate a Hamiltonian for a lattice with zero sites." )
5153
5254 pauli_map = {"X" : 1 , "Y" : 2 , "Z" : 3 }
5355
@@ -64,7 +66,7 @@ def heisenberg_hamiltonian(
6466 ls .append (string )
6567 weights .append (js [idx ])
6668
67- hamiltonian_matrix = tc . quantum . PauliStringSum2COO (ls , weight = weights , numpy = False )
69+ hamiltonian_matrix = PauliStringSum2COO (ls , weight = weights , numpy = False )
6870
6971 return hamiltonian_matrix
7072
@@ -97,7 +99,7 @@ def rydberg_hamiltonian(
9799 """
98100 num_sites = lattice .num_sites
99101 if num_sites == 0 :
100- return _create_empty_sparse_matrix ( shape = ( 1 , 1 ) )
102+ raise ValueError ( "Cannot generate a Hamiltonian for a lattice with zero sites." )
101103
102104 pauli_map = {"X" : 1 , "Y" : 2 , "Z" : 3 }
103105 ls : typing .List [typing .List [int ]] = []
@@ -132,6 +134,11 @@ def rydberg_hamiltonian(
132134 ls .append (zz_string )
133135 weights .append (coefficient )
134136
137+ # The interaction term V_ij * n_i * n_j, when expanded using
138+ # n_i = (1-Z_i)/2, becomes (V_ij/4)*(I - Z_i - Z_j + Z_i*Z_j).
139+ # This contributes a positive term (+V_ij/4) to the ZZ interaction,
140+ # but negative terms (-V_ij/4) to the single-site Z_i and Z_j operators.
141+
135142 z_coefficients [i ] -= coefficient
136143 z_coefficients [j ] -= coefficient
137144
@@ -142,6 +149,6 @@ def rydberg_hamiltonian(
142149 ls .append (z_string )
143150 weights .append (z_coefficients [i ]) # type: ignore
144151
145- hamiltonian_matrix = tc . quantum . PauliStringSum2COO (ls , weight = weights , numpy = False )
152+ hamiltonian_matrix = PauliStringSum2COO (ls , weight = weights , numpy = False )
146153
147154 return hamiltonian_matrix
0 commit comments