NLESC-JCER / QMCTorch

Pytorch Implementation of Real Space Quantum Monte Carlo Simulations of Molecular Systems
https://qmctorch.readthedocs.io/
Apache License 2.0
23 stars 2 forks source link

Type error when running one of the tutorials #154

Closed tonnylou44853 closed 8 months ago

tonnylou44853 commented 8 months ago

Hi! I was trying to run one of the tutorial examples on my laptop (MacOS) and noticed a bug:

Python 3.10.11 | packaged by conda-forge | (main, May 10 2023, 19:01:19) [Clang 14.0.6 ]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.14.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from torch import optim
   ...: from qmctorch.scf import Molecule
   ...: from qmctorch.wavefunction import SlaterJastrow
   ...: from qmctorch.solver import Solver
   ...: from qmctorch.sampler import Metropolis
   ...: from qmctorch.utils import set_torch_double_precision
   ...: from qmctorch.utils import (plot_energy, plot_data)
   ...: set_torch_double_precision()
INFO:QMCTorch|  ____    __  ______________             _
INFO:QMCTorch| / __ \  /  |/  / ___/_  __/__  ________/ /
INFO:QMCTorch|/ /_/ / / /|_/ / /__  / / / _ \/ __/ __/ _ \
INFO:QMCTorch|\___\_\/_/  /_/\___/ /_/  \___/_/  \__/_//_/

In [2]: mol = Molecule(load='./hdf5/H2_adf_dzp.hdf5')
INFO:QMCTorch|
INFO:QMCTorch| SCF Calculation
INFO:QMCTorch|  Loading data from ./hdf5/H2_adf_dzp.hdf5

In [3]: wf = SlaterJastrow(mol, configs='single_double(2,2)')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[3], line 1
----> 1 wf = SlaterJastrow(mol, configs='single_double(2,2)')

File ~/Projects/Work/JOSS-reviews/QMCTorch/qmctorch/wavefunction/slater_jastrow.py:53, in SlaterJastrow.__init__(self, mol, configs, kinetic, jastrow_kernel, jastrow_kernel_kwargs, cuda, include_all_mo)
     13 def __init__(self, mol, configs='ground_state',
     14              kinetic='jacobi',
     15              jastrow_kernel=PadeJastrowKernel,
     16              jastrow_kernel_kwargs={},
     17              cuda=False,
     18              include_all_mo=True):
     19     """Slater Jastrow wave function with electron-electron Jastrow factor
     20
     21     .. math::
   (...)
     50         >>> wf = SlaterJastrow(mol, configs='cas(2,2)')
     51     """
---> 53     super().__init__(mol, configs, kinetic, cuda, include_all_mo)
     55     # process the Jastrow
     56     if jastrow_kernel is not None:

File ~/Projects/Work/JOSS-reviews/QMCTorch/qmctorch/wavefunction/slater_jastrow_base.py:67, in SlaterJastrowBase.__init__(self, mol, configs, kinetic, cuda, include_all_mo)
     64 self.highest_occ_mo = torch.stack(self.configs).max()+1
     66 # define the atomic orbital layer
---> 67 self.ao = AtomicOrbitals(mol, cuda)
     69 # define the mo layer
     70 self.include_all_mo = include_all_mo

File ~/Projects/Work/JOSS-reviews/QMCTorch/qmctorch/wavefunction/orbitals/atomic_orbitals.py:90, in AtomicOrbitals.__init__(self, mol, cuda)
     88 else:
     89     with torch.no_grad():
---> 90         self.norm_cst = atomic_orbital_norm(
     91             mol.basis).type(dtype)
     93 self.cuda = cuda
     94 self.device = torch.device('cpu')

File ~/Projects/Work/JOSS-reviews/QMCTorch/qmctorch/wavefunction/orbitals/norm_orbital.py:35, in atomic_orbital_norm(basis)
     32 elif basis.harmonics_type == 'cart':
     34     if basis.radial_type.startswith('sto'):
---> 35         return norm_slater_cartesian(
     36             basis.bas_kx,
     37             basis.bas_ky,
     38             basis.bas_kz,
     39             basis.bas_kr,
     40             basis.bas_exp)
     42     elif basis.radial_type.startswith('gto'):
     43         return norm_gaussian_cartesian(
     44             basis.bas_kx, basis.bas_ky, basis.bas_kz, basis.bas_exp)

File ~/Projects/Work/JOSS-reviews/QMCTorch/qmctorch/wavefunction/orbitals/norm_orbital.py:112, in norm_slater_cartesian(a, b, c, n, exp)
    108 from scipy.special import factorial2 as f2
    110 lvals = a + b + c + n + 1.
--> 112 lfact = torch.as_tensor([np.math.factorial(2 * i)
    113                          for i in lvals]).type(torch.get_default_dtype())
    115 prefact = 4 * np.pi * lfact / ((2 * exp)**(2 * lvals + 1))
    117 num = torch.as_tensor(f2(2 * a.astype('int') - 1) *
    118                       f2(2 * b.astype('int') - 1) *
    119                       f2(2 * c.astype('int') - 1)
    120                       ).type(torch.get_default_dtype())

File ~/Projects/Work/JOSS-reviews/QMCTorch/qmctorch/wavefunction/orbitals/norm_orbital.py:112, in <listcomp>(.0)
    108 from scipy.special import factorial2 as f2
    110 lvals = a + b + c + n + 1.
--> 112 lfact = torch.as_tensor([np.math.factorial(2 * i)
    113                          for i in lvals]).type(torch.get_default_dtype())
    115 prefact = 4 * np.pi * lfact / ((2 * exp)**(2 * lvals + 1))
    117 num = torch.as_tensor(f2(2 * a.astype('int') - 1) *
    118                       f2(2 * b.astype('int') - 1) *
    119                       f2(2 * c.astype('int') - 1)
    120                       ).type(torch.get_default_dtype())

TypeError: 'numpy.float64' object cannot be interpreted as an integer

However, there is no error when I tried running it on a Linux desktop. I suspect this error is due to an internal change in Python/Numpy/PyTorch.

The fix that works for me is to change the following:

lfact = torch.as_tensor([np.math.factorial(int(2 * i)) for i in 
                         lvals]).type(torch.get_default_dtype())

on line 113 in https://github.com/NLESC-JCER/QMCTorch/blob/bfabcb030ab6147897d26e6730650de9d8900007/qmctorch/wavefunction/orbitals/norm_orbital.py#L113

NicoRenaud commented 8 months ago

Thanks @tonnylou44853 ! similar issue occur on certain install ... You're right it's safer to cast to int in the function. I' ve made the change in a separate branch and I will merge that as soon as the test are passing

NicoRenaud commented 8 months ago

It's all merged now thanks @tonnylou44853