An introduction to Quantum Programming with Q# – Part 1

Over the last few years, quantum computing has evolved so much that it can perform calculations that used to take 10.000 years in just a few minutes [1]. Unfortunately, we still can’t have our own personal quantum computer at home, but until then, we can have fun by testing the available quantum simulators. So let’s get started!

Before starting to code, we need to take a look at a little bit of theory about quantum computing. While in a classic computer any information is stored in a sequence of bits, having value 0 or 1, in a quantum computer the same information is described by one or more qubits. Following the principles of quantum mechanics, a single qubit is characterised by the superposition effect, which allows for simultaneous representation of the Zero state and the One state. The result is a linear combination of these two states, having the following form:

So at any time, a single qubit state corresponds to a linear combination of two complex number, and can be graphically represented by a Bloch sphere (image above). It’s easy to understand that until a measurement is taken, it isn’t possible to know the qubit state, but only calculate the probability of obtaining a particular result. Therefore, developing and executing a quantum program means manipulating a set of qubits to increase the probability of the system collapsing into a specific state.

But how can a quantum computer replicate this behaviour? At the physical level, three different approaches can be used to implement the features of a single qubit:

  • The nuclear spin alignment in a uniform magnetic field.
  • Two different photon polarisations.
  • Two discrete energy levels of an orbiting electron in a single atom.

The power of a quantum computer depends on the number of qubits contained. Companies like IBM, Google and Rigetti work hard to create new quantum devices characterised by an increasing number of qubits, allowing developers to exploit the computing power by the invocation of cloud web services.

Now that we know more about quantum computing, we are ready to create our first quantum application! Currently, there are various quantum computing simulators, and each one is implemented in a different programming language. In this tutorial, we are going to explore the power and the simplicity of the Microsoft quantum programming language, Q#.

This article takes for granted that you have already installed Visual Studio 2019 on your computer. If you haven’t done it yet, I suggest you follow one of the many online available setup tutorials, like this one.

The second thing you need to install is the Microsoft Quantum Development Kit, available here. Once installed, you will be able to create a new Quantum Application by taking the following steps:

  1. Open Visual Studio 2019.
  2. Click on the “Create New Project” button.
  3. Search for “Q# Application” .
  4. Complete the project wizard.
  5. Done! You have created your first quantum application! Easy, right?

Now it’s time to explore the project structure. Let’s begin with the Driver.cs file. By looking inside the Main function, we can see the following lines of code:

  using (var qsim = new QuantumSimulator())
  {
      HelloQ.Run(qsim).Wait();
  }

The QuantumSimulator class allows instantiating a quantum simulator, which is able to replicate the quantum computer behaviour. However, working on traditional CPUs, the effective computing speed can’t be the same as that on a real quantum computer. The Run method allows executing the HelloQ quantum operation, passing the instantiated quantum simulator as a parameter.

By taking a look at the Operation.qs file, we can see the structure of the HelloQ quantum operation. In the code below, we can declare a new quantum function by using the operation keyword, including the function name and the return value(s) type.

operation HelloQ () : Unit 
{
    Message("Hello quantum world!");
}

Great! But now we want to see our program in action! Click on the “Run” button of Visual Studio and you will finally be able to see the message, “Hello quantum world!” in a new execution window.

That’s all for now!
In the next part of this tutorial, we are going to explore some more advanced features of this programming language, developing a more complex example of quantum application.

Happy coding!

Bibliography

[1] Chelsea Whyte, Google has reached quantum supremacy.

Pubblicità