|
| 1 | +"""Tests for _jellium_hf_state.py.""" |
| 2 | +from __future__ import absolute_import |
| 3 | + |
| 4 | +from itertools import permutations |
| 5 | +from scipy.sparse import csr_matrix |
| 6 | + |
| 7 | +import numpy |
| 8 | +import unittest |
| 9 | + |
| 10 | +from fermilib.transforms import get_sparse_operator |
| 11 | +from fermilib.utils import expectation, get_ground_state, Grid, jellium_model |
| 12 | +from fermilib.utils._plane_wave_hamiltonian import wigner_seitz_length_scale |
| 13 | +from fermilib.utils._sparse_tools import jw_number_restrict_operator |
| 14 | + |
| 15 | +from fermilib.utils._jellium_hf_state import hartree_fock_state_jellium |
| 16 | + |
| 17 | + |
| 18 | +class JelliumHartreeFockStateTest(unittest.TestCase): |
| 19 | + def test_hf_state_energy_close_to_ground_energy_at_high_density(self): |
| 20 | + grid_length = 8 |
| 21 | + dimension = 1 |
| 22 | + spinless = True |
| 23 | + n_particles = grid_length ** dimension // 2 |
| 24 | + |
| 25 | + # High density -> small length_scale. |
| 26 | + length_scale = 0.25 |
| 27 | + |
| 28 | + grid = Grid(dimension, grid_length, length_scale) |
| 29 | + hamiltonian = jellium_model(grid, spinless) |
| 30 | + hamiltonian_sparse = get_sparse_operator(hamiltonian) |
| 31 | + |
| 32 | + hf_state = hartree_fock_state_jellium(grid, n_particles, |
| 33 | + spinless, plane_wave=True) |
| 34 | + |
| 35 | + restricted_hamiltonian = jw_number_restrict_operator( |
| 36 | + hamiltonian_sparse, n_particles) |
| 37 | + |
| 38 | + E_g = get_ground_state(restricted_hamiltonian)[0] |
| 39 | + E_HF_plane_wave = expectation(hamiltonian_sparse, hf_state) |
| 40 | + |
| 41 | + self.assertAlmostEqual(E_g, E_HF_plane_wave, places=5) |
| 42 | + |
| 43 | + def test_hf_state_energy_same_in_plane_wave_and_dual_basis(self): |
| 44 | + grid_length = 4 |
| 45 | + dimension = 1 |
| 46 | + wigner_seitz_radius = 10.0 |
| 47 | + spinless = False |
| 48 | + |
| 49 | + n_orbitals = grid_length ** dimension |
| 50 | + if not spinless: |
| 51 | + n_orbitals *= 2 |
| 52 | + n_particles = n_orbitals // 2 |
| 53 | + |
| 54 | + length_scale = wigner_seitz_length_scale( |
| 55 | + wigner_seitz_radius, n_particles, dimension) |
| 56 | + |
| 57 | + grid = Grid(dimension, grid_length, length_scale) |
| 58 | + hamiltonian = jellium_model(grid, spinless) |
| 59 | + hamiltonian_dual_basis = jellium_model( |
| 60 | + grid, spinless, plane_wave=False) |
| 61 | + |
| 62 | + # Get the Hamiltonians as sparse operators. |
| 63 | + hamiltonian_sparse = get_sparse_operator(hamiltonian) |
| 64 | + hamiltonian_dual_sparse = get_sparse_operator(hamiltonian_dual_basis) |
| 65 | + |
| 66 | + hf_state = hartree_fock_state_jellium( |
| 67 | + grid, n_particles, spinless, plane_wave=True) |
| 68 | + hf_state_dual = hartree_fock_state_jellium( |
| 69 | + grid, n_particles, spinless, plane_wave=False) |
| 70 | + |
| 71 | + E_HF_plane_wave = expectation(hamiltonian_sparse, hf_state) |
| 72 | + E_HF_dual = expectation(hamiltonian_dual_sparse, hf_state_dual) |
| 73 | + |
| 74 | + self.assertAlmostEqual(E_HF_dual, E_HF_plane_wave) |
| 75 | + |
| 76 | + def test_hf_state_plane_wave_basis_lowest_single_determinant_state(self): |
| 77 | + grid_length = 7 |
| 78 | + dimension = 1 |
| 79 | + spinless = True |
| 80 | + n_particles = 4 |
| 81 | + length_scale = 2.0 |
| 82 | + |
| 83 | + grid = Grid(dimension, grid_length, length_scale) |
| 84 | + hamiltonian = jellium_model(grid, spinless) |
| 85 | + hamiltonian_sparse = get_sparse_operator(hamiltonian) |
| 86 | + |
| 87 | + hf_state = hartree_fock_state_jellium(grid, n_particles, |
| 88 | + spinless, plane_wave=True) |
| 89 | + |
| 90 | + HF_energy = expectation(hamiltonian_sparse, hf_state) |
| 91 | + |
| 92 | + for occupied_orbitals in permutations( |
| 93 | + [1] * n_particles + [0] * (grid_length - n_particles)): |
| 94 | + state_index = numpy.sum(2 ** numpy.array(occupied_orbitals)) |
| 95 | + HF_competitor = csr_matrix(([1.0], ([state_index], [0])), |
| 96 | + shape=(2 ** grid_length, 1)) |
| 97 | + |
| 98 | + self.assertLessEqual( |
| 99 | + HF_energy, expectation(hamiltonian_sparse, HF_competitor)) |
0 commit comments