Skip to content

Commit 2d7cf32

Browse files
add backend agnostic tests
1 parent 2182cd9 commit 2d7cf32

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
- Add `Lattice` module (`tensorcircuit.templates.lattice`) for creating and manipulating various lattice geometries, including `SquareLattice`, `HoneycombLattice`, and `CustomizeLattice`.
88

9+
- Add `tc.templates.hamiltonians` for commom systems.
10+
911
- Add `DistributedContractor` in experimental module with new examples for fast implementation of distribution circuit simulation on jax backend.
1012

1113
- Add `circuit.amplitude_before()` method to return the corresponding tensornetwork nodes.

tensorcircuit/templates/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
from . import measurements
77
from . import conversions
88
from . import lattice
9+
from . import hamiltonians
910

1011
costfunctions = measurements

tests/test_hamiltonians.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import pytest
22
import numpy as np
3-
import tensorcircuit as tc
3+
from pytest_lazyfixture import lazy_fixture as lf
44

5+
import tensorcircuit as tc
56

67
from tensorcircuit.templates.lattice import (
78
ChainLattice,
@@ -44,7 +45,8 @@ def test_single_site(self):
4445
assert h.shape == (2, 2)
4546
assert h.nnz == 0
4647

47-
def test_two_sites_chain(self):
48+
@pytest.mark.parametrize("backend", [lf("npb"), lf("tfb"), lf("jaxb")])
49+
def test_two_sites_chain(self, backend):
4850
"""
4951
Test a two-site chain against a manually calculated Hamiltonian.
5052
This is the most critical test for scientific correctness.
@@ -60,7 +62,8 @@ def test_two_sites_chain(self):
6062
h_expected = j_coupling * (xx + yy + zz)
6163

6264
assert h_generated.shape == (4, 4)
63-
assert np.allclose(tc.backend.to_dense(h_generated), h_expected)
65+
print(tc.backend.to_dense(h_generated))
66+
assert np.allclose(tc.backend.to_dense(h_generated), h_expected, atol=1e-5)
6467

6568
def test_square_lattice_properties(self):
6669
"""
@@ -135,7 +138,8 @@ def test_zero_distance_robustness(self):
135138
except ZeroDivisionError:
136139
pytest.fail("The function failed to handle zero distance between sites.")
137140

138-
def test_anisotropic_heisenberg(self):
141+
@pytest.mark.parametrize("backend", [lf("npb"), lf("tfb"), lf("jaxb")])
142+
def test_anisotropic_heisenberg(self, backend):
139143
"""
140144
Test the anisotropic Heisenberg model with different Jx, Jy, Jz.
141145
"""

0 commit comments

Comments
 (0)