2021年3月21日日曜日

QFT part 5: adjustable number of qubits

As continued from QFT part 4 , the number of qubits is now adjustable.

NQubit = 3 and 4 are working correctly.

I think I made a mistake and updated on 1st of May, 2021.

namespace QFT_naive {
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Arrays;
open Microsoft.Quantum.Measurement;
open Microsoft.Quantum.Math;
@EntryPoint()
operation SayHello() : Unit {
let nQubit = 4;
let maxNum = PowI(2,nQubit);
mutable counts = new Int[maxNum];
mutable theta = new Double[maxNum];

for iQubit in 0..(nQubit-1){
set theta w/= iQubit <- calcTheta(IntAsDouble(iQubit+1));
}

for trian in 1..1000 {
use input = Qubit[nQubit] {
// 1/sqrt(8) (|000> + |001> + ,,, + |111>)
for iQubit in 0..(nQubit-1){
H(input[iQubit]);
}

// Star QFT
for iQubit in 0..(nQubit-1){
H(input[iQubit]);
for jQubit in 1..(nQubit-1-iQubit){
(Controlled R1)([input[jQubit+iQubit]],(theta[jQubit],input[iQubit]));
}
}
//H(input[0]);
//(Controlled R1)([input[1]],(theta[1],input[0]));
//(Controlled R1)([input[2]],(theta[2],input[0]));
//H(input[1]);
//(Controlled R1)([input[2]],(theta[1],input[1]));

//H(input[2]);

let numLoopSwap = Floor(IntAsDouble(nQubit)*0.5);


for iQubit in 0..(numLoopSwap-1) {
SWAP(input[iQubit],input[nQubit-1-iQubit]);
}
//SWAP(input[2],input[0]);

let res = MultiM(input);
let bit = ResultArrayAsInt(res);
set counts w/= bit <- counts[bit]+ 1;
ResetAll(input);
}
}
for j in 0..(maxNum-1) {
Message(IntAsString(counts[j]));
}
}
function calcTheta(l : Double) : Double {
return 2.0*PI()/(PowD(2.0,l));
}
}


0 件のコメント:

コメントを投稿