0% found this document useful (0 votes)
17 views14 pages

DJ-BV Algo

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views14 pages

DJ-BV Algo

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Lecture - 3

Deutsch-Jozsa algorithm, Bernstein-Vazirani


algorithm
Recap of previous lecture: Quantum Circuits

1. Building blocks of Quantum Circuits:


1. Quantum Gates: Three ways of decomposing the gates for analysis
1. Matrix multiplication
2. Tensor product decomposition
3. Computational basis decomposition.

2. How to design classical gates from quantum gates- example of Toffoli gate
3. Any unitary operation on a single gate can be decomposed into rotational
operations: U = e iα RZ (β)RY (γ)RZ (δ)

4. Decomposition of multi-qubit gates into single or two-qubit gates helps in


reducing the quantum errors. For this purpose, any unitary operation can be
decomposed into single qubit operations A, B and C such that ABC=I andU = e iα AXBXC

5. Phase kick back: Reading out a phase factor that is accumulated during the quantum
algorithms using a simple kick back technique.

(0 e iα)
1 0

=
U
Quantum Algorithms:

1. Deutsch-Jozsa algorithm: A simple algorithm based on oracle functions to


show the quantum parallelism.

3. Bernstein-Vazirani algorithm: Another simple algorithm based on quantum


algorithm.

4. Quantum Fourier transform and Phase estimation: Fourier transform which is


used in many areas and algorithms including Shor’s algorithm.

5. Shor’s algorithm: Prime number factorization-basis for quantum cryptography

6. Grover’s search algorithm: Unstructured search problems are tackled using


this algorithm.

Quizzes after each of these algorithms are discussed include implementation using
QISkit of IBMQ

For information and implementation: https://qiskit.org/textbook/ch-algorithms/


Deutsch-Jozsa algorithm

The simple quantum algorithm that performs better than its classical counterpart

Given a boolean function, f(x0, x1, . . . ) → 0 or 1 where xn is 0 or 1

The property of the Boolean function is that it is either balanced or constant function

A balanced function gives 0’s half the time and 1’s the other half.
A constant function is either 0 or 1 for any input.

The problem is to determine whether f is balanced or constant function assuming it


as an Oracle function.
‘target’ register. We give the transformation defined by the map |x, y⟩ → |x, y ⊕ f (x)⟩ a
name, Uf , and note that it is easily shown to be unitary. If y = 0, then the final state of the
second qubit is just the value f (x). (In Section 3.2.5 we show that given a classical circuit
for computing f there is a quantum circuit of comparable efficiency which computes the
transformation Uf on a quantum computer. For our purposes it can be considered to be
Quantum Parallelism
a black box.)

Data Registers

Target Registers

Figure 1.17. Quantum circuit for evaluating f (0) and f (1) simultaneously. Uf is the quantum circuit which takes

| 0⟩ + | 1⟩
inputs like |x, y⟩ to |x, y ⊕ f (x)⟩.
| 0,f(0)⟩ + | 1,f(1)⟩
| ψ⟩ = | 0⟩ ⊕ f( ) | ψ⟩ =
Consider the circuit shown(2) (2)not in the
in Figure 1.17, which applies Uf to an input
computational
√ basis. Instead, the data register is prepared in the superposition (|0⟩ +
|1⟩)/ 2, which can be created with a Hadamard gate acting on |0⟩. Then we apply Uf ,
f(x) is evaluated simultaneously
resulting in the state: for both the states 0 and 1.
|0, f (0)⟩ + |1, f (1)⟩
√ . (1.37)
2
This process can be generalised to functions on any n-qubits. For example in the case of n=2
This is a remarkable state! The different terms contain information about both f (0) and
The result of performing H-transform on 2 qubits initially at 0 state will be
f (1); it is almost as if we have evaluated f (x) for two values of x simultaneously, a feature
known as ‘quantum parallelism’. Unlike classical parallelism, where multiple circuits each

( )( )states.
| 0⟩for+multiple
| 1⟩ values|of0⟩x simultaneously,
+ | 1⟩ |exploiting
00⟩ + |the01⟩
built to compute f (x) are executed simultaneously, here a single f (x) circuit is employed
+ | 10⟩ + | 11⟩
| ψ⟩ =
to evaluate the function = by ability
of a quantum computer
(2)
to be in superpositions
(2)
of different 2
This procedure can easily be generalized to functions on an arbitrary number of bits, by
using a general operation known as the Hadamard transform, or sometimes
⊗n 1 the Walsh–

