# Qiskit implementation of Oracle for Grover's Algorithm is Incorrect?

def oracle(qc, binary_strings):
for string in binary_strings:
if string[-1] == '1':
for i, bit in enumerate(string[:-1]):
qc.x(i)
qc.z(i)
if bit == '0':
qc.x(i)


Working on an oracle that flips the amplitude of an entire binary string if last bit = '1'. This code should theoretically work (imo). Can someone explain if my code here is correct or incorrect? If it is incorrect is it due to the order of x and z gates? Thanks!

• I’m voting to close this question because this question is asking about ChatGPT's interpretation of a snippet of code but it's not clear if the OP is asking if ChatGPT's interpretation is correct, or if the code is correct and ChatGPT is wrong, or what in particular is problematic with the snippet of code. Commented Jul 31 at 18:51
• Thank you for your response @MarkSpinelli. I edited and cleared up some confusion in the question. Commented Aug 1 at 16:30

What you want is a python function that takes as an input an arbitrary list of elements, and generates an oracle that inverts the phase of only the elements in that list for which the least-significant qubit is $$1$$:

from qiskit.circuit.library import ZGate

def oracle(qc, binary_strings):
n = len(binary_strings[0])

for string in binary_strings:
reverse_string = string[::-1]
if reverse_string[0] == '1':
MCZGate = ZGate().control(n-1, ctrl_state=reverse_string[1:])
qc.append(MCZGate,range(n-1,-1,-1))


As an example, take the following list, for which only the first (001) and third (101) elements should be marked:

from qiskit import QuantumCircuit
from qiskit.quantum_info import Operator

binary_strings = ['001','010','101','110']

qc = QuantumCircuit(n)
oracle(qc, binary_strings)
Operator(qc)


This code generates the following output, which as expected, is a unitary that flips the phase of states $$|001\rangle$$ and $$|101\rangle$$:

$$\begin{bmatrix} 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & -1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & -1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\ \end{bmatrix}$$

• I am trying to invert the phase of the entire string when the LSB = '1'. Note that my qubits are all starting in a 0 phase which is why I apply the first X gate. Commented Aug 1 at 17:39
• The way to posed the question implies that what you want to invert the phase of any state for which the least significant qubit is $1$. If this is not what you want, please clarify exactly what is it that you are trying to accomplish and I will edit my answer. Commented Aug 1 at 17:45
• Sorry for the confusion. Say for example the binary string is 010011. I want this entire string to be marked using the oracle because the last bit = 1, indicating the solution. Does this help clarify? Commented Aug 1 at 17:49
• It sounds like what you need is not an oracle that marks any element for which its LSB is $1$, but rather a python function that implements an oracle that marks an element iff its LSB is $1$, is that correct? If so, please edit your question to reflect that. Also, what is this function supposed to do if the LSB is $0$? not create the oracle? Commented Aug 1 at 17:58
• I have an array of bits named binary_strings in which at least one row has an LSB = 1. There are about 5000 rows so I am using an oracle to mark the first solution state or solution string. What is the difference between writing an oracle or using a python function to call an oracle? Commented Aug 1 at 18:13

In oracle, we want to output f(x) = 1 if an LSB of input x is 1. As a result the phase of input x is inverted by phase kickback.

Let's assume we have 2 qubits for input x for simplicity. Then the code of oracle could be :

def oracle(circ, data_qubits, clause_qubits, output_qubit):
circ.cx(data_qubits[0], clause_qubits[0])

# f(x) = 1 if q0(LSB) is 1
circ.cx(clause_qubits[0], output_qubit)

circ.cx(data_qubits[0], clause_qubits[0])


The whole circuit would look like this :

Please note that in this particular case the number of solutions is half the entire search space, we would end up getting a uniform distribution by measurement.