I am working on a Grover’s algorithm in Qiskit with two oracles. Each oracle marks a different state as “good”. I want to add a few more oracles to mark specific states as “bad”, in order to decrease their probability relative to other states. Could someone advise on how to write such oracles..? Thank you
Here’s my current implementation:
from qiskit import QuantumCircuit, transpile, execute
from qiskit_aer import AerSimulator
import numpy as np
def oracle127(qc):
n = 10
qc.x([0, 1, 2])
qc.mcx([i for i in range(n)], n)
qc.x([0, 1, 2])
return qc
def oracle896(qc):
n = 10
qc.x([3, 4, 5, 6, 7, 8, 9])
qc.mcx([i for i in range(n)], n)
qc.x([3, 4, 5, 6, 7, 8, 9])
return qc
def main():
n = 10 # Number of qubits
qc = QuantumCircuit(n + 1, n)
qc.x(n)
qc.h(range(n + 1))
k = 2 # Number of oracles
r = int(np.pi * np.sqrt(2**n/k) / 4) # Number of iterations
for _ in range(r):
qc = oracle127(qc) # Apply first oracle
qc = oracle896(qc) # Apply second oracle
# Diffusion operation
qc.h(range(n))
qc.x(range(n))
qc.h(n - 1)
qc.mcx([i for i in range(n-1)], n - 1)
qc.h(n - 1)
qc.x(range(n))
qc.h(range(n))
qc.measure(range(n), range(n))
backend = AerSimulator()
compiled_circuit = transpile(qc, backend)
job = backend.run(compiled_circuit, shots=1000)
counts = job.result().get_counts()
print(counts)
main()