More generally, in the case
Hadamard transform. of n-qubits,
This operation is just n Hadamard H gates=acting in parallel| x⟩
on n
2n x prepared
qubits. For example, shown in Figure 1.18 is the case n = 2 with qubits initially
as |0⟩, which gives
x represents all possible configurations for n qubits- | 000...⟩, | 010...⟩, . . . .
|0⟩ + |1⟩ |0⟩ + |1⟩ |00⟩ + |01⟩ + |10⟩ + |11⟩
! "! "
√ √ = (1.38)
2 2 2
In the general case, applying U_f gives us the output as

1
2n ∑
| ψ⟩ = | x⟩ | f(x)⟩
x

But this parallelism is not useful directly because as the measurement is


performed on the output, it collapses into one of the state.

This requires ability to extract more information than just one value of f(x).
Deutsch-Jozsa algorithm will exploit the quantum parallelism to do just this.
Deutsch Problem:

To find whether the oracle function f(x) is balanced or constant function.


Quantum algorithms 33

We would need 2(n-1)+1


7.3 Atrial inputs
Simple Quantum to be certain that f(x) is constant. 133
Algorithm

In the case of quantum algorithm, we can find the solution after only one call of the
function. outperform classical computation. The problem Deutsch’s algorithm solves is a black box prob-

We consider the function f(x) as an oracle function: f : | x⟩ | y⟩ → | x⟩ | y ⊕ f(x)⟩


lem. Deutsch showed that his quantum algorithm has better query complexity than any possible
classical algorithm: it can solve the problem with fewer calls to the black box than is possible clas-
sically. While the problem it solves is too trivial to be of practical interest, the algorithm
Quantum algorithms 33
contains
For example: simple versions of a number of key elements of intrinsically quantum computation, including the
use of nonstandard bases and quantum analogs of classical functions applied to superpositions,
that will recur in more complex quantum algorithms.

7.3.1 Deutsch’s Problem

Deutsch’s Problem Given a Boolean function f : Z2 → Z2 , determine whether f is constant.


Figure 1.19. Quantum circuit implementing Deutsch’s algorithm.
Deutsch’s quantum algorithm, described in this section, requires only a single call to a black
where, box for Uf to solve the problem. Any classical algorithm requires two calls to a classical black
box for Cf , one for each input value. The key to Deutsch’s algorithm is the nonclassical ability to
Hadamard gates to give
"place the second qubit ofFigure the input to the black box in a superposition. The subroutine of section
|0⟩ + |1⟩ |0⟩ − |1⟩ 7.4.2 generalizes this trick. 1.19. Quantum circuit implementing Deutsch’s algorithm.
! "!
|ψ1 ⟩ = √ √ . (1.42)
2 2 Recall from 6.1 that Uf for a single bit function f takes two qubits of input and produces
is sent through two Hadamard gates to give
two qubits of√ output. On input |x⟩|y⟩,! Uf produces |x⟩|f"(x) ⊕ y⟩, so when |y⟩ = |0⟩, the result
ows that if we√apply Uf to the state |x⟩(|0⟩ − |1⟩)/Uf 2is then we obtain |0⟩ + |1⟩ |0⟩ − |1⟩
"!
of applying |x⟩|f (x)⟩. The|ψ1 algorithm√ applies√Uf to .the two-qubit state |+⟩|−⟩,
⟩(|0⟩ − |1⟩)/ 2. Applying Uf to |ψ1 ⟩ therefore leaves us with one
⟩=
2
of two values 1 (1.42) where the
in the2domain of f ,√and the third qubit is in the

first qubit is
Recall that whena superposition
| y⟩ = | 0⟩, y ⊕ f(x) =
of the
| x⟩ | f(x)⟩
2
A little |−⟩
thought 1
superposition = √shows (|0⟩that if we√We
− |1⟩). apply Uf to the state |x⟩(|0⟩ − |1⟩)/ 2 then we obtain
obtain
f (x) 2
the state (−1) |x⟩(|0⟩ − |1⟩)/ 2. Applying Uf to |ψ1 ⟩ therefore leaves us with one of
! " x
|0⟩ + |1⟩ |0⟩ − |1⟩

