Quantum Program The name 'BellTest' does not exist in the current context

妖精的绣舞 提交于 2019-12-10 13:34:58

问题


This is my first Q# program and i'm following this getting started link.https://docs.microsoft.com/en-us/quantum/quantum-writeaquantumprogram?view=qsharp-preview

Error is

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

I followed the steps and when building its having errors. I'm not sure how to import the operations from .qs file to driver c# file as this error looks like it can't find that operation.

Any help is really appreciated

Here is the code

Driver.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();

        }
    }
}

Bell.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);
        }    
    }
}

回答1:


I also got the same error, but I was able to do it by pressing the F5 key.

Perhaps the Visual Studio editor is not yet fully support to the .qs file. Namespace sharing does not seem to be working properly between .cs file and .qs file.

I was able to execute using your code in my development environment.

--

IDE: Visual Studio Community 2017 (Version 15.5.2)
Dev Kit: Microsoft Quantum Development Kit (0 and 1)




回答2:


I engage the same problem in microsoft.quantum.development.kit/0.3.1811.203-preview version.

The BellTest operation cannot recognised by VSC Pic of VSCode

What I do is,

  1. save all but keep VSCode open
  2. go to directory and delete anything in bin/ obj/ by /bin/rm -rf bin obj
  3. dotnet run
  4. you go back to check VSCode, the BellTest recognised by VSC now.


来源:https://stackoverflow.com/questions/47873243/quantum-program-the-name-belltest-does-not-exist-in-the-current-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!