I am trying to learn Q# and have very limited knowledge in Q#. As the title suggests, I am trying to pass an array of qubits into an operation using a python script to call the operation which is written in Q#.

For Example: Consider the following Q# code:

operation SlightlyComplicated(register : Qubit[], pauli : String[]):
        Bool { // Does something and returns a bool value

The above operation is saved in a file called Program.qs which is saved in the same folder as the python code under the name TempQuant and then here's the python code that is responsible for calling this operation:

import qsharp
from qsharp import Result
from Quantum.TempQuant import SlightlyComplicated

res = SlightlyComplicated.simulate(register = , pauli = ['X', 'Y', 'Z', 'Z'])

Since pauli is an array of Strings, it is easy to pass it through python since they share this type, but for types like qubit, Result, Pauli(which are exclusively present in Q#), how am I supposed to pass them through python into their respective counterparts in Q#?

For reference, I am using Visual Studio Code to write the codes.


Building off of Ryan's answer (https://quantumcomputing.stackexchange.com/a/12271/12224), it's important to recognize that the Qubit type should not only live just in the Q# code but also have its scope bounded by either a using statement or a borrowing statement. See Working with Qubits for more guidance on how to allocate qubits for use in an operation.

As an option for how to pass an initial state of a register as a series of ones and zeros, you could pass your input register from Python as an array of bools, and then use ApplyToEachIndex from Microsoft.Quantum.Cannon to map that array onto the array of qubits:

    operation test(bools : Bool[]) : Unit {
        using (qs = Qubit[Length(bools)]) {
            ApplyToEachIndex(InitializeFromBoolArray(_, bools, _), qs);

    operation InitializeFromBoolArray(index : Int, bools : Bool[], qubit : Qubit) : Unit {
        if (bools[index]) {

Note that this also uses a callable invocation expression to create an operation with the signature expected by ApplyToEachIndex.

| improve this answer | |

In general, any logic that relates to Q#-specific types like Qubit should live in your Q# code.

The specific answer depends on what you are trying to do. For example, if you want your Python code to specify the number of qubits, you could have something like an nQubits : Int parameter to your Q# operation. Your Python code could pass this as an integer, and then your Q# code could allocate the array of the appropriate number of qubits.

For the Pauli and Result types, there are actually Python enums defined such that you could pass those values directly if you want to. See qsharp.types.Pauli and qsharp.types.Result.

You may also find the answers to this similar question helpful: How do you send an array of qubits to an operation in Q#?

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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