Write a Q# standalone program to run on a local quantum simulator
مقالة
قراءة خلال 3 دقائق
شكرًا لك.
The Microsoft Quantum Development Kit contains several quantum simulators that allow you to test and run quantum programs locally, without having to access the Azure Quantum service.
Prerequisites
Set up your preferred local environment with the Microsoft Quantum Development Kit following the steps in Set up a Q# standalone environment.
Run a Q# program
Follow the instructions on the tab corresponding to your development environment that you created during set up.
From your environment (that is, either the conda environment you created, or the Python environment where you installed Jupyter), run the following command to start the Jupyter Notebook server:
jupyter notebook
If the Jupyter Notebook doesn't open automatically in your browser, copy and paste the URL provided by the command line into your browser.
Choose New → Q# to create a Jupyter Notebook with a Q# kernel, and add the following code to the first notebook cell:
operation SampleQuantumRandomNumberGenerator() : Result {
use q = Qubit(); // Allocate a qubit in the |0⟩ state.
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;
}
Run this cell of the notebook. You should see SampleQuantumRandomNumberGenerator in the output of the cell. When running in Jupyter Notebook, the Q# code is compiled, and the cell outputs the name of any operations that it finds.
In a new cell, run the operation you just created in a simulator by using the %simulate magic command:
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 run the cell repeatedly, you should see each result approximately half the time.
If you are receiving an error "'npm' is not recognized as an internal or external command", in the below steps, install node.js including npm. Alternatively, use our the command line templates to create a Q# project , or use Visual Studio.
To create a new project in VS Code:
Click View -> Command Palette and select Q#: Create New Project.
Click Standalone console application.
Navigate to the location to save the project. Enter the project name and click Create Project.
When the project is successfully created, click Open new project... in the lower right.
Inspect the project. You should see a source file named Program.qs, which is a Q# program that defines a simple operation to print a message to the console.
To run the application:
Click Terminal -> New Terminal.
At the terminal prompt, enter dotnet run.
You should see the following text in the output window Hello quantum world!
Note
Workspaces with multiple root folders are not currently supported by the VS Code Q# extension. If you have multiple projects within one VS Code workspace, all projects need to be contained within the same root folder.
To create a new Q# application in Visual Studio:
Open Visual Studio and click File -> New -> Project.
Type Q# in the search box, select Q# Application and click Next.
Enter a name and location for your application and click Create.
Inspect the project. You should see a source file named Program.qs, which is a Q# program that defines a simple operation to print a message to the console.
To run the application:
Select Debug -> Start Without Debugging.
You should see the text Hello quantum world! printed to a console window.
Note
If you have multiple projects within one Visual Studio solution, all projects contained in the solution need to be in the same folder as the solution, or in one of its sub-folders.
To run a Q# program in the console:
Create a new application:
dotnet new console -lang Q# -o runSayHello
Navigate to the application directory:
cd runSayHello
This directory should now contain a file Program.qs, which is a Q# program that defines a simple operation to print a message to the console. You can modfiy this template with a text editor and overwrite it with your own quantum applications.
Run the program:
dotnet run
You should see the following text printed: Hello quantum world!
Next steps
Now that you have run a Q# program on a local quantum simulator, learn how you can also run your program on remote quantum hardware.