Skip to content

Commit 11f1162

Browse files
committed
refactor(templates): Improve hamiltonian
Internal API Usage: I've switched to importing backend and dtypestr from .cons and PauliStringSum2COO from ..quantum as you suggested, avoiding direct calls to the top-level tc package. Error Handling: For both heisenberg_hamiltonian and rydberg_hamiltonian, I've added a check to raise a ValueError if num_sites is 0, which is indeed more explicit.
1 parent e8b1283 commit 11f1162

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

tensorcircuit/templates/hamiltonians.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
from typing import Any, List, Tuple, Union
33
import numpy as np
44
from tensorcircuit.cons import dtypestr, backend
5-
import tensorcircuit as tc
5+
from ..quantum import PauliStringSum2COO
66
from .lattice import AbstractLattice
77

88

99
def _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

1818
def 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

Comments
 (0)