tequilahub / tequila

A High-Level Abstraction Framework for Quantum Algorithms
MIT License
362 stars 101 forks source link

Simplest chemistry example not working with Python 3.8 #285

Closed doomspec closed 1 year ago

doomspec commented 1 year ago
import tequila as tq

# define a molecule within an active space
active = {"a1": [1], "b1":[0]}
molecule = tq.quantumchemistry.Molecule(geometry="Li 0.0 0.0 0.0\nH 0.0 0.0 1.6", basis_set='6-31g', active_orbitals=active, transformation="bravyi-kitaev")

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

# create an k-UpCCGSD circuit of order k
U = molecule.make_upccgsd_ansatz(order=1, include_singles=True)

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

# compute reference energies
fci = molecule.compute_energy("fci")
cisd = molecule.compute_energy("detci", options={"detci__ex_level": 2})

# optimize
result = tq.minimize(objective=E, method="BFGS", initial_values=0.0)

print("VQE : {:+2.8}f".format(result.energy))
print("CISD: {:+2.8}f".format(cisd))
print("FCI : {:+2.8}f".format(fci))

It will emit error

Traceback (most recent call last):
  File "/Users/zijian/PycharmProjects/pythonProject/hello.py", line 5, in <module>
    molecule = tq.quantumchemistry.Molecule(geometry="Li 0.0 0.0 0.0\nH 0.0 0.0 1.6", basis_set='6-31g', active_orbitals=active, transformation="bravyi-kitaev")
  File "/Users/zijian/opt/miniconda3/envs/hamil/lib/python3.8/site-packages/tequila/quantumchemistry/__init__.py", line 138, in Molecule
    return INSTALLED_QCHEMISTRY_BACKENDS[backend.lower()](parameters=parameters, transformation=transformation, orbital_type=orbital_type,
  File "/Users/zijian/opt/miniconda3/envs/hamil/lib/python3.8/site-packages/tequila/quantumchemistry/pyscf_interface.py", line 72, in __init__
    super().__init__(parameters=parameters, transformation=transformation, *args, **kwargs)
  File "/Users/zijian/opt/miniconda3/envs/hamil/lib/python3.8/site-packages/tequila/quantumchemistry/qc_base.py", line 94, in __init__
    self.integral_manager = self.initialize_integral_manager(active_orbitals=active_orbitals,
  File "/Users/zijian/opt/miniconda3/envs/hamil/lib/python3.8/site-packages/tequila/quantumchemistry/qc_base.py", line 514, in initialize_integral_manager
    manager = IntegralManager(one_body_integrals=one_body_integrals, two_body_integrals=two_body_integrals,
  File "/Users/zijian/opt/miniconda3/envs/hamil/lib/python3.8/site-packages/tequila/quantumchemistry/chemistry_tools.py", line 851, in __init__
    self.active_space = active_space
  File "/Users/zijian/opt/miniconda3/envs/hamil/lib/python3.8/site-packages/tequila/quantumchemistry/chemistry_tools.py", line 888, in active_space
    self._orbitals[i].idx = ii
TypeError: list indices must be integers or slices, not str

Environment: MacOS + Python 3.8 + PySCF

The problem appears only when an active space is chosen

doomspec commented 1 year ago

I believe I am using the latest version. I installed by pip install git+https://github.com/tequilahub/tequila.git

kottmanj commented 1 year ago

Hi Zijian, what might be the issue is that you are using pyscf as backend. The irrep-based definition of active spaces only works for psi4.

If you have both installed, then pyscf will be the default (makes less problems). Can you try: Molecule(...., backend="psi4") and see if you still get the problem.

If it solves the issue, please let me know. Then I can update the code accordingly that it throws a useful exception.

doomspec commented 1 year ago

Hi Jakob!

Thanks for the reply. I installed psi4 and it works. I tried to use PySCF because it supports higher versions of Python. But psi4 now also supports higher versions. It is written in the index README that but not all (optional) dependencies support it yet (e.g. Psi4). However, it looks good. Shall we change the README?

doomspec commented 1 year ago

BTW, there is a lot of error about the use of numpy.float. I have to manually change them to float to make the code work. As Psi4 now support Python3.8, shall we also try to upgrade tq to higher version?

kottmanj commented 1 year ago

Yes, good idea to change the readme. I should also update the tutorials at some point (... it's on the todo list).

Just to have it on record here, for this issue: active_orbitals = [1,4,5] works, where the indices are just the indices of the spatial orbitals (in whatever way the HF solver of the underlying backend determines them).

kottmanj commented 1 year ago

Can you raise a new issue with the numpy.float problems and point me to where they are happening?