# Implementation of the oracle of Grover's algorithm on IBM Q using three qubits

I am trying to get used to IBM Q by implementing three qubits Grover's algorithm but having difficulty to implement the oracle.

Could you show how to do that or suggest some good resources to get used to IBM Q circuit programming?

What I want to do is to mark one arbitrary state by flipping its sign as the oracle supposed to do.

For example, I have

$1/\sqrt8(|000\rangle+|001\rangle+|010\rangle+|011\rangle+|100\rangle+|101\rangle+|110\rangle+|111\rangle)$.

and I want to mark $|111\rangle$ by flipping its sign to $-|111\rangle$. I somehow understand that CCZ gate would solve the problem but we do not have CCZ gate in IBM Q. The combination of some gates will act the same as CCZ but I am not sure how to do that yet. And I am also struggling for the other cases not only for $|111\rangle$.

Two qubits case is simple enough for me to implement, but three qubits care is still confusing to me.

• Their documentation includes some examples of Grover's algorithm, although I do not remember how big the search space was. – Norrius Jun 1 '18 at 0:33
• Thanks for the comment. Yes, this page (quantumexperience.ng.bluemix.net/proxy/tutorial/full-user-guide/…) explains two qubits Grover's algorithm implementation. – Bick Jun 1 '18 at 14:12

I am answering my question. After some google search, I found this image showing CCZ gate by CNOT, T dagger, and T gate. I tried this on IBM Q and it worked. I want to explore why it works but that's another story.

For someone who is interested, here is my quantum circuit of Grover's algorithm finding |111> with one iteration.

• You want to look up standard circuits for the controlled-controlled-NOT gate (also called Toffoli). It's Fig. 4.9 in my version of Nielsen & Chuang. To convert into a controlled-controlled-phase gate, just stick a Hadamard on the target qubit both before and after (or, if there are Hadamards already at either end, just remove them). – DaftWullie Jun 1 '18 at 15:30

I implemented the same problem for multiple qubits using qiskit here.

For the 3 qubit state you presented, you can use an oracle like the one here (I'm using quirk just to show the amplitudes in real time). Note that the first three Hadamards (the ones before the ...) are there only to simulate a random input to the oracle and are not part of the oracle itself. In every case, as you can see from the amplitudes at the end of the circuit, only the $$|111\rangle$$ state gets flipped out, while all the other states remain unchanged.

In general, the idea is to simulate a CCZ gate using Hadamard on target bit following by a CCX gate and then another Hadamard on the target bit.