Home - ELIASdigital.com

QSharp: Getting Started - 13 May 2015 at 22:30

Articles about QSharp: Getting Started

C#, F#, Maths, QSharp, Quantum Computing

QSharp: Getting Started

Getting started with QSharp is easy. All that is required is firing up your favourite compiler, adding in the library reference, writing some code, and viewing the output.

Using Microsoft's excellent Visual Studio 2013, you will need to complete the following steps:

  1. Install Nuget (if you don't already have it)
  2. Create a new project
  3. Add the QSharp reference
  4. Write some code and view the output

Installing Nuget

If you haven't installed Nuget yet, then you can do so by visiting https://www.nuget.org/ where you will find the relevant instructions and files you will need to compelted the installation.

Nuget simplifies library distribution and maintenance. There is now no need to visit a vendor's site and obtain updated assembly information, as Nuget will advise you that package updates are available. Nuget also simplifies searching for a specific framework. Some of you might have found QSharp this way, by simply searching Nuget for 'quantum computing'.

Create a New Project

Within Visual Studio, select File, New, Project, choose your language, project type, give the project a name, and click OK. In the following example, I have chosen to create a new Console Application using the C# language.

Add The QSharp Reference

Once your new project has been created, you can add in the QSharp library reference. To do this, right click on the References node in your project, and select Manage Nuget Packages.

Select Online, nuget.org, the search for QSharp.

Once the package has been located, click Install - the package will now be downloaded and the reference will be added into the project. You will now be able to access types made available within the QSharp assembly.

Write Some Code

Now that the project and references have been setup, it's time to write some code. Generally, this process will involve creating a register, executing some command against the register, and viewing the output. Here is a sample project that performs the tasks outline above.

Using C#


#region Using References 

using System; 

using QSharp.Mathematics; 

#endregion 

namespace Example001GettingStarted 
{ 
    public class Program 
    { 
        public static void Main(string[] Arguments) 
        { 
            //  Construct a register, prepared with 3 random qubits
            Register oRegister = new Register(3); 

            //  Write the state vector to the console
            WriteStateVector(oRegister); 

            //  Ensure the register is normalised
            //  A register prepared with random qubits is guaranteed to be normalised
            if (oRegister.IsNormalised() == false) 
            { 
                oRegister.Normalise(); 
            } 

            //  Construct a NOT gate
            PauliXGate oPauliXGate = new PauliXGate(); 

            //  Apply the NOT gate
            oRegister.StateVector = oPauliXGate.ApplyTo(1, oRegister); 

            //  Write the state vector to the console
            WriteStateVector(oRegister); 
        } 

        private static void WriteStateVector(Register Register) 
        { 
            //  Control the output further using these flags
            bool bIsShowAlgebraicValues = true; 
            bool bIsShowCoefficientValues = true; 

            Console.WriteLine(); 

            foreach (ComputationalBasisState oComputationalBasisState in Register.StateVector) 
            { 
                //  By default, both algebraic and coefficient values are displayed - this can be confusing
                Console.WriteLine(oComputationalBasisState); 

                //  Use the overload to control the output
                //  Console.WriteLine(oComputationalBasisState.ToString(bIsShowAlgebraicValues, bIsShowCoefficientValues));
            } 

            Console.WriteLine(); 
        } 
    } 
} 

Using F#


namespace Example001GettingStarted 

open System 

open QSharp.Mathematics 

type Program() = 

    [<EntryPoint>] 
    static let main argv =  

        //  Construct a register, prepared with 3 random qubits
        let oRegister = new Register(3) 

        //  Write the state vector to the console
        Program.WriteStateVector(oRegister) 

        //  Ensure the register is normalised
        //  A register prepared with random qubits is guaranteed to be normalised
        if oRegister.IsNormalised() = false then  
            oRegister.Normalise() 

        //  Construct a NOT gate
        let oPauliXGate = new PauliXGate();  

        //  Apply the NOT gate
        oRegister.StateVector <- oPauliXGate.ApplyTo(1, oRegister);  

        //  Write the state vector to the console
        Program.WriteStateVector(oRegister);  

        0 // return an integer exit code

    static member WriteStateVector(register: Register) =  

        //  Control the output further using these flags
        let bIsShowAlgebraicValues = true;  
        let bIsShowCoefficientValues = true;  

        printfn "" 

        for oComputationalBasisState in register.StateVector do 
            //  By default, both algebraic and coefficient values are displayed - this can be confusing
            let sComputationalBasisState = oComputationalBasisState.ToString() 
            //  Use the overload to control the output
            //  let sComputationalBasisState = oComputationalBasisState.ToString(bIsShowAlgebraicValues, bIsShowCoefficientValues)

            printfn "%s" sComputationalBasisState 

        printfn "" 

 

Copyright © 2017 ELIASdigital.com