From 713a1ec11a37bab621a002ba03d6b5b10903e923 Mon Sep 17 00:00:00 2001 From: FreshJoa Date: Fri, 29 Jul 2022 10:27:33 +0200 Subject: [PATCH 1/3] test script --- python/tests.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 python/tests.py diff --git a/python/tests.py b/python/tests.py new file mode 100644 index 0000000..2bdc479 --- /dev/null +++ b/python/tests.py @@ -0,0 +1,46 @@ +import numpy as np +from collections import defaultdict + +import lattice_symmetries + + +def create_hubbard_hamiltonian(basis, t, U): + operator = -t * lattice_symmetries.Operator(basis, "c†↑₀ c↑₁", [(0, 1)]) + operator -= t * lattice_symmetries.Operator(basis, "c†↑₁ c↑₀", [(0, 1)]) + operator -= t * lattice_symmetries.Operator(basis, "c†↓₁ c↓₀", [(0, 1)]) + operator -= t * lattice_symmetries.Operator(basis, "c†↓₀ c↓₁", [(0, 1)]) + + operator += U * lattice_symmetries.Operator(basis, "n↑₀ n↓₀", [(0)]) + operator += U * lattice_symmetries.Operator(basis, "n↑₁ n↓₁", [(1)]) + + return operator + + +def calculate_hamiltonian_matrix(operator, basis_states): + hamiltonian_matrix = np.zeros((len(basis_states), len(basis_states))) + for ket_state in basis_states: + operator_dict = defaultdict(list) + for coeff, state in operator.apply_off_diag_to_basis_state(ket_state): + if coeff != 0: + operator_dict[coeff].append(state) + coeff = operator.apply_diag_to_basis_state(ket_state) + if coeff != 0: + operator_dict[coeff].append(ket_state) + for amplitude, states in operator_dict.items(): + for bra_state in basis_states: + if bra_state in states: + hamiltonian_matrix[basis_states.index(bra_state)][ + basis_states.index(ket_state)] = amplitude * states.count(bra_state) + + return hamiltonian_matrix + + +if __name__ == '__main__': + basis = lattice_symmetries.SpinfulFermionBasis(2, 2) + basis.build() + t = 1 + U = 2 + op = create_hubbard_hamiltonian(basis, t, U) + print(op) + h_matrix = calculate_hamiltonian_matrix(op, list(basis.states)) + print(h_matrix) From 93bb78ada4f89fb0184689f82988f07b7c9817c8 Mon Sep 17 00:00:00 2001 From: FreshJoa Date: Fri, 29 Jul 2022 14:49:09 +0200 Subject: [PATCH 2/3] python tests - hubbard model --- python/tests.py | 75 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 67 insertions(+), 8 deletions(-) diff --git a/python/tests.py b/python/tests.py index 2bdc479..cb50d8d 100644 --- a/python/tests.py +++ b/python/tests.py @@ -1,6 +1,5 @@ import numpy as np from collections import defaultdict - import lattice_symmetries @@ -20,12 +19,13 @@ def calculate_hamiltonian_matrix(operator, basis_states): hamiltonian_matrix = np.zeros((len(basis_states), len(basis_states))) for ket_state in basis_states: operator_dict = defaultdict(list) - for coeff, state in operator.apply_off_diag_to_basis_state(ket_state): - if coeff != 0: - operator_dict[coeff].append(state) coeff = operator.apply_diag_to_basis_state(ket_state) if coeff != 0: operator_dict[coeff].append(ket_state) + for coeff, state in operator.apply_off_diag_to_basis_state(ket_state): + if coeff != 0: + operator_dict[coeff].append(state) + for amplitude, states in operator_dict.items(): for bra_state in basis_states: if bra_state in states: @@ -35,12 +35,71 @@ def calculate_hamiltonian_matrix(operator, basis_states): return hamiltonian_matrix -if __name__ == '__main__': +def test_constructed_basis_states(): + basis = lattice_symmetries.SpinfulFermionBasis(2) + basis.build() + assert np.array_equal(basis.states, np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])) + + +def test_constructed_basis_states_with_one_particle(): + basis = lattice_symmetries.SpinfulFermionBasis(2, 1) + basis.build() + assert np.array_equal(basis.states, np.array([1, 2, 4, 8])) + + +def test_constructed_operator(): + basis = lattice_symmetries.SpinfulFermionBasis(2) + basis.build() + t = 1 + U = 2 + op = create_hubbard_hamiltonian(basis, t, U) + assert "2.0 × n↑₀ n↓₀ + -1.0 × c†↑₀ c↑₁ + 1.0 × c↑₀ c†↑₁ + 2.0 × n↑₁ n↓₁ + -1.0 × c†↓₀ c↓₁ + 1.0 × c↓₀ c†↓₁" == \ + str(op) + + +def test_hubbard_hamiltonian_matrix(): + basis = lattice_symmetries.SpinfulFermionBasis(2) + basis.build() + t = 1 + U = 2 + op = create_hubbard_hamiltonian(basis, t, U) + hamiltonian_matrix = calculate_hamiltonian_matrix(op, list(basis.states)) + valid_matrix = np.array([ + [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], + [0., 0., -1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], + [0., -1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], + [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], + [0., 0., 0., 0., 0., 0., 0., 0., -1., 0., 0., 0., 0., 0., 0., 0.], + [0., 0., 0., 0., 0., 2., -1., 0., 0., -1., 0., 0., 0., 0., 0., 0.], + [0., 0., 0., 0., 0., -1., 0., 0., 0., 0., -1., 0., 0., 0., 0., 0.], + [0., 0., 0., 0., 0., 0., 0., 2., 0., 0., 0., -1., 0., 0., 0., 0.], + [0., 0., 0., 0., -1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], + [0., 0., 0., 0., 0., -1., 0., 0., 0., 0., -1., 0., 0., 0., 0., 0.], + [0., 0., 0., 0., 0., 0., -1., 0., 0., -1., 2., 0., 0., 0., 0., 0.], + [0., 0., 0., 0., 0., 0., 0., -1., 0., 0., 0., 2., 0., 0., 0., 0.], + [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], + [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 2., -1., 0.], + [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -1., 2., 0.], + [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 4.]]) + + assert np.array_equal(hamiltonian_matrix, valid_matrix) + + +def test_hubbard_hamiltonian_matrix_with_defined_particle_number(): basis = lattice_symmetries.SpinfulFermionBasis(2, 2) basis.build() t = 1 U = 2 op = create_hubbard_hamiltonian(basis, t, U) - print(op) - h_matrix = calculate_hamiltonian_matrix(op, list(basis.states)) - print(h_matrix) + hamiltonian_matrix = calculate_hamiltonian_matrix(op, list(basis.states)) + valid_matrix = np.array( + [[0., 0., 0., 0., 0., 0.], + [0., 2., -1., -1., 0., 0.], + [0., -1., 0., 0., -1., 0.], + [0., -1., 0., 0., -1., 0.], + [0., 0., -1., -1., 2., 0.], + [0., 0., 0., 0., 0., 0.]]) + + assert np.array_equal(hamiltonian_matrix, valid_matrix) + + From 081f3a2be9e8b9be9466c50ce736f95b7b9c3c98 Mon Sep 17 00:00:00 2001 From: FreshJoa Date: Fri, 29 Jul 2022 15:07:18 +0200 Subject: [PATCH 3/3] pyhon tests --- python/tests.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python/tests.py b/python/tests.py index cb50d8d..4b21d9a 100644 --- a/python/tests.py +++ b/python/tests.py @@ -8,7 +8,6 @@ def create_hubbard_hamiltonian(basis, t, U): operator -= t * lattice_symmetries.Operator(basis, "c†↑₁ c↑₀", [(0, 1)]) operator -= t * lattice_symmetries.Operator(basis, "c†↓₁ c↓₀", [(0, 1)]) operator -= t * lattice_symmetries.Operator(basis, "c†↓₀ c↓₁", [(0, 1)]) - operator += U * lattice_symmetries.Operator(basis, "n↑₀ n↓₀", [(0)]) operator += U * lattice_symmetries.Operator(basis, "n↑₁ n↓₁", [(1)])