This algorithm can search the desired choice among unorganized database.
I'm going to talk about the case that I want to choose .
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.
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]
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])
This is the result on the QASM simulator.
And this is the result on the real device(ibmq_20_tokyo).
You can find the cases to maximize not only ,but also , and . So,feel free to try them on qiskit.