Skip to content

Commit

Permalink
Change BitFlipOracle to BitFlipOracleGate
Browse files Browse the repository at this point in the history
  • Loading branch information
gadial committed Feb 20, 2025
1 parent d94f490 commit df94414
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 53 deletions.
4 changes: 2 additions & 2 deletions qiskit/circuit/library/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@
QuantumVolume
PhaseEstimation
GroverOperator
BitFlipOracle
BitFlipOracleGate
PhaseOracle
PauliEvolutionGate
HamiltonianGate
Expand Down Expand Up @@ -672,6 +672,6 @@
from .phase_estimation import PhaseEstimation, phase_estimation
from .grover_operator import GroverOperator, grover_operator
from .phase_oracle import PhaseOracle
from .bit_flip_oracle import BitFlipOracle
from .bit_flip_oracle import BitFlipOracleGate
from .overlap import UnitaryOverlap, unitary_overlap
from .standard_gates import get_standard_gate_name_mapping
34 changes: 13 additions & 21 deletions qiskit/circuit/library/bit_flip_oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

from __future__ import annotations

from qiskit.circuit import QuantumCircuit
from qiskit.circuit import Gate

from qiskit.synthesis.boolean.boolean_expression import BooleanExpression


class BitFlipOracle(QuantumCircuit):
class BitFlipOracleGate(Gate):
r"""Bit-flip Oracle.
The Bit-flip Oracle object constructs circuits for any arbitrary
Expand All @@ -43,7 +43,8 @@ class BitFlipOracle(QuantumCircuit):
which is the standard format for specifying SATisfiability (SAT) problem instances in
`Conjunctive Normal Form (CNF) <https://en.wikipedia.org/wiki/Conjunctive_normal_form>`__,
which is a conjunction of one or more clauses, where a clause is a disjunction of one
or more literals. See :meth:`qiskit.circuit.library.bit_flip_oracle.BitFlipOracle.from_dimacs_file`.
or more literals.
See :meth:`qiskit.circuit.library.bit_flip_oracle.BitFlipOracleGate.from_dimacs_file`.
From 16 variables on, possible performance issues should be expected when using the
default synthesizer.
Expand All @@ -61,30 +62,21 @@ def __init__(
(default: by appearance)
"""
self.boolean_expression = BooleanExpression(expression, var_order=var_order)
oracle = self.boolean_expression.synth(circuit_type="bit")
self.oracle = self.boolean_expression.synth(circuit_type="bit")

super().__init__(oracle.num_qubits, name="Bit-flip Oracle")
super().__init__(name="Bit-flip Oracle", num_qubits=self.oracle.num_qubits, params=[])

self.compose(oracle, inplace=True, copy=False)

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. The input bitstring is expected to be
in little-endian order.
Returns:
True if the bitstring is a good state, False otherwise.
def _define(self):
"""
Defined by the synthesized bit-flip oracle
"""
return self.boolean_expression.simulate(bitstring[::-1])
self.definition = self.oracle

