-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring; new boolean expression code moved to synthesis, old code…
… restored.
- Loading branch information
Showing
15 changed files
with
620 additions
and
244 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
# This code is part of Qiskit. | ||
# | ||
# (C) Copyright IBM 2025. | ||
# | ||
# This code is licensed under the Apache License, Version 2.0. You may | ||
# obtain a copy of this license in the LICENSE.txt file in the root directory | ||
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
# | ||
# Any modifications or derivative works of this code must retain this | ||
# copyright notice, and modified files need to carry a notice indicating | ||
# that they have been altered from the originals. | ||
|
||
"""Bit-flip Oracle object.""" | ||
|
||
from qiskit.circuit import QuantumCircuit | ||
|
||
from qiskit.synthesis.boolean.boolean_expression import BooleanExpression | ||
|
||
|
||
class BitFlipOracle(QuantumCircuit): | ||
r"""Bit-flip Oracle. | ||
The Bit-flip Oracle object constructs circuits for any arbitrary | ||
input logical expressions. A logical expression is composed of logical operators | ||
`&` (`AND`), `|` (`OR`), `~` (`NOT`), and `^` (`XOR`). | ||
as well as symbols for literals (variables). | ||
For example, `'a & b'`, and `(v0 | ~v1) & (~v2 & v3)` | ||
are both valid string representation of boolean logical expressions. | ||
A bit-flip oracle for a boolean function `f(x)` performs the following | ||
quantum operation: | ||
.. math:: | ||
|x\rangle|y\rangle \mapsto |x\rangle|f(x)\oplus y\rangle | ||
For convenience, this oracle, in addition to parsing arbitrary logical expressions, | ||
also supports input strings in the `DIMACS CNF format | ||
<http://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>`__, | ||
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`. | ||
From 16 variables on, possible performance issues should be expected when using the | ||
default synthesizer. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
expression: str, | ||
var_order: list = None, | ||
) -> None: | ||
"""Creates a BitFlipOracle object | ||
Args: | ||
expression: A Python-like boolean expression. | ||
var_order(list): A list with the order in which variables will be created. | ||
(default: by appearance) | ||
""" | ||
self.boolean_expression = BooleanExpression(expression, var_order=var_order) | ||
oracle = self.boolean_expression.synth(circuit_type="bit") | ||
|
||
super().__init__(oracle.num_qubits, name="Bit-flip Oracle") | ||
|
||
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. | ||
""" | ||
return self.boolean_expression.simulate(bitstring[::-1]) | ||
|
||
@classmethod | ||
def from_dimacs_file(cls, filename: str): | ||
r"""Create a BitFlipOracle from the string in the DIMACS format. | ||
It is possible to build a BitFlipOracle from a file in `DIMACS CNF format | ||
<http://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>`__, | ||
which is a conjunction of one or more clauses, where a clause is a disjunction of one | ||
or more literals. | ||
The following is an example of a CNF expressed in the DIMACS format: | ||
.. code:: text | ||
c DIMACS CNF file with 3 satisfying assignments: 1 -2 3, -1 -2 -3, 1 2 -3. | ||
p cnf 3 5 | ||
-1 -2 -3 0 | ||
1 -2 3 0 | ||
1 2 -3 0 | ||
1 -2 -3 0 | ||
-1 2 3 0 | ||
The first line, following the `c` character, is a comment. The second line specifies that | ||
the CNF is over three boolean variables --- let us call them :math:`x_1, x_2, x_3`, and | ||
contains five clauses. The five clauses, listed afterwards, are implicitly joined by the | ||
logical `AND` operator, :math:`\land`, while the variables in each clause, represented by | ||
their indices, are implicitly disjoined by the logical `OR` operator, :math:`lor`. The | ||
:math:`-` symbol preceding a boolean variable index corresponds to the logical `NOT` | ||
operator, :math:`lnot`. Character `0` (zero) marks the end of each clause. Essentially, | ||
the code above corresponds to the following CNF: | ||
:math:`(\lnot x_1 \lor \lnot x_2 \lor \lnot x_3) | ||
\land (x_1 \lor \lnot x_2 \lor x_3) | ||
\land (x_1 \lor x_2 \lor \lnot x_3) | ||
\land (x_1 \lor \lnot x_2 \lor \lnot x_3) | ||
\land (\lnot x_1 \lor x_2 \lor x_3)`. | ||
Args: | ||
filename: A file in DIMACS format. | ||
Returns: | ||
BitFlipOracle: A quantum circuit with a bit-flip oracle. | ||
""" | ||
expr = BooleanExpression.from_dimacs_file(filename) | ||
return cls(expr) |
Oops, something went wrong.