qsharp icon indicating copy to clipboard operation
qsharp copied to clipboard

Sample for modern QDK project with Python

Open Manvi-Agrawal opened this issue 1 year ago • 5 comments
trafficstars

Is your feature request related to a problem? Please describe. Its a bit cumbersome to copy-paste code from Sample for modern QDK project with Python as described here.

Describe the solution you'd like Discussed with @tcNickolas who recommends creating a sample for this in the qsharp repository itself, since it's the main source of truth for Q# samples.

Manvi-Agrawal avatar Apr 14 '24 00:04 Manvi-Agrawal

Like this one? https://github.com/microsoft/qsharp/blob/main/samples/notebooks/project.ipynb

billti avatar Apr 17 '24 23:04 billti

This notebook is nice. It would be good if documentation could point to this notebook 📒

Manvi-Agrawal avatar Apr 18 '24 03:04 Manvi-Agrawal

The notebook sample is great for showing how to work with Q# only from a notebook, without Python.

  • It needs to be updated (or a new sample created) to show how to pass arguments from Python to Q#, and get the results from Q# into Python for post-processing.
  • We also need to cover the scenario of Q# + Python non-notebook project. Python files don't have Q# magic to rely on, so they need to use qsharp.eval, the syntax is not trivial to figure out.

tcNickolas avatar Apr 18 '24 17:04 tcNickolas

We do have the double factorized chemistry sample for resource estimation which is straight Python and Q# (https://github.com/microsoft/qsharp/blob/main/samples/estimation/df-chemistry/chemistry.py#L472), but agree we could do with something more accessible.

billti avatar Apr 24 '24 23:04 billti

I would suggest creating a new folder under https://github.com/microsoft/qsharp/blob/main/samples/ called "python_interop", and add two projects to it, each in its own folder:

  1. Teleportation project from our documentation, covering the basic no-inputs-no-outputs scenario, just calling Q# from Python, so that we can point to that project from that docs page.
  2. A slightly more complicated project that would call Q# with some input parameter and get some return value from it.
    • One issue I ran into when working with Q# results in Python was converting Q# Result type into something Python-readable (it used to be cast into an integer but now it's a special data type with constants qsharp.Result.Zero and qsharp.Result.One - this test has an example but we don't have it in actual samples).
    • Maybe we can do something like Q# taking N, generating N random bits, and returning an array of them, and Python counting the number of 1s in the results?

tcNickolas avatar May 02 '24 00:05 tcNickolas