Exercise - Use the Quantum Development Kit to run a quantum program
In this unit, you'll learn how to use the Quantum Development Kit (QDK) extension for Visual Studio Code to create and run Q# programs in a local development environment. You can also connect to your Azure Quantum workspace and run your programs on the quantum computers and simulators of the providers you selected for your workspace.
Install the required tools
Install Visual Studio Code.
Install the Azure Quantum Development Kit extension.
- You can also open Visual Studio Code on the Web, which already includes the Quantum Development Kit extension.
If you want to run your programs on real hardware, you also need:
- An Azure subscription. If you don't have an Azure subscription, create a free account before you begin.
- An Azure Quantum workspace.
Create and run a Q# program in Visual Studio Code
The QDK includes a set of Q# samples that you can use to learn more about Q# and quantum computing. To view the samples, open a new Q# file and type sample
, then select the sample you want to view from the list of options.
Open Visual Studio Code and select File > New Text File to create a new file.
Save the file as
Main.qs
. This file will contain the Q# code for your program.Copy and save the following code in the
Main.qs
file.operation Main() : Result { // Qubits are only accessible for the duration of the scope where they // are allocated and are automatically released at the end of the scope. use qubit = Qubit(); // Set the qubit in superposition by applying a Hadamard transformation. H(qubit); // Measure the qubit. There is a 50% probability of measuring either // `Zero` or `One`. let result = M(qubit); // Reset the qubit so it can be safely released. Reset(qubit); return result; }
Explore the Q# code
- Hover over the
H
operation to see a short summary of the definition. - Place your cursor in front of the
H
operation, right-click, and select Go to Definition. This opens a read-only view of the standard library code for the function.
Run the program locally
To run your program on the built-in simulator, click Run above the Main
operation or press Ctrl+F5. Your output will appear in the debug console.
Connect to Azure Quantum and submit your job
If you have an Azure Quantum workspace, you can connect to it from VS Code and submit your Q# programs to Azure Quantum. For this example, you submit the Main.qs
program to the Rigetti simulator.
- Open the QUANTUM WORKSPACES section in the Explorer view of the VS Code sidebar.
- Select Add an existing workspace and follow the prompts to connect to your preferred directory, subscription, and workspace.
- Once you are connected, expand your workspace and expand the Rigetti provider.
- Select rigetti.sim.qvm as your target.
- Click the ⏵︎ (Play) icon to the right of the target, or run the "Q#: Submit current Q# program" command from the Command Palette. If you get a popup, select Change the QIR target profile and continue.
- Enter a name and the number of shots for the job submission in the input boxes.
- Press Enter to submit the job. The job status will display at the bottom of the screen.
- Expand Jobs and hover over your job, which displays the times and status of your job.
- To view the results, select the cloud icon next to the job name to download the results from your workspace storage and display it in VS Code.
Extra - Create an Azure Quantum notebook
The QDK allows you to create Azure Quantum notebooks in VS Code. Azure Quantum notebooks are Jupyter notebooks that allow you to run quantum programs on the quantum computers and simulators of the providers you selected for your Azure Quantum workspace.
To create an Azure Quantum notebook:
- In VS Code, open the View menu and select Command Palette.
- Type Q#: Create an Azure Quantum notebook.
- A Jupyter Notebook opens in a new tab. The notebook includes a code cell that imports the required packages for the sample, connects to the Azure Quantum service, and runs a Q# program.