One of the hottest stories in technology right now is the emergence of Quantum computing. There are great expectations that it will forever alter our economic, industrial, academic, and societal landscape by solving complex problems that are beyond the capability of today’s most powerful machines. As introduced in “Getting Started with Microsoft Quantum Computing Programming and Q#,” you don’t have to wait for the arrival of Quantum computers to start developing programs for them. Microsoft has released a development kit for Developer Studio that can emulate qubits, which are the quantum equivalent to the traditional binary bit. In this tutorial, we’ll get a feel for the Q# language by writing a very simple “Hello World”-level program that adds some numbers together and displays the result in a console.
Creating the Project
This tutorial assumes that you’ve already downloaded and installed the Microsoft Quantum Development Kit. If you haven’t, please refer to the previously mentioned Codeguru article for details on how to get started.
In Visual Studio 2017, go to File > New Project > Visual C#. You should see three Q# project templates: Q# Application, Q# Library, and Q# Test Project.
Select the “Q# Application” project template, give your project a name of “ASimpleQSharpApplication,” and click OK. You might want to check “Create directory for solution” as well.
Figure 1: Starting a new project
That will create the project, as well as generate a couple of files: the quantum code (Operation.qs) and C# driver (Driver.cs). These two files are shown next.
Operation.qs:
namespace Quantum.ASimpleQSharpApplication { open Microsoft.Quantum.Primitive; open Microsoft.Quantum.Canon; operation Operation () : () { body { } } }
Driver.cs:
using Microsoft.Quantum.Simulation.Core; using Microsoft.Quantum.Simulation.Simulators; namespace Quantum.ASimpleQSharpApplication { class Driver { static void Main(string[] args) { } } }
The C# driver code contained in the Driver.cs file is the entry point of the application. This code will look familiar to you C# developers out there. Perhaps it shouldn’t be surprising that a language called Q# is similar to C#.
Coding the Add Operation
In Q#, an operation describes a side effect which quantum operations can have on quantum data and one or more functions which allow to modify classical data. Our program consists of one operation, named Add(). Q# programs may consist of many operations.
Our simple operation accepts two integers and returns their sum. Here’s the full code:
namespace Quantum.ASimpleQSharpApplication { open Microsoft.Quantum.Primitive; operation Add (a : Int, b : Int) : (Int) { body { return (a + b); } } }
To refer the Int type directly, we can import the Primitive namespace.
An operation begins with the “operation” keyword. An operation can accept zero or more arguments and can return either nothing or a value. Passing multiple values to a method through a single parameter as we are doing here are a form of tuple. First introduced as a part of .NET Framework 4.0, a tuple is a data structure that provides an easy way to represent a single set of data. To learn more about tuples, check out “Introduction to Tuples in .NET Framework 4.0.” An argument’s type is defined using a colon. The return type is also denoted after the arguments by a colon. Unlike traditional classical computing functions, an operation has an enclosed body section that contains the operation instructions.
Running Our Application
As mentioned earlier, the Driver class is the gateway to the program. As in the Java language, the static void Main() method is the application starting point. Because there is no real quantum processor, the MS Q# Development Kit employs a quantum simulator to execute the Q# program. The QuantumSimulator class is defined in the Microsoft.Quantum.Simulation.Simulators namespace.
Here’s the completed Driver code:
using System; using Microsoft.Quantum.Simulation.Core; using Microsoft.Quantum.Simulation.Simulators; namespace Quantum.ASimpleQSharpApplication { class Driver { static void Main(string[] args) { using (var sim = new QuantumSimulator()) { var res = Add.Run(sim, 100, 50).Result; Console.WriteLine(res); Console.WriteLine("Press any key to exit."); Console.ReadKey(); } } } }
In the preceding code, the using (var sim = new QuantumSimulator()) line instantiates the simulator.
The Add operation is then executed by invoking operation.Run(simulator, arguments). Its Result is then stored in a local var and written to the console.
Fixing Namespace Errors
When I tried to build my project, it failed with numerous namespace-related errors, such as:
The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
It turns out that this issue is not uncommon, at least in Windows. To fix it, you need to launch the Package Manager Console from within Visual Studio, by using the Tools > NuGet Package Manager > Package Manager Console command.
Then, from the Package Manager (PM) prompt, issue the “dotnet restore” command and press the <Enter> key.
It’ll take a couple of minutes, but after that, the PM> prompt will return.
Each package is licensed to you by its owner. NuGet is not responsible for, nor does it grant any licenses to, third-party packages. Some packages may include dependencies which are governed by additional licenses. Follow the package source (feed) URL to determine any dependencies. Package Manager Console Host Version 4.5.0.4685 Type 'get-help NuGet' to see all available NuGet commands. PM> dotnet restore Welcome to .NET Core! --------------------- Learn more about .NET Core @ https://aka.ms/dotnet-docs. Use dotnet --help to see available commands or go to https://aka.ms/dotnet-cli-docs. Telemetry -------------- The .NET Core tools collect usage data in order to improve your experience. The data is anonymous and does not include command-line arguments. The data is collected by Microsoft and shared with the community. You can opt out of telemetry by setting a DOTNET_CLI_TELEMETRY_OPTOUT environment variable to 1 using your favorite shell. You can read more about .NET Core tools telemetry @ https://aka.ms/dotnet-cli-telemetry. Restoring packages for C:\Users\blackjacques\source\repos\ ASimpleQSharpApplication\ASimpleQSharpApplication\ ASimpleQSharpApplication.csproj... Installing Microsoft.Quantum.Development.Kit 0.2.1802.2202-preview. Installing Microsoft.Quantum.Canon 0.2.1802.2202-preview. Generating MSBuild file C:\Users\blackjacques\source\repos\ ASimpleQSharpApplication\ASimpleQSharpApplication\obj\ ASimpleQSharpApplication.csproj.nuget.g.props. Generating MSBuild file C:\Users\blackjacques\source\repos\ ASimpleQSharpApplication\ASimpleQSharpApplication\obj\ ASimpleQSharpApplication.csproj.nuget.g.targets. Restore completed in 1.03 min for C:\Users\blackjacques\ source\repos\ASimpleQSharpApplication\ ASimpleQSharpApplication\ASimpleQSharpApplication.csproj. PM>
Now, when you build your project, it should work, even though the Driver code will still appear to contain errors.
Finally, click the Run button. You then should see a command prompt window appear with the result of 150, followed by the “Press any key to exit.” text.
Conclusion
Quantum computing promises to improve our lives in a number of ways, both big and small. Perhaps most exciting, is that it offers the possibility of finally creating machine intelligence that far surpasses our own. Finding out how that affects mankind will be fascinating, to say the least! By getting ahead of the curve, you yourself may be the one to make this pivotal breakthrough!