TL; DR:您必须创建一个Q#库项目(这将产生一个.csproj仅包含Q#文件的项目),并从纯F#应用程序中引用它。
您不能在同一项目中混合使用F#和Q#,因为它不会编译:Q#可以编译为C#进行本地模拟,并且您不能在同一项目中使用C#和F#。但是,您可以有两个使用不同语言的独立项目,它们都可以编译成MSIL并且可以互相引用。
这些步骤是:
创建Q#库QuantumCode并在其中编写代码。
假设您的代码有一个带有签名的入口点operation RunAlgorithm (bits : Int[]) : Int[](即,它将整数数组作为参数,并返回另一个整数数组)。
创建一个F#应用程序(为简单起见,使它成为针对.NET Core的控制台应用程序)FsharpDriver。
将对Q#库的引用添加到F#应用程序。
安装NuGet软件包Microsoft.Quantum.Development.Kit,它将对F#应用程序添加Q#支持。
您不会在中编写任何Q#代码FsharpDriver,但需要使用QDK提供的功能来创建运行量子代码的量子模拟器,并定义用于将参数传递给量子程序的数据类型。
用F#编写驱动程序。
// Namespace in which quantum simulator resides
open Microsoft.Quantum.Simulation.Simulators
// Namespace in which QArray resides
open Microsoft.Quantum.Simulation.Core
[<EntryPoint>]
let main argv =
printfn "Hello Classical World!"
// Create a full-state simulator
use simulator = new QuantumSimulator()
// Construct the parameter
// QArray is a data type for fixed-length arrays
let bits = new QArray<int64>([| 0L; 1L; 1L |])
// Run the quantum algorithm
let ret = QuantumCode.RunAlgorithm.Run(simulator, bits).Result
// Process the results
printfn "%A" ret
0 // return an integer exit code
Run Code Online (Sandbox Code Playgroud)我在此处发布了项目代码的完整示例(最初,该项目使用VB.NET中的Q#处理,但对于F#,所有步骤均相同)。