Giter VIP home page Giter VIP logo

grover-s-algorithm's Introduction

Grover's algorithm

The problem this algorithm is trying to aim.

This algorithm can search the desired choice among unorganized database.

Process to achive the goal.

I'm going to talk about the case that I want to choose .

1.Create the superposition.

The first thing you have to do is to put hadamard gates on all the qubits. In this repository, I prepared 2 qubits. So, I got 4 choices. screen shot 2019-01-06 at 11 30 44 pm

2. Put the phase oracle,which flips the phase of your desired choice.

After you create the superposition, you have to put something called the phase oracle.
The following codes is those of the phase oracle for .

def oracle0(qci,n,q1,q2):

    for i in range(n):    
    
      qci.s(q[i])     
      
    qci.cz(q[q1],q[q2]) 
    
    for i in range(n):  
    
        qci.s(q[i])     
         

In order to make sure that the phase of , you have to write the following code after you measure the quantum states.

backend = BasicAer.get_backend('statevector_simulator')  

job = execute(qc,backend)  

result = job.result()  

outputstate = result.get_statevector(qc, decimals=3)  

print(outputstate)  

The outcome would be [ -0.5+0.j 0.5+0.j 0.5+0.j 0.5+0.j]

3. Subtract probability of the phase from their average, and add it to the average again.

First, the probability of getting each states are 50% and the one for the desired choice is just flipped, which is -50%.

Then, I substract it from the average of all the probability((50+50+50-50)/4 = 25%), so the outcome would be 75%.
After that, I should add that value to the average again, which would end up to be 100%.

You can realize this by putting the following codes.

#bn is the number of qubits, which is 2 in this case.   
for j in range(bn):   
    qc.h(q[j])   
    qc.x(q[j])  
oracle0(qc,2,0,1)  
for k in range(bn):  
    qc.x(q[k])  
    qc.h(q[k])  

4. Measure the quantum states.

This is the result on the QASM simulator.
grover_00_sim

And this is the result on the real device(ibmq_20_tokyo).
grover_00_tokyo

You can find the cases to maximize not only ,but also , and . So,feel free to try them on qiskit.

grover-s-algorithm's People

Contributors

makotonakai avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.