entropicalabs / openqaoa

Multi-backend SDK for quantum optimisation
MIT License
113 stars 59 forks source link

Error: "'openqaoa' is not a package" when trying to run basic testing #247

Closed robliou closed 1 year ago

robliou commented 1 year ago

Prerequisites

Before raising this issue, I have already checked that I am:

Describe the bug

Hello, I am trying to do some basic testing of the openqaoa module. In particular, I am simply going through the initial walkthrough for MinimumVertexCover as described in the docs. I began by successfully cloning the OpenQAOA github file, and then using pip install . to sucessfully install all dependencies. I verified that all dependencies were succesfully installed, including openqaoa.

However, when I run my test file in Python, I get the error:

ModuleNotFoundError: No module named 'openqaoa.utilities'; 'openqaoa' is not a package

and the execution fails.

To Reproduce

Steps to reproduce the behavior:

  1. clone the openqaoa github file
  2. type pip install . or just manually install each required dependency
  3. in the base of the cloned folder, create a file called test.py and paste the following code into it:
    
    import networkx
    from openqaoa.problems import MinimumVertexCover

g = networkx.circulant_graph(6, [1]) vc = MinimumVertexCover(g, field=1.0, penalty=10) qubo_problem = vc.qubo


4. type ```python test.py``` to run the file
### Expected behavior 
I expect the file to run successfully, not to get the above error.

### Additional context
I also tried installing just the ```openqaoa``` dependency in a new folder and then running the file there (without cloning the original file), but got the same error.
vishal-ph commented 1 year ago

@robliou, thanks for trying out OpenQAOA and for raising the issue. I tried to reproduce the error you described above by following the To Reproduce. However, I get no errors on following the instructions above. See the attached screenshot below.

Can you please provide more information about your python environment? For instance, your version of python. Also, please verify that the openqaoa was successfully installed. I am not sure what you meant by "just manually install each required dependency", since you also need to install openqaoa package and not just the dependencies. Let me know if something is still unclear!

FYI, I ran the code on python 3.10 and the main branch of openqaoa. Screenshot 2023-06-08 at 15 20 23

robliou commented 1 year ago

Hi @vishal-ph , I appreciate your quick response. :)

So, to take this process a step further, the code in its entirety (based on the example) looks like this:

import networkx
from openqaoa.problems import MinimumVertexCover

g = networkx.circulant_graph(6, [1])
vc = MinimumVertexCover(g, field=1.0, penalty=10)
qubo_problem = vc.qubo

from openqaoa import QAOA  

q = QAOA()
q.compile(qubo_problem)
q.optimize()

However, trying to run this code gets the following error:

ModuleNotFoundError: No module named 'openqaoa.problems'; 'openqaoa' is not a package

I'm using Python 3.10.4, And I confirmed that openqaoa was properly installed (separately from the dependencies, which were also installed). a2

Note that I just did a full re-install from scratch on a completely separate Windows laptop and am still getting the same error:

a1

voidburn commented 1 year ago

@robliou I just tried the process on a clean windows 11 install (with python 3.10.11 installed from the microsoft store) and did not encounter the issue.

I think the first step to figure out whether this is a problem with your installation's site packages or an actual bug. To do this all it takes is for you to create a new environment in the openqaoa folder.

This can be accomplished by the following steps:

  1. Open a terminal window
  2. Go to the openqaoa folder (assuming you already cloned it somewhere): cd \path\to\openqaoa
  3. Create a new environment: python -m venv venv (this will take a while, and create a venv folder
  4. Activate this new venv: 4a. If you are using the standard command prompt type: venv\Scripts\activate.bat 4b. If you are using powershell you need to make sure your signing policy is adequate (you need to have launched the terminal as administrator for the first command), first run Set-ExecutionPolicy RemoteSigned then .\venv\Scripts\Activate.ps1
  5. Make sure that your environment is active, a (venv) will appear in front of your prompt
  6. Run the pip install . command (your new environment is without packages at this stage, we need to reinstall everything)
  7. Test if you can run your script within this environment

Please let me know if this fixes the issue. If that's the case, I'm afraid your site-packages might be the issue, and you might want to consider cleaning everything up to start fresh.

Heppy thoughts!

robliou commented 1 year ago

@voidburn

Hello!

I appreciate your prompt response. I just followed your suggestions to a T:

image

... but am still getting the same error:

image

Note that after trying this, I went ahead and deleted the entire openQAOA folder, re-cloned from github, and then re-followed the steps above. However, the outcome is the same.

Is it possible that this package doesn't support python version 3.10.4?

voidburn commented 1 year ago

@robliou It was worth a try! I'm going to find the installer for python 3.10.4 and try with that version. Meanwhile a couple of things you can try on your end:

  1. Delete the venv, recreate it but this time upgrade pip: python -m pip install --upgrade pip before running pip install . just in case something was not properly built with the older version.
  2. Upgrade to python 3.10.11 or downgrade to 3.9 (any install method should work, just make sure when you invoke python --version from the command line you get the expected new one)

This process of elimination (starting from the more obvious possible causes) will help us zero in on the actual culprit. Thank you for your help!

Let me know if changing the python version helps.

robliou commented 1 year ago

@voidburn

Thanks for your suggestions! I tried both methods, including upgrading to python 3.10.11, as well as upgrading the pip installer.

Then deleted venv file and re-cloned and re-installed.

image

Still the same issue... :( :( Weird, huh?

voidburn commented 1 year ago

I had no luck reproducing it even with python 3.10.4 (https://www.python.org/ftp/python/3.10.4/python-3.10.4-amd64.exe):

I tried both without upgrading pip before the install, and with the new version. It still runs your test.py without issues:

import networkx
from openqaoa.problems import MinimumVertexCover

g = networkx.circulant_graph(6, [1])
vc = MinimumVertexCover(g, field=1.0, penalty=10)
qubo_problem = vc.qubo

image

This is very weird!

voidburn commented 1 year ago

@shahidee44 Any idea what could be causing this issue? You've been developing on windows for a while, maybe you've seen something like this before!

voidburn commented 1 year ago

@robliou Can you try and rename your file from openqaoa.py to test.py and try again?

shahidee44 commented 1 year ago

Hmmm this looks weird. My suspicion might be there is something funky going on with the python install, maybe something to do with the paths. Other than @voidburn 's suggestion in renaming the openqaoa.py file to something else like test.py.

Can you try the following command:

python -c "import os, sys; print(os.path.dirname(sys.executable))"

This should give us a clue on where the python path you're using is coming from. If the path in the venv and outside of the environment is the same, that might be the first clue that something strange is going on at the python install level.

Also possible solution would be to uninstall python locally and try installing python through the windows store. That seemed to fixed some issues in the python install itself when people had issues with missing packages.

Let me know if any of these worked or could help us with a possible lead! :)

robliou commented 1 year ago

@robliou Can you try and rename your file from openqaoa.py to test.py and try again?

@voidburn Gave that a shot, also didn't work (I can see what you're trying to test with this; it so happens I have another file called openqaoa.py that is attempting to use the MaximumCut module from openqaoa.problems.problem):

image

robliou commented 1 year ago

@shahidee44 OK, so tried both methods suggested. Here are the results:

1) On my work laptop, uninstalled Python 3.10.11 and then downloaded from Microsoft store and installed. Then ran test.py again. Got the same error.

2) Ran the command line you suggested above, and got the following result:

