Quantum Computing Unit - 3
Quantum Computing Unit - 3
● Speed: Problems that grow in complexity exponentially may be solvable with exponentially
greater efficiency, resulting in exponential speedups in computation time.
● Scale: Problems that grow in memory requirements, such as simulating molecules in quantum
chemistry, beyond the capacities of supercomputers, can be solved with relatively small
quantum computers.
● Randomness: Problems that require humanlike decision making – such as generating art,
language, and music – are already being prototyped.
● Search: Problems with massive search spaces can have all possible solutions found at the
same time and with mathematically-proven optimality.
● Security: Cryptographic schemes that rely on the multiplication of prime numbers will
become vulnerable to decryption.
A good rule of thumb is that most applications that are possible today will continue to be
solved classically in the future. The applications that are not possible today, or otherwise require
tremendous time and classical resources, are the applications fueling investments into quantum
computing.
1
1.3 Challenges of Quantum Parallelism
Although quantum computation has the potential to disrupt entire industries, the path to
achieving such breakthroughs is not a simple one. Just a few of the challenges that must be
addressed along the way include:
● Control: Achieving quantum computational advantages is going to require large numbers of
qubits, and yet controlling relatively small numbers of qubits is currently difficult.
● Timing: Realizing accurate results will require the precise timing of quantum operations, to a
degree of precision beyond the capabilities of traditional clocks.
● Coherence: Realizing accurate results will require qubits that maintain their quantum states
without errors for much longer durations than is currently possible.
● Efficiency: Quantum algorithms, such as amplitude encoding, need to become classically
feasible to implement, due to their complexity and memory requirements.
Solutions to all of these problems are currently being researched. And while the road to
fault-tolerant quantum computing is a long one, steps toward this goal are happening every day.
2. Deutsch’s Algorithm
Deutsch algorithm, which was proposed by David Deutsch. Deutsch’s Algorithm: addresses the
question — Given a function f:0,1⟶0,1 and without knowing anything more than that, determine
whether f is a constant or a balanced function with the minimum number of function evaluations.
The classical algorithm requires two function calls to figure out the answer.
2
Example:
Consider a function that accepts a single bit as input and produces a single bit as output. As
x∈{0,1}, there are only 4 options that can act on this set and give us a single bit as output.
1) Identity Function: f(x) = 0 for x= 0 and f(x) = 1 for x = 1.
2) Bit-Flip Function: f(x) = 1 for x=0 and f(x) = 0 for x = 1.
3) f(x)=0, for x=0, 1
4) f(x)=1 for x=0, 1.
Option 1, 2 are examples of balanced function and 3, 4 are examples of constant function.
● Deutsch’s algorithm addresses the question — Given a function f:0,1⟶0,1 and without knowing
anything more than that, determine whether f is a constant or a balanced function with the
minimum number of function evaluations.
● The classical algorithm requires two function calls to figure out the answer. Instead of one bit as
input, if we have n bits as input then we need minimum 2 calls to maximum (2**(n-1) +1) calls
to be certain that f(x) is constant or balanced.
● But a quantum computer can solve this problem using only one call to the function f(x),
provided we have the function f implemented as a quantum oracle, which maps the |x⟩ |y⟩ state
to |x⟩ |f(x) ⊕ y⟩.
● If apply this unitary operator to our input state which is now in |+⟩ |0⟩ state, will get:
● Expression above is important as we can note that with a single query we can get
information about every possible value of f(x) in a single step (x∈0,1).
● Note that 0⊕ f(0) = f(0) and 0⊕ f(1) = f(1).
3
● Can think of this state as evaluating f(x) for two values of x simultaneously, which is known
as ‘quantum parallelism’.
● Deutsch algorithm exploits this superposition state to find the global property of the
function f, whether it is balanced or constant by calculating the expression below:
● Will go step by step to do the math and understand what we expect at the output.
2.3 Step by Step Process:
2.3.1 Hadamard Gates in Parallel:
● Already know the action of H gates in parallel from a previous post on uniform
superposition. H⊗ H |0⟩|1⟩ = |+⟩|-⟩ = 0.5(|00⟩ -|01⟩+|10⟩-|11⟩).
2.3.2 Action of Unitary Operator:
● Now we need to apply the unitary operator U_f to this state. Let’s apply term by term and
below is the action of this operator on state |00⟩
● From the expression above, depending on whether f(0)=1 or f(0)=0, we have either |00⟩ or
|01⟩. Can combine this as — |0, 0⊕ f(0)⟩ = |0, f(0)⟩. Similarly, action of U_f on the
remaining terms can be summarized as below:
● From here, consider the Constant function case i.e. f(0) = f(1). In this case Eq. 4 reduces to:
● In case of a balanced function we have f(0)≠f(1), Since f(x) is a binary function, will have
1⊕f(0)=f(1); 1⊕f(1)=f(0). Using these, eq. 4 can be reduced to:
4
2.3.3 Apply the Final Hadamard Gate:
● Now, left with only one step i.e. apply H⊗ I to eq. 5.1 and eq. 5.2 respectively to find the
output of constant and balanced cases. Since H gate is its own inverse, we recall H|0⟩ = |+⟩
and H|+⟩ = |0⟩; Using this from eq. 5.1, will get:
● Here, note that the first qubit is in state |0⟩, compare this with the balanced function case
where it will get first qubit in state |1⟩ as below:
● Thus, it can clearly depend upon the measurement result on the first qubit whether the
function is balanced or constant.
DISCUSSION QUESTIONS:
1. What is meant by quantum parallelism?
2. List the steps involved in Deutsch's algorithm.
3. Mention the demerits of Deutsch's algorithm.
5
Lecture Notes Template
Deutsch’s Algorithm:
● Addresses the question — Given a function f:0,1⟶0,1 and without knowing anything more
than that, determine whether f is a constant or a balanced function with the minimum
number of function evaluations.
● The classical algorithm requires two function calls to figure out the answer.
1
Deutsch-Jozsa algorithm: is the n-bit extension of the single bit Deutsch algorithm. The circuit
for this particular problem is as below:
Now, it will be for n qubits. To make the underlying mathematics relatively simpler we will
introduce a very commonly used trick — Phase Kickback trick.
1.1 Phase Kickback Trick:
● Consider for any x∈{0,1}, action of U_f on the state |x⟩|−⟩, We will get the expression
below
● For f(x)=0, U_f (|x⟩|−⟩)=|x⟩|−⟩; For f(x)=1, U_f (|x⟩|−⟩)=−|x⟩|−⟩, i.e. -1 global phase factor
has been produced. These 2 cases can be combined in a single expression as below
will make use of the expression above later to simplify the calculation of Deutsch-Jozsa algorithm.
1.2 Hadamard Gates in Parallel:
● We have n qubits intitialized to |0⟩ and another one initialized to |1⟩. Hadamard gates are
applied to each input. We already know the result of action of n H gates in parallel to n
inputs initialized to |0⟩ which is described in a previous post in detail. The result is as below
2
● Since we have n inputs initialized to |0⟩ and one single qubit to |1⟩, combining with the
previous expression we get
● apply U_f which turns |x, y⟩ to |x, y⊕f(x)⟩. Important to note that |0⊕f(x)⟩=|f(x)⟩. After
applying U_f we have
● In the equation above we have used the phase kickback trick that we discussed in previous
section. After the unitary operation with U_f, applying H gate to n qubit state |x⟩ gives
● Here x⋅y is sum of bitwise product i.e. x0y0⊕x1y1⊕x2y2……etc. Let’s consider the
probability of measuring |0⟩⊗n (all outputs in state |0⟩), which is actually our state |y⟩. Here,
if f(x) is balanced then for half the terms it is 0 and for half it is 1. Thus, we will get zero
amplitude of getting output |0⟩⊗n. We will only get an outcome of |0⟩⊗n when f(x) is
constant.
DISCUSSION QUESTIONS:
1. Define DJ algorithm.
2. What is the need of the DJ algorithm for quantum problems?
3. Define Hadamard gate of DJ algorithm.
3
Lecture Notes Template
1
Result when QFT is applied on an n qubit system |x⟩.
There are two equivalent representations of QFT as shown in the figure given above, but the
second one is more useful in understanding how the quantum circuit is built.
1.2 QFT on Bloch sphere
Fig 7 represents the same 4 qubit system after QFT has been applied to it. Each qubit is
represented as a rotation about the z axis, therefore, it will lie on the x-y plane. Qubit 0 rotates π/8
radians every increment and this angle increases by two times as we move the qubits to the right.
Qubit 1 rotates π/4 radians, qubit 2 rotates π/2 radians and qubit 3 rotates π radians with every
increment.
1.3 Components necessary for QFT circuit
1.3.1 Hadamard Gate:
The Hadamard gate introduces superposition in a qubit. When applied on a qubit in computational
basis, it converts it into Hadamard basis and vice versa.
Here H represents Hadamard gate and R2 represents UROT gate with k=2
Step 1: Apply H gate on first qubit, which is |j1⟩.
Step 2: Apply CROT gate on |j1⟩ with k=2 and |j2⟩ as control qubit.
Step 3: Apply CROT gate on |j1⟩ with k=3 and |j3⟩ as control qubit.
After these 3 steps, we obtain the final state of |j1⟩ as
4
Step 6: Apply H gate on |j3⟩.
On comparing these final states obtained above to the QFT formula on n-qubits discussed earlier, it
is clear that the order of the qubits have been reversed. The final result of |j1⟩ should be the final
state of |j3⟩ according to the QFT equation. On extending this circuit to n qubit systems, we can see
that we have to swap the values of kth qubit and the (n+1-k)th qubit to get the correct answer.
5
Shows conversion of a signal between time and frequency domain
Fourier transform is an important mathematical operation that has applications in a wide
variety of fields. It allows us to transform signals between domains, most commonly time and
frequency domains. It essentially allows us to do a basis transformation.
Every signal can be decomposed into a collection of complex exponential signals of certain
frequencies. To find out the amplitudes of signals of each frequency. This allows us to find out how
much each frequency contributes to the total signal. This can be done using Fourier transform. It
converts amplitude vs time graph to amplitude vs frequency graph. It has applications in signal and
image.
DISCUSSION QUESTIONS:
1. What is meant by QFT?
2. What is the purpose of QFT in QC?
3. List the applications of QFT.
6
Lecture Notes Template
1
where ∣x⟩ is the state corresponding to the element x.
1.1.2 Diffusion operator:
The diffusion operator is a quantum operation that amplifies the amplitudes of the states that
correspond to the marked element. The diffusion operator can be written as
where θ = arcsin(1/√N) is the angle between the initial state and the marked state.
As k increases, the probability of measuring the marked state approaches 1.
○ Therefore, Grover’s algorithm can be shown to correctly find the marked state in
O(sqrt(N)) time complexity with high probability.
1.1.5 Pseudo Code:
Input: N: number of items in the list, oracle(x): a function that returns true if x is the target item,
and false otherwise
Step 1: Initialize state
Hadamard transform on all qubits
Step 2: Iterate over Grover’s algorithm
for k = 1 to sqrt(N) do
# Step 2a: Apply the oracle
Apply the oracle to the state
# Step 2b: Apply the diffusion operator
Hadamard transform on all qubits
Apply an X gate on all qubits
Apply a multi-controlled Z gate (which flips the sign of the state only if all qubits are in the state
|1>)
Apply an X gate on all qubits
Hadamard transform on all qubits
end for
Step 3: Measure the state and output the result
Measure the state and output the result
3
1.1.6 Code
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister, Aer, execute
# Define the black box function
def oracle(circuit, register, marked_state):
for i in range(len(marked_state)):
if marked_state[i] == '1':
circuit.x(register[i])
circuit.cz(register[0], register[1])
for i in range(len(marked_state)):
if marked_state[i] == '1':
circuit.x(register[i])
# Define the Grover diffusion operator
def grover_diffusion(circuit, register):
circuit.h(register)
circuit.x(register)
circuit.h(register[1])
circuit.cx(register[0], register[1])
circuit.h(register[1])
circuit.x(register)
circuit.h(register)
# Define the Grover algorithm
def grover(marked_state):
# Initialize a quantum register
# of n qubits
n = len(marked_state)
qr = QuantumRegister(n)
cr = ClassicalRegister(n)
circuit = QuantumCircuit(qr, cr)
# Apply the Hadamard gate
# to each qubit
circuit.h(qr)
# Repeat the following procedure
# O(sqrt(2 ^ n)) times
num_iterations = int(round((2 ** n) ** 0.5))
4
for i in range(num_iterations):
# Apply the black box function f
# to the current state to mark
# the solution
oracle(circuit, qr, marked_state)
# Apply the Grover diffusion
# operator to amplify the amplitude
# of the marked solution
grover_diffusion(circuit, qr)
# Measure the state to obtain
# a solution x
circuit.measure(qr, cr)
# Run the circuit on a simulator
backend = Aer.get_backend('qasm_simulator')
job = execute(circuit, backend, shots = 1)
result = job.result()
counts = result.get_counts()
x = list(counts.keys())[0]
return x
# Test the Grover algorithm
marked_state = '101'
result = grover(marked_state)
print(f"The marked state is {result}")
OUTPUT:
https://learning.quantum.ibm.com/course/fundamentals-of-quantum-algorithms/grovers-algorithm
DISCUSSION QUESTIONS:
1. What is meant by Grover’s algorithm?
2. List the various applications used by Grover's algorithm.
3. Mention the limitation of Grover's algorithm.
6
7
Lecture Notes Template
2.3 Algorithm
2.3.1 Setup
The input consists of two registers (namely, two parts): the upper p qubits comprise the first
register, and the lower n qubits are the second register.
which is the state of the second register after the Hadamard transform
Geometric visualization of Grover's algorithm shows that in the two-dimensional space spanned by
|α⟩ and |β⟩, the Grover operator is a counterclockwise rotation; hence, it can be expressed as
Note that the second register is actually in a superposition of the eigenvectors of the Grover
operator (while in the original quantum phase estimation algorithm, the second register is the
required eigenvector). This means that with some probability, we approximate θ and with some
probability, we approximate 2π - θ. Those two approximations are equivalent.
3
is determined by the error within estimation of the value of θ. The quantum phase estimation
algorithm finds, with high probability, the best p-bit approximation of θ this means that if p is large
enough, we will have ∆ ≈ 0, hence |∆𝑀| ≈ 0.
DISCUSSION QUESTIONS:
1. Define Quantum search algorithm.
2. What is meant by quantum counting?
3. Mention the Steps to evaluate quantum counting.