Q# environment setup and first software

Before we go directly to writing code and start creating our first quantum programs, it is necessary to prepare the environment. Q# can be run in Visual Studio, Visual Studio Code or using python and Jupyter Notebooks. In this walkthrough, you will work using Visual Studio 2022.

 
Setting up the environment itself is not particularly difficult. Because as we have Visual Studio 2022 installed, we only need to install Microsoft Quantum Development Kit available at:
https://marketplace.visualstudio.com/items?itemName=quantum.DevKit64
 
It will allow us to create projects using the Q# language, recognize Q# syntax, use quantum simulators and libraries.
 
Quantum computers actually work more like coprocessors and for their work it is necessary to use classical computers that send requests with initial parameters and wait for the results. Therefore, at the beginning, let’s create a Console Application called Host and a static Program class in it.

 

using Microsoft.Quantum.Simulation.Simulators;
using Quantum.QuantumDreamLibrary;
using System;

namespace QuantumDream
{
    static class Program
    {
        static void Main(string[] args)
        {
            using (var sim = new QuantumSimulator())
            {
                
                  for (int i = 0; i < 100; i++)
                  {
                      var result = GetRandomResult.Run(sim).Result;
                      Console.WriteLine(result);
                  }

                Console.WriteLine("Press any key to continue");
                Console.ReadKey();
            }
        }

    }
}

This is standard C# code, and there is nothing unusual about the syntax. However, it is worth paying attention to the imports from the quantum world and the lines:

using (var sim = new QuantumSimulator())

As we use a classic computer without access to the real functionalities of a quantum computer, we have to use a simulator, just like in other programming industries, e.g. mobile programming. There are several types of simulators, but the simulator used here is a stateful simulator that fully simulates a quantum machine on a local computer. It is worth remembering, however, that such a simulator is limited to about 30 qubits.
Another important line is:

var result = GetRandomResult.Run(sim).Result;

This is our reference to the quantum operation. In this case, it will simply draw a number (0 or 1). We pass the created simulator and any parameters to the Run Method (in this case, there are none). Then we wait for the result (Result – an object from the quantum library with possible results Zero and One)

Then we create what probably many users are most interested in, that is a quantum project using Q#. We add a Q# Library project to our solution.

namespace Quantum.QuantumDreamLibrary {

    open Microsoft.Quantum.Canon;
    open Microsoft.Quantum.Intrinsic;

    @EntryPoint()  //Creating entry point for operation
    operation GetRandomResult() : Result { //Operation returns Result (Zero or One)
        use q = Qubit(); //assigning qubit, it's state is |0>
        H(q); //using Hadamar gate, puts the qubit into superpositions state where the chances of getting |0> and |1> are equal to 50%
        return M(q); //Taking measurement and returning a value
    }
}

For everything to work, it is necessary to download the quantum libraries and add references between the host project and the library.

Running the projects should give us random qubit results.

The entire project is available for download here

Leave a Comment

WordPress Video Lightbox Plugin