-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove dependance of BooleanExpression
on tweedledum
#13769
base: main
Are you sure you want to change the base?
Conversation
One or more of the following people are relevant to this code:
|
Pull Request Test Coverage Report for Build 13175136785Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @gadial for taking care of this - it looks great!
Note that there are some necessary API changes here, which makes sense and it's also allowed for Qiskit 2.0, but probably need better release notes, as well as raise deprecation warnings in Qiskit 1.4.
Did you make some experiments to check the performance compared to Tweedledum?
|
||
|
||
@HAS_TWEEDLEDUM.require_in_instance | ||
class BooleanExpression(ClassicalElement): | ||
class BooleanExpression(Gate): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note the API change here (changed types)
quantum register. | ||
synthesizer: A callable that takes self and returns a Tweedledum | ||
circuit. | ||
circuit_type: which type of oracle to create, 'bit' or 'phase' flip oracle. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note the API change in this function
@@ -50,40 +44,18 @@ class PhaseOracle(QuantumCircuit): | |||
|
|||
def __init__( | |||
self, | |||
expression: Union[str, ClassicalElement], | |||
synthesizer: Optional[Callable[[BooleanExpression], QuantumCircuit]] = None, | |||
expression: str, | |||
var_order: list = None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note the API change in the init
|
||
|
||
@HAS_TWEEDLEDUM.require_in_instance | ||
class BooleanExpression(ClassicalElement): | ||
class BooleanExpression(Gate): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The easiest deprecation path would be to just deprecate BooleanExpression
in place and have the new version in the circuit library.
It might also be worth considering a renaming to avoid too much confusion with the new object. What does this circuit implement exactly? Does it flip a target bit if the boolean expression is satisfied or does it something in-place? If it's the first, maybe BitflipOracle
could be a good name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a sound strategy - we already have a PhaseOracle
so we can add BitFlipOracle
which will be similar but with a different synthesis, and both gates would rely on a non-gate version of BooleanExpression
(maybe with a different name) which handles parsing, simulating (to create the truth table) and synthesizing (since we might want to invest in optimized algorithms later.
The main question is where to put those files. We can put BitFlipOracle
next to PhaseOracle
in qiskit.circuit.library
but we still need to put the NewBooleanExpression
module (which would consist at least of boolean_expression
, boolean_expression_visitor
and boolean_expression_synth
files) somewhere that makes sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in e403028
Summary
Rewrites the
BooleanExpression
class such that expression parsing and circuit synthesis does not rely on thetweedledum
library, and moves it to thesynthesis
library. Adds aBitFlipOracle
class to preserve the user-facing usage ofBooleanExpression
.Partially addresses #13755.
Details and comments
BooleanExpression
is used to describe classical boolean functions and synthesize phase/bit-flip oracles for them. Currently it is used directly within qiskit only for thePhaseOracle
class.The current implementation of
BooleanExpression
relies on thetweedledum
library for parsing and synthesizing. Since qiskit 2.0 will stop usingtweedledum
, this PR makes the code ofBooleanExpression
independent, in the cost of lower capabilities.These are the main changes:
ast
library, using a customboolean_expression_visitor
. Parsing DIMACS files is done directly using regexp.boolean_expression_synth
module) using a straightforward approach seemingly also employed bytweedledum
, given a representation of the function as ESOP (Exclusive sum of squares).BooleanExpression
into ESOP representation was also added. While it features basic optimizations to reduce the size of the ESOP, it needs to generate the full truth table of the represented function, making it a viable approach only for functions on a relatively low number of variables. This can be improved upon in the future if required, using more sophisticated ESOP generation and minimization techniques (such as using BDDs or SAT-solvers).synthesis/boolean
. A new class,BitFlipOracle
was added to the circuit library. The oldBooleanExpression
code remains unchanged and will be deprecated in a separate PR along with the rest of theclassicalfunction
library.