0% found this document useful (0 votes)
121 views

An Introduction To Quantum Computing With Qiskit Implementations

The document provides an introduction to quantum computing concepts including: - Qubits can exist in superposition of states |0> and |1> unlike classical bits. - Measurement collapses the qubit state to either |0> or |1>. - Common quantum gates like Hadamard, CNOT, and Toffoli gates are used to manipulate qubit states. - The Deutsch-Jozsa algorithm is presented as an example to solve problems more efficiently than classical computers using quantum properties like superposition and entanglement.

Uploaded by

aysenur
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
121 views

An Introduction To Quantum Computing With Qiskit Implementations

The document provides an introduction to quantum computing concepts including: - Qubits can exist in superposition of states |0> and |1> unlike classical bits. - Measurement collapses the qubit state to either |0> or |1>. - Common quantum gates like Hadamard, CNOT, and Toffoli gates are used to manipulate qubit states. - The Deutsch-Jozsa algorithm is presented as an example to solve problems more efficiently than classical computers using quantum properties like superposition and entanglement.

Uploaded by

aysenur
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

DRP Türkiye 2023

An Introduction to Quantum
Computing with Qiskit
Implementations

Mentee Name: Ayşenur Mentor Name: Feride Ceren


Yazoğlu Köse

A Report submitted for the DRP Türkiye


Quantum Computing
September 2, 2023
Contents
1 Abstract 2

2 Qubits and Measurement 2


2.1 Qubits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.2 Superposition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.3 Measurement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

3 Quantum Gates 3

4 Some Quantum Computing Concepts 5


4.1 Entanglement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
4.2 Making Non-Reversible Functions Reversible . . . . . . . . . . . . . . 6

5 The Deutsch-Jozsa Algorithm 6


5.1 The Problem: Is f constant or balanced? . . . . . . . . . . . . . . . . . 6
5.1.1 Classical Solution . . . . . . . . . . . . . . . . . . . . . . . . 6
5.2 Quantum Solution: The Deutsch-Jozsa Algorithm . . . . . . . . . . . . 7

6 Qiskit Implementation of The Deutsch-Jozsa Algorithm 9

1
1 Abstract
This report mainly includes essential quantum computing concepts, offering insights
into qubits, superposition, gates, algorithms, and their practical implementation. Aim
of the study is to being able to understand The Deutsch-Jozsa Algorithm.

2 Qubits and Measurement

2.1 Qubits
Definition 2.1 (Qubit). A qubit (quantum bit) is the fundamental unit of quantum in-
formation. It is the quantum analogue of a classical bit. Unlike classical bits, which can
only be in states 0 or 1, qubits can exist in a linear combination of both states thus can
be represented as ∀α,β ∈ C , |ψ⟩ = α |0⟩ + β |1⟩ where |0⟩ and |1⟩ are the basis states
corresponding to classical 0 and 1.

If there are more than one qubits to examine together, they are expressed using the
tensor product. For example two state |0⟩ qubits written as
|0⟩ ⊗ |0⟩ = |00⟩ .
Definition 2.2 (Auxiliary Qubit). An auxiliary qubit is an additional qubit used in quan-
tum computation to aid in performing specific operations or manipulations on the pri-
mary input qubits. It plays a supportive role rather than being directly involved in the
main computational task.
Definition 2.3 (Basis States). The fundamental states in which a quantum system can
be measured are called basis states. In a qubit system, the two basis states are conven-
tionally denoted as |0⟩ and |1⟩. These states are orthogonal and represent the classical
binary values 0 and 1 respectively. They are represented as column vectors:
   
1 0
|0⟩ = , |1⟩ =
0 1

2.2 Superposition
Any qubit exist in the linear combination of basis states called superpositions. It is
the ability of a quantum system to exist in a combination of multiple states simultane-
ously.
∀α,β ∈ C , |ψ⟩ = α |0⟩ + β |1⟩ is a general qubit in superposition of two basis states
|0⟩ and |1⟩.

2.3 Measurement
Measurement is the procedure of evaluating the state of a qubit. Any qubit carries
probabilistic information about which basis state it’s closer to or which basis state it

2
prefers more to collapse to. Measuring the qubit collapses its quantum state to one
of the basis states, with the probabilities of measurement outcomes determined by the
norm square of the amplitudes.
In a quantum circuit diagram, measurement is represented as a meter symbol:

Definition 2.4 (Normalization Condition). ∀α,β ∈ C , ∥α∥2 + ∥β ∥2 = 1 is called "nor-


malization condition".
Definition 2.5 (Probability Amplitudes). ∀α,β ∈ C , ∥α∥2 + ∥β ∥2 = 1. α and β are
called "probability amplitudes".

