c# - 量子程序名称 'BellTest'在当前上下文中不存在

标签 c# quantum-computing q#

这是我的第一个 Q# 程序,我正在关注此入门链接。 https://learn.microsoft.com/en-us/quantum/quantum-writeaquantumprogram?view=qsharp-preview

错误是

The name 'BellTest' does not exist in the current context but its defined in the Bell.cs

我按照这些步骤进行构建时出现了错误。我不确定如何将操作从 .qs 文件 导入驱动程序 c# 文件,因为这个错误看起来像是找不到该操作。

非常感谢任何帮助

这是代码

驱动.cs

using Microsoft.Quantum.Simulation.Core;
using Microsoft.Quantum.Simulation.Simulators;

namespace Quantum.Bell
{
    class Driver
    {
        static void Main(string[] args)
        {
            using (var sim = new QuantumSimulator())
            {
                // Try initial values
                Result[] initials = new Result[] { Result.Zero, Result.One };
                foreach (Result initial in initials)
                {
                    var res = BellTest.Run(sim, 1000, initial).Result;
                    var (numZeros, numOnes) = res;
                    System.Console.WriteLine(
                        $"Init:{initial,-4} 0s={numZeros,-4} 1s={numOnes,-4}");
                }
            }
            System.Console.WriteLine("Press any key to continue...");
            System.Console.ReadKey();

        }
    }
}

贝尔.qs

namespace Quantum.Bell
{
    open Microsoft.Quantum.Primitive;
    open Microsoft.Quantum.Canon;

    operation Set (desired:Result,q1:Qubit) : ()
    {
        body
        {

             let current = M(q1);

            if (desired != current)
            {
                X(q1);
            }

        }
    }

    operation BellTest (count : Int, initial: Result) : (Int,Int)
    {
        body
        {
            mutable numOnes = 0;
            using (qubits = Qubit[1])
            {
                for (test in 1..count)
                {
                    Set (initial, qubits[0]);

                    let res = M (qubits[0]);

                    // Count the number of ones we saw:
                    if (res == One)
                    {
                        set numOnes = numOnes + 1;
                    }
                }
                Set(Zero, qubits[0]);
            }
            // Return number of times we saw a |0> and number of times we saw a |1>
            return (count-numOnes, numOnes);
        }    
    }
}

最佳答案

我也遇到了同样的错误,但我可以通过按 F5 键来完成。

可能 Visual Studio 编辑器还没有完全支持 .qs 文件。 .cs 文件和 .qs 文件之间的命名空间共享似乎无法正常工作。

我能够在我的开发环境中使用您的代码执行。

--

IDE:Visual Studio Community 2017(版本 15.5.2)
开发工具包:Microsoft Quantum 开发工具包(0 和 1)

关于c# - 量子程序名称 'BellTest'在当前上下文中不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47873243/

相关文章:

c# - 如何确定我的应用程序的控制台窗口何时获得或失去焦点?

c# - 为什么泛型 IList<> 不继承非泛型 IList

quantum-computing - 什么是 QBit,我多久可以得到一台量子计算机?

c# - 我可以在 Q# 中使用 lambda 来对量子位进行操作吗?

c# - 滚动没有发生

c# - 如何在c#中获取Json值

algorithm - P 与 NP 是否与经典计算机与量子计算机可解决的问题相同?

visual-studio-2015 - 在 Q# Quantum 开发工具包中出现错误 "Released qubits are not in zero state"

quantum-computing - Q#中如何实现格罗弗扩散算子?