Tuesday, February 21, 2023

Quantum Computing -- Qiskit Terra - Unexpected probabilities (due to misinterpretation of the result)

These days, I'm trying to use the things I learned from physics in the field of computer science.  That is, I'm using IBM Quantum Computing Lab and Composer to run Python codes utilizing the Quantum Computing algorithms and I do it on real Quantum Computers (and Quantum Simulators). It is very exciting and I find it (I mean Quantum Computing) very enjoyable, but I must admit that it is difficult at first, -- after all it is different than classical computing. I mean, we still have those operators, those quantum gates (somewhat similar with the classical logic gates), but we exploit the power of the quantum computing by using those two phenomena called superposition and entanglement, and these are a little bit confusing.. It is an interesting journey, made me go back to the Linear algebra, Matrices, Tensor Products, Logic gates and stuff like that.

Well.. I will go a little quick and concise in this one. So, we have QBITs, and we do the measurements at the right place to get our results.(measuring collapses the wave function (according to the Copenhagen Interpretation of Quantum Mechanics) and make us lose those quantum states -- that's why it is important) . Actually I have lots to say about this subject but this will be the subject of a future blog post, so let's concentrate on this particular issue that I 've faced while using Qiskit Terra. (Note that-- when I try a similar code with similar logical expressions with Qiskit Aqua(which is deprecated), the expected outputs are produced). 

As you may already sensed, actually it wasn't an issue. I mean the thing I identified as an issue is actually by design.. So, I will just share some information, something I learned on the way running Grover's Algorithm. In short; I was doing some quantum computing gymnastics, and I saw (thought) that some outputs are not aligned with the truth tables.

Lets see;

##Importing what I need 

from qiskit import *
from qiskit.tools.visualization import plot_histogram
from qiskit.circuit.library import PhaseOracle
from qiskit.algorithms import Grover, AmplificationProblem
from qiskit.visualization import plot_histogram

##Creating my Oracle (this has nothing to do with Oracle Corporation  :) Just check Grover's algorithm to understand the concept..

oracle = PhaseOracle('((A & C) | (B & D)) & ~(C & D)')
problem = AmplificationProblem(oracle=oracle, is_good_state=oracle.evaluate_bitstring)
backend = Aer.get_backend('qasm_simulator')
grover = Grover(quantum_instance=backend)
result = grover.amplify(problem)

##Printing the expected result in a chart and get the result as text (as a list actually) as well.

print(result.circuit_results[0])
{'1101': 251, '0111': 274, '0011': 231, '1100': 268}

plot_histogram(result.circuit_results[0])


Well.. As you may understand from the code above, I was trying to find the correct results ( aligned with the expected truth table) for my logical expression ((A & C) | (B & D)) & ~(C & D)') and I used Grover for doing that.

Qiskit Aqua's LogicalExpressionOracle, sorts the variables alphabetically, so an expected output which is A & B& D should have been there in the outputs 1011, but it was not there. This is the issue (not an issue actually) that I mentioned in the first paragraph.. I must admit that I spent lots of time for this and finally learned that, Qiskit Terra sorts the variables by the order of their appearance! 

So in this case, the order is A then C then B and finally D. So, for instance we should see 1101 as an expected result (A & B & D) and, as you see above, it is there. So no problem with Terra. I was the one who had a lack of information :)
AC is also a correct result , and it corresponds to 0011, and it is already seen in the results.. 

The truth table with a format of ABCD (alphabetically sorted from left to right) can be converted to Terra's style truth table like the following;

ABCD     DBCA
0101 - >   1100 *
1010 - >   0011 *
1101 - >   1101
1110 - >   0111

As you see, it is completely consistent with the output generated with Qiskit.
Just wanted to share this with you.. Maybe it will be of use to someone.

No comments :

Post a Comment

If you will ask a question, please don't comment here..

For your questions, please create an issue into my forum.

Forum Link: http://ermanarslan.blogspot.com.tr/p/forum.html

Register and create an issue in the related category.
I will support you from there.