Exemplo n.º 1
0
    def test_lih_dipole(self):
        """Calculate the LiH dipole
        """
        norb = 6
        nalpha = 2
        nbeta = 2
        nele = nalpha + nbeta
        au2debye = 2.5417464157449032

        dip_ref, dip_mat, lih_ground = build_lih_data.build_lih_data('dipole')

        wfn = Wavefunction([[nele, nalpha - nbeta, norb]])
        wfn.set_wfn(strategy='from_data',
                    raw_data={(nele, nalpha - nbeta): lih_ground})

        hwfn_x = wfn._apply_array(tuple([dip_mat[0]]), e_0=0. + 0.j)
        hwfn_y = wfn._apply_array(tuple([dip_mat[1]]), e_0=0. + 0.j)
        hwfn_z = wfn._apply_array(tuple([dip_mat[2]]), e_0=0. + 0.j)
        calc_dip = numpy.array([fqe.vdot(wfn, hwfn_x).real, \
                              fqe.vdot(wfn, hwfn_y).real, \
                              fqe.vdot(wfn, hwfn_z).real])*au2debye
        for card in range(3):
            with self.subTest(dip=card):
                err = abs(calc_dip[card] - dip_ref[card])
                self.assertTrue(err < 1.e-5)
Exemplo n.º 2
0
    def test_lih_energy(self):
        """Checking total energy with LiH
        """
        eref = -8.877719570384043
        norb = 6
        nalpha = 2
        nbeta = 2
        nele = nalpha + nbeta
        h1e, h2e, lih_ground = build_lih_data.build_lih_data('energy')

        elec_hamil = general_hamiltonian.General((h1e, h2e))
        wfn = Wavefunction([[nele, nalpha - nbeta, norb]])
        wfn.set_wfn(strategy='from_data',
                    raw_data={(nele, nalpha - nbeta): lih_ground})

        ecalc = wfn.expectationValue(elec_hamil)
        self.assertAlmostEqual(eref, ecalc, places=8)
Exemplo n.º 3
0
    def test_hartree_fock_init(self):
        h1e, h2e, _ = build_lih_data('energy')
        elec_hamil = get_restricted_hamiltonian((h1e, h2e))
        norb = 6
        nalpha = 2
        nbeta = 2
        wfn = Wavefunction([[nalpha + nbeta, nalpha - nbeta, norb]])
        wfn.print_wfn()
        wfn.set_wfn(strategy='hartree-fock')
        wfn.print_wfn()
        self.assertEqual(wfn.expectationValue(elec_hamil), -8.857341498221992)
        hf_wf = numpy.zeros((int(binom(norb, 2)), int(binom(norb, 2))))
        hf_wf[0, 0] = 1.
        self.assertTrue(numpy.allclose(wfn.get_coeff((4, 0)), hf_wf))

        wfn = Wavefunction([[nalpha + nbeta, nalpha - nbeta, norb],
                            [nalpha + nbeta, 2, norb]])
        self.assertRaises(ValueError, wfn.set_wfn, strategy='hartree-fock')
    def test_moleculardata_to_restricted_hamiltonian(self):
        """
        Convert an OpenFermion MolecularData object to a
        fqe.RestrictedHamiltonian
        """
        h1e, h2e, _ = build_lih_data('energy')
        # dummy geometry
        geometry = [['Li', [0, 0, 0], ['H', [0, 0, 1.4]]]]
        charge = 0
        multiplicity = 1
        molecule = MolecularData(geometry=geometry,
                                 basis='sto-3g',
                                 charge=charge,
                                 multiplicity=multiplicity)
        molecule.one_body_integrals = h1e
        molecule.two_body_integrals = numpy.einsum('ijlk', -2 * h2e)
        molecule.nuclear_repulsion = 0

        restricted_ham = openfermion_utils.molecular_data_to_restricted_fqe_op(
            molecule=molecule)
        h1e_test, h2e_test = restricted_ham.tensors()
        self.assertTrue(numpy.allclose(h1e_test, h1e))
        self.assertTrue(numpy.allclose(h2e_test, h2e))
