Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Latest commit

 

History

History
95 lines (66 loc) · 3.12 KB

csinstall.md

File metadata and controls

95 lines (66 loc) · 3.12 KB
title author ms.author ms.date ms.topic ms.custom uid
Develop with Q# + C#
natke
nakersha
9/30/2019
article
how-to
microsoft.quantum.install.cs

Using Q# with C# and F#

Q# is built to play well with .NET languages such as C# and F#. In this guide, we'll demonstrate how to use Q# with a host program written in a .NET language.

Prerequisites

  • Install the Quantum Development Kit for use with Q# command-line projects.

Creating a Q# library and a .NET host

The first step is to create projects for your Q# library, and for the .NET host that will call into the operations and functions defined in your Q# library.

  • Create a new Q# library
    • Go to File -> New -> Project
    • Type "Q#" in the search box
    • Select Q# Library
    • Select Next
    • Choose a name and location for your library
    • Make sure that "place project and solution in same directory" is unchecked
    • Select Create
  • Create a new C# or F# host program
    • Go to FileNewProject
    • Select "Console App (.NET Core")" for either C# or F#
    • Select Next
    • Under solution, select "add to solution"
    • Choose a name for your host program
    • Select Create
  • Create a new Q# library

    dotnet new classlib -lang Q# -o quantum
    
  • Create a new C# or F# console project

    dotnet new console -lang C# -o host  
    
  • Add your Q# library as a reference from your host program

    cd host
    dotnet add reference ../quantum/quantum.csproj
    
  • [Optional] Create a solution for both projects

    dotnet new sln -n quantum-dotnet
    dotnet sln quantum-dotnet.sln add ./quantum/quantum.csproj
    dotnet sln quantum-dotnet.sln add ./host/host.csproj
    

Calling into Q# from .NET

Once you have your projects set up following the above instructions, you can call into Q# from your .NET console application. The Q# compiler will create .NET classes for each Q# operation and function that allow you to run your quantum programs on a simulator.

For example, the .NET interoperability sample includes the following an example Q# operation:

:::code language="qsharp" source="~/quantum/samples/interoperability/dotnet/qsharp/Operations.qs" range="67-75":::

To call this operation from .NET on a quantum simulator, you can use the Run method of the ReconstructOracleParameters .NET class generated by the Q# compiler:

:::code language="csharp" source="~/quantum/samples/interoperability/dotnet/csharp/Host.cs" range="4-":::

:::code language="fsharp" source="~/quantum/samples/interoperability/dotnet/fsharp/Host.fs" range="4-":::


What's next?

Now that you have Quantum Development Kit set up for both Q# command-line programs, and for interoperability with .NET, you can write and run your first quantum program.