mod03lec18 - Grover's algorithm Programming

NPTEL-NOC IITM
7 Sept 202128:05

Summary

TLDRThis video tutorial delves into the implementation of Grover's search algorithm using Qiskit, a quantum computing framework. The presenter guides viewers through creating a quantum circuit to search for an element in an array of size 16 using a 4-qubit register. The tutorial covers the creation of a phase oracle, the application of multi-controlled Toffoli gates, and the reflection about the uniform superposition state. It also explores iteratively applying Grover's oracle and reflection to increase the probability of measuring the marked element. The session concludes with a discussion on the algorithm's effectiveness and ponders future considerations like handling multiple marked elements and unknown quantities.

Takeaways

  • 🔍 The video discusses the implementation of Grover's search algorithm using Qiskit, a Python library for quantum computing.
  • 🧩 The example focuses on searching for an element in an array of size 16, which can be addressed using a 4-qubit register.
  • 🌐 An ancillary qubit is required to store the state |-⟩, which is used to convert a standard oracle into a phase oracle.
  • 🔑 Grover's algorithm involves creating a superposition of all states, applying the oracle, and reflecting about the uniform superposition state.
  • 🎯 The oracle function, represented as f(x), is crucial for identifying the marked element, which is the element we are searching for.
  • 🤔 The implementation of the oracle is demonstrated, showing how to use a multi-controlled Toffoli gate to achieve the desired functionality.
  • 📊 The video uses a state vector simulator to visualize the state of the qubits throughout the algorithm's execution.
  • 📉 The script includes a step-by-step guide on how to apply Grover's algorithm iteratively, including the use of phase oracles and reflections.
  • 📊 A histogram is used to show the probability distribution of measured states, highlighting the marked element with the highest probability.
  • 🔮 The video concludes with considerations for future exploration, such as handling multiple marked elements and scenarios where the number of elements is unknown.

Q & A

  • What is the primary focus of the video script?

    -The primary focus of the video script is the implementation of Grover's search algorithm using Qiskit, a Python library for working with quantum computers.

  • What is the size of the array that Grover's search is applied to in the script?

    -In the script, Grover's search is applied to an array of size 16, with indices labeled from 0 to 15.

  • How many qubits are required to address elements in an array of size 16?

    -Four qubits are required to address elements in an array of size 16, as 4 qubits can address elements from 0 to 2^4 - 1, which is 15.

  • What is the purpose of the ancillary qubit in the Grover's search circuit?

    -The ancillary qubit is used to store the state |-⟩ and is necessary to turn the standard oracle into a phase oracle, which is a requirement for Grover's search algorithm.

  • What is the marked element in the Grover's search example provided in the script?

    -The marked element in the Grover's search example is the element with index 13, which is represented by the bits 1 0 1 1 in reverse order due to Qiskit's least significant bit to most significant bit ordering.

  • What is the role of the oracle function in Grover's search?

    -The oracle function in Grover's search is represented by a function f(x), which is 1 if x is the marked element and 0 otherwise. It is implemented as a unitary operation that flips the phase of the marked state.

  • What is a multi-controlled Toffoli gate and how is it used in the script?

    -A multi-controlled Toffoli gate is a quantum gate that takes multiple control qubits and a single target qubit. If all control qubits are in state |1⟩, it flips the state of the target qubit. In the script, it is used to implement the oracle for Grover's search.

  • How is the reflection about the uniform superposition state achieved in the script?

    -The reflection about the uniform superposition state is achieved by first rotating the uniform superposition state to the state of all zeros using Hadamards, then reflecting about the state of all zeros, and finally rotating back to the uniform superposition state.

  • What is the initial state of the qubits in the Grover's search circuit as described in the script?

    -The initial state of the qubits in the Grover's search circuit is a uniform superposition of all states for the first n qubits, and the final qubit is in the state |-⟩.

  • How does the script visualize the state of the qubits during Grover's search?

    -The script visualizes the state of the qubits by projecting the state vector onto the 2D plane spanned by the marked state |a⟩ and the uniform superposition of unmarked states |e⟩, and then plotting the projection.

  • What is the outcome of running Grover's search algorithm as described in the script?

    -The outcome of running Grover's search algorithm as described in the script is that the highest probability measurement is the marked element, which is |1011⟩ in this case, demonstrating the effectiveness of Grover's algorithm.

Outlines

00:00

🔍 Introduction to Grover's Search Algorithm using Qiskit

The video begins with an introduction to implementing Grover's search algorithm using Qiskit. The focus is on searching for an element in an array of size 16 using a 4-qubit register. The importance of an ancillary qubit for the phase oracle is emphasized, which is necessary to transform the standard oracle into a phase oracle. The video explains the setup, including the need for n classical bits to measure the data qubits at the end of the circuit. An example of a marked element with index 13 is used to illustrate how the element is represented in binary and how the oracle function works in the context of Grover's algorithm.