3 Quantum Gates
Quantum gates carry around and manipulate the quantum information by transform-
ing the state of a qubit into other states. This can be visualized as rotations on Bloch
Sphere.
Gates are linear maps, meaning, we can distribute them across superpositions. They
also keep the total probability of a qubit being one of the two states equal to 1 thus
normalization condition, ∥α∥2 + ∥β ∥2 = 1, is satisfied after the gate has acted.
Since gates are linear maps, we can express them using matrices. Due to normal-
ization condition, matrices that represent them must be unitary. All unitary matrices
express a quantum gate. Most common quantum gates are: Pauli X-Gate (NOT Gate),
Pauli Y-Gate, Pauli Z-Gate, Hadamard Gate, CNOT Gate (Controlled NOT Gate), Tof-
foli Gate (CCNOT Gate).
Definition 3.1 (Pauli X-Gate (NOT Gate)). Flips the state of a qubit, changing |0⟩ to
|1⟩ and vice versa. Matrix representation:
 
0 1
1 0
Circuit representation:

Truth table:

Input Output
|0⟩ |1⟩
|1⟩ |0⟩
Definition 3.2 (Pauli Y-Gate). Introduces a phase shift and swaps |0⟩ and |1⟩ states.
Matrix representation:  
0 −i
i 0
Circuit representation:

3
Y

Truth table:

Input Output
|0⟩ i |1⟩
|1⟩ −i |0⟩
Definition 3.3 (Pauli Z-Gate). Introduces a phase shift to the |1⟩ state. Matrix repre-
sentation:  
1 0
0 −1
Circuit representation:

Truth table:

Input Output
|0⟩ |0⟩
|1⟩ − |1⟩
|0⟩+|1⟩
Definition 3.4 (Hadamard Gate). Creates superposition by transforming |0⟩ to √
2
|0⟩−|1⟩
and |1⟩ to √
2
. Matrix representation:
 
1 1 1

2 1 −1
Circuit representation:

Truth table:

Input Output
|0⟩+|1⟩
|0⟩ √
2
|0⟩−|1⟩
|1⟩ √
2
Definition 3.5 (CNOT Gate (Controlled NOT Gate)). Acts on two qubits. Applies a
Pauli-X gate to the target qubit(flips the target qubit) if the control qubit is |1⟩. Matrix
representation:  
1 0 0 0
0 1 0 0
 
0 0 0 1
0 0 1 0

4
Circuit representation:

control
target

Truth table:

Input Output
|00⟩ |00⟩
|01⟩ |01⟩
|10⟩ |11⟩
|11⟩ |10⟩
Definition 3.6 (Toffoli Gate (CCNOT Gate)). Acts on three qubits. Applies a Pauli-X
gate to the target qubit if both control qubits are |1⟩.
 
1 0 0 0
0 1 0 0
 
0 0 0 1
0 0 1 0

Circuit representation:

control
control
target

Truth table:

Input Output
|000⟩ |000⟩
|001⟩ |001⟩
|010⟩ |010⟩
|011⟩ |011⟩
|100⟩ |100⟩
|101⟩ |101⟩
|110⟩ |111⟩
|111⟩ |110⟩

4 Some Quantum Computing Concepts

4.1 Entanglement
Entanglement is relationship between two or more quantum systems when these
systems are interconnected in a way that state of one system instantaneously affects

5
the state of other. It forms the basis for protocols like quantum teleportation and quan-
tum cryptography. Additionally, quantum computers can use entanglement to perform
certain computations more efficiently than classical computers through algorithms.

4.2 Making Non-Reversible Functions Reversible


Quantum gates are inherently reversible. Because of that, for any function to be
suitable for the quantum computing environment it must be reversible. If our system
involves non-reversible functions we need to build their analogous reversible versions.
Being reversible means that we want to be able to derive the input from the output.
The easiest way to to it is to make the input also an output. In general our aim is to build
such a function that it also gives its input as an output in addition to its actual output so
that we don’t lose information regarding the input.
In quantum computing, auxiliary qubits are used for preserving information about
the input qubit by linking the auxiliary to the input qubit. Quantum entanglement or
operations which affect more than one qubit like CNOT-Gate are used in the purpose of
linking together the auxiliary qubit and the input qubit.
Input Output 1

Input Function Output Function

Auxiliary Input
Figure 1:Non-reversible function Figure 2:Reversible function

5 The Deutsch-Jozsa Algorithm

5.1 The Problem: Is f constant or balanced?


f : {0, 1}n → {0, 1} is an n-length binary function which can be either constant or
balanced. If the function is balanced it means that it maps half of the inputs to 0 and the
other half to 1. We want to determine whether the function is constant or balanced.
Suppose n = 1, there are 4 possible configurations for the function.

x f (x) x f (x) x f (x) x f (x)


0 0 0 0 0 1 0 1
1 0 1 1 1 0 1 1
Constant Balanced Balanced Constant

