tequilahub / tequila

A High-Level Abstraction Framework for Quantum Algorithms
MIT License
369 stars 103 forks source link

orbital_energies does not exist with PySCF backend #214

Closed jjgoings closed 2 years ago

jjgoings commented 2 years ago
import tequila as tq
geomstring="H 0.0 0.0 0.0\n Li 0.0 0.0 1.8"
mol = tq.Molecule(geometry=geomstring, backend='pyscf', basis_set='sto3g')
print("orb energies: ", mol.molecule.orbital_energies)

gives

orb energies:  None

which causes problems downstream, say doing

U = mol.make_ansatz(name="UCCSD")

which then yields the error

Traceback (most recent call last):
  File "test.py", line 18, in <module>
    U = mol.make_ansatz(name="UCCSD")
  File "/X/miniconda3/lib/python3.8/site-packages/tequila/quantumchemistry/qc_base.py", line 1283, in make_ansatz
    return self.make_uccsd_ansatz(*args, **kwargs)
  File "/X/miniconda3/lib/python3.8/site-packages/tequila/quantumchemistry/qc_base.py", line 1525, in make_uccsd_ansatz
    amplitudes = self.compute_mp2_amplitudes()
  File "/X/miniconda3/lib/python3.8/site-packages/tequila/quantumchemistry/qc_base.py", line 1672, in compute_mp2_amplitudes
    ei = fij[:nocc]
TypeError: 'NoneType' object is not subscriptable
kottmanj commented 2 years ago

Thank you for pointing that out in detail. The pyscf interface was added later and not all features are there yet (kind of forgot about this).

I added a hotfix. Hopefully, that solves most of those issues. If not please let me know.

Issues are being fixed in PR#215 will be in devel branch for a while until the next official version update.

If you want to avoid it in the current version you'll unfortunately need to use psi4 as backend.

jjgoings commented 2 years ago

Hey, thanks. Looks like that fixed part of it! But now I run into another issue:

import tequila as tq
geomstring="H 0.0 0.0 0.0\n Li 0.0 0.0 1.8"
mol = tq.Molecule(geometry=geomstring, backend='pyscf', basis_set='sto3g')
print("orb energies: ", mol.molecule.orbital_energies)

# get the qubit hamiltonian
H = mol.make_hamiltonian()

# get the ansatz (circuit)
U = mol.make_ansatz(name='UCCSD')

# define the expectation value
E = tq.ExpectationValue(H=H, U=U)

# minimize the expectation value
result = tq.minimize(E)

yields

converged SCF energy = -7.85001869716682
orb energies:  [-2.3544583  -0.26775989  0.07604109  0.16349906  0.16349906  0.48968509]
Traceback (most recent call last):
  File "test.py", line 16, in <module>
    result = tq.minimize(E)
  File "/X/miniconda3/lib/python3.8/site-packages/tequila/optimizers/__init__.py", line 133, in minimize
    return v.minimize(
  File "/X/miniconda3/lib/python3.8/site-packages/tequila/optimizers/optimizer_scipy.py", line 426, in minimize
    return optimizer(objective=objective,
  File "/X/miniconda3/lib/python3.8/site-packages/tequila/optimizers/optimizer_scipy.py", line 149, in __call__
    param_keys, param_values = zip(*active_angles.items())
ValueError: not enough values to unpack (expected 2, got 0)
kottmanj commented 2 years ago

You were faster than me :-) I was still fiddling with the defaults that I changed from mp2 to zero-guess (but had to make things consistent, so that zero-guess would not lead to the elimination of all operators later). It should work now.

FYI: MP2 pre-screening for UCCSD is now deactivated by default. if you want to use MP2-Prescreening call it like this: make_ansatz(name="UCCSD", initial_amplitudes="mp2")

jjgoings commented 2 years ago

Haha, sorry about that :) LGTM now, I'll open a new issue if I find anything else. Thanks for all your work on this project -- I really like it!