author | description | ms.author | ms.date | ms.service | ms.subservice | ms.topic | title | uid |
---|---|---|---|---|---|---|---|---|
KittyYeungQ |
This document provides a basic guide to submit and run Q# applications in Azure Quantum using Q# Jupyter Notebooks. |
kitty |
02/01/2021 |
azure-quantum |
core |
article |
Submit jobs to Azure Quantum with Q# Jupyter Notebooks |
microsoft.quantum.submit-jobs.jupyter |
This document provides a basic guide to submit and run Q# applications in Azure Quantum using Q# Jupyter Notebooks.
- An Azure Quantum workspace in your Azure subscription. To create a workspace, see Create an Azure Quantum workspace.
Follow these steps to install Jupyter Notebook and the current version of the IQ# kernel, which powers the Q# Jupyter Notebook and Python experiences.
-
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 of the required QDK components.
You are now set up to use Q# Jupyter Notebooks and Q# integration to run quantum programs on Azure Quantum.
-
Run
jupyter notebook
from the terminal where your conda environment is activated. This starts the notebook server and opens Jupyter in a browser. -
Create your Q# notebook (via New → Q#) and write your Q# program.
-
If you've never used Q# with Jupyter, follow the steps in Create your first Q# notebook.
-
Write your Q# operations directly in the notebook. Running the cells will compile the Q# code and report whether there are any errors.
-
For example, you could write a Q# operation that looks like this:
operation GenerateRandomBit() : Result { use q = Qubit(); H(q); let r = M(q); Reset(q); return r; }
-
-
Once you have your Q# operations defined, use the
%azure.*
magic commands to connect and submit jobs to Azure Quantum. You'll use the resource ID of your Azure Quantum workspace in order to connect. (The resource ID can be found on your workspace page in the Azure Portal.)If your workspace was created in an Azure region other than "West US", you also need to specify this as the
location
parameter to%azure.connect
.-
For example, the following commands will connect to an Azure Quantum workspace and run an operation on the
ionq.simulator
target:%azure.connect "/subscriptions/.../Microsoft.Quantum/Workspaces/WORKSPACE_NAME" location="West US" %azure.target ionq.simulator %azure.execute GenerateRandomBit
where
GenerateRandomBit
is the Q# operation that you have already defined in the notebook.
-
-
After submitting a job, you can check its status with the command
%azure.status
or view its results with the command%azure.output
. You can view a list of all your jobs with the command%azure.jobs
.
Some helpful tips while using Q# Jupyter Notebooks:
-
Use the command
%lsmagic
to see all of the available magic commands, including the ones for Azure Quantum. -
Detailed usage information for any magic command can be displayed by simply appending a
?
to the command, for example,%azure.connect?
. -
Documentation for magic commands is also available online: %azure.connect, %azure.target, %azure.submit, %azure.execute, %azure.status, %azure.output, %azure.jobs
Now that you know how to submit jobs to Azure Quantum, you can try to run the different samples we have available or try to submit your own projects. In particular, you can view a sample written entirely in a Q# Jupyter notebook.