5.1.1 Classical Solution

To have enough information to determine if the function is constant or balanced,


function must be evaluated 2 times; for f(0) and f(1). This gives us a O(2) class com-
plexity. For any n this means O(2n−1 + 1) complexity. However by using quantum

6
computing methods, behavior of the function can be determined with only 1 execution
for any n, which means O(1) complexity.

5.2 Quantum Solution: The Deutsch-Jozsa Algorithm


The Deutsch-Jozsa Algorithm is not necessarily a trivial algorithm for Quantum
Computing overall, it’s importance arise at the point it shows us for the first time that
we can -in certain situations- make faster calculations using quantum algorithms than
we make using classical algorithms. And by faster we mean an "exponential speedup"
in solving certain problems compared to classical computers.

Figure 1: Circuit representing The Deutsch-Jozsa algorithm for n=1

|ψ0 ⟩ = |0⟩ |1⟩


|ψ1 ⟩ = H |0⟩ H |1⟩
  
|0⟩ + |1⟩ |0⟩ − |1⟩
= √ √
2 2
1
= (|0⟩ + |1⟩) (|0⟩ − |1⟩)
2
1
= (|00⟩ − |01⟩ + |10⟩ − |11⟩)
2
|ψ2 ⟩ = U f |ψ1 ⟩
1 
= U f |00⟩ −U f |01⟩ +U f |10⟩ −U f |11⟩
2
1
= (|0⟩ |0 ⊕ f (0)⟩ − |0⟩ |1 ⊕ f (0)⟩ + |1⟩ |0 ⊕ f (1)⟩ − |1⟩ |1 ⊕ f (1)⟩)
2
1
= [|0⟩ (|0 ⊕ f (0)⟩ − |1 ⊕ f (0)⟩) + |1⟩ (|0 ⊕ f (1)⟩ − |1 ⊕ f (1)⟩)]
2

If f is constant, there are two possibilities:

7
x f (x) 0 ⊕ f (x) 0 ⊕ f (x) x f (x) 0 ⊕ f (x) 0 ⊕ f (x)
0 0 0 1 0 1 1 0
0 0 0 1 0 1 1 0

1

 [|0⟩ (|0⟩ − |1⟩) + |1⟩ (|0⟩ − |1⟩)]
 if f = 0
|ψ2 ⟩ = 2
 1 [|0⟩ (|1⟩ − |0⟩) + |1⟩ (|1⟩ − |0⟩)]

if f = 1
2
1
 (|0⟩ + |1⟩) + (|0⟩ − |1⟩)
 if f =0
= 2
− 1 (|0⟩ + |1⟩) + (|0⟩ − |1⟩) if f

=1
2

(−1) f (0)
|ψ2 ⟩ = (|0⟩ + |1⟩)(|0⟩ − |1⟩)
2
(|0⟩ + |1⟩) (|0⟩ − |1⟩)
= (−1) f (0) √ √
2 2
f (0)
= (−1) |+⟩|−⟩

If f is balanced, there are two possibilities:


x f2 (x) 0 ⊕ f2 (x) 0 ⊕ f2 (x) x f3 (x) 0 ⊕ f3 (x) 0 ⊕ f3 (x)
0 0 0 1 0 1 1 0
1 1 1 0 1 0 0 1

1

 [|0⟩ (|0⟩ − |1⟩) + |1⟩ (|1⟩ − |0⟩)] if f = f2

|ψ2 ⟩ = 2
 1 [|0⟩ (|0⟩ − |1⟩) + |1⟩ (|1⟩ − |0⟩)] if f = f3

2

(−1) f (0)
|ψ2 ⟩ = (|0⟩ − |1⟩) · (|0⟩ − |1⟩)
2
(|0⟩ − |1⟩) (|0⟩ − |1⟩)
= (−1) f (0) √ · √
2 2
f (0)
= (−1) |−⟩ · |−⟩

Collecting the results:


