0
$\begingroup$

I'm trying to run a Q# skript from python host. To do so I've installed qsharp package for python and followed all instructions as described under following link: https://learn.microsoft.com/de-de/azure/quantum/install-overview-qdk?tabs=tabid-vscode%2Ctabid-dotnetcli#use-q-and-python-with-visual-studio-and-visual-studio-code

I used .NET CLI and PIP instead of conda to install requieries.

And I still get this error message (see the screenshot)Error that I'm getting when tryinng executing Q# code from python host

This is how bouth scripts look like Left: Python host script  Right: Q# Hello World script

I would like to know if I'm missing something? Perhaps there are certain versions of python, .NET and so on that must be used mandatory? I would appreciate your help very much!

$\endgroup$
2
  • $\begingroup$ Hi and welcome to Quantum Computing SE. The question is concerning general issue with software installation and programming. It is no directly on quantum computing. Please ask the questions on Stack Overflow instead. $\endgroup$ Feb 6, 2023 at 5:42
  • $\begingroup$ It seems like you're getting a python error trying to do a relative import, not necessarily a qsharp error. To do a relative import like this in python, make sure your project is structured as a package with an __init__.py file $\endgroup$
    – ryanhill1
    Feb 6, 2023 at 17:18

1 Answer 1

2
$\begingroup$

I don't think the cause is specific version of Python or .NET because, if you were able to do the appropriate installs like below, then all the requirements should have been met.

pip install --upgrade azure-quantum
pip install qsharp
dotnet tool install -g Microsoft.Quantum.IQSharp
dotnet iqsharp install

When you run import qsharp, the IQ# kernel will get started (Source) and then it will will look for .qs files in the current working directory ( Source 1, Source 2). Then it exposes the Q# code/namespaces from the .qs files as Python modules/callables.

Check to see if no errors surfaced from calling import qsharp.

Note: That's why, although a good guess, that the comment by ryanhill1 does not apply here. The IQ# Kernel won't parse any .py files including __init__.py. That would normally apply for pure Python-based toolkits (like Qiskit), but not for Q#, which relies on some .NET components.

Normally, we would assume that the current working directory would be the same directory as the .py or .ipynb file, but it's not always the case.

In this case, make sure that when you call your .py file (with python myscript.py or when you start a jupyter notebook that you are in the path that contains the .qs file that you want to import.

For that you would normally just have to cd in your terminal/CLI to that path first, before running your script/notebook.

Alternatively, you can try to add this to the top of your .py file, which will change the working directory to the script's location before the import qsharp. Note: it may not work from inside a notebook.

os.chdir(os.path.dirname(os.path.abspath(__file__)))

Hope it helps. If not, let me know and I can look for other potential causes.

$\endgroup$
1
  • $\begingroup$ Thank you very much. It helped me a lot. Everything works now. $\endgroup$
    – Deniss
    Feb 16, 2023 at 17:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.