Q# – Operation Type

In Q#, operation is a callable routine. The routine contains code to execute a quantum operation. The operation takes tuple value as input and the output is returned as a single value. The sample code is shown below for operation.

operation GetSum(x: Int, y: Int): Int { 

    body { 

        mutable z = 0; 

        set z = x + y; 

        return (z); 

    }

Q#  project and solution is created using the following commands.

dotnet new console -lang Q# --output Hello

Visual  Studio Code is used to open the project. The project will have two files Driver.cs (C# driver) and Operations.qs (Q# ). The Operations.qs is renamed as Hello.qs.

The code snippet below shows the Hello.qs after adding the operation GetSum.

Code Snippet : GetSum Example

namespace Hello

{

    open Microsoft.Quantum.Canon;

    open Microsoft.Quantum.Primitive;

    operation GetSum(x: Int, y: Int): Int { 

    body { 

        mutable z = 0; 

        set z = x + y; 

        return (z); 

    

}

}

The Driver file will have the following code to execute GetSum function.

using System;

using Microsoft.Quantum.Simulation.Core;

using Microsoft.Quantum.Simulation.Simulators;

namespace Hello

{

    class Driver

    {

        static void Main(string[] args)

        {

            using (var qsim = new QuantumSimulator())

            {

                System.Console.WriteLine("Sum of 4 and 5 is");

                         var result = GetSum.Run(qsim,4,5).Result;

                System.Console.WriteLine(result);

            }

        }

    }

}

Run the following commands  to execute the project in Visual Studio Code:

^F5 

In the terminal, you can execute the build by running the command below.

dotnet run


The screenshot of the output is attached below:

 

GetSum

Leave a comment