2021年5月1日土曜日

Inverse QFT

Based on the last QFT , I tried the inverse QFT.

I got the result of  [640, 654, 573, 666, 615, 601, 635, 654, 616, 603, 646, 602, 629, 605, 593, 668].
This means that the initial state of QFFT is recovered by iQFFT.

The codes of QFT and iQFT are as follows (as far as I understand).



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];
mutable revSeq = new Int[nQubit];

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

for iQubit in 0..(nQubit-1){
set revSeq w/= iQubit <- nQubit-1-iQubit;
}
for trial in 1..10000 {
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]));
}

}

let numLoopSwap = Floor(IntAsDouble(nQubit)*0.5);
for iQubit in 0..(numLoopSwap-1) {
SWAP(input[iQubit],input[nQubit-1-iQubit]);
}
// Start Inverse QFFT
for iQubit in 0..(numLoopSwap-1) {
SWAP(input[iQubit],input[nQubit-1-iQubit]);
}
for iQubit in revSeq{
for jQubit in 0..(nQubit-1)-iQubit-1{
let kQubit = revSeq[(nQubit-1)-jQubit];
let controlQubit = nQubit-kQubit-1;
let rotationIndex= nQubit-(kQubit+iQubit)-1;
(Controlled R1)([input[controlQubit]],(-1.0*theta[rotationIndex],input[iQubit]));
}
H(input[iQubit]);
}
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 件のコメント:

コメントを投稿