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

2

6

This is my first Q# program and i'm following this getting started link.https://learn.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);
        }    
    }
}
Nephrosis answered 18/12, 2017 at 17:9 Comment(1)
I received that error a couple of times when I had a compile error in the q# code.Pikestaff
P
3

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)

Pileup answered 20/12, 2017 at 1:10 Comment(2)
thank you i reinstalled the kit and it worked...not sure what was the reasonNephrosis
I have the latest build and I get the same error on VisualStudioCode and template 0.3.1810.2508-preview". F5 does not help actually.Babb
S
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.
Sicard answered 14/11, 2018 at 5:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.