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:

qft-circuit

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:

phase estimation

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.294297-0.223043im     0.0347742-0.137414im   …     0.117615+0.0169064im 
 -0.0326638-0.109294im    -0.0222498-0.177438im       -0.0104814+0.0790309im 
 -0.0497362-0.00965007im    0.218527-0.0926471im     -0.00701182+0.171334im  
  -0.111122-0.195045im    -0.0591316+0.0129269im       0.0827819-0.125385im  
   0.131755-0.0578539im   -0.0282198+0.0875759im       0.0313921-0.0209468im 
 -0.0728595-0.171818im     0.0428313+0.130385im   …    0.0755811+0.205365im  
  -0.144067+0.0124465im    0.0316049-0.112585im       -0.0351874-0.19142im   
  0.0474282-0.175715im    -0.0341212+0.0328072im       -0.111753-0.0669124im 
 -0.0569603-0.29456im      0.0229237+0.240152im        0.0948298+0.146547im  
 0.00114039+0.0181344im    -0.190474+0.15546im         0.0507382+0.0227639im 
           ⋮                                      ⋱                          
  0.0653379+0.0600281im     0.195819+0.132476im        0.0644743-0.013761im  
  -0.104718-0.252632im    -0.0365651-0.0298727im      -0.0159868-0.0100876im 
  -0.130408+0.232582im     -0.134743-0.0643121im  …   -0.0866909-0.0524094im 
  -0.140112-0.0249471im     0.297347+0.103814im        -0.177877-0.257782im  
  -0.170795+0.131408im    -0.0105651-0.0599562im      -0.0317493+0.172933im  
  0.0101381-0.0967289im   -0.0446344+0.0396157im       -0.054306+0.146977im  
  0.0668601+0.077193im    -0.0424254+0.0260169im       0.0546098+0.185768im  
  -0.106585+0.226365im    -0.0131628-0.0197556im  …     0.135179+0.115947im  
  0.0276893+0.0838622im   -0.0870531+0.0752533im        0.285651-0.00784995im

and then generate the state $ψ$

psi = P[:, 3]
32-element Array{Complex{Float64},1}:
   0.12286358920727583 + 0.0454495206415344im  
    0.0888087009352901 - 0.14558781824125522im 
 -0.040776296127027864 + 0.02083372538841998im 
   -0.1476828448947093 + 0.0354781984691553im  
  -0.13110869134695538 + 0.09276535049523599im 
     0.132331283357637 - 0.05387556172209546im 
   0.15059982177166906 + 0.028346084877186647im
  0.012478460902325808 - 0.13024509461016281im 
  -0.03956585158075887 - 0.05591954728221702im 
   0.11419762231493572 - 0.011405406983168581im
                       ⋮                       
  -0.06693473955322883 + 0.16504488977227155im 
  -0.19464658587095832 - 0.24432765206819143im 
   0.05628493196706094 - 0.17466302756302587im 
   0.10478209156333526 + 0.06605986681410553im 
   0.32623149085494124 + 0.0im                 
   0.24311993492529804 - 0.07038233795980622im 
 -0.056574356398019145 - 0.12005207301064293im 
  0.002390378837026655 - 0.2435672552464866im  
  -0.02223195343090274 + 0.11082205539548001im 

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!


This page was generated using Literate.jl.