(Download source code from here.)
In this post, I’ll explain quantum algorithms of Quantum Phase Estimation with Q# implementation.
Series : Quantum algorithm’s implementation (Q#)
- Programming Quantum Algorithm for Beginners (Bernstein-Vazirani algorithm)
- Programming Quantum Search (Grover’s Algorithm)
- Programming Quantum Phase Estimation
- Programming Quantum Arithmetic (Adder, Multiplier, and Exponentiation)
- Programming Quantum Period Finding (Shor’s Algorithm)
Quantum Fourier Transform (QFT)
Before describing the idea of Quantum Phase Estimation, let me start with Quantum Fourier Transform (QFT), which is the quantum state’s unitary transformation defined as below :
For arbitrary states in general, this transformation can be written as :
If you’re familiar with classical Discrete Fourier Transform, you will find this QFT (Quantum Fourier Transform) has the same form and keeps the same characteristics, such as linearity, pairwise products, and invertibility.
The classical one is used for finding time-based sine waves, and QFT can also be used for the same purpose as follows.
It’s known that Quantum Fourier Transform (QFT) can be implemented by the following circuit, where . I don’t describe why this circuit realizes QFT, but the proof is well-described in the lecture “Chem/CS/Phys191: Qubits, Quantum Mechanics, and Computers – Quantum Fourier Transform (UC Berkeley)” if you’re interested.
Note that the last part of SWAP can be implemented by n / 2 times combination of 2 bit’s SWAP : . (As I’ll show you later, we use Q# function
Microsoft.Quantum.Canon.SwapReverseRegister()
for this operation in this post.)
where
Figure 01 : Quantum Fourier Transform (QFT)
Quantum Phase Estimation
As you will find in this post, QFT will help you to find phase in quantum state (i.e, quantum phase estimation), and it can also be applied for more advanced algorithms, such as, Shor’s algorithm (which I will discuss in the future post).
Now let me start to explain the idea of Quantum Phase Estimation, using QFT.
Here we assume that some state is represented by the following
. Here
is given, but
is unknown.
The goal of Phase Estimation is to find the unknown using state
.
Note : See here for quantum phase in 2 state system.
As you can easily see, if is written as
for some integer “
“, this equation is the same as the right side of QFT. Then hopefully we might get integer “
” (eventually, the result
) by
transformation (running QFT circuit from backwards) and measuring the result’s integer. (For instance, if
, we can expect that the integer “a” results into
.)
Here I skip the proof, but it’s known that you can get the approximate integer “” (i.e, it holds
) by
with high probabilities, even if it’s not precise
. That is, even if
with some error
, you can get the approximate integer part “
” with high probability by
.
When increases, you will get more accurate
with the estimated integer value “
“.
Note : If you’re interested in the proof of this approximation, you can refer “Principles of Quantum Computation and Information“. (See Volume 1 – Chapter 3.12 “Quantum phase estimation”.)
This algorithm can also be applied to the following another problem.
Here we assume we have a given unitary and eigenstate
.
is unitary, then corresponding eigenvalue is written as
for some
. Now we want to find this
using a given
and
.
Because of , you can find
(where r is integer).
Now we consider Controlled- operation (where j is integer), in which we apply
to the second qubit, conditional on the first qubit. We then apply this operation to
as follows. :
Note : Here, we can get the first qubit by
.
Now let’s expand this transformation to arbitrary dimension, and see the following circuit.
In this circuit, is the dimension of eigenvector
and
is an arbitrary integer. (i.e,
where q is the number of qubits
, and
is then represented by
unitary matrix.)
Figure 2 : Transform with n register qubits
By the previous equation, this circuit (Figure 2) will generate the following states :
As you can see in the right side of this equation, the representation of this first qubits (i.e,
) is the same formula as previous phase estimation problem. You can then find
by applying
for the first
qubits.
As a result, you can estimate eigenvalue with the following circuit.
Figure 3 : Eigenvalue Estimation
By the way, recall that is an arbitrary integer. How large should we set for
in this circuit ?
As I have mentioned above, you can get more accurate approximate value by increasing this . But if you increase
more and more, the complexity (the cost) of this phase estimation will increase rapidly (exponentially), as above circuit shows.
Therefore you must carefully set to meet the requirements between cost (complexity) and accuracy in quantum phase estimation algorithm.
Q# Programming for Quantum Phase Estimation
In this post, I’ll implement eigenvalue estimation for a given and eigenstate
.
To simplify our example, we assume that the following unitary operator is given as a black-box oracle operator. (As you can see later, this is easily implemented by
Rz()
operator in Q#.) :
As you can easily see, this unitary operator has eigenvalues : and
, and corresponding eigenstates :
and
. In this example, we give a eigenstate
and try to estimate eigenvalue
.
Note : Phase Estimation example in official GitHub repo uses Faster Phase Estimation (not Quantum Phase Estimation), which is the optimized iterative phase estimation algorithm based on the classical post-processing ideas. For Faster Phase Estimation, please see this paper by Microsoft Research.
In this post, we show you Quantum Phase Estimation (above algorithm) by QFT.
Now let’s start our programming.
In fact, Q# provides high-level operator (in Microsoft.Quantum.Standard
) for both Quantum Fourier Transform (QFT) and Phase Estimation, and you don’t need to implement these algorithms by yourself. (See QFT() and QuantumPhaseEstimation() in the reference document.)
But here, let me implement these algorithms with primitive operators for the purpose of your learning.
First we implement QFT (see Figure 1 above) as follows.
As I have mentioned above, you can implement the swap operation for all qubits by combinations of 3 CNOT gates, but here I have used
Microsoft.Quantum.Canon.SwapReverseRegister()
for simplifying our sample code.
operation QFTImpl (qs : Qubit[]) : Unit is Adj + Ctl{ body (...) {let nQubits = Length(qs); for i in 0 .. nQubits - 1{ H(qs[i]); for j in i + 1 .. nQubits - 1 {Controlled R1Frac([qs[j]], (1, j - i, qs[i])); }} Microsoft.Quantum.Canon.SwapReverseRegister(qs); }}
Next we implement quantum eigenvalue estimation algorithm described in above Figure 3. (This logic (circuit) invokes as follows.)
Here the argument “oracle
” is a black-box unitary operator , and “
targetState
” is corresponding eigenstate, and “controlRegister
” is register qubits in Figure 3.
operation QuantumPhaseEstimationImpl (oracle : (Qubit[] => Unit is Adj + Ctl), targetState : Qubit[], controlRegister : Qubit[]) : Unit is Adj + Ctl{ body (...) {let nQubits = Length(controlRegister);Microsoft.Quantum.Canon.ApplyToEachCA(H, controlRegister); for idxControlQubit in 0 .. nQubits - 1{ let control = (controlRegister)[nQubits - 1 - idxControlQubit]; let power = 2 ^ idxControlQubit; Controlled PowerOracle([control], (oracle, targetState, power)); //// You can also write as follows, //// Or use Microsoft.Quantum.Canon.DiscreteOracle instead //for idxPower in 0 .. power - 1 //{ // Controlled oracle([control], targetState); //}}Adjoint QFTImpl(controlRegister); }}operation PowerOracle (oracle : (Qubit[] => Unit is Adj + Ctl), targetState : Qubit[], power : Int) : Unit is Adj + Ctl { body (...) {for idxPower in 0 .. power - 1{ oracle(targetState);} }}
Now we define a black-box unitary (oracle) as follows.
As I have mentioned above, this eigenvalue (corresponding eigenvector ) is
.
Then our goal is to estimate the following “eigenphase
” (i.e, ) .
operation ExpOracle (eigenphase : Double, register : Qubit[]) : Unit is Adj + Ctl { body (...) {Rz(2.0 * eigenphase, register[0]); }}
Now we combine these operators.
First we generate ExpOracle()
(i.e, ) with some
eigenphase
.
Next we prepare eigenstate and
register qubits.
Finally we execute estimation by calling QuantumPhaseEstimationImpl()
.
For simplification, I have used Microsoft.Quantum.Arithmetic.MeasureInteger()
for getting measured integer, but you can easily implement the same logic by measuring each register qubits and sum all bits as binary number.
Note (BigEndian and LittleEndian) :
For instance, the measured integer of(1st qubit is 0 and others are 1) becomes 3 when you use Big Endian (BigEndian). However, it becomes
when you use Little Endian (LittleEndian).
In our case, we need Big Endian.
operation PhaseEstimationSample (eigenphase : Double) : Double { let oracle = ExpOracle(eigenphase, _); // Generate (Qubit[] => Unit) with eigenphase let n = 10; mutable estPhase = 0.0; use (eigenstate, phaseRegister) = (Qubit[1], Qubit[n]) {X(eigenstate[0]);QuantumPhaseEstimationImpl(oracle, eigenstate, phaseRegister);let estReg = Microsoft.Quantum.Arithmetic.MeasureInteger( Microsoft.Quantum.Arithmetic.BigEndianAsLittleEndian(Microsoft.Quantum.Canon.BigEndian(phaseRegister)));set estPhase = 2.0 * PI() * IntAsDouble(estReg) / IntAsDouble(2 ^ n);Reset(eigenstate[0]); } return estPhase;}
Now let’s invoke quantum logic from Python or .NET (C#).
import randomrandom.seed(1000)eigenphase = random.uniform(0.0, 1.0) * 3.0 * 2.0est = PhaseEstimationSample.simulate(eigenphase=eigenphase)print("expected {}, estimated {}".format(eigenphase, est))eigenphase = random.uniform(0.0, 1.0) * 3.0 * 2.0est = PhaseEstimationSample.simulate(eigenphase=eigenphase)print("expected {}, estimated {}".format(eigenphase, est))eigenphase = random.uniform(0.0, 1.0) * 3.0 * 2.0est = PhaseEstimationSample.simulate(eigenphase=eigenphase)print("expected {}, estimated {}".format(eigenphase, est))
You will then get the following results and find that the phase () is well-estimated by algorithm.
You can download and run this source code from here.
In previous posts, we saw phase kickback method in quantum, which is used in several primitive algorithms for black-box problems.
As you saw above, Quantum Fourier Transform (QFT) translates a bit shift into phase shifts, which can be used in more advanced algorithms, such as, Quantum Phase Estimation.
In the next post, I will introduce several basic quantum operations for the preparation of famous Shor’s algorithm.
Reference :
Principles of Quantum Computation and Information
Categories: Uncategorized
4 replies»