2020年7月17日金曜日

Rewriting sample codes in Quantum Algorithm Implementations for Beginners (Bell state)

I have changed the strategy of learning Quantum algorithms.
Now, I am following  "Quantum Algorithm Implementations for Beginners".

The first trial is to construct Bell state and check if the understanding is correct.
By running my sample code the numbers of measurements observed in 1000 trials are;
|00>:494, |01>:0, |10>:0, |11>:506
There is a good agreement with the textbook

A sample code is;
Q# (Program.qs)
namespace Bell2 {

    open Microsoft.Quantum.Canon;
    open Microsoft.Quantum.Intrinsic;
    
    operation Bell() : (Int,Int,Int,Int) {
        Message("Hello quantum world!");

        mutable n00 = 0;
        mutable n01 = 0;
        mutable n10 = 0;
        mutable n11 = 0;

        for(trian in 1..1000){
        using ( (qubit0, qubit1) = (Qubit(), Qubit()) ){
            H(qubit0);
            CNOT(qubit0,qubit1);

            let res0 = M(qubit0);
            let res1 = M(qubit1);

            if(res0 == Zero){
                if(res1 == Zero){
                    set n00 += 1;
                }else{
                    set n01 += 1;
                }
            }else{
                if(res1 == Zero){
                    set n10 += 1;
                }else{
                    set n11 += 1;
                }

            }
            Reset(qubit0);
            Reset(qubit1);
        }
        }

        //Message("00:#, 01:#, 10:#, 11:#");
        return(n00,n01,n10,n11);
    }
}

C# (Driver.cs)

using System;

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

namespace Bell2
{
    class Driver
    {
        static void Main(string[] args)
        {
            using (var qsim = new QuantumSimulator())
            {
            long a,b,c,d;
            (a,b,c,d) = Bell.Run(qsim).Result;
            Console.WriteLine("{0},{1},{2},{3}",a,b,c,d);
            }

            
        }
    }
}

Otherwise, files are created automatically when one creates a new project, namely,
  • Go to View -> Command Palette
  • Select Q#: Create New Project
  • Select Standalone console application
  • Navigate to the location on the file system where you would like to create the application
  • Click on the Open new project... button, once the project has been created

0 件のコメント:

コメントを投稿