Exemplo n.º 5
0
    def test_lih_ops(self):
        """Check the value of the operators on LiH
        """
        norb = 6
        nalpha = 2
        nbeta = 2
        nele = nalpha + nbeta

        _, _, lih_ground = build_lih_data.build_lih_data('energy')

        wfn = Wavefunction([[nele, nalpha - nbeta, norb]])
        wfn.set_wfn(strategy='from_data',
                    raw_data={(nele, nalpha - nbeta): lih_ground})

        operator = S2Operator()
        self.assertAlmostEqual(wfn.expectationValue(operator), 0. + 0.j)
        operator = SzOperator()
        self.assertAlmostEqual(wfn.expectationValue(operator), 0. + 0.j)
        operator = TimeReversalOp()
        self.assertAlmostEqual(wfn.expectationValue(operator), 1. + 0.j)
        operator = NumberOperator()
        self.assertAlmostEqual(wfn.expectationValue(operator), 4. + 0.j)
        self.assertAlmostEqual(wfn.expectationValue(operator, wfn), 4. + 0.j)
Exemplo n.º 6
0

# TODO: Delete or make unit test?
if __name__ == "__main__":
    from openfermion import FermionOperator
    import numpy
    import fqe
    from fqe.unittest_data import build_lih_data, build_hamiltonian

    numpy.set_printoptions(floatmode='fixed',
                           precision=6,
                           linewidth=80,
                           suppress=True)
    numpy.random.seed(seed=409)

    h1e, h2e, wfn = build_lih_data.build_lih_data('energy')
    lih_hamiltonian = fqe.get_restricted_hamiltonian(([  # type: ignore
        h1e, h2e
    ]))
    print(lih_hamiltonian._tensor)
    lihwfn = fqe.Wavefunction([[4, 0, 6]])
    lihwfn.set_wfn(strategy='from_data', raw_data={(4, 0): wfn})

    time = 0.01
    ops = FermionOperator('2^ 0', 3.0 - 1.j)
    ops += FermionOperator('0^ 2', 3.0 + 1.j)
    print(ops)
    sham = fqe.get_sparse_hamiltonian(ops, conserve_spin=False)
    evolved = fqe.time_evolve(lihwfn, time, sham)
    evolved.print_wfn()
    nbody_evol = lihwfn.apply_generated_unitary(time,
Exemplo n.º 7
0
                                 guess_vecs,
                                 nele=nele,
                                 sz=sz,
                                 norb=norb)
    return dl_w, dl_v


# TODO: Make this a unit test?
if __name__ == "__main__":
    eref = -8.877719570384043
    norb = 6
    nalpha = 2
    nbeta = 2
    sz = nalpha - nbeta
    nele = nalpha + nbeta
    h1e, h2e, lih_ground = build_lih_data("energy")
    h2e_zeros = np.zeros_like(h2e)
    elec_hamil = fqe.restricted_hamiltonian.RestrictedHamiltonian((h1e, h2e))
    wfn = fqe.Wavefunction([[nele, nalpha - nbeta, norb]])
    wfn.set_wfn(strategy="from_data",
                raw_data={(nele, nalpha - nbeta): lih_ground})
    graph = wfn.sector((4, 0)).get_fcigraph()
    ecalc = wfn.expectationValue(elec_hamil)

    # Generate Guess Vecs for Davidson-Liu
    guess_vec1_coeffs = np.zeros((graph.lena(), graph.lenb()))
    guess_vec2_coeffs = np.zeros((graph.lena(), graph.lenb()))
    alpha_hf = fqe.util.init_bitstring_groundstate(2)
    beta_hf = fqe.util.init_bitstring_groundstate(2)
    alpha_hf_idx = fqe.util.init_bitstring_groundstate(2)
    beta_hf_idx = fqe.util.init_bitstring_groundstate(2)