# Quantum Fourier Transformation and Phase Estimation

Let's use Yao first

using Yao

## Quantum Fourier Transformation

The Quantum Fourier Transformation (QFT) circuit is to repeat two kinds of blocks repeatly: The basic building block control phase shift gate is defined as

$R(k)=\begin{bmatrix} 1 & 0\\ 0 & \exp\left(\frac{2\pi i}{2^k}\right) \end{bmatrix}$

Let's define block A and block B, block A is actually a control block.

A(i, j) = control(i, j=>shift(2π/(1<<(i-j+1))))
A (generic function with 1 method)

Once you construct the blockl you can inspect its matrix using mat function. Let's construct the circuit in dash box A, and see the matrix of $R_4$ gate.

R4 = A(4, 1)
(n -> control(n, 4, 1 => shift(0.39269908169872414)))

If you have read about preparing GHZ state, you probably know that in Yao, we could just leave the number of qubits, and it will be evaluated when possible.

R4(5)
nqubits: 5
control(4)
└─ (1,) shift(0.39269908169872414)

its matrix will be

mat(R4(5))
32×32 LinearAlgebra.Diagonal{Complex{Float64},Array{Complex{Float64},1}}:
1.0+0.0im      ⋅          ⋅          ⋅      …      ⋅              ⋅
⋅      1.0+0.0im      ⋅          ⋅             ⋅              ⋅
⋅          ⋅      1.0+0.0im      ⋅             ⋅              ⋅
⋅          ⋅          ⋅      1.0+0.0im         ⋅              ⋅
⋅          ⋅          ⋅          ⋅             ⋅              ⋅
⋅          ⋅          ⋅          ⋅      …      ⋅              ⋅
⋅          ⋅          ⋅          ⋅             ⋅              ⋅
⋅          ⋅          ⋅          ⋅             ⋅              ⋅
⋅          ⋅          ⋅          ⋅             ⋅              ⋅
⋅          ⋅          ⋅          ⋅             ⋅              ⋅
⋮                                        ⋱     ⋮
⋅          ⋅          ⋅          ⋅             ⋅              ⋅
⋅          ⋅          ⋅          ⋅             ⋅              ⋅
⋅          ⋅          ⋅          ⋅      …      ⋅              ⋅
⋅          ⋅          ⋅          ⋅             ⋅              ⋅
⋅          ⋅          ⋅          ⋅             ⋅              ⋅
⋅          ⋅          ⋅          ⋅             ⋅              ⋅
⋅          ⋅          ⋅          ⋅             ⋅              ⋅
⋅          ⋅          ⋅          ⋅      …  1.0+0.0im          ⋅
⋅          ⋅          ⋅          ⋅             ⋅      0.92388+0.382683im

Then we repeat this control block over and over on different qubits, and put a Hadamard gate to ith qubit to construct i-th B block.

B(n, k) = chain(n, j==k ? put(k=>H) : A(j, k) for j in k:n)
B (generic function with 1 method)

We need to input the total number of qubits n here because we have to iterate through from k-th location to the last.

Now, let's construct the circuit by chaining all the B blocks together

qft(n) = chain(B(n, k) for k in 1:n)
qft(4)
nqubits: 4
chain
├─ chain
│  ├─ put on (1)
│  │  └─ H gate
│  ├─ control(2)
│  │  └─ (1,) shift(1.5707963267948966)
│  ├─ control(3)
│  │  └─ (1,) shift(0.7853981633974483)
│  └─ control(4)
│     └─ (1,) shift(0.39269908169872414)
├─ chain
│  ├─ put on (2)
│  │  └─ H gate
│  ├─ control(3)
│  │  └─ (2,) shift(1.5707963267948966)
│  └─ control(4)
│     └─ (2,) shift(0.7853981633974483)
├─ chain
│  ├─ put on (3)
│  │  └─ H gate
│  └─ control(4)
│     └─ (3,) shift(1.5707963267948966)
└─ chain
└─ put on (4)
└─ H gate


