block-hczhai / pyblock3-preview

pyblock3: an efficient python block-sparse tensor library
GNU General Public License v3.0
21 stars 12 forks source link

Error when using to_dense() #11

Closed schusteritsch closed 5 months ago

schusteritsch commented 5 months ago

I have been trying to recover the dense tensors from sparse tensors by using to_dense(). This follows on from what was described here https://github.com/block-hczhai/block2-preview/issues/64, i.e. how to get the MPS in tensor form in block2, and the answer recommends converting to block3.

I have used essentially the same code as in the above issue:

import numpy as np
from pyblock2._pyscf.ao2mo import integrals as itg
from pyblock2.driver.core import DMRGDriver, SymmetryTypes
from pyblock3.block2.io import MPSTools, MPOTools

from pyscf import gto, scf

mol = gto.M(atom="N 0 0 0; N 0 0 1.1", basis="sto3g", symmetry="d2h", verbose=9)
mf = scf.RHF(mol).run(conv_tol=1E-14)
ncas, n_elec, spin, ecore, h1e, g2e, orb_sym = itg.get_rhf_integrals(mf,
    ncore=0, ncas=None, g2e_symm=8)

driver = DMRGDriver(scratch="./tmp", symm_type=SymmetryTypes.SZ, n_threads=4)
driver.initialize_system(n_sites=ncas, n_elec=n_elec, spin=spin, orb_sym=orb_sym)

print("number of electrons:", n_elec)
print("number of orbitals:", ncas)
print("spin:", spin)

mpo = driver.get_qc_mpo(h1e=h1e, g2e=g2e, ecore=ecore, add_ident=False, iprint=1)

ket = driver.get_random_mps(tag="GS", bond_dim=250, nroots=1)
bond_dims = [250] * 4 + [500] * 4
noises = [1e-4] * 4 + [1e-5] * 4 + [0]
thrds = [1e-10] * 8
energies = driver.dmrg(mpo, ket, n_sweeps=5, bond_dims=bond_dims,
    noises=noises, thrds=thrds, iprint=1)
print('DMRG energy = %20.15f' % energies)

ket = driver.adjust_mps(ket, dot=1)[0]
print(ket.n_sites, ket.canonical_form)

pyket = MPSTools.from_block2(ket)

I then try to get the tensors for each site. This works for sites 0 to 2, so the following for instance gives me an expected output:

pyket.tensors[2].to_dense()

However for sites >2 this gives an error about attribute 'infos':

>>> pyket.tensors[3].to_dense()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "removed/lib/python3.9/site-packages/pyblock3/algebra/core.py", line 1400, in to_dense
    return self.to_sliceable(infos=infos).to_dense()
  File "removed/lib/python3.9/site-packages/pyblock3/algebra/core.py", line 412, in to_dense
    r = np.zeros(sh, dtype=self.dtype)
  File "removed/lib/python3.9/site-packages/pyblock3/algebra/core.py", line 390, in dtype
    for v in self.ravel():
  File "removed/lib/python3.9/site-packages/pyblock3/algebra/core.py", line 304, in __getitem__
    r.infos = list(self.infos)
AttributeError: 'NoneType' object has no attribute 'infos'
>>> 

I've tried this both with the up to date pip version (pyblock3==0.2.8) as well as the current version on github (pyblock3==0.2.9rc4).

Any suggestions? Not sure if I did something wrong.

hczhai commented 5 months ago

Thanks for reporting the problem. This is now fixed in https://github.com/block-hczhai/pyblock3-preview/commit/dcc87b7ced166aa66a2b5ae372679f858e5564c5. You can retry after upgrading to pyblock3==0.2.9rc5.

schusteritsch commented 5 months ago

Great! Thanks for fixing this so quickly. Seems to work fine now, so I will close this.