Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions qiskit/circuit/library/phase_oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
9 changes: 9 additions & 0 deletions test/python/algorithms/test_grover.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()