Q# language: How to write quantum code in Visual Studio

Microsoft is making a big bet on a quantum computing future. Here’s how you are going to write code that uses those new machines

Q# language: How to write quantum code in Visual Studio
IBM Research

The future of the computer isn’t silicon; we’re already at the limits of Moore’s Law in terms of the performance we can get from traditional transistors. We’re also working on much bigger problems, especially when it comes to cryptography and in mathematical modeling; problems that require days of compute time even on the largest supercomputers.

So where do we go from here? Microsoft Research, like Google and IBM, has been investing heavily in quantum computing. Much of its research has been in basic physics, working with universities around the world to produce efficient low-temperature environments and stable quantum computing environments. But creating a qubit—the probabilistic quantum bit that essentially replaces the 0’s and 1’s of a traditional bit—is only part of the story. What’s also needed is a way to program a quantum computer and interpret the qubits’ probabilistic state.

Constructing quantum computers

The architecture of a quantum program is relatively simple: A traditional program gets values from user input or from other code. It then passes those values to a quantum application that sets the qubits in a quantum processor, using one of many quantum algorithms, before passing the results back to the parent application.

It’s a process very similar to the one I used at my first programming job, writing Fortran finite-element analysis code that used a vector processor attached to a supercomputer to handle matrix algebra. The vector libraries I used to build and solve my 3D electromagnetic models worked on both that specialized hardware or on a math coprocessor in a desktop workstation, so I could test my code before using expensive supercomputer time.

Microsoft recently released its Quantum Development Kit, built around its new Q# language. Designed to use familiar constructs to help program applications that interact with qubits, it takes a similar approach to working with coprocessors, providing libraries that handle the actual quantum programming and interpretation, so you can write code that hands qubit operations over to one Microsoft’s quantum computers.

Bridging the classical and quantum computing worlds isn’t easy, so don’t expect Q# to be like Visual Basic. It is more like using that set of Fortran mathematics libraries, with the same underlying assumption: that you understand the theory behind what you’re doing.

One element of the Quantum Development Kit is a quantum computing primer, which explores issues around using simulators, as well as providing a primer in linear algebra. If you’re going to be programming in Q#, an understanding of key linear algebra concepts around vectors and matrices is essential—especially eigenvalues and eigenvectors, which are key elements of many quantum algorithms.

Getting started with Q#

The development kit downloads as a Visual Studio extension, so you can use it with all versions of Microsoft’s main development environment, including the free Community edition. The installer includes the Q# language, a local quantum simulator, and libraries that support embedding Q# modules in your .Net code. Once installed, you can connect to Microsoft’s Q# Github repository to clone and download sample code and additional libraries. It’s a quick process; the installer takes a couple of minutes to download and run on a reasonably powerful development PC. The libraries are hosted on Nuget, so you can quickly update to the latest versions.

With a working quantum computer still a few years away, the Quantum Development Kit is limited to working with simulated quantum computers. Microsoft’s research systems have yet to produce a working topological qubit, but results have been promising. So, until there are published results and Azure gets its quantum coprocessors, you’re limited to experimenting with local- and cloud-hosted simulators. Because they’re limited to using traditional programming techniques, they’re not going to handle the full range of complex mathematical operations that quantum computing promises. But they do give a feel for what a small number of qubits can do.

Much of the work you need to do in building a quantum program is in constructing a quantum computer out of qubit transformations. The Q# language handles the process for you, because it includes expressions for many quantum gate structures, as well as common quantum algorithms. The language itself will feel familiar to .Net developers, with a structure that’s somewhere between C# and F#.

quantum code in Q# IDG

A test run of a quantum teleportation app on a simulator running in Visual Studio.

Quantum programming basics

You’ll find most Q# programs relatively simple, because what you’re doing is setting up arrays of qubits and applying mathematical transformations to them. Although the underlying problem is complex (or at least likely to take a lot of compute time using traditional compute resources), you’re relying on the quantum computer to handle the work for you, and its quantum algorithms mean you can use a small number of connected qubits to solve your problem.

One important thing to note is that some quantum languages, like the one used by DWave in its quantum computers, are designed to work with quantum annealing, not the gate model used in Microsoft’s quantum hardware.

Where the Q# language differs from the familiar is in its support for quantum algorithms. This starts with types: Q# is a strongly typed language, adding new types that represent qubits and groups of qubits. Another key difference is between Q# operations and functions. Operations contain quantum operations, while functions are purely for classical code, though they can work with the results from a quantum operation.

Quantum algorithms and libraries

Q# also includes specific operation types that work with quantum algorithms, including those that calculate the adjoint results of a matrix of qubits, and others that help construct qubit circuits, triggered only if control qubits are correctly set.

It’s important to remember that where Q# uses Zero and One in results as variables to handle qubits, they’re not the same as binary 0 and 1. Instead they’re representations of the eigenvalues of the vectors stored in the qubits.

You use the Q# standard libraries to build and construct your quantum applications. These include a set of quantum primitives that define the gates you’re building out of your qubits, as well as applying quantum operators and measuring the results. The libraries are divided into two parts: the prelude for setting up your quantum computer, and the canon for operating the machine. It’s important to understand the differences between these two parts of the libraries, because they need to kept separate in your code. Using the canon operators runs the quantum machine, with operators that handle specific quantum algorithms; for example, applying a Quantum Fourier Transform or finding common divisors of two numbers.

Q# isn’t a language for beginners. Although it simplifies some quantum operations, it does depend on having a knowledge of how a quantum computer operates, as well as understanding the basics of quantum computation. If you’ve worked with linear algebra and probabilities, you’ll have a head start, but it’s still worth spending time first with Microsoft’s tutorials and samples.

Copyright © 2018 IDG Communications, Inc.