2
$\begingroup$

My goal in writing this algorithm in Q# was that func would either output (1,2) or (10,20), since the output result can be either One or Zero. However, I sometimes have (1,20) or (10,2) as output. Does anyone know why this happens?

    operation func () : (Int,Int)
{
    mutable res000 = Zero;
    mutable int1 = 0;
    mutable int2 = 0;

    using (goop = Qubit[1])
    {

       H(goop[0]);

       set res000 = M(goop[0]);


       if(res000 == Zero)
       {
           set int1 = 1;
           set int2 = 2;
       }
       else
       {
           set int1 = 10;
           set int2 = 20;
       }

       ResetAll(goop);
    }

    return (int1,int2);
}

Edit:

Here's another bit of information. I also have two projection functions, and I want the projection functions to output func:

    operation Pr0 (m:Int,n:Int) : Int
{
    return m;
}

operation Pr1 (m:Int,n:Int) : Int
{
    return n;
}

operation func () : (Int,Int)
{
    mutable res000 = Zero;
    mutable int1 = 0;
    mutable int2 = 0;

    using (goop = Qubit[1])
    {

       H(goop[0]);

       set res000 = M(goop[0]);


       if(res000 == Zero)
       {
           set int1 = 1;
           set int2 = 2;
       }
       else
       {
           set int1 = 10;
           set int2 = 20;
       }

       ResetAll(goop);
    }

    return (int1,int2);
}

operation testPr1 () : (Int,Int)
{
    return (Pr0(func()),Pr1(func()));
}

Here are the C# codes:

    class Driver
{
    static void Main(string[] args)
    {
        using (var sim = new QuantumSimulator())
        {
            var res3 = testPr1.Run(sim).Result;
            Console.WriteLine(res3);

        }

        Console.WriteLine("Press any key to continue...");
        Console.ReadKey();
    }
}
$\endgroup$
  • 1
    $\begingroup$ Could you please provide more details about your environment - OS, editor, QDK version? This should not be the case and I can't reproduce this behavior on my setup. $\endgroup$ – Mariia Mykhailova Nov 26 '18 at 19:09
  • 1
    $\begingroup$ @Mariia I use Visual Studio 2017 (Community Version) on Windows. The QDK version is 0.3.18.10. That phenomenon happens when I run the program several times. $\endgroup$ – qubb Nov 26 '18 at 22:10
  • $\begingroup$ @qubb: Would you be willing to provide the C# code that you used to call into the Q# code above? Thanks! $\endgroup$ – Chris Granade Nov 27 '18 at 3:01
  • $\begingroup$ @Mariia, Chris: Please see the edit. $\endgroup$ – qubb Nov 28 '18 at 1:07
3
$\begingroup$

Thanks for posting the full Q# code! The problem is in testPr1: you're calling func() twice, and returning the first element of the tuple returned from the first call, and the second element of the tuple returned from the second call. Each call operates on a different qubit and performs a separate random measurement, so all 4 possible combinations should show up.

To get the results you're looking for, try replacing the body of testPr1 with something like:

let res = func();
return (Pr0(res), Pr1(res));
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.