image

It looks like the same path as before, but with \venv\Scripts appended.

Is this indicative of a broader issue?

Tks,

robliou commented 1 year ago

@voidburn @shahidee44

Interestingly enough, it appears somewhere along the way, the original error for the original test.py file got cleared out. Cool!

By running this code:

import networkx
from openqaoa.problems import MinimumVertexCover

g = networkx.circulant_graph(6, [1])
vc = MinimumVertexCover(g, field=1.0, penalty=10)
qubo_problem = vc.qubo

from openqaoa import QAOA  

#Create the QAOA
from openqaoa import QAOA  

q = QAOA()
q.compile(qubo_problem)
q.optimize()

print('this is q', q)

I can now get the following output:

image

Which I guess is a step in the right direction?

After all, my intent is to see whether I can use OpenQAOA to solve MaxCut questions. Running the suggested code for the MaxCut problem, however, still returns an error similar to our original one:

_max_cutsuggested.py

import numpy as np
import networkx as nx
#from openqaoa.utilities import plot_graph

# Generating a graph of nodes

G = nx.generators.fast_gnp_random_graph(n=6, p=0.6, seed=42)

from openqaoa.utilities import plot_graph
plot_graph(G)

from openqaoa.problems.problem import MaximumCut

maxcut_prob = MaximumCut(G)
maxcut_qubo = maxcut_prob.qubo

maxcut_qubo.hamiltonian.expression

returns this: image

I'm assuming you guys aren't getting this error when trying to import MaximumCut?

voidburn commented 1 year ago

It is indeed a package name collision issue:

Here's the contents of test.py:

import networkx
from openqaoa.problems import MinimumVertexCover

g = networkx.circulant_graph(6, [1])
vc = MinimumVertexCover(g, field=1.0, penalty=10)
qubo_problem = vc.qubo

Here's my folder structure with test.py, and the result of launching test.py: image

Now this is what happens when I copy test.py to openqaoa.py: image

robliou commented 1 year ago

Very interesting, and thanks for posting the suggestion!

Based on my limited understanding, I looked up 'package name collision issue', and it appears it may be the result of when a file name is the same as a package name (in this case openqaoa)?

However, in my file, I've changed the file named openqaoa.py to max_cut_suggested.py,

image

but am still getting this error:

image

Perhaps there is a conflict between my project name and the module name? Therefore, I changed my project name from openqaoa to openqaoa_file, but the error still did not go away:

image

Perhaps my understanding of what a package name collision issue means is incorrect. Could you please explain further where I may be going wrong here?

Thanks again,

voidburn commented 1 year ago

@robliou Once installed in your environment openqaoa will be available everywhere, try moving your test files to a dedicated folder under you projects\python one, see if that actually gets rid for good of these issues first! Working from within the openqaoa cloned folder should only be done if you intend to contribute fixes or new code to it.

If this doesn't fix the maxcut problem please let us know, in which case we should close this issue and you can open another one specifically for that, since we've established that the main topic of this one has been figured out and solved.

Thanks!

Edit: My bad, didn't see that your second screenshot did exactly that. I'd say we should close this issue, and you can open a new one where the guys can try and reproduce that problem and have a look at it! This doesn't look at all like a package name collision anymore, it's likely a different issue!

Please do so!

Thank you

robliou commented 1 year ago

@voidburn

Will do, and thanks for helping to solve the initial issue. At least we got one down!

:)