-
Notifications
You must be signed in to change notification settings - Fork 209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Help with refactoring / migrating OO-VQE #1219
Comments
@mrossinek May I know where this Traceback (most recent call last):
Cell In[29], line 39
oovqe_result = oovqe.solve(driver)
Cell In[28], line 463 in solve
self._initialize_additional_parameters(driver)
Cell In[28], line 376 in _initialize_additional_parameters
self._set_operator_and_vqe(driver)
Cell In[28], line 319 in _set_operator_and_vqe
operator, aux_operators = self._do_transform(self._qmolecule, self._transformation)
Cell In[28], line 99 in _do_transform
self._hf_energy = qmolecule.hf_energy
AttributeError: 'ElectronicStructureProblem' object has no attribute 'hf_energy' def _set_operator_and_vqe(self, driver: BaseDriver):
""" Initializes the operators using provided driver of qmolecule."""
if not isinstance(self._transformation, FermionicOp):
raise QiskitError('OrbitalOptimizationVQE requires a FermionicTransformation.')
if not isinstance(driver, PySCFDriver):
raise QiskitError('OrbitalOptimizationVQE only works with PySCF Drivers.')
if self._qmolecule is None:
# in future, self._transformation.transform should return also qmolecule
# to avoid running the driver twice
self._qmolecule = driver.run()
operator, aux_operators = self._do_transform(self._qmolecule, self._transformation)
else:
operator, aux_operators = self._do_transform(self._qmolecule, self._transformation) |
Hi @buttercutter, please excuse the late reply. The I also saw your question here (#629 (comment)) and just noticed that I never replied. In fact, you can do orbital rotation with the PySCF plugin but you need to use the The existence of this That said, let me at least reply to some questions you raised here.
I cannot access this Google Colab link. If you have a specific code question, I suggest you upload a minimal Gist (not the massive Jupyter notebook of your first link). Alternatively, you can also embed a small Python script directly in a comment here.
The
What you are looking at here is code belonging to the The ooVQE implementation should follow the interface set forth by the |
I will deal with The following code is the use of I am having trouble loading the resulting orbital-rotated hamiltonian into qiskit-nature. Please advise. """# **Orbital rotation using pySCF CASSCF library**
In the context of the CASSCF (Complete Active Space Self-Consistent Field) method, "rotate_orb_cc" and "rotate_mo" are two different functions used to rotate the molecular orbitals (MOs) during the CASSCF optimization process.
"rotate_orb_cc" stands for "rotate orbitals using coupled-cluster theory". This function performs a rotation of the MOs using a coupled-cluster approach, which involves solving for the amplitudes of the excitation operators that describe the correlation effects beyond the Hartree-Fock approximation. This method is generally more computationally expensive but can lead to more accurate results.
On the other hand, "rotate_mo" simply rotates the MOs using a diagonalization method. This method is less computationally expensive but may be less accurate than the coupled-cluster approach.
In summary, the main difference between "rotate_orb_cc" and "rotate_mo" is the method used to rotate the MOs during the CASSCF optimization process. "rotate_orb_cc" uses a coupled-cluster approach, while "rotate_mo" uses a simpler diagonalization method.
Credit : https://github.com/qiskit-community/qiskit-nature/issues/1219
"""
from pyscf import gto, scf, mcscf
# Set up molecule
mol = gto.M(atom='H 0 0 0; F 0 0 1.1', basis='cc-pvdz')
# SCF calculation
mf = scf.RHF(mol)
mf.kernel()
# CASSCF calculation
mc = mcscf.CASSCF(mf, 4, 4)
mc.kernel()
"""After CASSCF calculation, get the arguments needed
rotate_orb_cc() needs:
mo: MO coefficients
fcivec: CI vector
fcasdm1: Spin-separated 1-particle RDM
fcasdm2: Spin-separated 2-particle RDM
eris: transformed electron repulsion integrals
which are generated from the CASSCF calculation. Passing those in allows rotate_orb_cc() to do the coupled-cluster orbital rotation.
"""
mo = mc.mo_coeff
fcivec = mc.ci
fcasdm1, fcasdm2 = mc.fcisolver.make_rdm12(fcivec, mc.ncas, mc.nelecas)
eris = mc.ao2mo(mo)
# Print MO coefficients before rotation
print('MO coefficients before:')
print(mc.mo_coeff)
# Rotate orbitals using coupled-cluster (rotate_orb_cc)
mc.rotate_orb_cc(mo, fcivec, fcasdm1, fcasdm2, eris)
# Print MO coefficients after rotation
print('MO coefficients after:')
print(mc.mo_coeff)
"""rotate_mo() needs:
mc.mo_coeff: Current MO coefficients
u: Rotation matrix
"""
# CASSCF calculation
mc = mcscf.CASSCF(mf, 4, 4)
mc.kernel()
# Get MO coefficients
mo_coeff = mc.mo_coeff
# Make CI-driven 1-RDM and 2-RDM
fcivec = mc.ci
casdm1, casdm2 = mc.fcisolver.make_rdm12(fcivec, mc.ncas, mc.nelecas)
# Pass as a tuple
rdm = (casdm1, casdm2)
# Compute orbital rotation gradient
g_orb = mc.get_grad(mo_coeff, rdm)
dx = g_orb[0]
# Get U matrix with the gradient
u = mc.update_rotate_matrix(dx)
# Rotate orbitals using diagonalization (rotate_mo)
mc.rotate_mo(mc.mo_coeff, u)
"""Here is one way to take the rotated CASSCF orbitals from pySCF and create a Hamiltonian file for use in Qiskit Nature:
1. After rotating the orbitals in pySCF, export the one- and two-electron integrals:
"""
# In pySCF
h1 = mc.get_hcore() # One-electron integrals
h2 = mc.get_h2eff(mo_coeff) # Two-electron integrals
"""2. Convert the integrals to Numpy arrays:"""
h1 = np.array(h1)
h2 = np.array(h2)
# Convert numpy arrays to lists
h1 = h1.tolist()
h2 = h2.tolist()
"""3. Create a Python dictionary containing the Hamiltonian parameters:"""
ham_dict = {
'one_body_integrals': h1,
'two_body_integrals': h2
}
"""4. Save the dictionary to a Hamiltonian file:"""
from qiskit_nature.second_q.hamiltonians import Hamiltonian
from qiskit_nature.second_q.operators import FermionicOp
from qiskit_nature.second_q.mappers import JordanWignerMapper
class MolecularHamiltonian(Hamiltonian):
def __init__(self, one_body_integrals, two_body_integrals, basis):
self.one_body_integrals = one_body_integrals
self.two_body_integrals = two_body_integrals
self.basis = basis
def second_q_op(self):
fo = FermionicOp()
for orbital1, integral in self.one_body_integrals.items():
fo += integral * (f'^{orbital1} a_{orbital1}')
for orbital1, orbital2, integral in self.two_body_integrals.items():
fo += integral * (f'^{orbital1} ^{orbital2} a_{orbital1} a_{orbital2}')
# Convert FermionicOp to SparseLabelOp using Jordan-Wigner transform
return JordanWignerMapper().transform(fo)
@property
def register_length(self):
max_orbital = max(orbital for orbital in self.one_body_integrals.keys())
max_orbital = max(max_orbital, *self.two_body_integrals.keys())
# Add 1 to account for zero orbital
return max_orbital + 1
# Build Hamiltonian from dict
hamiltonian = MolecularHamiltonian(
one_body_integrals=ham_dict['one_body_integrals'],
two_body_integrals=ham_dict['two_body_integrals'],
basis='sto-3g'
)
"""5. In Qiskit Nature, load the Hamiltonian file:"""
from qiskit_nature.second_q.drivers import PySCFDriver
driver = PySCFDriver(atom='H 0 0 0; F 0 0 1.1',
basis='sto-3g',
charge=0,
spin=0,
hamiltonian=hamiltonian)
# Set Hamiltonian property
driver.set_property(Energies([hamiltonian])) |
If you are initially working with PySCF directly, the That is exactly, what the plugin https://github.com/qiskit-community/qiskit-nature-pyscf has been developed for. You can see an example on how this works with Regarding your errors:
If all you want is to use a different set of orbitals (the fully rotated once from the result of a prior CASSCF calculation) you can do something similar to how it is done in the plugin repo: https://github.com/qiskit-community/qiskit-nature-pyscf/blob/4969f3b250dd02b9986d7ff6ea97d65ed7e0de9a/qiskit_nature_pyscf/qiskit_solver.py#L156-L183 I would like to take this moment to ask whether you are developing a purely Qiskit Nature-based implementation of the ooVQE algorithm which does not require PySCF? If not and you are only looking for support, I suggest that the quantum computing stackexchange is a better place to get support. The Github issue tracker is not a support system and should be used for bug reports, feature requests, or code design discussions. |
I can help with developing a purely Qiskit Nature-based implementation of the ooVQE algorithm which does not require PySCF. Let me know when I should start doing so for @mrossinek Looking at nvidia's cuda-quantum, their quantum chemistry code is still using Feel free to correct me if wrong / miss anything. |
No more transitions or class API changes are planned at the moment, so this should not block you any longer.
I do not see how this is relevant. If you want an ooVQE implementation to be merged into Qiskit Nature it should be an implementation of the Due to current priorities, I do not currently know of anyone who will be able or has the time to help you in case you get stuck. |
I am now working on refactoring / migrating OO-VQE code
I need some technical help on migrating
_do_transform()
code since it involves dipole and spin orbitalRelevant issues : #629 (review) , FermionicTransformation class replacement by QubitConverter class
The text was updated successfully, but these errors were encountered: