Hello World in Q#

Hello World in Q#

Table of contents

Prerequisites

Before we dive into writing our first Q# program, let’s review some of the fundamental concepts of quantum computing described in my other articles:

At first you need to set up a development environment. Either Visual Studio or Visual Studio Code can be used. For Visual Studio, you need to install the [Microsoft Quantum Development Kit] (marketplace.visualstudio.com/items?itemName..). For VS Code install the quantum.quantum-devkit-vscode extension. Sadly there is no Q# code formatter available at the time of writing (2023). Also the IntelliSens is seriously lacking.

This tutorial uses Visual Studio, but all steps might also be completed using VSCode (In VSCode press Ctrl+Shift+P, then select Create new project and proceed as follows).

There are two ways to run a Q# application. You can either use a C# host project that uses Q# code as a static library. Or you can implement a pure Q# application. The first option is more versatile as it gives you full control over the application. You can implement a UI or apply some logic to adjust input parameters for a Q# algorithm. We will stick with the second option however, as it is simpler and sufficient for this article.

Once the development environment is established, open up the Visual Studio and select New Project. Then use the Q# Application template:

The following project structure will be created:

Keep in mind that the .NET offers an environment that only simulates quantum computer. Unless you have access to such, certain mechanisms might work differently. A good example is random number generation. Putting a qubit into a superposition state and then measuring its value results in a truly random number. However, the same code used in classical deterministic computers will result only in pseudo-random numbers. This matters only in very specific scenarios, e.g. cryptography. Besides that, the .NET emulator is a great way to explore the quantum world and just have pure joy in trying different mechanisms and ideas.

Hello World

Let's start with the simplest example, Hello World program:

 namespace Quantum.QSharpApplication1
 {
    open Microsoft.Quantum.Canon;
    open Microsoft.Quantum.Intrinsic;

    @EntryPoint()
    operation HelloQ() : Unit
    {
        Message("Hello hashnode!");
    }
 }

If you have basic knowledge of C#, this code should be self-explanatory to you. It begins with a namespace declaration. Namespaces work the same way as in C#. It is followed by an open statement, that mimics the using statement from C#.

The next line contains @EntryPoint(). It marks the entry point to the application (the method that should be treated as the 'Main'). It is required if you are using the Q#-only model. However, if a C# hosting application is used, this attribute must not be used as it will result in a runtime exception.

The next four lines contain the definition of a simple method that uses the built-in function Message to print the string to the console.

Build (Ctrl+Shift+B) and run (F5) the following program to see the desired output.