I'm utilizing Grover's algorithm to solve a straightforward problem. It presents eight options, but only four are correct (the 1's in the S column)
$$\begin{array}{c|c|c|c} q_0& q_1\ & q_2\ &S \\ \hline 0 & 0 & 0 &1 \\ \hline 0 & 0 & 1 &1 \\ \hline 0 & 1 & 0 &1 \\ \hline 0 & 1 & 1 &0 \\ \hline 1 & 0 & 0 &0 \\ \hline 1 & 0 & 1 &0 \\ \hline 1 & 1 & 0 &0 \\ \hline 1 & 1 & 1 &1 \\ \hline \end{array}$$
Why can't Grover's algorithm apparently solve this simple situation?
The oracle produce the state (the order is $|q_2 q_1 q_o \rangle$):
$-\frac{\sqrt{2}}{4} |000\rangle+\frac{\sqrt{2}}{4} |001\rangle- \frac{\sqrt{2}}{4} |010\rangle+\frac{\sqrt{2}}{4} |011\rangle- \frac{\sqrt{2}}{4} |100\rangle+\frac{\sqrt{2}}{4} |101\rangle+\frac{\sqrt{2}}{4} |110\rangle- \frac{\sqrt{2}}{4} |111\rangle$
where the solutions clearly are $|000 \rangle$ , $|010 \rangle$ , $|100 \rangle$ and $|111 \rangle$, but the code did not find any.
Here is the code
arquivo='002.dimac' with open(arquivo, 'r') as f: dimacs = f.read() print(dimacs) # let's check the file is as promised # steps 2 & 3 of Grover's algorithm from qiskit import QuantumCircuit from qiskit.circuit.library import GroverOperator from qiskit.circuit.library import PhaseOracle oracle = PhaseOracle.from_dimacs_file(arquivo) grover_operator = GroverOperator(oracle) qc=QuantumCircuit(3) qc.h([0,1,2]) qc = qc.compose(grover_operator) qc.measure_all() qc.draw() # Simulate the circuit from qiskit import Aer, transpile sim = Aer.get_backend('aer_simulator') t_qc = transpile(qc, sim) counts = sim.run(t_qc,shots=1024).result().get_counts() print(counts) Output:
c example DIMACS-CNF 3-SAT p cnf 3 4 -1 2 3 0 1 -2 -3 0 -1 -2 3 0 -1 2 -3 0 ┌───┐┌────┐ ░ ┌─┐ q_0: ┤ H ├┤0 ├─░─┤M├────── ├───┤│ │ ░ └╥┘┌─┐ q_1: ┤ H ├┤1 Q ├─░──╫─┤M├─── ├───┤│ │ ░ ║ └╥┘┌─┐ q_2: ┤ H ├┤2 ├─░──╫──╫─┤M├ └───┘└────┘ ░ ║ ║ └╥┘ meas: 3/═══════════════╩══╩══╩═ 0 1 2 {'001': 122, '110': 119, '101': 125, '100': 110, '111': 138, '011': 139, '010': 126, '000': 145} ```