05:00

🛠️ Implementing the Oracle using Multi-Controlled Toffoli Gates

The paragraph delves into the implementation of the oracle function using multi-controlled Toffoli gates. It explains the concept of a multi-controlled Toffoli gate, which acts on n control qubits and a single target qubit, and how it can be used to implement the oracle for Grover's algorithm. The video provides a detailed walkthrough of how to construct the oracle for a specific marked element, using the example of a marked element with all bits set to 1 and then generalizes to other cases, including the case where the marked element is '1 0 1 1'. The process involves applying Pauli-X gates to specific qubits before and after the multi-controlled Toffoli gate to ensure the correct implementation of the oracle.

10:03

🔄 Reflecting About the Uniform Superposition State

This section discusses the second key component of Grover's algorithm: reflecting about the uniform superposition state. The process involves creating a uniform superposition of all states, which is the initial state for Grover's algorithm. The video explains how to achieve this by applying Hadamard gates to all qubits, including the ancillary qubit. The reflection about the uniform superposition state is then described, which is necessary to move the state vector closer to the marked state. The video hints that the steps for this reflection will be similar to those used for the oracle application and that the multi-controlled Toffoli gate will be used again.

15:04

📈 Visualizing the State Vector and Grover's Algorithm Iterations

The focus of this paragraph is on visualizing the state vector and the effect of Grover's algorithm iterations. The video demonstrates how to use a state vector simulator to see the actual state of the qubits after each step of the algorithm. It shows the initial state vector and how it changes as Grover's algorithm is applied. The video then explains how to project the state vector onto the subspace spanned by the marked state and the uniform superposition of unmarked states. The changes in the state vector after each iteration of Grover's algorithm are visualized, showing how the vector rotates towards the marked state with each iteration.

20:05

🔍 Iterating Grover's Algorithm and Optimizing the Number of Iterations

This section discusses the iterative process of Grover's algorithm and how to determine the optimal number of iterations. The video explains that further rotations beyond a certain point will only decrease the angle with the marked state, so it's crucial to find the right number of iterations. The process involves computing the initial angle and then determining the number of iterations that will keep the state vector in the desired quadrant. The video then shows how to apply the full Grover's algorithm, including the oracle and reflection about the uniform superposition state, multiple times. The results are visualized to show the state vector's progression towards the marked state.

25:07

🎯 Finalizing Grover's Algorithm and Measuring the Qubits

The final paragraph covers the completion of Grover's algorithm and the measurement of qubits. The video demonstrates how to create an initial circuit with a uniformly superposed state and then apply the optimal number of Grover's oracle and reflection operations. The use of a quantum simulator to measure the qubits and obtain a histogram of results is shown. The highest probability measurement corresponds to the marked element, confirming the success of Grover's algorithm. The video concludes with further points for consideration, such as handling multiple marked elements and scenarios where the number of marked elements is unknown, encouraging viewers to explore these aspects on their own.

Mindmap

Keywords

💡Grover's Search

Grover's Search is a quantum algorithm designed to find a specific item from an unsorted database with a higher probability of success compared to classical algorithms. In the video, Grover's Search is implemented using Qiskit to search for an element in an array of size 16. The algorithm is central to the video's theme, demonstrating how quantum computing can be leveraged to solve search problems more efficiently.

💡Qiskit

Qiskit is an open-source quantum computing software development kit by IBM. It allows users to create, edit, and invoke quantum circuits. In the script, Qiskit is used to implement Grover's Search algorithm, showcasing its utility in practical quantum programming and its importance in the development of quantum algorithms.

💡Qubit

A qubit, short for quantum bit, is the fundamental unit of quantum information. Unlike classical bits, qubits can exist in a state of superposition, allowing them to represent both 0 and 1 simultaneously. In the video, a 4-qubit register is used to address elements in an array, illustrating the concept's role in quantum computing's parallelism and computational power.

💡Oracle

In the context of Grover's Search, the Oracle is a black box function that can identify the correct solution(s) to a problem within a search space. The script describes how the Oracle is implemented in Qiskit, marking it as a crucial component that allows Grover's algorithm to function by differentiating between the marked item and the unmarked ones.

💡Phase Oracle

A Phase Oracle is a concept used in Grover's Search to introduce a phase shift for the correct solution state, making it distinguishable from other states. The script explains how the standard Oracle is turned into a Phase Oracle using an ancillary qubit, which is essential for the algorithm's ability to amplify the probability of the correct solution.

💡Multi-controlled Toffoli

The Multi-controlled Toffoli gate, also known as the MC-TOFFOLI or CCX gate, is a quantum logic gate used to implement the Oracle in Grover's Search. As described in the script, it takes multiple control qubits and applies a Toffoli gate to a target qubit based on the state of the controls, which is vital for creating the Oracle function that can identify the marked element.

💡Reflection