## Wrap QFT to an external block

In most cases, functions are enough to wrap quantum circuits, like A and B we defined above, but sometimes, we need to dispatch specialized methods on certain kinds of quantum circuit, or we want to define an external block to export, thus, it's useful to be able to wrap circuit to custom blocks.

First, we define a new type as subtype of PrimitiveBlock since we are not going to use the subblocks of QFT, if you need to use its subblocks, it'd be better to define it under CompositeBlock.

struct QFT{N} <: PrimitiveBlock{N} end
QFT(n::Int) = QFT{n}()
Main.ex-index.QFT

Now, let's define its circuit

circuit(::QFT{N}) where N = qft(N)
circuit (generic function with 1 method)

And forward mat to its circuit's matrix

YaoBlocks.mat(::Type{T}, x::QFT) where T = mat(T, circuit(x))

You may notice, it is a little ugly to print QFT at the moment, this is because we print the type summary by default, you can define your own printing by overloading print_block

YaoBlocks.print_block(io::IO, x::QFT{N}) where N = print(io, "QFT($N)") Since it is possible to use FFT to simulate the results of QFT (like cheating), we could define our custom apply! method: using FFTW, LinearAlgebra function YaoBlocks.apply!(r::ArrayReg, x::QFT) α = sqrt(length(statevec(r))) invorder!(r) lmul!(α, ifft!(statevec(r))) return r end Now let's check if our apply! method is correct: r = rand_state(5) r1 = r |> copy |> QFT(5) r2 = r |> copy |> circuit(QFT(5)) r1 ≈ r2 true We can get iQFT (inverse QFT) directly by calling adjoint QFT(5)'  [†]QFT(5) QFT and iQFT are different from FFT and IFFT in three ways, 1. they are different by a factor of$\sqrt{2^n}$with$n$the number of qubits. 2. the bit numbering will exchange after applying QFT or iQFT. 3. due to the convention, QFT is more related to IFFT rather than FFT. ## Phase Estimation Since we have QFT and iQFT blocks we can then use them to realize phase estimation circuit, what we want to realize is the following circuit: using Yao First we call Hadamard gates repeatly on first n qubits. Hadamards(n) = repeat(H, 1:n) Hadamards (generic function with 1 method) Then in dashed box B, we have controlled unitaries: ControlU(n, m, U) = chain(n+m, control(k, n+1:n+m=>matblock(U^(2^(k-1)))) for k in 1:n) ControlU (generic function with 1 method) each of them is a U of power$2^(k-1)$. Since we will only apply the qft and Hadamard on first n qubits, we could use Concentrator, which creates a context of a sub-scope of the qubits. PE(n, m, U) = chain(n+m, # total number of the qubits concentrate(Hadamards(n), 1:n), # apply H in local scope ControlU(n, m, U), concentrate(QFT(n)', 1:n)) PE (generic function with 1 method) we use the first n qubits as the output space to store phase$ϕ$, and the other m qubits as the input state which corresponds to an eigenvector of oracle matrix U. The concentrator here uses focus! and relax! to manage a local scope of quantum circuit, and only active the first n qubits while applying the block inside the concentrator context, and the scope will be relax!ed back, after the context. This is equivalent to manually focus! then relax! fullly activated r = rand_state(5) ArrayReg{1, Complex{Float64}, Array...} active qubits: 5/5 first 3 qubits activated focus!(r, 1:3) ArrayReg{1, Complex{Float64}, Array...} active qubits: 3/5 relax back to the original relax!(r, 1:3) ArrayReg{1, Complex{Float64}, Array...} active qubits: 5/5 In this way, we will be able to apply small operator directly on the subset of the qubits. Details about the algorithm can be found here: Quantum Phase Estimation Algorithm Now let's check the results of our phase estimation. First we need to set up a unitary with known phase, we set the phase to be 0.75, which is 0.75 * 2^3 == 6 == 0b110 . # using LinearAlgebra N, M = 3, 5 P = eigen(rand_unitary(1<<M)).vectors θ = Int(0b110) / 1<<N phases = rand(1<<M) phases[0b010+1] = θ U = P * Diagonal(exp.(2π * im * phases)) * P' 32×32 Array{Complex{Float64},2}: -0.221805-0.163765im 0.143412+0.0443704im … -0.0357113-0.0046107im 0.0594511-0.114205im -0.290718-0.0943699im -0.0896703-0.0491766im -0.11794+0.0877386im 0.0349155+0.0502977im -0.224744+0.0700713im 0.018016+0.0556609im -0.02606+0.057906im -0.0109687+0.0869953im 0.172814+0.131303im -0.0111638-0.0657195im -0.116167+0.17282im -0.0291284-0.0207461im -0.109179+0.301891im … 0.171757-0.0389728im 0.179648+0.00350392im 0.0215536+0.0384987im 0.178424-0.0623305im -0.0383668-0.197842im -0.166125-0.104603im -0.0662219-0.0539236im -0.121225+0.100243im -0.0249649+0.105043im 0.188443+0.308625im -0.16327+0.00735859im -0.144226-0.230074im 0.115982+0.00316332im ⋮ ⋱ 0.0306327+0.0385703im 0.154436+0.0597145im 0.123607-0.0121001im -0.0420878-0.0691909im -0.0138479-0.0686411im -0.0544969+0.0277096im -0.18735-0.078418im 0.0292886-0.0180086im … -0.153629+0.0523213im -0.137854-0.043463im 0.0558171-0.0406754im -0.17275-0.0948326im 0.193949+0.238076im 0.046613+0.177992im -0.123499-0.0553409im -0.13043-0.0333137im 0.135731+0.0770629im 0.190921-0.0963759im -0.147655-0.235514im 0.0791569+0.171042im 0.0490463-0.00589173im -0.0516279+0.029022im 0.0694433+0.0104537im … 0.154085-0.0254419im 0.00717134+0.0150907im -0.259765-0.0414928im 0.0280891-0.255202im  and then generate the state$ψ\$

