question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

from ... import ... behavior doesn't work for Q# libraries

See original GitHub issue

Describe the bug The following code:

import qsharp
qsharp.packages.add("Microsoft.Quantum.Chemistry")
qsharp.reload()
from Microsoft.Quantum.Chemistry.JordanWigner.VQE import EstimateEnergy

throws this error:

ModuleNotFoundError: No module named 'Microsoft'

Expected behavior I would expect to be able to use from ... import ... with libraries.

System information Running in iqsharp-based Docker container.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
guenpcommented, Oct 5, 2020

Yes indeed that’s what I was thinking, apologies for not being more clear. I like the approach you suggest as well especially because it’s much simpler. In line with your suggestion, instead of saving to a temp location we can indeed just compile directly. On top of that I was thinking to have a convenience function import_qsharp_operation or simply import_operation that abstracts away the part where you compile the Q# code:

import qsharp
estimate_energy_op = qsharp.import_operation(namespace="Microsoft.Quantum.Chemistry.JordanWigner.VQE", operation="EstimateEnergy")

where

def import_operation(namespace: str, operation: str):
   wrapper_code = qsharp.generate_wrapper(operation, namespace)
   return qsharp.compile(wrapper_code)

For the second use case of generating the boilerplate code, as per your suggestion I agree this could be a nice starting point if people want to tinker with the generated Q# code. If I understood correctly, are you suggesting something like this?

def generate_qsharp_program(operation: str, source_namespace: str, target_namespace: str, path: str = None):
   wrapper_code = qsharp.generate_wrapper(operation, source_namespace)
   entrypoint_code, project_code = qsharp.generate_project(source_namespace, target_namespace)
   save_files(entrypoint_code, project_code, path) # save the files at the given location or at current directory if path is None
1reaction
rmshaffercommented, Oct 1, 2020

@guenp Thanks for the ping on this. So, this won’t directly solve the problem, but there is already code in IQ# for auto-generating Q# wrappers around an arbitrary Q# operation, which is similar to what you are proposing, I believe: https://github.com/microsoft/iqsharp/blob/249490833c5d4c0728c911eab796b7b22394ffd6/src/Core/Compiler/CompilerService.cs#L148-L168

We could consider exposing some functionality similar to that via Python. Do you have any thoughts on what a convenient way to consume this might look like?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can I import a local module with the same name as ...
In Python 2.x, you can gain the ability to disambiguate between your local queue and the standard-library one by adding the following line ......
Read more >
How to import circuit libraries into qiskit
The way you imported the packages, including the QFT circuit is correct. You just didn't execute it correctly.
Read more >
Python import: Advanced Techniques and Tips
This tutorial will provide a thorough overview of Python's import statement and how it works. The import system is powerful, and you'll learn...
Read more >
pykx.q Library Reference Card
This library can be installed by calling a helper function within PyKX , this function will move all the required files and libraries...
Read more >
'module' is not Accessed Pylance(Pylance is greyed out)
When I am importing the python module 'requests'. PyLance or VSCode underlines it with yellow meaning warning. It is also shown in the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found