In quantum mechanics, reflection refers to the process of inverting the phase of a quantum state. The script discusses how Grover's algorithm uses reflection operators to invert the amplitude of the correct solution state, increasing its probability of being measured. This concept is central to understanding the iterative process of Grover's Search.

💡Uniform Superposition

A uniform superposition state is a quantum state where each possible state is equally probable. In the script, creating a uniform superposition state is the initial step in Grover's Search, achieved by applying Hadamard gates to each qubit. This state is essential for Grover's algorithm to explore the solution space effectively.

💡State Vector Simulator

The State Vector Simulator is a tool used in Qiskit to simulate the state of qubits in a quantum circuit. The script mentions using this simulator to visualize the state of qubits after each iteration of Grover's algorithm, allowing the viewer to understand how the algorithm manipulates the quantum state to find the solution.

💡Amplitude Amplification

Amplitude Amplification is a technique used in Grover's Search to increase the probability of measuring the correct solution state. The script explains how Grover's algorithm iteratively applies the Oracle and reflection about the uniform superposition state to amplify the amplitude of the marked item, bringing it closer to the uniform superposition state.

Highlights

Introduction to the implementation of Grover's search using Qiskit.

Importing the necessary Qiskit modules for the demonstration.

Explaining the case of searching for an element in an array of size 16 using a 4 qubit register.

Discussion on the need for an ancillary qubit to store the state |cat-⟩.

Creating a circuit with n data qubits and an ancillary qubit for the |cat-⟩ state.

Introduction to the concept of the oracle function in Grover's algorithm.

Marking an element with index 13 and its representation in binary.

Explanation of the oracle function as a reversible operation in the algorithm.

Introduction to the multi-controlled Toffoli gate for implementing the oracle.

Detailed explanation of the multi-controlled Toffoli gate and its function.

Implementation of the oracle for the case where the marked element is a string of all ones.

General case implementation of the oracle for a marked element of 1 0 1 1.

Discussion on the application of the multi-controlled Toffoli gate in the oracle.

Introduction to the concept of reflecting about the uniform superposition state.

Explanation of the steps to reflect about the state of all zeros using Hadamards.

Building the initial circuit with a superposition of all states and the |cat-⟩ state on the final qubit.

Running the circuit using a state vector simulator to visualize the state changes.

Projection of the state vector onto the subspace spanned by |ket a⟩ and |ket e⟩.

Iterative application of Grover's oracle and reflection about the uniform superposition state.

Computation of the initial angle theta naught for the state vector.

Discussion on the effect of multiple marked elements on Grover's algorithm.

Final circuit construction with the determined number of Grover iterations.

Use of the qasm simulator for a real-world scenario where the state vector is not accessible.

Observation of the highest probability of measuring the marked element at the end of the algorithm.

Conclusion and future considerations for the algorithm's application.

Transcripts

play00:01

[Music]

play00:15

uh hello everyone

play00:17

today we look at the implementation of

play00:19

grover search using kiskit

play00:22

so let's get started

play00:25

so first

play00:26

as always we need to import

play00:28

the cascade modules

play00:30

so let's do that

play00:34

now here we are going to look at the

play00:36

case where we need to search for an

play00:38

element

play00:39

in an array of size 16

play00:42

so here the elements are the indices are

play00:45

labeled from 0 up to 15

play00:47

and we can address this using a 4 qubit

play00:51

register

play00:52

the 4 qubit register can address

play00:54

elements from 0 to 2 to the power 4

play00:56

minus 1 which is 15

play00:59

and apart from these four data

play01:02

data qubits we also need an ancillar

play01:04

qubit to store the state cat minus

play01:08

as we had seen in the theory lecture

play01:10

this allows us to turn the standard

play01:12

oracle into a phase oracle

play01:16

so

play01:17

let's see

play01:18

so we have n is 4 n is the

play01:21

number of bits

play01:23

for for the index

play01:24

and we create n plus one qubits

play01:28

uh so we have n qubits to address uh

play01:31

these indices and then

play01:33

an extra qubit for the ancillary state

play01:36

get minus

play01:37

and our circuit also has n classical

play01:40

bits these are used to measure the

play01:43

end data qubits at the end of the

play01:45

circuit

play01:47

so let's say we have one element marked

play01:50

which is the element with index 13.

play01:53

since 13 in binary is 1 1 0 1

play01:58

the marked element is represented by

play02:01

these these bits in the reverse order

play02:03

which is 1 0 1 1.

play02:05

the reason being that cascade considers

play02:09

the queue bits from lsb to msb that is

play02:12

from least significant bit to most

play02:14

significant bit so if i just

play02:17

order these bits 1 1 0 1 from the least

play02:20

significant bit to most significant bit

play02:22

i get 1 0 1 1 so the 0th qubit is 1 then

play02:26

0 then 1

play02:28

so our marked

play02:30

element is represented by the index 1 0

