2020年10月27日火曜日

2020年10月23日金曜日

Paiza Rank-A

After several attempts, I reached Rank-A in Paiza. 
Hope this helps my job hunting :)


Quantum Fourier Transform (QFT) Part 1

I'm trying to implement Quantum Fourier Transform (QFT), not from "Quantum Algorithm Implementations for Beginners" but Quantum Dojo (https://dojo.qulacs.org/ja/latest/notebooks/2.3_quantum_Fourier_transform.html# ).
That's because "Beginners" does not explain the algorithm in detail.

Quantum Dojo's one needs the computation of  1/sqrt(8)(|000>+|001>+,,,+|111> (in 3 qubits case), which is not clear in Q#.

I think this could be formed by applying the Adamard Gate for all qubits, and in order to make sure that I am correct, I have implemented the following code.

The results are:117, 107, 125, 138, 128, 144, 133, 108.
Although there are errors because of random numbers, in general looks OK.

namespace QFT_naive {
    open Microsoft.Quantum.Canon;
    open Microsoft.Quantum.Intrinsic;
    open Microsoft.Quantum.Convert;
    open Microsoft.Quantum.Arrays;
    

    @EntryPoint()
    operation SayHello() : Unit {
        Message("Hello quantum world!");
        mutable counts = new Int[8];
        mutable nres0 = 0;
        mutable nres1 = 0;
        mutable nres2 = 0;

        for(trian in 1..1000){
        using ( (input) = (Qubit[3] ) ){
            // 1/sqrt(8) (|000> + |001> + ,,, + |111>)
            H(input[0]);
            H(input[1]);
            H(input[2]);

            let res0 = M(input[0]);
            let res1 = M(input[1]);
            let res2 = M(input[2]);
            if (res0 == Zero){
                set nres0 = 1;
            }else{
                set nres0 = 0;
            }
            if (res1 == Zero){
                set nres1 = 1;
            }else{
                set nres1 = 0;
            }
            if (res2 == Zero){
                set nres2 = 1;
            }else{
                set nres2 = 0;
            }
            let bit = nres0 + nres1*2 + nres2*4;
            set counts w/= bit <- counts[bit]+ 1;
            ResetAll(input);
        }
        }
        for (j in 0..7) {
           Message(IntAsString(counts[j]));
        }
    }
}

2020年10月16日金曜日

With NEW SDK "Rewriting sample codes in Quantum Algorithm Implementations for Beginners (Bell state)"

 Since the new SDK does not use Driver.cs, the Bell state example needs to be rewritten without the C# part.

Now you only need to write Program.qs, and the example looks like,
Result is |00> 500, |01> 0, |10> 0, |11> 500. Looks still valid.

namespace QFT_naive {
    open Microsoft.Quantum.Canon;
    open Microsoft.Quantum.Intrinsic;
    open Microsoft.Quantum.Convert;
    

    @EntryPoint()
    operation SayHello() : Unit {
        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(IntAsString(n00));
        Message(IntAsString(n01));
        Message(IntAsString(n10));
        Message(IntAsString(n11));
    }
}