Get started with Q# and an Azure Quantum notebook
First-time users automatically get free $500 (USD) Azure Quantum Credits for use with each participating quantum hardware provider. If you have consumed all the credits and you need more, you can apply to the Azure Quantum Credits program.
Learn how to run Q# code in a Jupyter Notebook in the Azure Quantum portal. A Jupyter Notebook is a document that contains both rich text and code and can run in your browser, and can run Q# and Python code in Azure Quantum. Notebooks can be created directly in the Azure Quantum portal, and offer features such as preloaded connection information and standard Q# libraries.
In this article, you will run a sample notebook on Azure Quantum that executes a simple quantum random number generator written in Q# and Python.
For more information about using Jupyter Notebooks with Azure Quantum, see Work with Jupyter Notebooks in an Azure Quantum workspace.
Before you begin, you need the following prerequisites to use Jupyter Notebooks in an Azure Quantum workspace.
- An Azure account with an active subscription. If you don’t have an Azure account, register for free and sign up for a pay-as-you-go subscription.
- An Azure Quantum workspace. See Create an Azure Quantum workspace.
Copy a sample notebook
To get started, copy a sample notebook from the notebook gallery.
Log in to the Azure portal and select your Azure Quantum workspace.
In the Sample gallery, locate the Hello, world: Q# notebook tile, select either the IonQ or Quantinuum provider (the sample code is identical) and select Copy to my notebooks.
The sample notebook can be found under My notebooks and you can now run the notebook.
Run the notebook
To run the sample notebook, follow these steps for your selected provider.
In My notebooks, select the hello-world-qsharp-ionq notebook.
To run the full program from top to bottom, select Run all.
To walk through the example and run each cell individually from top to bottom, select the cell you want to run and select the run icon.
Stepping through the program on IonQ
The hello world program runs a simple quantum random number generator and displays a histogram of the results.
Some things to note:
- The kernel: In the upper right of the notebook, you can see that the notebook is running the Python 3 (ipykernel) kernel, which is the default Python shell for Jupyter Notebooks. When you create a notebook in Azure Quantum, you can select either the Python 3 (ipykernel) or the Azure Quantum Q# kernel. Both kernels are fully compatible with Q# code.
- 1st cell: Preloads your subscription information to connect to the Azure Quantum service.
- 2nd cell: Retrieves the available targets (quantum computers and simulators) in your workspace.
- 3rd and 4th cells: The Q# code that defines the program. Note the
%%qsharpmagic command which allows you to enter Q# code directly into the notebook when using the Python 3 (ipykernel) kernel.
- 5th cell: Sets the target and submits the job.
- 6th and 7th cells: Plot and display the result. The results should be roughly split between 0 and 1.
If you have any questions or run into any issue using Azure Quantum, bookmark Azure Quantum office hours and join our open office hours every Thursday 8∶30 AM Pacific Time zone (PT).
Submit and view feedback for