Develop with Q# and Python

Install the QDK to develop Python host programs to call Q# operations.

Install the qsharp Python package

  1. Install Miniconda or Anaconda.

  2. Open an Anaconda Prompt.

    • Or, if you prefer to use PowerShell or pwsh: open a shell, run conda init powershell, then close and re-open the shell.
  3. Create and activate a new conda environment named qsharp-env with the required packages (including Jupyter Notebook and IQ#) by running the following commands:

    conda create -n qsharp-env -c quantum-engineering qsharp notebook
    
    conda activate qsharp-env
    
  4. Run python -c "import qsharp" from the same terminal to verify your installation and populate your local package cache with all required QDK components.

That's it! You now have both the qsharp Python package and the IQ# kernel for Jupyter, which provides the core functionality for compiling and executing Q# operations from Python and allows you to use Q# Jupyter Notebooks.

Choose your IDE

While you can use Q# with Python in any IDE, we highly recommend using Visual Studio Code (VS Code) IDE for your Q# + Python applications. With the QDK Visual Studio Code extension you gain access to richer functionality such as warnings, syntax highlighting, project templates, and more.

If you would like to use VS Code:

If you would like to use a different editor, the instructions above have you all set.

Write your first Q# program

Now you are ready to verify your qsharp Python package installation by writing and executing a simple Q# program.

  1. Create a minimal Q# operation by creating a file called Operation.qs and adding the following code to it:

    namespace Qrng {
        open Microsoft.Quantum.Intrinsic;
    
        operation SampleQuantumRandomNumberGenerator() : Result {
            using (q = Qubit())  { // Allocate a qubit.
                H(q);             // Put the qubit to superposition. It now has a 50% chance of being 0 or 1.
                let r = M(q);     // Measure the qubit value.
                Reset(q);
                return r;
            }
        }
    }
    
  2. In the same folder as Operation.qs, create a Python program called host.py to simulate the Q# SampleQuantumRandomNumberGenerator() operation:

    import qsharp
    from Qrng import SampleQuantumRandomNumberGenerator
    
    SampleQuantumRandomNumberGenerator.simulate()
    
  3. From the environment you created during installation (i.e., the conda or Python environment where you installed qsharp), run the program:

    python host.py
    
  4. You should see the result of the operation you invoked. In this case, because your operation generates a random result, you will see either Zero or One printed on the screen. If you execute the program repeatedly, you should see each result approximately half the time.

Note

  • The Python code is just a normal Python program. You can use any Python environment, including Python-based Jupyter Notebooks, to write the Python program and call Q# operations. The Python program can import Q# operations from any .qs files located in the same folder as the Python code itself.

Next steps

Now that you have installed the Quantum Development Kit in your preferred environment, you can follow this tutorial to write and run your first quantum program.