(
(−1) f (0) |+⟩|−⟩ if f = constant
|ψ2 ⟩ =
(−1) f (0) |−⟩|−⟩ if f = balanced
(
(−1) f (0) (H |+⟩) |−⟩ if f = constant
|ψ3 ⟩ =
(−1) f (0) (H |−⟩) |−⟩ if f = balanced
(
±|0⟩|−⟩ if f = constant
|ψ3 ⟩ =
±|1⟩|−⟩ if f = balanced

8
This can be generalized as follows.
(
|0⊗n ⟩|−⟩ if f = constant
|ψ3 ⟩ = ±
±|1⊗n ⟩|−⟩ if f = balanced

Figure 2: Circuit representing The Deutsch-Jozsa algorithm

6 Qiskit Implementation of The Deutsch-Jozsa Algorithm


1 import numpy as np
2 import Qiskit
3

4 # importing needed packages from Qiskit


5 from qiskit import IBMQ , Aer
6 from qiskit . providers . ibmq import least_busy
7 from qiskit import QuantumCircuit , transpile
8
9 # import basic plot tools
10 from qiskit . visualization import plot_histogram
Listing 1: Preparing the working environment[1]

1 # set the length of the n - bit input string .


2 n = 4

1 def dj_oracle ( case , n ) :


2 # We need to make a QuantumCircuit object to return
3 # This circuit has n +1 qubits : the size of the input ,
4 # plus one output qubit
5 oracle_qc = QuantumCircuit ( n +1)
6
7 # the case in which oracle is balanced
8 if case == " balanced " :
9 # First generate a random number that tells us which CNOTs
to
10 # wrap in X - gates :
11 b = np . random . randint (1 ,2** n )

9
12 # Next , format ’b ’ as a binary string of length ’n ’,
padded with zeros :
13 b_str = format (b , ’0 ’+ str ( n ) + ’b ’)
14 # Next , we place the first X - gates . Each digit in our
binary string
15 # corresponds to a qubit , if the digit is 0 , we do nothing
, if it ’s 1
16 # we apply an X - gate to that qubit :
17 for qubit in range ( len ( b_str ) ) :
18 if b_str [ qubit ] == ’1 ’:
19 oracle_qc . x ( qubit )
20 # Do the controlled - NOT gates for each qubit , using the
output qubit as the target :
21 for qubit in range ( n ) :
22 oracle_qc . cx ( qubit , n )
23 # Next , place the final X - gates
24 for qubit in range ( len ( b_str ) ) :
25 if b_str [ qubit ] == ’1 ’:
26 oracle_qc . x ( qubit )
27

28 # Case in which oracle is constant


29 if case == " constant " :
30 # First decide what the fixed output of the oracle will be
31 # ( either always 0 or always 1)
32 output = np . random . randint (2)
33 if output == 1:
34 oracle_qc . x ( n )
35
36 oracle_gate = oracle_qc . to_gate () # . to_gate () converts quantum
circuit into quantum gate
37 oracle_gate . name = " Oracle " # To show when we display the
circuit
38 return oracle_gate
39 # return oracle_qc
Listing 2: Generating the possible U f s

1 def dj_algorithm ( oracle , n ) :


2 dj_circuit = QuantumCircuit ( n +1 , n )
3 # Set up the output qubit :
4 dj_circuit . x ( n )
5 dj_circuit . h ( n )
6 # And set up the input register :
7 for qubit in range ( n ) :
8 dj_circuit . h ( qubit )
9 # Let ’s append the oracle gate to our circuit :
10 dj_circuit . append ( oracle , range ( n +1) )
11 # Finally , perform the H - gates again and measure :
12 for qubit in range ( n ) :
13 dj_circuit . h ( qubit )
14

15 for i in range ( n ) :
16 dj_circuit . measure (i , i )
17
18 return dj_circuit
Listing 3: Adding all the elements to the circuit

1 oracle_gate = dj_oracle ( ’ balanced ’ , n )

10
2 dj_circuit = dj_algorithm ( oracle_gate , n )
Listing 4: Specifying the oracle

1 aer_sim = Aer . get_backend ( ’ aer_simulator ’)


2 t r a n s p i l e d _ d j _ c i r c u i t = transpile ( dj_circuit , aer_sim )
3
4 results = aer_sim . run ( t r a n s p i l e d _ d j _ c i r c u i t ) . result ()
5 answer = results . get_counts ()
6 plot_histogram ( answer )
Listing 5: Simulating the oracle using Aer

1 oracle_gate = dj_oracle ( ’ constant ’ , n )


2 dj_circuit = dj_algorithm ( oracle_gate , n )
Listing 6: Specifying the oracle

1 aer_sim = Aer . get_backend ( ’ aer_simulator ’)


2 t r a n s p i l e d _ d j _ c i r c u i t = transpile ( dj_circuit , aer_sim )
3
4 results = aer_sim . run ( t r a n s p i l e d _ d j _ c i r c u i t ) . result ()
5 answer = results . get_counts ()
6 plot_histogram ( answer )
Listing 7: Simulating the oracle using Aer

Figure 3: Simulation result for balanced function.

11
Figure 4: Simulation result for constant function.

References
[1] Deutsch-Jozsa Algorithm. en. URL : https : / / learn . qiskit . org (visited on
08/22/2023).
[2] Michael A. Nielsen and Isaac L. Chuang. Quantum Computation and Quantum
Information: 10th Anniversary Edition. 1st ed. Cambridge University Press, June
2012. ISBN: 9781107002173 9780511976667. DOI: 10.1017/CBO9780511976667.
URL : https://www.cambridge.org/core/product/identifier/9780511976667/
type/book (visited on 08/21/2023).

12

You might also like