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!

Hi @Stephan, I have a related issue. I am trying to make a initial state but for a system with an specific activate space, lets say 4 active electrons and 4 active orbitals. There is a way to “initialize” the state using CASCI or CASSCF? I know that inport_state is not supporting those methods, but can you give me a hint on how to do it?

Best,
Raúl

Thank you for your question @Raul_Guerrero !

I’m tagging @Stepan so he can get the notification and respond next week :smiley:

Great! Thank you very much. :slight_smile :wink:

Hi @Raul_Guerrero ,

Stepan is writing up an answer with a code example :smiley: . We’ll get back to you with this answer in the next couple of days!

1 Like

Hey @Raul_Guerrero! Of course, no problem – below I attach a simple script that should cover your use case. I used the specific example of N2 in the active space with (4e, 4o), but you can adjust as needed.

The script first contains the essential function that allows to construct the wavefunction from the PySCF’s CASCI solver object – the _casci_state function – and then it includes a short example of its usage. The final wf_statevector object is a normal PennyLane statevector of length 2^(num_qubits) that you can plug directly into qml.StatePrep to initialize your circuit.

There are two possible uses you might have in mind here:

  1. you might be interest in preparing an initial state from CASCI within the active space
  2. you might want the state within the full space, but prepared with the CASCI method (i.e. built inside the active space then padded back to the full space size)

The current script is set up for case (1). As you can see from the comments on lines 88-89, it only requires minimal changes to set it up for case (2).

This script comes almost directly from our open-source Xanadu repository tailored for preparing initial states, Overlapper. If you are interested in initial state preparation for chemical systems, I would recommend you take a look at the repo and the examples, you might find other useful code examples! :smiling_face:

Let me know if you have any other questions!

minimal_example_casci_init_state.py (4.3 KB)

1 Like

Hi @Stepan, wow, that’s awesome. Thank you so much, I really appreciate it. I will play with it and let you know how it goes.

Thank you also @CatalinaAlbornoz for your assistance.

:nerd_face:

Best!

1 Like

Hi @Stepan and @CatalinaAlbornoz, I implemented the code and it is working perfectly.
Thank you very much for your valuable help.

Very best,
Raúl

That’s great to hear @Raul_Guerrero !
Enjoy using PennyLane!