def test_diagonal(): """Test some of the functions in Diagonal.""" bad_diag = np.zeros((5, 5), dtype=np.complex128) with pytest.raises(ValueError): diagonal_hamiltonian.Diagonal(bad_diag) diag = np.zeros((5, ), dtype=np.complex128) test = diagonal_hamiltonian.Diagonal(diag) assert test.dim() == 5 assert test.rank() == 2
def test_apply_diagonal(self): wfn = Wavefunction([[2, 0, 2]]) wfn.set_wfn(strategy='random') data = numpy.random.rand(2) hamil = diagonal_hamiltonian.Diagonal(data) out1 = wfn._apply_diagonal(hamil) fac = 0.5 hamil = diagonal_hamiltonian.Diagonal(data, e_0=fac) out2 = wfn._apply_diagonal(hamil) out2.ax_plus_y(-fac, wfn) self.assertTrue((out1 - out2).norm() < 1.0e-8)
def get_diagonal_hamiltonian(hdiag: 'numpy.ndarray', e_0: complex = 0. + 0.j ) -> 'diagonal_hamiltonian.Diagonal': """Initialize a diagonal hamiltonian Args: hdiag (numpy.ndarray) - diagonal elements e_0 (complex) - scalar part of the Hamiltonian """ return diagonal_hamiltonian.Diagonal(hdiag, e_0=e_0)
def test_apply_number(self): norb = 4 test = numpy.random.rand(norb, norb) diag = numpy.random.rand(norb * 2) diag2 = copy.deepcopy(diag) e_0 = 0 for i in range(norb): e_0 += diag[i + norb] diag2[i + norb] = -diag[i + norb] hamil = diagonal_hamiltonian.Diagonal(diag2, e_0=e_0) hamil._conserve_number = False wfn = Wavefunction([[4, 2, norb]], broken=['number']) wfn.set_wfn(strategy='from_data', raw_data={(4, 2): test}) out1 = wfn.apply(hamil) hamil = diagonal_hamiltonian.Diagonal(diag) wfn = Wavefunction([[4, 2, norb]]) wfn.set_wfn(strategy='from_data', raw_data={(4, 2): test}) out2 = wfn.apply(hamil) self.assertTrue( numpy.allclose(out1._civec[(4, 2)].coeff, out2._civec[(4, 2)].coeff))
def process_rank2_matrix(mat: numpy.ndarray, norb: int, e_0: complex = 0. + 0.j) -> 'hamiltonian.Hamiltonian': """Look at the structure of the (1, 0) component of the one body matrix and determine the symmetries. Args: mat (numpy.ndarray) - input matrix to be processed norb (int) - the number of orbitals in the system e_0 (copmlex) - scalar part of the Hamiltonian Returns: (Hamiltonian) - resulting Hamiltonian """ if not numpy.allclose(mat, mat.conj().T): raise ValueError('Input matrix is not Hermitian') diagonal = True for i in range(1, max(norb, mat.shape[0])): for j in range(0, i): if mat[i, j] != 0. + 0.j: diagonal = False break if diagonal: return diagonal_hamiltonian.Diagonal(mat.diagonal(), e_0=e_0) if mat[norb:2 * norb, :norb].any(): return gso_hamiltonian.GSOHamiltonian(tuple([mat]), e_0=e_0) if numpy.allclose(mat[:norb, :norb], mat[norb:, norb:]): return restricted_hamiltonian.RestrictedHamiltonian(tuple([mat]), e_0=e_0) spin_mat = numpy.zeros((norb, 2 * norb), dtype=mat.dtype) spin_mat[:, :norb] = mat[:norb, :norb] spin_mat[:, norb:2 * norb] = mat[norb:, norb:] return sso_hamiltonian.SSOHamiltonian(tuple([spin_mat]), e_0=e_0)