Generating circuits for different molecule implementations

I have been trying to generate efficient circuits implementing molecule specific ansatz for different molecules. I can see that Pennylane has a nice functionality to build the hamiltonian of the molecule in the following fashion:

symbols = [“Li”, “H”]
geometry = np.array([0.0, 0.0, 0.0, 0.0, 0.0, 2.969280527])
H, qubits = qchem.molecular_hamiltonian( symbols, geometry, active_electrons=2, active_orbitals=5 )

Similarly on https://pennylane.ai/qml/demos/tutorial_quantum_chemistry.html we see an example of H2O. While this functionality is great I need to implement it for multiple molecules (including ones that can take more qubits and are more complicated as well?). So I am curious if there is any reference available to be able to do this quickly for different molecules without determining the "symbols’ and the “geometry” for each molecule individually. Please let me know if any such more examples of more chemistry applications are available in Pennylane somewhere or beyond?
Thanks in advance.

Hi @Rquant, the functionality you’re suggesting sounds very interesting. I haven’t seen anything that does exactly what you describe but I encourage you to check out or demos on quantum chemistry.

The demo on adaptive circuits might be particularly helpful.

I will check with my colleagues if there’s some functionality that does what you need.

I hope this helps.

Hi @Rquant,

This website has a link to geometries for a wide variety of molecules: https://cccbdb.nist.gov/expgeom1x.asp

You can just type the chemical formula for the molecule and find the equilibrium coordinates of each atom. For example, if you type H20, you’ll be redirected to the page for the water molecule

You’ll find the equilibrium coordinates under the “Bond descriptions” section. Just keep in mind that these coordinates are given in Angstroms, while PennyLane uses atomic units. You can convert them easily using for example this website https://www.unitconverters.net/length/angstrom-to-a-u-of-length.htm

