2020年10月23日金曜日

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]));
        }
    }
}

0 件のコメント:

コメントを投稿