Take a first step of Azure Quantum

Let’s submit your first Q# program through Azure Quantum.

Before your first Q# program

To make a Q# program, you may need these tools:

These things will help you execute your Q# programs locally in Visual Studio Code. Please install to follow steps below.

Create a QRNG project

First of all, open Visual Studio Code and click Command Palette... in the View tap.

command_palette

In the search box, enter Q#: Create new project... and click it. I can see it before I typed it because I’ve tested it before to write this post.

new

Then, select Standalone console application and enter your own project name. If you’ve done to write it, select Create Project.

created

After a while, you can see a pop-up window at the right below. Please Click Open new project... in that window. It will let you open the project folder you’ve just maken.

If you successfully open this project, you can see two files: Program.qs and HelloWorld.csproj. The .qs file is the main program and the other is for project settings. Double click this Q# file and open it to execute.

program.qs

  1. Click New Terminal in the Terminal tap
  2. Run dotnet run

     dotnet run
    

If the program executes well, it may print out a string: Hello quantum world!. If there is an error, please make sure that you are in the project folder that you make from jobs above. However, this program is boring. Let’s make it more interesting!

Using one of quantum gates, you can generate a real random number. To get this number, you need a qubit and a hardmard gate. If you measure a qubit with the hardmard gate, the result is 0 or 1 with a 50/50 chance. This is called superposition which is a characteristic of qubits. This program is often called as QRNG which means Quantum Random Number Generator.

Now change codes in Program.qs.

namespace QRNG {

    open Microsoft.Quantum.Canon;
    open Microsoft.Quantum.Intrinsic;
    // import measurement
    open Microsoft.Quantum.Measurement;
    
    @EntryPoint()
    operation GenerateRandomBit() : Result {
        // Make a qubit
        use q = Qubit();
        // Put the qubit to superposition
        H(q);
        // It now has a 50% chance of being measured 0 or 1
        // Measure the qubit value
        return M(q);
    }
}

Execute it the same way before, then you can see it prints out Zero or One randomly like the image below.

dotnet_run

What did you get from your first QRNG program? Zero or One?

Use real devices in Azure Quantum

Now you may have a QRNG program and execute it serveral times in your local. However, every result that you got are not from actual quantum computers because you executed the QRNG program with a simulator of QDK in your local. The simulator imitates real devices so it enables you to run quantum programs locally. This is definitely useful to learn quantum computing and test your program, but some of you may be curious the result from actual one. I will share how to send a job to real quantum computers below.

To use real quantum devices in Azure, you need an Azure account. Please prepare one.

  1. Install Azure CLI (.NET Core SDK 3.1 not required)
  2. Install the Azure CLI quantum extension executing az extension add -n quantum in the terminal extension
  3. Open .csproj file and add <ExecutionTarget>ionq.qpu</ExecutionTarget> between two <PropertyGroup>
  4. Enter az login in the terminal and login your Azure account through a web browser
  5. Enter az account set -s YourSubscriptionID in the terminal
  6. (It takes time) Make a Azure Quantum workspace through the Azure Portal
  7. Enter az quantum workspace set -g YourResourceGroup -w YourWorkspace -l YourLocation -o table and wait for the table output *IMPORTANT: write YourLocation in mixed case surrounded by quotes or in lower case with no spaces or quotes
  8. Enter az quantum target list -o table to check available providers table
  9. Enter az quantum job submit --target-id ionq.qpu -o table to submit a job *You can replace ionq.qpu with any of the ones in Target-id

Enter az quantum job show -o table --job-id YourJobID to check the status of submitted job. If the status is changed to Succeeded, you can see your output to enter the command below.

az quantum job output -o table --job-id YourJobID

I get this result from Azure.

final_result

The frequency is not accurately 50/50 because there is quantum noise for the actual devices.

References


💬 Any comments and suggestions will be appreciated.

Leave a comment