コード例 #1
0
ファイル: test_h2.py プロジェクト: NLESC-JCER/QMC
class TestH2(unittest.TestCase):
    def setUp(self):

        # optimal parameters
        self.opt_r = 0.69  # the two h are at +0.69 and -0.69
        self.opt_sigma = 1.24

        # molecule
        self.mol = Molecule(atom='H 0 0 -0.69; H 0 0 0.69',
                            unit='bohr',
                            calculator='pyscf',
                            basis='sto-3g')

        # wave function
        self.wf = Orbital(self.mol,
                          kinetic='auto',
                          configs='single(2,2)',
                          use_jastrow=True)

        # sampler
        self.sampler = Metropolis(nwalkers=1000,
                                  nstep=2000,
                                  step_size=0.5,
                                  ndim=self.wf.ndim,
                                  nelec=self.wf.nelec,
                                  init=self.mol.domain('normal'),
                                  move={
                                      'type': 'all-elec',
                                      'proba': 'normal'
                                  })

        self.hmc_sampler = Hamiltonian(nwalkers=100,
                                       nstep=200,
                                       step_size=0.1,
                                       ndim=self.wf.ndim,
                                       nelec=self.wf.nelec,
                                       init=self.mol.domain('normal'))

        # optimizer
        self.opt = optim.Adam(self.wf.parameters(), lr=0.01)

        # solver
        self.solver = SolverOrbital(wf=self.wf,
                                    sampler=self.sampler,
                                    optimizer=self.opt)

        # ground state energy
        self.ground_state_energy = -1.16

        # ground state pos
        self.ground_state_pos = 0.69

    def test_single_point(self):

        self.solver.wf.ao.atom_coords[0, 2] = -self.ground_state_pos
        self.solver.wf.ao.atom_coords[1, 2] = self.ground_state_pos
        self.solver.sampler = self.sampler

        # sample and compute observables
        _, e, v = self.solver.single_point()

        print('Energy   :', e)
        print('Variance :', v)

        # assert(e>self.ground_state_energy and e<-1.)
        assert (e > 2 * self.ground_state_energy and e < 0.)
        assert (v > 0 and v < 5.)

    def test_single_point_hmc(self):

        self.solver.wf.ao.atom_coords[0, 2] = -self.ground_state_pos
        self.solver.wf.ao.atom_coords[1, 2] = self.ground_state_pos
        self.solver.sampler = self.hmc_sampler

        # sample and compute observables
        _, e, v = self.solver.single_point()

        print('Energy   :', e)
        print('Variance :', v)

        # assert(e>self.ground_state_energy and e<-1.)
        assert (e > 2 * self.ground_state_energy and e < 0.)
        assert (v > 0 and v < 5.)

    def test_geo_opt(self):

        self.solver.wf.ao.atom_coords[0, 2].data = torch.tensor(-0.37)
        self.solver.wf.ao.atom_coords[1, 2].data = torch.tensor(0.37)

        self.solver.configure(task='geo_opt')
        self.solver.observable(['local_energy', 'atomic_distances'])
        self.solver.run(50, loss='energy')

        # load the best model
        best_model = torch.load('model.pth')
        self.solver.wf.load_state_dict(best_model['model_state_dict'])
        self.solver.wf.eval()

        # sample and compute variables
        _, e, v = self.solver.single_point()
        e = e.data.numpy()
        v = v.data.numpy()

        # it might be too much to assert with the ground state energy
        assert (e > 2 * self.ground_state_energy and e < 0.)
        assert (v > 0 and v < 2.)
コード例 #2
0
#mol = Molecule(atom='H 0 0 -0.37; H 0 0 0.37', basis_type='gto', basis='sto-3g')

# define the wave function
wf = OrbitalH2(mol)

#sampler
sampler = Metropolis(nwalkers=1000,
                     nstep=1000,
                     step_size=0.5,
                     ndim=wf.ndim,
                     nelec=wf.nelec,
                     move='one')

# optimizer
opt = Adam(wf.parameters(), lr=0.01)

# solver
solver = SolverOrbital(wf=wf, sampler=sampler, optimizer=opt)
#solver.single_point()

# plot the molecule
#plot_molecule(solver)

# optimize the geometry
solver.configure(task='geo_opt')
solver.observable(['local_energy', 'atomic_distances'])
solver.run(5, loss='energy')

# plot the data
plot_observable(solver.obs_dict, e0=-1.16)
コード例 #3
0
ファイル: helium.py プロジェクト: NLESC-JCER/QMC
# solver
solver = SolverOrbital(wf=wf, sampler=sampler, optimizer=opt, scheduler=None)

if 1:
    pos, e, v = solver.single_point(ntherm=-1, ndecor=100)
    # eloc = solver.wf.local_energy(pos)
    # plt.hist(eloc.detach().numpy(), bins=50)
    # plt.show()

    # pos = solver.sample(ntherm=0, ndecor=10)
    # obs = solver.sampling_traj(pos)
    # plot_energy(obs, e0=-8.)

if 0:

    solver.configure(task='wf_opt', freeze=['ao', 'mo'])
    solver.observable(['local_energy'])

    solver.initial_sampling(ntherm=1000, ndecor=100)
    solver.resampling(nstep=25,
                      step_size=0.2,
                      resample_from_last=True,
                      resample_every=1,
                      tqdm=False)

    solver.ortho_mo = False

    data = solver.run(50,
                      batchsize=None,
                      loss='energy',
                      grad='manual',
コード例 #4
0
                     nelec=wf.nelec,
                     init=mol.domain('normal'),
                     move={
                         'type': 'all-elec',
                         'proba': 'normal'
                     })

# optimizer
opt = Adam(wf.parameters(), lr=0.01)

# solver
solver = SolverOrbital(wf=wf, sampler=sampler, optimizer=None)
pos, _, _ = solver.single_point()

# pos = solver.sample(ntherm=0, ndecor=10)
# obs = solver.sampling_traj(pos)
# plot_energy(obs, e0=-1.16)

# optimize the wave function
solver.configure(task='wf_opt', freeze=['mo', 'bas_exp'])
solver.observable(['local_energy'])
solver.run(5, loss='energy')

# # optimize the geometry
# solver.configure(task='geo_opt')
# solver.observable(['local_energy','atomic_distances'])
# solver.run(5,loss='energy')

# plot the data
# plot_energy(solver.obs_dict, e0=-1.16)