[英]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.