Side Note: If you receive a Code 400 error, then change the number of shots. This has worked without fail for me.
Now, implementing the 3-qubit algorithm on IBM Q turned out to be the most difficult part yet — with the exception of downloading QISKit and Python of course. The simulations all ran so well with a 0.90 or greater probability of the algorithm getting the right answer.
But, when I ran it on IBM Q:
I might as well just have used a random number generator.
I posted the issue to GitHub in hopes of finding a solution to the seemingly unsolvable issue. I got no solution, but I did get an explanation. In IBM Q, the Toffoli gates are implemented using a series of six CX gates and many single-qubit gates. These 2-qubit CX gates introduce a decent amount of error into the circuit and considering that two full iterations of Grover’s algorithm used four Toffoli gates, it’s clear why the data looked so much like random noise. You can read the full explanation here. Keep in mind, though, their Toffoli gate deconstruction is actually incorrect, the correct one is:
Anyways, so I needed a better Toffoli gate using less 2-qubit gates and Maziar proposed a solution with three controlled-unitary phase shift gates and two CNOT gates which seems like it’s better, but alas, the cU gate is implemented through two 2-qubit gates.
To mitigate this issue, I changed the number of iterations to one because two iterations introduced too many errors. Furthermore, I ran the circuit on IBM Q 5 Tenerife, which the good Samaritan on GitHub suggested. Turns out, the lower error rates on IBM Q 5 Tenerife are extremely helpful. I ended up with the results:
After seeing it worked, I tried the 2 iteration again, but this time on IBM Q 5. It returned worse results than just 1 iteration but still much better than the results on IBM Q 16.