Chem.import_state doesn't work for dmrg state

Hi!
I was trying to use the qml.qchem.inport() to convert dmrg result from block2 to a state vector. But when I try to run the example:

mol = gto.M(atom=[[‘H’, (0, 0, 0)], [‘H’, (0,0,0.71)]], basis=‘sto3g’)
mf = scf.RHF(mol).run()
mc = mcscf.CASCI(mf, 2, 2)
ncas, n_elec, spin, ecore, h1e, g2e, orb_sym = itg.get_rhf_integrals(mf, mc.ncore, mc.ncas, g2e_symm=8)
driver = DMRGDriver(scratch=“./tmp”, symm_type=SymmetryTypes.SU2)
driver.initialize_system(n_sites=ncas, n_elec=n_elec, spin=spin, orb_sym=orb_sym)
mpo = driver.get_qc_mpo(h1e=h1e, g2e=g2e, ecore=ecore)
ket = driver.get_random_mps(tag=“GS”)
wavefunction = driver.get_csf_coefficients(ket)
wf_dmrg = import_state(wavefunction, tol=1e-1)
print(wf_dmrg)

It returns the following error message:


ValueError Traceback (most recent call last)
Cell In[16], line 10
8 ket = driver.get_random_mps(tag=“GS”)
9 wavefunction = driver.get_csf_coefficients(ket)
—> 10 wf_dmrg = import_state(wavefunction, tol=1e-1)
11 print(wf_dmrg)

File ~/anaconda3/lib/python3.11/site-packages/pennylane/qchem/convert.py:615, in import_state(solver, tol)
613 wf_dict = _dmrg_state(solver, tol=tol)
614 else:
→ 615 raise ValueError(
616 “For tuple input, the supported objects are”
617 " tuple(list[str], array[float]) for SHCI calculations with Dice library and "
618 “tuple(list[int], array[float]) for DMRG calculations with the Block2 library.”
619 )
620 else:
621 raise ValueError(
622 “The supported objects are RCISD, UCISD, RCCSD, and UCCSD for restricted and”
623 " unrestricted configuration interaction and coupled cluster calculations, and"
624 " tuple(list[str], array[float]) for SHCI calculations with Dice library and "
625 “tuple(list[int], array[float]) for DMRG calculations with the Block2 library.”
626 )

ValueError: For tuple input, the supported objects are tuple(list[str], array[float]) for SHCI calculations with Dice library and tuple(list[int], array[float]) for DMRG calculations with the Block2 library.

I wonder if it’s because it didn’t use it correctly or there is some problem in the code? Thanks!

I use the following way to make the import_state() works but I haven’t check if it gives the right result.

psi_state = import_state((wavefunction[0].astype(int).tolist(), wavefunction[1]))

Hi @funnybear and welcome to the PennyLane community! We are very excited to see your interest in the initial state functionality.

To your point, your solution is exactly correct! As the error message indicates, the issue is with the object type of wavefunction. The problem is that the Block2 function get_csf_coefficients returns two objects, a numpy array of determinants and a numpy array of coeffs. But the type check inside PennyLane’s import_state function would like the first object to be a list rather than an array. So exactly as you write, it is enough to change the type of the first object. For example, in our Initial State Preparation demo, this is how we do it – see especially the third line:

# post-process the MPS to get an initial state
dets, coeffs = driver.get_csf_coefficients(ket, iprint=0)
dets = dets.tolist()
wf_dmrg = import_state((dets, coeffs), tol=1e-1)

By the way, in a recent PR we have just improved the initial state functionality by addressing a small sign convention issue. If you want to use the most up-to-date version – and see the new code examples – take a look at the master version of PennyLane.

And if you would like a comprehensive intro to the initial state functionality, check out the complete Initial State Preparation demo here for many more useful tips and tricks.

Let us know if you have any more questions, we’re always here to help!