two possibilities:1
! "! "
± √ In this case,
√ ifUff (0) = f (1)= Uf (|0⟩ +
(|+⟩|−⟩) ⎧ |1⟩)(|0⟩ − |1⟩)


2 2 2 |0⟩ + |1⟩ |0⟩ − |1⟩

⎪ ! "! "
⎪ ± √ √ if f (0) = f (1)
⎨ ⎪

= (1.43) 2 2


!
|0⟩ − |1⟩
"!
|0⟩ − |1⟩
" 1 |ψ2 ⟩ = (1.43)
= (|0⟩(|0 ⊕⎪⎪⎪⎪f (0)⟩|0⟩
−− |1|1⟩
⊕ f (0)⟩) + |1⟩(|0 ⊕ − |1 ⊕

|0⟩ − |1⟩ f (1)⟩ f (1)⟩)) .
if f (0) ̸= f (1). 2
⎪ ! "! "
⎩ ±

⎪ √ √ ⎩ ± √ √ if f (0) ̸= f (1).
2 2 2 2
In otherThe
words,
final Hadamard gate on the first qubit thus gives us
! |−⟩
superposition ! = 2 (|0⟩ − |1⟩). We " obtain "
1 11!
1 Uf (|+⟩|−⟩) = Uf= Uf(|0⟩
U1f (|+⟩|−⟩) = +(|0⟩ + |1⟩)(|0⟩
|1⟩)(|0⟩
(|0⟩(|0
1 ⊕
−f|1⟩) −−
(0)⟩ |1⟩)
|1"⊕ f (0)⟩) + |1⟩(|0 ⊕ f (1)⟩ − |1 ⊕ f (1)⟩)) .
= (|0⟩(|0 ⊕ f (0)⟩
= (|0⟩(|0 Uf ⊕ − |1 ⊕
f (0)⟩
(|+⟩|−⟩)2−= U2f2⊕ f+
f |1
(0)⟩) |1⟩(|0
(|0⟩
(0)⟩)++ ⊕ f (1)⟩
|1⟩(|0
|1⟩)(|0⟩ f−(1)⟩
−⊕|1⟩) |1 ⊕−f|1(1)⟩))
⊕ f (1)⟩))
. .
2 2 2
In other 1 words, 1 ⊕
In otherInwords,
other words, = = −(0)⟩|1 ⊕−
(|0⟩(|0 1 f (0)⟩
(|0⟩(|0 ⊕ f⊕ |1
f (0)⟩) ⊕ f+(0)⟩)
|1⟩(|0+⊕|1⟩(|0
(0)⟩) f (1)⟩ ⊕ − |1f− ⊕ f (1)⟩))
(1)⟩ −f |1 ⊕. f. (1)⟩)) .
In other words, 1 1
2 =
2 21 # (|0⟩(|01 f (0)⟩ − |1 ⊕ f + |1⟩(|0 ⊕ f (1)⟩ |1 ⊕ (1)⟩))
1 # #
1 words,UfIn(|+⟩|−⟩) = |x⟩(|0 ⊕ f (x)⟩ − |1 ⊕ f (x)⟩).
Uf (|+⟩|−⟩) = In=other
Uf (|+⟩|−⟩) |x⟩(|0
In other ⊕ fwords,
|x⟩(|0 other
(x)⟩ −
⊕words, |1 ⊕−f2|1
f (x)⟩ ⊕ f (x)⟩).
(x)⟩).
2 x=0 2 1
x=0
x=0
1 # 11 #
1
= 1 #
|x⟩(|0
= ⊕√1f|x⟩(|0 ⊕ −ff|1 ⊕− |11|1
⊕⊕ √1 (|0⟩ − |1⟩) = |−⟩. When f (x) =
U1f (|+⟩|−⟩) When
U f (|+⟩|−⟩)
f (x) = 0, (x)⟩⊕
(|0 (x)⟩
(x)⟩ f−(x)⟩). f (x)⟩).
f (x)⟩) becomes
When
Whenf(x)=0,(x) =f 0,
fWhen (x)√= 2
0,
(|0 1
Uf⊕√(|+⟩|−⟩)
f(|0
2 1
(x)⟩ ⊕− =
|1
f2(x)⟩ ⊕−f |1
x=02
2⊕|x⟩(|0
(x)⟩) x=0
2 ⊕becomes
f becomes
(x)⟩) f (x)⟩ 1
√ (|0⟩
2
− |1√−⊕
2
|1⟩)
(|0⟩f−
1
=|1⟩)
(x)⟩).|−⟩.= When|−⟩. When f (x)2 f=(x) 1, = 1,
√1 (|0 ⊕ √1f(|0 √ (|0 ⊕ f (x)⟩ 1x=0 − |1
1 ⊕ f (x)⟩) becomes √ (|1⟩ − |0⟩) = −|−⟩. Therefore

(x)⟩ −
f |1
(x)⟩⊕ −f |1
(x)⟩)⊕ f becomes
2 (x)⟩) becomes
1 =
√ (|1⟩ √ −
0, f√1(x)⟩
|0⟩)
(|1⟩
2(|0 ⊕
−= |0⟩)
−|−⟩. =
−(x)⟩)
−|−⟩.
Therefore Therefore
(x)⟩) 2becomes
|1 ⊕ fbecomes √1 (|0⟩ − |1⟩) = |−⟩. When f (x) = 1,
2 2 When f (x)When = 0, f√(x) 2
(|0 2⊕
2 − f|1(x)⟩
⊕ f √1 (|0⟩
2
2− |1⟩) = |−⟩. When f (x) = 1,
$(|0 ⊕ 1 √1Therefore
$ $ 1% When %
1 √ (|0 ⊕ f (x)⟩
f √1(x)
2 − |1
= f 0,
(x)⟩
1
√−(|0 |1 ⊕ ⊕ %
f f (x)⟩
(x)⟩) −
becomes
1 |11 ⊕ √1f (|1⟩
(x)⟩) − becomes
|0⟩) = −|−⟩. (|0⟩ − |1⟩) = |−⟩. When f (x) = 1,
When 1f(x)=1, #11 # 2 1 1 #1 1
1 # ⊕
1# f 2
(x)⟩) becomes 1 2√# (|1⟩ − 2 |0⟩) = −|−⟩. Therefore 2
Uf √ f U √ |x⟩|−⟩
√ (|0
|x⟩|−⟩ $=2 √ f (−1)# U⊕= f √$√
(x)⟩ −
f (x)|1
(−1) ⊕
|x⟩|−⟩
ff
|x⟩|−⟩.
(x)
% (x)⟩) =
|x⟩|−⟩. becomes
√ √1 (|1⟩
(−1) f (x)
− |0⟩) = −|−⟩. Therefore
|x⟩|−⟩.
%1 1 2
2 x=0 2 x=0 2 1U 212x=0 1 1 2 x=0 f (x)
#
1$ # x=0
f √ x=0|x⟩|−⟩ % 1 # = √ (−1) |x⟩|−⟩.
Uf √ f (x)|x⟩|−⟩ 1 2 = √ (−1) 2 1x=0 |x⟩|−⟩.
f (x)
For f constant, 1
2isFor # x=0
justfisa constant,
just a physically 1 #
meaningless
is(−1)
just global
phase,phase, thesostate
the state is simply
2(−1) fa(x)physically so meaningless global phase, so the state is simp
f (x)
For f constant, f
(−1)U(−1) (x)

x=0 physically
|x⟩|−⟩ = meaningless
x=0√ global |x⟩|−⟩. is simply
|+⟩|−⟩.|+⟩|−⟩. Forconstant,f
f not constant, For ftheconstant,
term (−1) f (x) is just a physically meaningless
f (x)
negates exactly one of thenegates
terms global
in thephase, so the state is simply
superposition
For f not |+⟩|−⟩.
the 2term For
(−1) f not
(−1)
f (x)
constant,
negates 2 exactlythe term
one of
(−1)the terms
f (x) in the exactly
superposition one of the terms in the superpositio
so,aup For f constant,
to a global |+⟩|−⟩.
x=0 For
(−1) not
ff(x)
is just aIf physically
constant,
|−⟩|−⟩. the
x=0 term (−1) meaningless
f (x)
negates global
exactly onephase, soHtheto
of the terms in state
the is simply
superposition
so, up to global phase, phase,
theso,state the
up isstate
to |−⟩|−⟩.
a is
global If we
phase, we
apply the apply
the
state the
Hadamard
is Hadamard
|−⟩|−⟩. transformation
transformation
If we apply Hthe to the
Hadamard the transformation H to t
|+⟩|−⟩. For so,
f up toconstant,
not a global phase,the term the(−1)statefis(x)|−⟩|−⟩.
negates If exactly
we applyone theofHadamard
the terms transformation
in the H to the
superposition
firstand
first qubit qubit
then For measure
andmeasure
then it,constant,
ffirst with it,certainty
qubit with(−1)
and certainty
then
f
we
(x)
iswe
obtain
measure justobtain
ait,physically
|0⟩ |0⟩
the in
inwith first
certainty meaningless
thecase
firstand
caseobtain
we and
|1⟩ in global
|1⟩
the thephase,
|0⟩insecond
in thesecond
first socasetheand
state
|1⟩isinsimply
the secon
so, up to a first
global qubit and
phase, then
the measure
state is it, with
|−⟩|−⟩. certainty
If we we
apply obtain
the |0⟩ in the
Hadamard first case and
transformation |1⟩ in the
H second
to the
|+⟩|−⟩.
f(x) For f not constant, the term (−1)
f (x)
negates exactly one of the terms in the superposition
If f is a constant, first (−1) so, up toisa just globalaphase,
qubit and then measure physically
it, with certainty
the state is meaningless
we obtain |0⟩ inglobal
|−⟩|−⟩. If we apply
the first case phase
the Hadamard
and |1⟩and in thethe second
transformation H to the
first qubit and then measure it, with certainty we obtain |0⟩ in the first case and |1⟩ in the second
state simply becomes | + ⟩ | − ⟩
1 1
((−1)0 | 0⟩ | − ⟩ + (−1)1 | 1⟩ | − ⟩)
2∑
If f is balanced, Uf ( | x⟩ | − ⟩) =
2
= | − ⟩| − ⟩
After H transform to the first qubit, U_f gives either 0 or 1 depending on whether f
is constant or balanced
finally gettingclassical
a 1, telling algorithm her that she Bob’s
can usefunction
thereforeis requires
balanced.2nThe /2 + best 1 queries. Note that in each
deterministic
letter, she
classical algorithm Alice
cansends Bob n bitsrequires
use therefore of information.
2n /2 + 1Furthermore,
queries. Note in that
this in
example,
each physical
distance
letter, Alice sends Bobis n being bits used to artificiallyFurthermore,
of information. elevate the cost in ofthis calculating
example,f (x), but this is not
physical
Deutsch-Jozsa needed
algorithm
distance is being usedinto theartificially
general problem,elevate wherethe cost f (x)
of may be inherently
calculating f (x), but difficult
this to
is calculate.
not
needed in the general If Bobproblem,
and Alicewhere were ablef (x)tomayexchange qubits, instead
be inherently difficult of just classical bits, and if Bob
to calculate.
If Bob and agreed
Alice were to calculate
able to fexchange(x) usingqubits,
a unitary transform
instead of just , then Alice
Ufclassical bits,could
and ifachieve
Bob her goal
Generalization ofinDeutsch
agreed to calculate justf one(x) using problem
a unitary to
correspondence with n Bob,
transforminput Ufqubits
using is the
the algorithms
Quantum
, then following
Alice could Deutsch-Jozsa
achieve her goal algorithm
algorithm.
35

Analogously to Deutsch’s algorithm, Alice has an n qubit register to store her query
in just one correspondence with Bob, using the following algorithm.
in, and a single qubit register which she will give to Bob, to store the answer in. She
Analogously to Deutsch’s algorithm, Alice has an n qubit register to store her query
begins by preparing both her query and answer registers in a superposition state. Bob
in, and a single qubit register which she will give to Bob,Quantum to store the answer in.35 She
algorithms
will evaluate f (x) using quantum parallelism and leave the result in the answer register.
begins by preparing both her query and answer registers in a superposition state. Bob
Alice then interferes states in the superposition using a Hadamard transform on the query
will evaluate fregister,
(x) using andquantum
finishes by parallelism
performing anda leave
suitable themeasurement
result in thetoanswer register.
determine whether f was
Alice then interferes
constantstates in the superposition using a Hadamard transform on the query
or balanced.
register, and finishes by performing
The specific steps of athe suitable
algorithm measurement
are depictedtoindetermine
Figure 1.20. whether was the states
Let usffollow
constant or balanced.
through this circuit. The input state
The specific steps of the algorithm are depicted in Figure 1.20. Let us follow the states
Figure 1.20. Quantum circuit implementing the general Deutsch–Jozsa algorithm. The wire with a ‘/’ through it
through this circuit.
represents a set of nThe
qubits, input state
similar to the |ψ0 ⟩ = |0⟩⊗n |1⟩
common engineering notation. (1.46)

The is input
similar state
to that ofis Equation = (1.41),
⊗n but here the query register describes the state of n
(1.46)
evenly weighted superposition of 0 and 1. Next, the|1⟩
|ψ 0 ⟩ |0⟩ function f is evaluated (by Bob)
usingqubits
Uf : |x, all
y⟩ →prepared in the
|x, y ⊕f (x)⟩, |0⟩
giving state. After the Hadamard transform on the query register
is similar to thatandof theEquation
Hadamard (1.41),
gate but
on the here the query
answer register
! (−1)f (x) |x⟩ " |0⟩ −|1⟩ #
register
we have describes the state of n
qubits all prepared in the |0⟩|ψstate. 2⟩ = After√2the Hadamard

2
. transform on#(1.48)
the query register
|x⟩ |0⟩ − |1⟩
n
"
Figure 1.20. Quantum circuit implementing the general Deutsch–Jozsa algorithm. The wire with a ‘/’ through it
x
!
After H- and the
transform Hadamard
on
represents a setthe
of
gate on the
data answer
qubits register
|ψ1 ⟩ = we have√ √ . (1.47)
Alice now hasn aqubits,
set ofsimilar
qubitstointhe common
which engineering
the result notation.
of Bob’sn 2#n
function 2
evaluation is stored
x∈{0,1}
in the amplitude of the qubit superposition! state. |x⟩ She |0⟩
now− interferes
|1⟩ terms in the super-
"
position using a Hadamard
The query register =is nowonathe
|ψ1 ⟩transform √query register.
superposition √ To of determine
all the result of
. values, and the answer (1.47)
register is in an
evenly weightedtransform
the Hadamard superposition of
it helps to 0
firstand 2
1.
calculateNext,
n the the
effect 2
offunction
the f
Hadamard is evaluated
transform (by Bob)
After U_f is operated
using
on
: |x,
on aUstate the
|x⟩.y⟩By

query/data
checking the (x)⟩,
|x, y ⊕f = 0 and
and x =target
cases xgiving 1 separatelyregisters,
x∈{0,1}n
we see that for awe
singleget
f √
qubit H|x⟩ = (−1)xz
|z⟩/ 2. Thus
$
The query register is now
z a superposition
$! (−1)f (x)
of all values, and# the answer register is in an
x1 z|x⟩ |0⟩ −|1⟩
"
+·· +x
z1 ,...,zn (−1) 1 z
⊗n |ψ 2 ⟩
H |x1 , . . . , xn ⟩ =
= √ √
n n
|z
√1 , . . . , zn.⟩ . (1.49)
(1.48)
x 2 2n
n 2
This
Alice nowcan has
be summarized more succinctly
a set of qubits in whichin the
the very
resultuseful equationfunction evaluation is stored
of Bob’s
in the amplitude of the qubit superposition state.
z (−1)
x·z
|z⟩ She now interferes terms in the super-
$
⊗n
H |x⟩ = √ , (1.50)
n
qubit H|x⟩ = z (−1) |z⟩/ 2. Thus
x1 z1 +·· +xn zn
(−1) |z1 , . . . , zn ⟩
$
+·· +x
xn ⟩ n=(−1)
⊗n z1x
,...,z
z n zn
|z1n, . . . , zn ⟩ (1.49)
$ 1 1
⊗n
H |x1 , . . .z,1 ,...,z n
√ .
H |x1 , . . . , xn ⟩ = √ 2 . (1.49)
2n
This can be summarized more succinctly in the very useful equation
This can be summarized more succinctly in the very $ useful x·zequation
(−1) |z⟩
Applying H on the first n qubits
⊗n
H |x⟩ = z
√ , (1.50)
(− 1)x·z
|z⟩ 2
$ n
⊗n
H z
|x⟩ = √ , (1.50)
where x · z is the bitwise inner 2n
product
of x and z, modulo 2. Using this equation
x . zevaluate
and (1.48) we can now = x0z0|ψ⊕ x z ⊕ . . . . ⊕ xn yn is the sum of bitwise
3 ⟩, 1 1
product
where x · z is the bitwise inner product of x and z, modulo 2. Using this equation
x·z+f (x)
and (1.48) we can now evaluate |ψ|ψ3 ⟩,⟩ = (−1) |z⟩ |0⟩ −|1⟩
! ! " #
3 √ . (1.51)
zx·z+f
x (x)
2 n
# 2
! ! (−1) |z⟩ |0⟩ −|1⟩
"
Finally, Alice now ⟩=
|ψ3 observes the query register. Note √ the amplitude
that . (1.51)
for the state |0⟩⊗n is
f (x) n z x
2 n
2
x (−1) /2 . Let’s look at the two possible cases – f constant and f balanced – to
$

Alice now observes discernthe


what happens.
query In the Note
register. case where
that ftheis constant
amplitude the for
amplitude
the state |0⟩⊗nis is+1 or
for |0⟩ ⊗n

f (x) n−1, depending on the constant value f (x) takes. Because |ψ3 ⟩ is of unit length it follows
(−If
1) f is constant,
. Let’s lookthe output
at the is same
two possible
be zero,as
cases – the input and
f constant bits. f balanced – to
$
/2
x that all the other amplitudes must and an observation will yield
⊗n0s for all qubits
discernIf what happens.
f is balanced, In register.
in the query the caseIfwhere
output fisis not f issame
balanced constantthe the
then as theamplitude
input
positive for |0⟩
bits
and negative is +1 orto the
contributions
−1, dependingamplitude
on the constant
for |0⟩⊗nvalue f (x)
cancel, takes.
leaving Because |ψ
an amplitude ⟩ is of
of3zero, andunit length it follows
a measurement must yield
that all the other amplitudes
a result must
other than beatzero,
0 on least and an observation
one qubit in the query will yield Summarizing,
register. 0s for all qubits
if Alice
in the query register. If f is balanced then the positive and negative contributions to the
amplitude for |0⟩⊗n cancel, leaving an amplitude of zero, and a measurement must yield
a result other than 0 on at least one qubit in the query register. Summarizing, if Alice
The inverse of the Hadamard gate is the Hadamard gate itself. Thus, by Step 4, we essentially reverse Step
Why does
quantum this
state ofwork?
all-zero at the first register.

When the hidden Boolean function is constant, the quantum states before and after querying the oracle are the sa
When the hidden Boolean function is balanced, the quantum state after querying the oracle is o
The inverse of the Hadamard gate is the Hadamard gate itself. Thus, by Step 4, we essentially reverse Step 2 to obtain the ini
quantum state
quantum before
state querying
of all-zero theregister.
at the first oracle. Thus, by Step 4, when reverting the operation, we must end up
Implementation
thatof DJ algorithm
is orthogonal to the initial quantum state of all-zero at the first register. This means we should never ob
When the hidden Boolean function is balanced, the quantum state after querying the oracle is orthogonal to the
quantum state before querying the oracle. Thus, by Step 4, when reverting the operation, we must end up with a quantum sta
that is orthogonal to the initial quantum state of all-zero at the first register. This means we should never obtain the all-zero s
1. Prepare
Quantumthe states: Apply H-transform to the query bits.
Oracle
2. Implementation of the Oracle function:
The key to the Deutsch-Josza Algorithm is the implementation of the quantum oracle.
Quantum Oracle

In the
For case function,
of constant function,
The key to the Deutsch-Josza Algorithm is the implementation of the quantum oracle.
a constant it is simple:
For a constant function, it is simple:
1. if f(x) = 0, then apply the 𝐼 𝐼 gate to the qubit in register 2.
2. 1.if iff(x)
f(x)=
= 1,
0, then
thenapply
applythethe 𝑋 𝑋togate
𝐼 𝐼 gate the qubit
to thein register
qubit in2.register 2.
2. if f(x) = 1, then apply the 𝑋 𝑋 gate to the qubit in register 2.

For
If f aFor
isbalanced
balanced function, there are many different circuits we can create. One of the ways we can guarantee
function,
a balanced function, there are many different circuits we can create. One of the ways we can guarantee our circuit is
balanced is by
balanced performing
is by performing aaCNOT
CNOT forfor each
each qubitqubit in register
in register 1, with
1, with the the
qubit in qubit2in
register asregister 2 For
the target. as example:
the target. For
111 110

2. Worked Example
Let's go through a specfic example for a two bit balanced function with hidden bitstring 𝑎 = 3 𝑎 = 3.

A. The first register of two qubits is initialized to |0⟩ |0⟩ and the second register qubit to |1⟩ |1⟩

|𝜓0 ⟩ = |00⟩1 |1⟩2

F. Apply Hadamard on all qubits

1 1
|𝜓1 ⟩ = (|00⟩1 + |01⟩1 + |10⟩1 + |11⟩1 ) (|0⟩2 − |1⟩2 )
2 √2‾
I. For 𝑎 = 3 𝑎 = 3, (11 in binary) the oracle function can be implemented as Q𝑓 = 𝐶 𝑋1𝑎 𝐶 𝑋2𝑎 Q𝑓 = 𝐶𝑋1𝑎 𝐶𝑋2𝑎 ,

1
|𝜓2 ⟩ = [|00⟩1 (|0 ⊕ 0 ⊕ 0⟩2 − |1 ⊕ 0 ⊕ 0⟩2 )
2√2‾
+ |01⟩1 (|0 ⊕ 0 ⊕ 1⟩2 − |1 ⊕ 0 ⊕ 1⟩2 )
+ |10⟩1 (|0 ⊕ 1 ⊕ 0⟩2 − |1 ⊕ 1 ⊕ 0⟩2 )
+ |11⟩1 (|0 ⊕ 1 ⊕ 1⟩2 − |1 ⊕ 1 ⊕ 1⟩2 )]

R. Thus
Made with Jupyter Book

Thus,
1
|𝜓2 ⟩ = [|00⟩1 (|0⟩2 − |1⟩2 ) − |01⟩1 (|0⟩2 − |1⟩2 )
2√2‾
− |10⟩1 (|0⟩2 − |1⟩2 ) + |11⟩1 (|0⟩2 − |1⟩2 )]
1 1
= (|00⟩1 − |01⟩1 − |10⟩1 + |11⟩1 ) (|0⟩2 − |1⟩2 )
2 √2‾
1 1 1
= (|0⟩10 − |1⟩10 ) (|0⟩11 − |1⟩11 ) (|0⟩2 − |1⟩2 )
√2‾ √2‾ √2‾

T. Apply Hadamard on the first register

|𝜓3 ⟩ = |1⟩10 |1⟩11 (|0⟩2 − |1⟩2 )

U. Measuring the first two qubits will give the non-zero 1111, indicating a balanced function.

3. Qiskit Implementation
We now implement the Deutsch-Josza algorithm for the example of a two bit balanced function with hidden bitstrin
First let's do our imports:
Made with Jupyter Book 11/04/20, 10(12 AM
Final Circuit for DJ algorithm:
Made with Jupyter Book 11/04/20, 10(12 AM

Made with Jupyter Book 11/04/20


oracle_gate = dj_oracle('balanced', n)
dj_circuit = dj_algorithm(oracle_gate, n)
dj_circuit.
.draw('mpl') provider = IBMQ.
.get_provider(hub=
='ibm-q')
backend = least_busy(provider.
.backends(filters=
=lambda x: x..configuration().
.n_qubits >= (n+
+1) and
not x.
.configuration().
.simulator and x..status().
.operational==
==True
True))
print("least busy backend: ", backend)

least busy backend: ibmq_valencia

# Run our circuit on the least busy backend. Monitor the execution of the job in the queue
from qiskit.tools.monitor import job_monitor

shots = 1024
job = execute(dj_circuit, backend=
=backend, shots=
=shots)

job_monitor(job, interval = 2)

Job Status: job has successfully run

And see the resultsof running this circuit: # Get the results of the computation
results = job.
.result()
answer = results.
.get_counts()
results = execute(dj_circuit, backend=
=backend, shots=
=1024).
.result()
Let's see= the
answer results.
QASM simulator
output:
.get_counts()
plot_histogram(answer)
plot_histogram(answer) Real IBM results
# use local simulator
backend = BasicAer.
.get_backend('qasm_simulator')
shots = 1024
results = execute(dj_circuit, backend=
=backend, shots=
=shots).
.result()
answer = results.
.get_counts()

plot_histogram(answer)

4. Experiment with Real Devices


We can run the circuit on the real device as shown below. We first look for the least-busy device that can handle ourAs
circuit.
we can see, most of the results are 1111 . The other results are due to errors in the quantum computation.

# Load our saved IBMQ accounts and get the least busy backend device with less than or equal to (n+1) qubits
IBMQ.
.load_account()

5. Problems

You might also like