DJ-BV Algo
DJ-BV Algo
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 (δ)
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:
Quizzes after each of these algorithms are discussed include implementation using
QISkit of IBMQ
The simple quantum algorithm that performs better than its classical counterpart
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.
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
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:
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-
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
$
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⟩
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‾
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
# 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)
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)
# 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