Q# is packaged with Microsoft Quantum Development Kit and is currently supported on 64-bit environments only. To begin with, you should have Visual Studio 2019 installed along with .NET core 3.1 SDK. Read my article on Getting started with Quantum Programming if you haven’t done that already.
Installing QDK
Install Microsoft Quantum Development Kit from Visual Studio Marketplace. Just download the extension and double click the file to open it in Visual Studio installer. Please note that this extension is currently compatible with Visual Studio 2019 only.
Creating Q# Project
Once QDK is installed, you are ready to write your first Q# program. Open Visual Studio 2019, click on Create a new Project. In the “Search for templates” bar type Q#. In search result you will see several options like Q# Application, Q# Library, Q# Test Project and so on. Select Q# Application.
Once you create the project, By default, scaffolding feature in Visual Studio will generate a Hello World code for you.
Hello World
Your solution will look similar to this:
Program.qs is the file were the actual Q# program is present.
namespace Quantum.QSharpApplication1 {
//open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Intrinsic;
@EntryPoint()
operation HelloQ () : Unit {
Message("Hello Q# World!");
}
}
The csproj file will have the following content:
<Project Sdk="Microsoft.Quantum.Sdk/0.22.187631">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>
Run the project using F5 or Ctrl+F5.
Understanding the Hello World code
In earlier versions of QDK, you would need two projects or applications to run a Quantum Program – a Driver application and a Quantum application.
In the latest version of QDK, you can do it in one single application, using the @EntryPoint annotation. @EntryPoint() defines the first operation that must be invoked at application start up. It is equivalent to the Main method in C#. operation
is nothing but what you would call a function
in todayβs programming languages. HelloQ
is the name of the operation
used in this program.
Unit
is the return type of the operation HelloQ
. Unit
is equivalent to void
, meaning the operation HelloQ
does not return any value. Other return types present in Q# are Int
, Double
, BigInt
, Bool
, String
, Range
, Result
, Qubit
, etc.
open
is equivalent to using
keyword in C#. In the hello world example here, open Microsoft.Quantum.Intrinsic;
imports the given library which contains the definition of the operation Message
. Message
is similar to the C# function Console.Write
Troubleshooting NuGet Packages
I have observed that Visual Studio struggles to download Microsoft.Quantum.* packages for the first time. You might see dependencies related errors in your Project
If you try to download the package using NuGet Package Manager Console, you might end up getting another error that says – Install-Package : The package is missing the required nuspec file. Path: C:\Users\the.user\.nuget\packages\fsharp.core\4.7.0 To resolve this problem, in your Visual Studio menu bar, goto Tools -> Options -> Nuget Package Manager and select Clear All NuGet Cache(s)
Next, right check on your project and select Rebuild. And Vola! NuGet will be able to restore the packages!
Conclusion
We have not invoked anything related to Quantum programming in this Hello World
program. This program just ensures that your QuantumSimulator setup is ready to be explored.
Stay tuned for more complicated quantum programs and welcome to the Quantum World of programming