play02:33

1 1

play02:34

and ah now let's see how does our oracle

play02:38

look like

play02:40

the oracle function

play02:42

uh

play02:43

now is is represented by some function f

play02:46

where f x is one if x is the marked

play02:48

element or the amount index and 0

play02:50

otherwise

play02:53

so we need to be able to apply this

play02:55

reversibly as we had seen in the theory

play02:58

lecture again this can be represented by

play03:01

unitary which maps the state get x

play03:05

where x is the data register

play03:07

or the index register and get y where y

play03:10

is a single output qubit

play03:12

so this is mapped to get x and get y x

play03:15

or f x we had also seen that when y is

play03:18

in the state get minus this will bring

play03:20

it to minus 1 to the power f x

play03:23

get x and k minus

play03:26

so we now have to implement such an

play03:28

oracle so note that over here we already

play03:30

know the marked element but in general

play03:33

in in a general setting we will be just

play03:35

be given this oracle as a black box we

play03:37

won't have to implement it right so in

play03:39

our case for for the purposes of

play03:41

demonstration we are also implementing

play03:43

the oracle

play03:44

ourself so first let's see that how do

play03:48

we actually implement such an oracle uh

play03:50

to do so we need the concept of

play03:53

something called as a

play03:54

multi-controlled toefl so let's see what

play03:57

is actually a multi controlled toefl

play04:10

so a multicontrolled toefl gate is a

play04:13

general gate

play04:14

which takes n

play04:17

control qubits say x 1 up to so on x n

play04:23

these are our control qubits

play04:26

and it takes a

play04:28

single

play04:30

single target qubit so we have n control

play04:36

qubits

play04:39

next we have a single

play04:43

target qubit

play04:46

and if these are in this

play04:48

classical or standard basis state x one

play04:51

up to x n and y

play04:55

then the output after the application of

play04:58

multi control toefl is going to be just

play05:00

x 1 up to x n and then y

play05:03

xor

play05:05

and of x 1 up to x n so let's see how it

play05:07

is going to be so the symbol for multi

play05:09

control topoly is very similar to a c

play05:11

naught in fact this gate is quite

play05:14

similar to a c naught it is a

play05:15

generalization of c naught to n controls

play05:19

and here ah

play05:21

the output is also going to be x 1 x 2

play05:24

up to x n

play05:26

and then y

play05:27

xor

play05:29

x 1 up to

play05:31

x n so this is an and of x 1 up to x n

play05:35

and then we

play05:37

take an xor with y

play05:42

so let's write it so u might be

play05:45

controlled awfully which i'll

play05:48

uh which i'll abbreviate it as msmct

play05:52

acts on the control register x

play05:55

and the output register y

play05:58

and it produces x

play06:01

and

play06:02

y plus

play06:05

x 1 up to x n the and of these things

play06:11

so x is actually

play06:13

x 1 up to x and only i am just

play06:15

abbreviating it as x over here

play06:18

so what is happening is that if each of

play06:20

these x i's are 1 then the output bit is

play06:23

flipped

play06:25

if any of them is 0 it is not flipped so

play06:28

we are essentially taking and of all

play06:30

these

play06:31

and this is how mighty control toefl

play06:33

gate looks like now why are we

play06:35

discussing this over here the reason why

play06:38

we were looking at this was to implement

play06:40

our oracle

play06:42

so our oracle

play06:44

uh

play06:45

was supposed to be f x

play06:50

which equals

play06:51

1

play06:52

if

play06:54

x is

play06:55

marked

play07:00

index and 0 otherwise

play07:06

now if you see

play07:08

what we actually had to

play07:10

do was that we had to implement this

play07:12

oracle u subscript f which acts on x and

play07:17

y

play07:20

and it produces x

play07:22

get x and get y x or f x

play07:26

so suppose if the marked element if

play07:28

marked index was just

play07:33

marked index was

play07:36

a string of all ones

play07:42

for this case i uh we have just

play07:44

implemented our effects why because this

play07:47

is what the multi control toefl was

play07:49

doing

play07:50

so if the market element was on the

play07:53

string of all ones then we know that f x

play07:55

is going to be just x 1 x 2 up to x n

play07:59

right because if each of these x is

play08:02

going to be 1 then f x would be 1 if any

play08:04

of them is 0 then this product or this

play08:06

and is also going to be 0 so for f x

play08:09

equals x 1 up to the product of x 1 up

play08:12

to x n or the and of x 1 up to x n we

play08:14

have we have we already know that

play08:17

this circuit is going to be just mct

play08:21

the multi controlled of fully right

play08:23

so we have already seen how to implement

play08:27

the oracle for the case where x is a

play08:30

market index and which is that we just

play08:32

have to use the multi controlled top

play08:33

value

play08:35

uh with xs control and y is the target

play08:38

qubit

play08:39

now let's take another case

play08:41

say if marked index was say all zeros

