# Defining an oracle with Qiskit

I have a 8-qubits circuit whose final vector state may be for instance: $$\frac{\sqrt{2}}{4} |00010101\rangle+\frac{\sqrt{2}}{4} |00101010\rangle+\frac{\sqrt{2}}{4} |01010110\rangle+\frac{\sqrt{2}}{4} |01101001\rangle+\frac{\sqrt{2}}{4} |10011010\rangle+\frac{\sqrt{2}}{4} |10100101\rangle+\frac{\sqrt{2}}{4} |11011001\rangle+\frac{\sqrt{2}}{4} |11100110\rangle$$ I'd like to apply the Grover search to amplify the states that are 11...... (the two last ones here)

I guess something like

problem = AmplificationProblem(oracle,state_preparation=circ, is_good_state=[6,7])
from qiskit.algorithms import Grover
from qiskit.primitives import Sampler

grover = Grover(sampler=Sampler())
result = grover.amplify(problem)

should work, but I don't know how to write the oracle. Any help would be welcome.

If I'm understanding correctly, you want an oracle that detects when the first two qubits are set to $$|1\rangle$$. You could do this using a Toffoli gate and an ancilla qubit as follows.

import qiskit

data    = qiskit.QuantumRegister(8, "d")
ancilla = qiskit.QuantumRegister(1, "anc")
qc      = qiskit.QuantumCircuit(data, ancilla)

qc.toffoli(data[0], data[1], ancilla[0])

d_0: ──■──
│
d_1: ──■──
│
d_2: ──┼──
│
d_3: ──┼──
│
d_4: ──┼──
│
d_5: ──┼──
│
d_6: ──┼──
│
d_7: ──┼──
┌─┴─┐
anc: ┤ X ├
└───┘

So the ancilla qubit detects when q_0 and q_1 are both $$|1\rangle$$. Then, you can use this oracle in Grover's.

• Thanks for your answer (and sorry to reply so late: I was in a "no Internet area" for a few days). I indeed finally used a mcx gate. Jun 13, 2023 at 8:21