@classmethod
def from_dimacs_file(cls, filename: str):
r"""Create a BitFlipOracle from the string in the DIMACS format.
r"""Create a BitFlipOracleGate from the string in the DIMACS format.
It is possible to build a BitFlipOracle from a file in `DIMACS CNF format
It is possible to build a BitFlipOracleGate from a file in `DIMACS CNF format
<https://web.archive.org/web/20190325181937/https://www.satcompetition.org/2009/format-benchmarks2009.html>`__,
which is the standard format for specifying SATisfiability (SAT) problem instances in
`Conjunctive Normal Form (CNF) <https://en.wikipedia.org/wiki/Conjunctive_normal_form>`__,
Expand Down Expand Up @@ -123,7 +115,7 @@ def from_dimacs_file(cls, filename: str):
filename: A file in DIMACS format.
Returns:
BitFlipOracle: A quantum circuit with a bit-flip oracle.
BitFlipOracleGate: A quantum gate with a bit-flip oracle.
"""
expr = BooleanExpression.from_dimacs_file(filename)
return cls(expr)
19 changes: 6 additions & 13 deletions releasenotes/notes/boolean_expression_update-39c19edbbe71ba0d.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ upgrade_circuits:
- |
The :class:`.PhaseOracle` no longer relies on the `tweedledum` library
but might not be synthesized as effectively as before.
:class:`.BitFlipOracle` was added as an alternative to
:class:`.BitFlipOracleGate` was added as an alternative to
directly synthesizing :class:`.BooleanExpression`, as this
class is removed in Qiskit 2.0.
Expand Down Expand Up @@ -31,17 +31,18 @@ upgrade_circuits:
features_circuits:
- |
The new :class:`.PhaseBitFlipOracle` has the same interface as
:class:`.PhaseOracle`, but synthesizes a bit flip oracle instead
The new :class:`.BitFlipOracleGate` has the same interface as
:class:`.PhaseOracle` (except the `evaluate_bitstring` method),
but synthesizes a bit flip oracle instead
of a phase flip oracle, meaning it acts on one additional qubit
and can be seen a applying a controlled X operation, where the
control is the value of the expression encoded by the oracle.
.. code-block:: python
from qiskit.circuit.library.bit_flip_oracle import BitFlipOracle
from qiskit.circuit.library.bit_flip_oracle import BitFlipOracleGate
bool_expr = "(x0 & x1 | ~x2) & x4"
oracle = BitFlipOracle(bool_expr)
oracle = BitFlipOracleGate(bool_expr)
print(oracle)
.. code-block:: text
Expand All @@ -56,11 +57,3 @@ features_circuits:
┌─┴─┐┌─┴─┐┌─┴─┐
q_4: ┤ X ├┤ X ├┤ X ├
└───┘└───┘└───┘
.. code-block:: python
print(oracle.evaluate_bitstring("1010"))
.. code-block:: text
True
21 changes: 4 additions & 17 deletions test/python/circuit/library/test_phase_and_bitflip_oracles.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from numpy import sqrt, isclose

from qiskit.circuit import QuantumCircuit
from qiskit.circuit.library import PhaseOracle, BitFlipOracle
from qiskit.circuit.library import PhaseOracle, BitFlipOracleGate
from qiskit.quantum_info import Statevector
from test import QiskitTestCase # pylint: disable=wrong-import-order

Expand Down Expand Up @@ -98,22 +98,9 @@ def test_variable_order(self, expression, var_order, good_states):


@ddt
class TestBitFlipOracle(QiskitTestCase):
class TestBitFlipOracleGate(QiskitTestCase):
"""Test bit-flip oracle object."""

@data(
("x | x", "1", True),
("x & x", "0", False),
("(x0 & x1 | ~x2) ^ x4", "0110", False),
("xx & xxx | ( ~z ^ zz)", "0111", True),
)
@unpack
def test_evaluate_bitstring(self, expression, input_bitstring, expected):
"""BitFlipOracle(...).evaluate_bitstring"""
oracle = BitFlipOracle(expression)
result = oracle.evaluate_bitstring(input_bitstring)
self.assertEqual(result, expected)

@data(
("x | x", "01"),
("~x", "10"),
Expand All @@ -125,7 +112,7 @@ def test_evaluate_bitstring(self, expression, input_bitstring, expected):
@unpack
def test_statevector(self, expression, truth_table):
"""Circuit generation"""
oracle = BitFlipOracle(expression)
oracle = BitFlipOracleGate(expression)
num_qubits = oracle.num_qubits
circuit = QuantumCircuit(num_qubits)
circuit.h(
Expand Down Expand Up @@ -155,7 +142,7 @@ def test_statevector(self, expression, truth_table):
@unpack
def test_variable_order(self, expression, var_order, truth_table):
"""Circuit generation"""
oracle = BitFlipOracle(expression, var_order=var_order)
oracle = BitFlipOracleGate(expression, var_order=var_order)
num_qubits = oracle.num_qubits
circuit = QuantumCircuit(num_qubits)
circuit.h(
Expand Down

0 comments on commit df94414

Please sign in to comment.