play08:52

let me say i work with just four

play08:55

four qubits which is the case we have so

play08:58

and we have these four bits so let's say

play08:59

the marked index was zero zero zero zero

play09:02

in this case

play09:04

uh

play09:05

my fx should be one only if each of

play09:07

these is 0 each of my xi's are 0 and it

play09:10

should be

play09:11

1 otherwise right

play09:13

so one way to implement it is as follows

play09:16

that i can say that my f x is going to

play09:19

be

play09:20

uh

play09:21

x1 complement

play09:23

x2 complement

play09:25

x3 complement and x4 complement so note

play09:28

that in each of these is 0 then the

play09:31

product or the and of x1 complement x2

play09:33

complement x3 complement x4 complement

play09:35

is going to be 1 otherwise it is going

play09:37

to be

play09:38

0 right because if any of these is 1

play09:40

then the corresponding complement is 0

play09:42

and so the whole and is going to be 0.

play09:45

now let's take the general case or

play09:49

let us take the case which we have over

play09:51

here

play09:52

wherein the marked element is say

play09:54

uh

play09:56

say mark element

play10:03

is

play10:04

what we have over here which is

play10:07

1 0 1 1 in kisskids ordering right

play10:12

so

play10:12

here my f x would have been

play10:17

x 1 x 2 complement

play10:20

x 3 x 4 and if you see that this is

play10:24

going to be 1 if and only if x 1 is 1 x

play10:26

3 is 1 x 4 s 1 and x 2 complement is 1

play10:30

which means x 2 is 0 right so basically

play10:32

if x is the math element it is going to

play10:34

be 1 otherwise it is going to be 0 and

play10:37

now if you notice the general pattern

play10:38

what we have to do is that we have to

play10:40

define f x to be the bit flip over all

play10:43

those cases where the index

play10:45

wherein the marked index has that width

play10:48

0 otherwise if the index is 1 then we

play10:50

don't flip that bit and this is the

play10:52

general thing

play10:53

and now once we flip those bits we can

play10:55

actually apply the multi control toefl

play10:59

right so it's actually controlling on x

play11:01

i being zero if if the marked element

play11:05

has if with zero and controlling one x i

play11:07

being one if the marked element is i

play11:09

output one

play11:10

so here this actually gives us a very

play11:12

simple circuit

play11:18

circuit for

play11:19

u or a curl or u f

play11:21

which is that

play11:24

say we have x one and so on up to x n

play11:27

and then i put uh

play11:29

i put in poly x

play11:32

if

play11:34

i switch is 0

play11:38

otherwise i don't put a poly x on each

play11:40

of these ith bits and then i control and

play11:43

then i apply this multi controlled

play11:46

toefl

play11:50

which controls my target register y

play11:54

right because once we have uh applied an

play11:57

x i can actually i'm controlling on

play11:59

those bits being zero

play12:01

which have which have the uh

play12:04

which which basically have the uh

play12:06

element marked index being zero at that

play12:09

bit so

play12:12

but i also need to now correct these

play12:14

back so if the isbit is 0 i am going to

play12:17

apply x once before mct and once after

play12:19

mct so that i restore the state

play12:22

of the input register and then i don't y

play12:25

xor

play12:26

ah

play12:27

f x right so this is what i will be able

play12:30

to get

play12:32

so just think about it convince yourself

play12:35

that it works

play12:36

and now let me let us go back to the

play12:40

so here essentially we look for those so

play12:43

here this is our function apply

play12:44

underscore oracle

play12:46

this takes as input our circuit in which

play12:49

is in the variable ckt it takes as input

play12:52

the

play12:53

marked index as a list of bits

play12:56

these bits are in the same order as

play12:57

biscuits orders its bits

play13:00

and then i have an integer n which is

play13:02

the number of data qubits this excludes

play13:04

the cat minus state right this n

play13:06

excludes the get measured because

play13:07

including that we would have had n plus

play13:09

one qubits and ah what we do in the

play13:12

circuit is that uh we look for uh those

play13:15

bits which are which are not

play13:17

uh which are zero ah in the index of the

play13:20

marked element okay so we look for those

play13:22

bits which are zero in the index of the

play13:24

marked element ah we apply a poly x

play13:27

scales on precisely those qubits

play13:29

and uh then we do a multi controlled

play13:32

properly

play13:34

again the

play13:35

the control is the are the control the

play13:37

controlling qubits are those from zero

play13:39

to n minus one crystal starts the

play13:41

ordering of the bits from zero and the

play13:44

target is the qubit n which is the final

play13:46

qubit

play13:47

and then finally we again ah do a poly x

play13:50

on the same qubits we did poly x earlier

play13:53

this actually restores the states of the

play13:56

input qubits

play13:57

right so this is our complete function

play13:59

to apply the oracle

play14:03

now

play14:04

uh the second ingredient of the growers

play14:07