Then to build the Hamiltonian, just pass a list of symbols, e.g., `[‘H’, ‘H’, ‘O’] and a list of the coordinates, just like in the example for LiH.

Hope that helps!

Juan Miguel

Hi Catalina, Juan,

I wanted to ask another thing as well. What is the default simulator used for the default qubit on this page. The issue is the anstatz geenration and pruning is very slow for larger circuits. On qiskit, I see that the qasm simulator is much better performing than the statevector simulator. So I was wondering if pennylane allows same level of functionality to generate the ansatz from adapt VQE.

Hi @Rquant,

Great question. We do in fact have a high performance simulator built on C++ called lightning.qubit.

You can also use it together with the adjoint differentiation method, which will bring you significant speed.

This is an example of how you can use it:

import pennylane as qml

dev = qml.device('lightning.qubit', wires=2) 
@qml.qnode(dev, diff_method="adjoint")

Note that the adjoint differentiation only works on simulators and not on actual quantum hardware.

Please let me know if this works for you!

Hye do I have to do something special to use lightning.qubit version? WHat is the maximum qubits it supports. I get an “invalid operation(core dumped)” whenever I try to use it.

Hi @Rquant,

lightning.qubit comes with PennyLane from version 0.18 onwards. If you have an older version of PennyLane you can upgrade it by using pip install --upgrade pennylane.

If you don’t want to upgrade your version of PennyLane you could use pip install pennylane-lightning. You might have some versioning issues with this second alternative though so updating PennyLane is the preferred option.

The maximum number of qubits it supports is given by your machine. For a standard laptop you should be able to use up to 15-20 qubits.

Please let me know if this works for you!

Hey I seem to have pennlyane-lightning installed. Yet I still get this error. Here is my code

symbols = [“C”, “H”, “H”, “H”, “H”]
geometry = np.array([0, 0, 0, 1.1859920304, 1.1859920304, 1.1859920304, 1.1859920304, -1.1859920304, -1.1859920304, -1.1859920304, -1.1859920304, 1.1859920304, -1.1859920304, 1.1859920304, -1.1859920304])
H, qubits = qchem.molecular_hamiltonian(symbols, geometry, active_electrons= 8, active_orbitals= 8)
active_electrons = 8
singles, doubles = qchem.excitations(active_electrons, qubits)
print(f"Total number of excitations = {len(singles) + len(doubles)} And {qubits} " )
hf_state = qchem.hf_state(active_electrons, qubits)
dev = qml.device(“lightning.qubit”, wires=qubits)
@qml.qnode(dev, diff_method=“adjoint”)
def circuit_1(params, wires, excitations):
qml.BasisState(hf_state, wires=wires)
for i, excitation in enumerate(excitations):
if len(excitation) == 4:
qml.DoubleExcitation(params[i], wires=excitation)
else:
qml.SingleExcitation(params[i], wires=excitation)
print(len(doubles))

But I get the following error:

/home/user/.local/lib/python3.7/site-packages/pyscf/lib/misc.py:46: H5pyDeprecationWarning: Using default_file_mode other than ‘r’ is deprecated. Pass the mode to h5py.File() instead.
h5py.get_config().default_file_mode = ‘a’
Total number of excitations = 360 And 16
Illegal instruction (core dumped)

Is this because I am using python 3.7? Or something less trivial?

Hi @Rquant, I’ve ran your code on a Python 3.7 virtual environment and I didn’t get any errors.

You should check that you have the latest versions of pennylane and pennylane-qchem installed. You can upgrade pennylane-qchem by using pip install --upgrade pennylane-qchem

Please let me know if this works for you.

Hi Catalina, sorry for the extremely late reply. So I am still facing this issue. For your refenence can you try to run this code and let me knopw if you face any issue?
My versions of main packages are:
PennyLane 0.22.1 PennyLane-Lightning 0.22.1 PennyLane-Qchem 0.21.0

This is in a virtual enviornment in Python 3.7.5

But the error I am getting is:

h5py.get_config().default_file_mode = ‘a’
Total number of excitations = 804 And 20
Illegal instruction (core dumped)

My full code is attached I hope that helps in solating what issue I am facing maybe.adapt_vqe_gen.py (4.5 KB)

Hi @Rquant, I’m getting a different error when I run your new code.

The previous code still works well for me but since you added lots of things in the new code it’s hard for me to see where the error lies.

I suggest that you take the following code and add lines 1 by 1 until you can detect the source of the error. Please let me know what it was when you find it, or if you have any further questions. If the following code doesn’t run for you, what operating system are you running on? There may be an issue there.

import pennylane as qml
from pennylane import qchem
from pennylane import numpy as np

symbols = ['C', 'H', 'H', 'H', 'H']
geometry = np.array([0, 0, 0, 1.1859920304, 1.1859920304, 1.1859920304, 1.1859920304, -1.1859920304, -1.1859920304, -1.1859920304, -1.1859920304, 1.1859920304, -1.1859920304, 1.1859920304, -1.1859920304])
H, qubits = qchem.molecular_hamiltonian(symbols, geometry, active_electrons= 8, active_orbitals= 8)
active_electrons = 8
singles, doubles = qchem.excitations(active_electrons, qubits)
print(f"Total number of excitations = {len(singles) + len(doubles)} And {qubits} " )

hf_state = qchem.hf_state(active_electrons, qubits)

dev = qml.device('lightning.qubit', wires=qubits)
@qml.qnode(dev, diff_method='adjoint')
def circuit_1(params, wires, excitations):
    qml.BasisState(hf_state, wires=wires)
    for i, excitation in enumerate(excitations):
        if len(excitation) == 4:
            qml.DoubleExcitation(params[i], wires=excitation)
        else:
            qml.SingleExcitation(params[i], wires=excitation)
print(len(doubles)) 

I hope this helps.

Hi Catalina. I am pretty sure the error comes from using lightning qubit. I added assert false statements to interrupt execultion. I see the error (Illegal instruction core dumped) if the assert is placed just after the line “dev = qml.device(‘lightning.qubit’, wires=qubits)”.

Any line above is not causing any issue. My code is here for your reference again (note the assert in line number 40).

adapt_vqe_gen.py (4.3 KB)

Slightly updated code again (the error is pretty similar, if I use default.qubit, that works but using lightening.qubit which causes the “Invalid command (core dumped)” error:
adapt_vqe_gen.py (4.4 KB)

Hi @Rquant, could you please send me the output of import pennylane as qml; qml.about() ?

I still don’t get your error so I think the problem is with your operating system or environment. M1 Macs can create problems when using lightning so I’m thinking this might be your case.

This is what I get:
I am using an ubuntu based server:

WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with ‘-m pip’ instead of running pip directly.
Name: PennyLane
Version: 0.22.2
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: https://github.com/XanaduAI/pennylane
Author:
Author-email:
License: Apache License 2.0
Location: /nobackup/rsharma3/QC_work/QCCDSecrun/python3.8_env/lib/python3.8/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, retworkx, scipy, semantic-version, toml
Required-by: PennyLane-Lightning, PennyLane-Qchem
Platform info: Linux-4.15.0-173-generic-x86_64-with-glibc2.27
Python version: 3.8.0
Numpy version: 1.22.3
Scipy version: 1.8.0
Installed devices:

  • default.gaussian (PennyLane-0.22.2)
  • default.mixed (PennyLane-0.22.2)
  • default.qubit (PennyLane-0.22.2)
  • default.qubit.autograd (PennyLane-0.22.2)
  • default.qubit.jax (PennyLane-0.22.2)
  • default.qubit.tf (PennyLane-0.22.2)
  • default.qubit.torch (PennyLane-0.22.2)
  • lightning.qubit (PennyLane-Lightning-0.22.1)

Hi @Rquant thanks for discussing this. As @CatalinaAlbornoz mentioned, we are unable to reproduce this locally. I have a few suggestions to figure this out:

  • Create a fresh Python environment with the latest PL release. If your environment was upgraded from an older version, it may be possible that some old binaries were not thoroughly cleaned, and hence are causing the issue.
  • If this does not work, print the output or their Linux environment LD_LIBRARY_PATH using echo $LD_LIBRARY_PATH as there may be a conflict somewhere in there. This lists the path used to identified shared library locations on your system. If something in there has conflicting symbol-names with those used by Lightning, we may hit an issue. If it is empty, then this is likely not the case.
  • Lastly, if all else fails, run the script through a debugger via gdb --args python ./path_to_file.py and type run on the prompt. This loads GDB and runs Python through it. As Lightning has compiled modules, it should be able to identify which functions calls are causing the issue. It should hopefully give a more detailed error message about what is happening.

If possible, it would be best to also create a smaller minimum working example of this error — the simplest possible circuit that reproduces it on your end. This should help with hitting the error quicker and best much faster to debug using the last point above, as debugger tend to slow programs down a lot. Let us know about the above and we can try to help more.