Use the QDK for quantum computing with Q# and Python
Learn how to install the Quantum Development Kit (QDK) to develop Python host programs that call Q# operations.
Install the qsharp
Python package
Install Miniconda or Anaconda. Note: 64-bit installation required.
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.
- Or, if you prefer to use PowerShell or pwsh: open a shell, run
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
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 provide the core functionality for compiling and running 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:
- Install VS Code (Windows, Linux and Mac).
- Install the QDK extension for 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 running a simple Q# program.
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 { use 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; } }
In the same folder as
Operation.qs
, create a Python program calledhost.py
to simulate the Q#SampleQuantumRandomNumberGenerator()
operation:import qsharp from Qrng import SampleQuantumRandomNumberGenerator print(SampleQuantumRandomNumberGenerator.simulate())
From the environment you created during installation (that is, the conda or Python environment where you installed
qsharp
), run the program:python host.py
You should see the result of the operation you invoked. In this case, because your operation generates a random result, you will see either
0
or1
printed on the screen. If you run 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 tested the Quantum Development Kit in your preferred environment, you can follow this tutorial to write and run your first quantum program.