search algorithm which we had seen in

play14:08

the theory lecture also was that we need

play14:11

to be able to reflect about the uniform

play14:13

superposition state

play14:16

ah and as we had seen in the theory

play14:18

lecture the way to do that was to first

play14:20

rotate the uniformly superposed state to

play14:22

the state consisting of all zeros and

play14:25

you know that you can do this using

play14:26

hadamards then you reflect about the

play14:28

state of all zeros and then you again

play14:30

rotate the state of all zeros back to

play14:32

uniformly superpower state

play14:35

now interesting thing now is that how do

play14:37

we reflect about zeros

play14:39

uh the hint is that the steps are going

play14:41

to be very similar for the steps for the

play14:44

application of oracle

play14:45

and one more thing is that you will

play14:48

again need to use the multi-controlled

play14:50

toefl gate

play14:52

i have given the reference to the same

play14:54

her in the cell

play14:56

and uh

play14:58

just

play14:58

to

play14:59

uh re-emphasize what this my tk uh the

play15:04

multi-controlled affiliate requires is

play15:06

that it requires us input the list of

play15:08

control qubits and the target qubits we

play15:10

don't need answer lucky bit over here

play15:14

so you will have to use this library to

play15:16

implement the reflection about the

play15:18

uniformly superposed state

play15:21

okay so this is left as an exercise for

play15:23

you

play15:24

i have already implemented it so i am

play15:26

just going to import this from my

play15:27

solution

play15:29

uh now once you would have implemented

play15:32

the superposition of so so let us start

play15:35

building the circuit now okay so these

play15:37

were the basic ingredients of our

play15:38

circuit uh when we start building our

play15:41

circuit

play15:42

uh we first need to create a

play15:45

superposition of all states right this

play15:47

is this is because this is going to be

play15:49

our initial state so this is for the

play15:51

first n qubits the qubits from 0 to n

play15:53

minus 1

play15:54

on the final qubit

play15:56

we have to put in the state cat minus

play15:59

okay so this is something which we have

play16:01

to do and what you can do is that if you

play16:03

apply a hard amount on the qubits from 0

play16:05

to n minus 1 you will be able to obtain

play16:07

a uniform superpose state on those

play16:10

on the final qubit which which is

play16:12

initially in the state get 0 as all

play16:14

qubits are uh what you will do is that

play16:17

we will first apply a polyx which will

play16:19

bring it to state 1 and then we will

play16:21

apply a hard amount which will bring it

play16:23

to the state ket minus okay hard amount

play16:25

on cat 1 is going to be 10 minus

play16:27

so let's see so we do uh

play16:30

poly x on the last qubit

play16:33

we apply a harder mode on all the qubits

play16:35

including the last one and this will

play16:37

give me my required initial state

play16:40

so let us see how the circuit looks like

play16:43

so now the circuit looks like as follows

play16:45

there is an x on the final qubit there

play16:47

is a hard amount on all the qubits

play16:49

including the final ones the final one

play16:50

is change to the state get minus the

play16:54

first four as in from 0 to 3 they are

play16:56

changed to the state

play16:58

of the uniform uniform superposition of

play17:00

all all possible classical speech

play17:05

so let's run the circuit and we'll

play17:07

obtain the state vector

play17:10

to

play17:11

since we want to actually obtain the

play17:13

state vector we are going to use state

play17:15

vector simulator

play17:16

ah this actually allows us to see the

play17:19

actual state

play17:20

of the qubits note that in real world

play17:23

you won't have access to the state

play17:25

right because this forbids the laws of

play17:27

quantum mechanics but right now we are

play17:30

doing a demonstration just to see that

play17:32

how how these states actually rotate

play17:35

under the influence of these reflection

play17:37

operators so just to see the effect we

play17:39

we are going to use the state vector

play17:40

simulator ah to be able to run the

play17:43

simulator we first need to assemble our

play17:45

circuit using the assemble function and

play17:48

then we run our simulator on the

play17:50

assembled queue object and we obtain the

play17:52

result

play17:53

from the result we can obtain the data

play17:55

which is the state vector

play17:58

now we want to look at the state vector

play18:00

only on the first 10 qubits the final

play18:02

one we already know it will always

play18:04

remain in get minus so to look at the

play18:06

first and qubits we index the elements

play18:09

from 0 up to 2 to the power

play18:11

this gives us the state vector on the

play18:13

first n qubits this is size 2 to the

play18:15

power n

play18:17

and now since it's very difficult to

play18:18

visualize such a large vector we will

play18:21

actually plot it ah we'll plot it

play18:23

projection on the 2d plane we actually

play18:25

know that this vector lies in the 2d

play18:27

space spanned by get a and cat e so cat

play18:30

a is the marked element over here and

play18:32

ket e is the superposition of all the

play18:35

unmarked states right so you can refer

play18:37

back to the notation from the theory

play18:39

lecture

