qiskit-community / qiskit-aqua

Quantum Algorithms & Applications (**DEPRECATED** since April 2021 - see readme for more info)
https://qiskit.org/aqua
Apache License 2.0
576 stars 376 forks source link

VQE for molecules giving OSerror #1142

Closed mertall closed 4 years ago

mertall commented 4 years ago

Information

What is the current behavior?

Error in atexit._run_exitfuncs: Traceback (most recent call last): File "/usr/lib/python3.8/concurrent/futures/process.py", line 102, in _python_exit thread_wakeup.wakeup() File "/usr/lib/python3.8/concurrent/futures/process.py", line 90, in wakeup self._writer.send_bytes(b"") File "/usr/lib/python3.8/multiprocessing/connection.py", line 183, in send_bytes self._check_closed() File "/usr/lib/python3.8/multiprocessing/connection.py", line 136, in _check_closed raise OSError("handle is closed") OSError: handle is closed

Steps to reproduce the problem

I have attached the file I am using. I am simply running the program with WSL Ubuntu 20.04 and python 3.8.2 in vs code

What is the expected behavior?

It should be returning graphs of the VQE energy and Exact Energy.

Suggested solutions

I think maybe this has to do with the optimizer? or maybe the program is running out of memory?

import numpy as np
import matplotlib.pyplot as plt
import copy
from qiskit import BasicAer
from qiskit.aqua import aqua_globals, QuantumInstance
from qiskit.aqua.algorithms import VQE, NumPyEigensolver
from qiskit.aqua.components.optimizers import SLSQP
from qiskit.chemistry.components.initial_states import HartreeFock
from qiskit.chemistry.components.variational_forms import UCCSD
from qiskit.chemistry.drivers import PySCFDriver
from qiskit.chemistry.core import Hamiltonian, QubitMappingType

molecule = 'H .0 .0 .0; Li .0 .0 {0}'
distances = np.arange(0.5,4.0,0.1)
vqe_energies = []
exact_energies = []

for d in distances:   
    driver = PySCFDriver(molecule.format(d), basis='sto3g')
    qmolecule = driver.run()
    operator =  Hamiltonian(qubit_mapping=QubitMappingType.PARITY,
                            two_qubit_reduction=True, freeze_core=True,
                            orbital_reduction=[-3, -2])
    qubit_op, aux_ops = operator.run(qmolecule)

    result = NumPyEigensolver(qubit_op, aux_operators=aux_ops).run()
    exact_energies.append(np.real(result.eigenvalues))
    optimizer = SLSQP(maxiter=5)

    initial_state = HartreeFock(
                                operator.molecule_info['num_orbitals'],
                                operator.molecule_info['num_particles'],
                                qubit_mapping=operator._qubit_mapping,
                                two_qubit_reduction=operator._two_qubit_reduction)

    var_form = UCCSD(
                    num_orbitals=operator.molecule_info['num_orbitals'],
                    num_particles=operator.molecule_info['num_particles'],
                    initial_state=initial_state,
                    qubit_mapping=operator._qubit_mapping,
                    two_qubit_reduction=operator._two_qubit_reduction)

    vqe = VQE(qubit_op, var_form, optimizer)
    backend = BasicAer.get_backend("statevector_simulator")
    vqe_result = np.real(vqe.run(backend)['eigenvalue'])
    vqe_energies.append(vqe_result)

plt.plot(distances, vqe_energies, label='VQE')
plt.plot(distances,exact_energies, label='Exact')
plt.xlabel('Interatomic distance')
plt.ylabel('Energy')
plt.legend(loc='upper right')
woodsp-ibm commented 4 years ago

The Qiskit code tries to take advantage of multiprocessing, where its going though parts that can run in parallel, with a goal of speeding up things overall. This seems to be failing under WSL in the area of multiprocessing. Now we do test under Windows directly and support that as a platform, same for Linux directly. One thing you might like to try is to set num_processes to 1 in aqua_globals and see if restricting computation to a single process will allow it to run for you.

mertall commented 4 years ago

I did so but none of my graphs are coming up. It seems like it is terminating early? good news is the error code is gone... not sure if that is because it isn't executing everything.

woodsp-ibm commented 4 years ago

You need to add a plt.show() at the end to make sure the graph appears when you run this outside of a notebook as python script.

I am not sure what you expect to see in the plot since you are plotting the electronic energy part of the problem, which is what the algorithm is solving. To see the 'normal' sort of dissociation curve you need to add in the nuclear repulsion energy and also the frozen energy from the core orbitals done via freeze_core. You can call process_algorithm_result from the Hamiltonian class, from which you got the qubit_op in order to compute a full result. The energy field in that result will plot like the curve you maybe expecting.

manoelmarques commented 4 years ago

I was able to run it and show the graph under WSL-2 Ubuntu 20.04 and python 3.8.3 (Anaconda env.) in vs code. plt.show() at the end.

Very important: You need a XServer running, please look at: https://stackoverflow.com/questions/43397162/show-matplotlib-plots-in-ubuntu-windows-subsystem-for-linux-wsl1-wsl2

First I added to my .bashrc: export DISPLAY=grep -oP "(?<=nameserver ).+" /etc/resolv.conf:0.0 Once it ran I got in my case DISPLAY=172.27.32.1:0.0

Then I installed VcXsrv on Windows, enabled private/public firewall access and started it with "Disable access control" ticked.

I was able to show the graph with plt.show() at the end. I never got the multiprocess error you mentioned. My Windows is Windows Version 2004 (OS Build 19041.388) running on a VMWare Fusion VM.

mertall commented 4 years ago

thank you @manoelmarques @woodsp-ibm ... I added plt.show() but forgot to include the updated script in this issue. I will take both suggestions into consideration and will update this issue tomorrow with my results. again, thank you for the help so far!

woodsp-ibm commented 4 years ago

@mertall Did the information help you? If the issue is solved for you then please feel free to close this issue off.