Skip to content

Commit

Permalink
Fixes according to PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
gadial committed Feb 16, 2025
1 parent ad5cdd0 commit b4878c6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 24 deletions.
40 changes: 21 additions & 19 deletions qiskit/circuit/library/phase_oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,33 +66,35 @@ def __init__(
# ignore deprecation warning for BooleanExpression; implementation is changing in 2.0
import warnings

warnings.simplefilter("ignore", DeprecationWarning)
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
from qiskit.circuit.classicalfunction.boolean_expression import BooleanExpression
from qiskit.circuit.classicalfunction.classical_element import ClassicalElement

from qiskit.circuit.classicalfunction.boolean_expression import BooleanExpression
from qiskit.circuit.classicalfunction.classical_element import ClassicalElement

if not isinstance(expression, ClassicalElement):
expression = BooleanExpression(expression, var_order=var_order)
if not isinstance(expression, ClassicalElement):
expression = BooleanExpression(expression, var_order=var_order)

self.boolean_expression = expression
self.boolean_expression = expression

if synthesizer is None:
if synthesizer is None:

def synthesizer(boolean_expression):
from tweedledum.synthesis import pkrm_synth # pylint: disable=import-error
from qiskit.circuit.classicalfunction.utils import tweedledum2qiskit
def synthesizer(boolean_expression):
from tweedledum.synthesis import pkrm_synth # pylint: disable=import-error
from qiskit.circuit.classicalfunction.utils import tweedledum2qiskit

truth_table = boolean_expression._tweedledum_bool_expression.truth_table(
output_bit=0
)
tweedledum_circuit = pkrm_synth(truth_table, {"pkrm_synth": {"phase_esop": True}})
return tweedledum2qiskit(tweedledum_circuit)
truth_table = boolean_expression._tweedledum_bool_expression.truth_table(
output_bit=0
)
tweedledum_circuit = pkrm_synth(
truth_table, {"pkrm_synth": {"phase_esop": True}}
)
return tweedledum2qiskit(tweedledum_circuit)

oracle = expression.synth(synthesizer=synthesizer)
oracle = expression.synth(synthesizer=synthesizer)

super().__init__(oracle.num_qubits, name="Phase Oracle")
super().__init__(oracle.num_qubits, name="Phase Oracle")

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

def evaluate_bitstring(self, bitstring: str) -> bool:
"""Evaluate the oracle on a bitstring.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,33 @@ deprecations_circuits:
.. code-block:: python
phase_flip_oracle = PhaseOracle(bool_expr)
controlled_phase_flip_oracle = phase_flip_oracle.control(1)
qc.h(qr_y)
qc.compose(controlled_phase_flip_oracle, qubits=[*qr_y, *qr_x], inplace=True)
qc.h(qr_y)
from qiskit import QuantumRegister, QuantumCircuit
from qiskit.circuit.library.phase_oracle import PhaseOracle
bool_expr = "(x0 & x1 | ~x2) & x4"
qr_x = QuantumRegister(4, "x")
qr_y = QuantumRegister(1, "y")
bit_flip_oracle = QuantumCircuit(qr_x, qr_y)
phase_flip_oracle = PhaseOracle(bool_expr)
controlled_phase_flip_oracle = phase_flip_oracle.control(1)
bit_flip_oracle.h(qr_y)
bit_flip_oracle.compose(controlled_phase_flip_oracle, qubits=[*qr_y, *qr_x], inplace=True)
bit_flip_oracle.h(qr_y)
print(bit_flip_oracle)
Which results in
.. code-block:: text
┌───────────────┐
x_0: ─────┤0 ├─────
│ │
x_1: ─────┤1 ├─────
│ Phase Oracle │
x_2: ─────┤2 ├─────
│ │
x_3: ─────┤3 ├─────
┌───┐└───────┬───────┘┌───┐
y: ┤ H ├────────■────────┤ H ├
└───┘ └───┘

0 comments on commit b4878c6

Please sign in to comment.