play18:40

so let's build our get a and get e

play18:43

states

play18:44

right so cat a uh is is just a

play18:49

vector with with one uh element which is

play18:52

one which is the thirteenth element

play18:53

right so this is the

play18:55

this is just a basis state uh

play18:57

corresponding to the 13th element and

play19:00

ket e is the superposition of all other

play19:02

states and how we obtain it is that uh

play19:05

so it's it's basically first

play19:08

we have a

play19:09

so there's a 0 in the 13th position and

play19:11

then there is a uniform superposition on

play19:14

the remaining ones so we obtain just by

play19:17

creating a vector of all ones and then

play19:19

subtracting k a which gives a

play19:21

a 1 on every bit except the 13th one and

play19:26

and then we normalize it

play19:29

so this is our cat a vector so note that

play19:32

there is one in the 13th position this

play19:33

is a get e vector note that there is 0

play19:35

in the 13th position and the remaining

play19:37

are

play19:39

equal and this vector is again

play19:41

normalized

play19:43

so

play19:44

let's project let's write a function to

play19:46

project our vector on to subspace

play19:48

spanned by these two vectors

play19:50

so we have this function get projection

play19:52

this basically just takes the dot

play19:53

product of psi with get a and get e

play19:55

respectively note that k e and k e are

play19:58

already normalized so there is no

play19:59

further normalization needed on the

play20:01

projection vector

play20:03

so we can actually obtain the projection

play20:05

vector once we are done we also have

play20:07

written a function to plot this

play20:09

projection vector

play20:12

so let us see how does our initial

play20:14

vector look like when we have run the

play20:16

circuit to produce a statement

play20:19

so we see that this is our initial

play20:21

vector so cat a is the x axis over here

play20:24

get e is the y axis over here that is

play20:26

the marked element okay

play20:28

so get a is the y axis get e is the x

play20:30

axis

play20:31

so here uh we see that it is quite close

play20:34

to

play20:35

x axis right it's further away from y

play20:38

axis so actually

play20:39

it's close to ah so it's since it's a

play20:42

uniform superposition state ah it's

play20:44

actually a bit far from the actual

play20:46

marked element it lies more closer

play20:48

towards the uniform superposition of

play20:50

unmarked elements

play20:52

and

play20:54

so let's now bring it closer to y axis

play20:58

which is the mark limit state

play21:00

and to do that we need to

play21:02

iteratively apply our phase vehicle and

play21:04

the reflection about the uniform so let

play21:06

us first apply our phase oracle

play21:08

okay so we already have written the

play21:10

function above

play21:13

and let us draw the circuit so this is

play21:15

how the circuit would look like

play21:18

okay so note that here

play21:20

our

play21:21

marked index was

play21:24

labeled as 1 0 1 1 since the qubit 1

play21:28

since the first qubit is in the state 0

play21:30

for the marked set of marked indices

play21:33

we have an x

play21:35

before and after mct and then there is

play21:37

an mct in between okay so we have

play21:39

discussed it in detail now

play21:41

now let us see how do we reflect this

play21:44

vector about the uniform state

play21:47

so

play21:49

to reflect it about the uniform state

play21:51

and to do so

play21:53

we will actually use the function which

play21:56

you have to complete as part of the

play21:57

exercise so i have already imported this

play22:00

function

play22:01

uh

play22:03

so but you will have to implement it

play22:05

again for the exercise

play22:07

and

play22:09

this is how the grover circuit now looks

play22:11

like

play22:12

once we have the

play22:14

grovers oracle and then we have the

play22:17

reflection about the uniformly

play22:19

superpower state okay

play22:23

now we are going to run the circuit

play22:25

again and

play22:27

we will actually plot the state vector

play22:29

again after running the circuit

play22:34

so

play22:35

we again repeat the steps we obtain our

play22:37

state vector simulator we assemble our

play22:39

circuit we run the state vector

play22:40

simulator on the assembled circuit

play22:43

okay and then we obtain the result from

play22:45

which we get the state vector we project

play22:47

the state vector onto the state spanned

play22:49

by uh the subspace standard by k a and k

play22:52

t and then finally we project uh we plot

play22:55

the vector we plot the projected vector

play22:58

so we see that uh the angle where the x

play23:00

axis is increased or it has moved

play23:02

towards the y axis which consists of the

play23:04

marked elements so earlier this was how

play23:07

the vector looked like now it is making

play23:09

a larger angle with the x axis right its

play23:11

its a bit closer to y axis now

play23:14

so we have indeed rotated it

play23:16

now let us do another step of the phase

play23:18

oracle followed by reflection about the

play23:20

uniform okay so we apply our oracle and

play23:23

then we reflect about the uniform

play23:24

superposition

play23:26

let's

play23:27

draw the circuit now and see how it

play23:29

looks like

play23:30

so the circuit has now

play23:32

uh two iterations of

play23:36

