Commit 157fc88f authored by Viacheslav Ostroukh's avatar Viacheslav Ostroukh 🚲
Browse files

test conjugation of array-like types

If onsite/hopping value was supplied to the builder as array-like instead of
proper array, Hamiltonian could be generated silently without any Hermitian
conjugation, if required.

To expose this, `test_builder.py::test_hermitian_conjugation` is
parametrized with different types of values to ensure that Numpy arrays,
tinyarrays, lists and tuples are all conjugated properly.

See issue #406.
parent 7f5a7dc0
...@@ -241,11 +241,17 @@ def test_construction_and_indexing(): ...@@ -241,11 +241,17 @@ def test_construction_and_indexing():
unknown_hoppings, sym) unknown_hoppings, sym)
def test_hermitian_conjugation(): @pytest.mark.parametrize('value', (
def f(i, j, arg): ta.array([[1, 2j], [3 + 1j, 4j]]),
np.array([[1, 2j], [3 + 1j, 4j]]),
[[1, 2j], [3 + 1j, 4j]],
((1, 2j), (3 + 1j, 4j)),
))
def test_hermitian_conjugation(value):
def f(i, j):
i, j = i.tag, j.tag i, j = i.tag, j.tag
if j[0] == i[0] + 1: if j[0] == i[0] + 1:
return arg * ta.array([[1, 2j], [3 + 1j, 4j]]) return value
else: else:
raise ValueError raise ValueError
...@@ -256,8 +262,13 @@ def test_hermitian_conjugation(): ...@@ -256,8 +262,13 @@ def test_hermitian_conjugation():
syst[fam(0), fam(1)] = f syst[fam(0), fam(1)] = f
assert syst[fam(0), fam(1)] is f assert syst[fam(0), fam(1)] is f
assert isinstance(syst[fam(1), fam(0)], builder.HermConjOfFunc) assert isinstance(syst[fam(1), fam(0)], builder.HermConjOfFunc)
assert (syst[fam(1), fam(0)](fam(1), fam(0), 2) == assert np.all(
syst[fam(0), fam(1)](fam(0), fam(1), 2).conjugate().transpose()) np.asarray(
syst[fam(1), fam(0)](fam(1), fam(0))
) == np.asarray(
syst[fam(0), fam(1)](fam(0), fam(1))
).conjugate().transpose()
)
syst[fam(0), fam(1)] = syst[fam(1), fam(0)] syst[fam(0), fam(1)] = syst[fam(1), fam(0)]
assert isinstance(syst[fam(0), fam(1)], builder.HermConjOfFunc) assert isinstance(syst[fam(0), fam(1)], builder.HermConjOfFunc)
assert syst[fam(1), fam(0)] is f assert syst[fam(1), fam(0)] is f
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment