MicrosoftDocs/quantum-docs-pr

python code cannot find the namespace declared in qsharp code

zhizeng8 opened this issue · 15 comments

I'm a new comer. I failed to run the hello world with python.
from HelloWorld import SayHello
ModuleNotFoundError: No module named 'HelloWorld'
It seems the python code cannot find the namespace declared in qsharp code.
I am using vs code, how can I fix it? Thank you!


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Hi @zhizeng8, did you install the QDK for Python?

Installation link: https://docs.microsoft.com/en-us/quantum/install-guide/pyinstall

Hi @zhizeng8, did you install the QDK for Python?

Installation link: https://docs.microsoft.com/en-us/quantum/install-guide/pyinstall

I have installed QDK, is (QDK) different from (QDK for python)? Thank you! @geduardo

I can run Q# command line applications successfully and import qsharp in .py successfully. The only problem is from HelloWorld import SayHello. Thank you! @geduardo

@zhizeng8, your .qs file must be in the same folder as the .py file. If that still doesn't work, can you share the contents of your .qs file?

namespace HelloWorld {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;

operation SayHello() : Unit {
    Message("Hello from quantum world!");
}

}

Can you confirm that the .py and .qs files are located in the same folder? Can you copy/paste the output you get when running python?

You can also try running python -c "import qsharp; qsharp.reload()" from the folder with your .py and .qs files, if you have recently upgraded to a new version.

Also, so we know what versions you have installed, can you run pip show qsharp and dotnet iqsharp --version and report the output?

.py and .qs files are located in the same folder.

Traceback (most recent call last):
File "/Users/zengzhi/VscodeProjects/HelloQuantum/hello_world.py", line 3, in
from HelloWorld import SayHello
ModuleNotFoundError: No module named 'HelloWorld'

Name: qsharp
Version: 0.11.2006.403
Summary: Python client for Q#, a domain-specific quantum programming language
Home-page: https://github.com/microsoft/iqsharp
Author: Microsoft
Author-email: None
License: UNKNOWN
Location: /Library/anaconda3/lib/python3.7/site-packages
Requires: jupyter-client
Required-by: qdk

iqsharp: 0.11.2006.403
Jupyter Core: 1.3.60623.0
.NET Runtime: .NETCoreApp,Version=v3.1

Can you ensure that the .qs file is saved? Try closing and re-opening it from the same folder to ensure that it has the correct contents?

Yes, it is saved.

Can you please run the following Python script to see whether the Python code is correctly finding your Q# operation?

import qsharp
qsharp.reload()
print(qsharp.get_available_operations())

You should see:

['HelloWorld.SayHello']

It returns:
['Qrng.SampleQuantumRandomNumberGenerator', 'Qrng.SampleRandomNumber', 'Qrng.SampleRandomNumberInRange']

It finally works after I delete the Program.qs file.

Thank you very much!

😆 I was in midst of typing a long potential thought/suggestion, and it's comforting to hear it wasn't that. Happy you got it working!

(And @rmshaffer thanks for the .get_available_operations() tip! Been having some trouble between the chem library and Python, and it should prove useful in sorting that out)

I see! This is an interesting issue caused by the fact that you have multiple .qs files, and one of your .qs files must have contained an @EntryPoint() operation, which was causing this operation in the other file to be invisible to Python. I can reproduce this as well. I will open an issue in the https://github.com/microsoft/iqsharp repo for this. Thank you for reporting, and I'm glad you got it working!