How to use variable from python cell to qsharp cell in jupyter notebooks

Razeen ud-Din / PhD Scholar 20 Reputation points
2024-07-10T07:47:28.2166667+00:00

I am working on jupyter notebook, My first cell runs a python script and return some outputs in form of lists (also lists of lists). My next cell is qsharp cell in which I have qsharp script. I want to use the output of my first cell as an input for my qsharp cell.... But the problem I am facing that qsharp cell didn't recongnize the lists that I get from my first cell... How can I resolve this?

Azure Quantum
Azure Quantum
An Azure service that provides quantum computing and optimization solutions.
71 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Prrudram-MSFT 24,661 Reputation points
    2024-07-11T17:05:06.78+00:00

    Hi @Razeen ud-Din / PhD Scholar

    Thank you for reaching out to the Microsoft Q&A platform.

    The Python and Q# runtime environments are largely separate, and Q# can’t just “see” variables or values defined in Python (and vice-versa).If you want to pass values from Python to Q#, you must evaluate a Q# expression which embeds those values, such as defining a Q# operation with those values within it or calling a Q# operation passing the values as arguments.

    It’s not the most elegant, but the most common types that need to be passed (e.g. numbers and lists of numbers) have the same text representation in both languages.

    We are working on better docs & samples for this. For now, one of the clearest (though not very brief) examples is our DF Chemistry sample. For example, see the code at this line: https://github.com/microsoft/qsharp/blob/main/samples/estimation/df-chemistry/chemistry.py#L479

    Let me know if that helps of you have further questions.
    If I have answered your query, please click "Accept as answer" as a token of appreciation

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.