简体   繁体   English

为什么 Q# 没有检测到 F# 或 C# 命名空间?

[英]Why Q# does not detect either F# or C# namespace?

I am making a library in Q# and trying to add a project reference to an F# (or C#) library written by me.我正在 Q# 中创建一个库,并尝试将项目引用添加到我编写的 F#(或 C#)库。 But it does not work.但它不起作用。

After adding the appropriate reference, I checked my Q# project.csproj file and I could see that the reference was added there that means the process of adding was done properly.添加适当的引用后,我检查了我的 Q# project.csproj 文件,我可以看到引用已添加到那里,这意味着添加过程已正确完成。 But when I try to run the code I receive this message: "error QS6104: No namespace with that name exists" which refers to the line including the name of that F# (or C#) library.但是当我尝试运行代码时,我收到以下消息:“错误 QS6104:不存在具有该名称的命名空间”,它指的是包含该 F#(或 C#)库的名称的行。 I wonder why it happens.我想知道为什么会这样。 Is it due to some fundamental differences between Q# and F# but Q# is created based on F# and C# therefore is there any solution to solve the problem?是不是因为 Q# 和 F# 之间存在一些根本差异,但 Q# 是基于 F# 和 C# 创建的,因此有什么解决方案可以解决这个问题吗? Here is my code snippet:这是我的代码片段:

    namespace MyQSharpLibrary
    {
        open Microsoft.Quantum.Canon;
        open Microsoft.Quantum.Intrinsic;
        open MyFSharpLib;
        ...
        ...
    }

MyFSharpLib is the library I defined in F# and after adding reference to it inside "MyQSharpLibrary" I can see following markdown code in MyQSharpLibrary.csproj: MyFSharpLib 是我在 F# 中定义的库,在“MyQSharpLibrary”中添加对它的引用后,我可以在 MyQSharpLibrary.csproj 中看到以下 markdown 代码:

    <Project Sdk="Microsoft.NET.Sdk">
      ...
      ...
       <ItemGroup>
        <ProjectReference Include="..\MyFSharpLib\MyFSharpLib.fsproj"/>
       </ItemGroup>  
    </Project>

But after running MyQSharpLibrary, I receive error message: "error QS6104: No namespace with that name exists" which refers to the line containing "open MyFSharpLib;"但在运行 MyQSharpLibrary 后,我收到错误消息:“错误 QS6104:不存在具有该名称的命名空间”,它指的是包含“open MyFSharpLib;”的行

open directives in Q# only allow you to import Q# code, not F# or C# code. Q# 中的open指令只允许您导入 Q# 代码,而不是 F# 或 C# 代码。 Q# does not in general allow execution of arbitrary classical code, since it is designed to be executed on a quantum device which in general might not have this ability. Q# 通常不允许执行任意经典代码,因为它被设计为在通常可能不具备此能力的量子设备上执行。


If you're looking for a workaround that can be done in a simulation but will not be portable to a quantum device, you can write a simulator that extends one of the existing ones and exposes new intrinsic functions to Q# code that implement your classical computation.如果您正在寻找一种可以在模拟中完成但不能移植到量子设备的解决方法,您可以编写一个模拟器来扩展现有的一个模拟器,并向 Q# 代码公开新的内在函数来实现您的经典计算。

For example, take a look at what the Quantum Katas do to gather statistics about the program execution: they define CounterSimulator which implements intrinsic functions such as GetOracleCallsCount , and a matching Q# code in Utils.qs which exposes them to Q# code.例如,看看 Quantum Katas 是如何收集有关程序执行的统计信息的:它们定义了CounterSimulator实现了诸如 GetOracleCallsCount 之类的内在函数,以及GetOracleCallsCount中的匹配 Q# 代码, 它将它们暴露给 Q# 代码。 Then this project is added as a dependency to the katas that need this functionality, for example, DeutschJozsaAlgorithm - you'll see the project reference in DeutschJozsaAlgorithm.csproj file and a matching open Quantum.Kata.Utils;然后将此项目作为依赖项添加到需要此功能的 katas,例如DeutschJozsaAlgorithm - 您将在DeutschJozsaAlgorithm.csproj文件中看到项目引用以及匹配的open Quantum.Kata.Utils; in Tests.qs .Tests.qs中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么C#方法无法使用VS找到Q#操作的名称? - Why can't C# method find the name of a Q# Operation using VS? 无法在 Visual Studio Code 中构建 Q#/C# 项目 - Not able to build Q#/C# project in Visual Studio Code F#和C#的CLR相同,那么为什么F#比C#快 - F# and C# 's CLR is same then why is F# faster than C# 为什么嵌套字典的反序列化会在C#中引发异常但在F#中有效? - Why does deserialization of nested dictionary throws an exception in C# but works in F#? 如果将C#字符串与F#analogue进行比较,为什么C#字符串的标题为“不可变”? - Why does C# string have a title “Immutable” if to compare it with F# analogue? C#中的“对象表达”是否存在于F#中? - Does “Object Expression” exist in C# like in F#? 为什么 F# 代码产生与 C# 不同的结果 - Why F# code yields different result than C# 为什么这个F#代码比C#代码慢? - Why is this F# code slower than the C# equivalent? 如何在QuantumSimulator Run()命令中将C#数组传递到Q#操作中? - How to pass C# array into a Q# operation within QuantumSimulator Run() command? 从同一解决方案打开 c# 命名空间,在 f# 中引用项目 - open c# namespace from same solution, referenced project in f#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM