author | description | ms.author | ms.date | ms.service | ms.subservice | ms.topic | no-loc | title | uid | ||
---|---|---|---|---|---|---|---|---|---|---|---|
bradben |
Learn how to create a Q# application using .NET languages. Q# is built to work well with .NET languages such as C# and F#. |
brbenefield |
03/30/2022 |
azure-quantum |
qdk |
how-to |
|
Develop with Q# and .NET |
microsoft.quantum.install-qdk.overview.cs |
The Q# programming language is built to work well with .NET languages such as C# and F#. In this guide, we demonstrate how to use Q# with a host program written in a .NET language.
First we create the Q# application and .NET host, and then demonstrate how to call to Q# from the host.
- Install the Quantum Development Kit (QDK) for use with Q# projects.
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.
Follow the instructions in the tab corresponding to your development environment. If you are using an editor other than Visual Studio or VS Code, simply follow the command prompt steps.
-
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
cd .. dotnet new sln -n quantum-dotnet dotnet sln quantum-dotnet.sln add ./quantum/quantum.csproj dotnet sln quantum-dotnet.sln add ./host/host.csproj
- 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 File → New → Project
- 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
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 example of a Q# operation:
/// Instantiates the oracle and runs the parameter restoration algorithm.
operation RunAlgorithm(bits : Bool[]) : Bool[] {
Message("Hello, quantum world!");
// construct an oracle using the input array
let oracle = ApplyProductWithNegationFunction(bits, _, _);
// run the algorithm on this oracle and return the result
return ReconstructOracleParameters(Length(bits), oracle);
}
To call this operation from .NET on a quantum simulator, you can use the Run
method of the RunAlgorithm
.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-":::
Now that you have the Quantum Development Kit set up for both Q# applications and interoperability with .NET, you can write and run your first quantum program.