diff --git a/qiskit/circuit/library/phase_oracle.py b/qiskit/circuit/library/phase_oracle.py index be3301c117e1..bd1033fe55ab 100644 --- a/qiskit/circuit/library/phase_oracle.py +++ b/qiskit/circuit/library/phase_oracle.py @@ -79,12 +79,15 @@ def synthesizer(boolean_expression): def evaluate_bitstring(self, bitstring: str) -> bool: """Evaluate the oracle on a bitstring. This evaluation is done classically without any quantum circuit. + Args: - bitstring: The bitstring for which to evaluate. + bitstring: The bitstring for which to evaluate. The input bitstring is expected to be + in little-endian order. + Returns: True if the bitstring is a good state, False otherwise. """ - return self.boolean_expression.simulate(bitstring) + return self.boolean_expression.simulate(bitstring[::-1]) @classmethod def from_dimacs_file(cls, filename: str): diff --git a/releasenotes/notes/fix_phaseoracle_evaluate-52be05221d2761aa.yaml b/releasenotes/notes/fix_phaseoracle_evaluate-52be05221d2761aa.yaml new file mode 100644 index 000000000000..6b89c0fb7f8c --- /dev/null +++ b/releasenotes/notes/fix_phaseoracle_evaluate-52be05221d2761aa.yaml @@ -0,0 +1,8 @@ +--- +fixes: + - | + Fix a bug where the bit-order in + :meth:`qiskit.circuit.library.PhaseOracle.evaluate_bitstring` did not agree with the + order of the measured bitstring. This fix also affects the execution of Grover's algorithm + if the oracle is specified as :class:`~qiskit.circuit.library.PhaseOracle`, which now + correctly identifies the correct bitstring. diff --git a/test/python/algorithms/test_grover.py b/test/python/algorithms/test_grover.py index c45f7bc5e118..54c1f539dbfb 100644 --- a/test/python/algorithms/test_grover.py +++ b/test/python/algorithms/test_grover.py @@ -243,6 +243,15 @@ def test_max_probability(self): result = grover.amplify(problem) self.assertEqual(result.max_probability, 1.0) + def test_oracle_evaluation(self): + """Test oracle_evaluation for PhaseOracle""" + oracle = PhaseOracle("x1 & x2 & (not x3)") + problem = AmplificationProblem(oracle, is_good_state=oracle.evaluate_bitstring) + grover = Grover(quantum_instance=self.qasm) + result = grover.amplify(problem) + self.assertTrue(result.oracle_evaluation) + self.assertEqual("011", result.top_measurement) + if __name__ == "__main__": unittest.main()