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


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.

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

3 Answers 3


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.

Grover's algorithm finding |111> with one iteration

  • 2
    $\begingroup$ 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). $\endgroup$
    – DaftWullie
    Commented Jun 1, 2018 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.


Thanks to @DaftWullie's comment. Here is my implementation of Grover's search for 110 on the IBM quantum platform Grover's search for 110 with outcome Outcome for Grover's search for 110

As DaftWullie said, the H gates around the decomposition of CCZ can be deleted for both ends, which brings us Grover's search for 110 With Less H Gates with outcome Outcome for Grover's search for 110 With Less H Gates

For the decomposition of H and Toffoli gates, you can always find a solution over the network.

Happy Quantum! :)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.