of a grover's required followed by

play23:37

reflection about the uniformly

play23:39

superposed state

play23:42

so this is four parts now so

play23:44

uh

play23:45

go versus recall reflection about

play23:46

uniform supposed state global required

play23:48

reflection amount uniform supreme coast

play23:50

speed

play23:51

and again we follow the same steps

play23:53

to obtain the state vector after running

play23:56

this complete circuit we also then ah

play23:58

project the state vector onto this space

play24:01

bandwidth at e and get a and this is how

play24:03

it looks like and now we see that it is

play24:05

very close to y axis right it is moving

play24:07

closer and closer so initially

play24:10

this is how it looked like after

play24:12

rotation thing once this is how it

play24:14

appears after rotating another time this

play24:16

is it was

play24:18

so we see that it indeed rotates

play24:19

clockwise towards the ket state

play24:23

so let's now do another step of

play24:26

grover's vehicle followed by reflection

play24:28

about the uniform superpower state and

play24:30

now we see that this big chunk of the

play24:32

circuit is now repeating thrice

play24:35

so this is the term as a

play24:37

global circle followed by reflection

play24:39

globalization followed by reflection

play24:40

about uniform globals that require

play24:41

followed by reflection about uniform so

play24:43

this these two steps are repeated thrice

play24:46

and we as before we obtain the state

play24:48

vector and we project it on to the 2d

play24:50

space and then we plot the statement

play24:54

and now we see it is going in the second

play24:56

quadrant

play24:57

right so after this

play24:58

further rotations are only going to

play25:00

decrease its angle with the y axis so

play25:02

further rotations will not help much

play25:06

uh in real world we actually can't plot

play25:08

the state vector again and again we

play25:10

actually need to decide the number of

play25:12

iterations in advance so let's do that

play25:14

now to do so we first need to compute

play25:17

the initial angle theta naught

play25:19

ah okay and

play25:21

this is again an exercise for you

play25:24

to compute the initial angle of the

play25:25

state vector

play25:30

once you are done so if this angle is

play25:32

theta naught we know that each rotation

play25:34

increases this angle by twice theta

play25:36

naught after t rotation it is going to

play25:38

be 2 t plus 1 theta so you need to find

play25:40

the largest such t so that it remains in

play25:42

quadrant one

play25:44

this is again left as an exercise for

play25:46

you

play25:49

and

play25:50

now to

play25:52

complete the story we are going to run

play25:55

it capital t number of times so i have

play25:57

already obtained the value of capital t

play25:59

since i had implemented that

play26:00

and ah now we create an initial circuit

play26:03

which consists of uniformly superposed

play26:05

state on first n cubits and i get minus

play26:07

on the final qubit and next we are going

play26:10

to apply capital t times our oracle and

play26:12

our

play26:14

reflection about the uniform and then

play26:16

finally we measure our qubits right

play26:21

so this is how the final circuits looks

play26:23

like

play26:25

okay

play26:26

and

play26:28

let's see the effect this time we are

play26:30

going to use the quasim simulator uh the

play26:33

reason being that

play26:35

we want to actually just measure the

play26:37

qubits we are not looking at the state

play26:38

vector now this scenario is closer to

play26:40

real world also

play26:43

so we have obtained our result and let's

play26:46

obtain the count of every element after

play26:48

which we can plot on histogram

play26:50

and we see that uh

play26:52

the highest probability is that of

play26:54

measuring the state to be one month zero

play26:56

one right and this was actually our

play26:58

marked element also

play27:01

so we this grover's algorithm indeed

play27:03

does work

play27:05

ah

play27:06

and

play27:07

here are a few more points to ponder

play27:09

about in future

play27:11

what happens when there are more than

play27:12

one among element right so figure out

play27:14

how the algorithm is going to change

play27:16

how much what would be the initial angle

play27:18

in that case how much would be the

play27:20

rotation in each step and if possible

play27:22

you can implement it also in criscite

play27:24

this is not graded this part is an

play27:27

ungraded part but it will be good for

play27:29

you to think about

play27:30

and what if you don't even know the

play27:32

number of elements and what would be the

play27:34

modifications that will be needed there

play27:36

so these are questions to ponder about

play27:37

for future uh these are not for

play27:40

evaluation as such

play27:41

ah

play27:43

okay so this ends the session for today

play27:46

uh

play27:47

the original paper for

play27:49

for

play27:50

double research was given by love grover

play27:52

in 96

play27:54

here you can read about it more from the

play27:56

elson and charles book also and the

play27:58

implementation you can also see from

play28:00

discrete text

play28:02

okay

Rate This

5.0 / 5 (0 votes)

関連タグ
Quantum ComputingGrover's AlgorithmQiskitOracle FunctionSuperposition StatePhase OracleQuantum CircuitMulti-Controlled ToffoliState VectorQuantum Simulation
英語で要約が必要ですか?