psi = P[:, 3]
32-element Array{Complex{Float64},1}:
-0.004991335642450273 - 0.2277988838860045im
-0.16574785561959446 - 0.007300973032369137im
0.019130372673147364 - 0.16556187642109887im
0.0802135500560454 - 0.04643358978365406im
0.3516762179914539 + 0.0im
0.10860222753528975 - 0.21725602863394108im
-0.0373283064911936 + 0.05270915809300915im
-0.005011748731003658 - 0.08229967462077996im
0.25689633324470584 - 0.012499716619308229im
-0.08966107361038585 - 0.012354250572535077im
⋮
0.006588815523354139 - 0.011553505494405073im
0.10137338673470353 + 0.026762738796603756im
-0.0822426786131844 - 0.08068328757178837im
-0.1861293931994568 + 0.13144338291287627im
-0.0009945313484759789 + 0.11095436680155599im
0.049961907187387355 + 0.24580253513311953im
-0.10639396186551088 - 0.1237223405267666im
-0.17513629447074197 - 0.1701725820318281im
-0.26708381415936544 + 0.06750436404880808im 

In the phase estimation process, we will feed the state to circuit and measure the first n qubits processed by iQFT.

r = join(ArrayReg(psi), zero_state(N))
r |> PE(N, M, U)
ArrayReg{1, Complex{Float64}, Array...}
active qubits: 8/8

Since our phase can be represented by 3 qubits precisely, we only need to measure once

results = measure(r, 1:N; nshots=1)
1-element Array{BitBasis.BitStr{3,Int64},1}:
011 ₍₂₎

Recall that our QFT's bit numbering is reversed, let's reverse it back

using BitBasis
estimated_phase = bfloat(results[]; nbits=N)
0.75

the phase is exactly 0.75!