-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
separate boolean optmization in boolopt module
- Loading branch information
Showing
10 changed files
with
240 additions
and
76 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Copyright 2023 Davide Gessa | ||
|
||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
|
||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# isort:skip_file | ||
|
||
from .sympytransformer import SympyTransformer # noqa: F401 |
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,63 @@ | ||
# Copyright 2023 Davide Gessa | ||
|
||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
|
||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from sympy.logic import And, Not, Or, Xor | ||
|
||
from . import SympyTransformer | ||
|
||
|
||
class remove_ITE(SympyTransformer): | ||
def visit_ITE(self, expr): | ||
c = self.visit(expr.args[0]) | ||
return self.visit( | ||
Or( | ||
And(c, self.visit(expr.args[1])), | ||
And(Not(c), self.visit(expr.args[2])), | ||
) | ||
) | ||
|
||
|
||
class remove_Implies(SympyTransformer): | ||
def visit_Implies(self, expr): | ||
return self.visit(Or(Not(self.visit(expr.args[0])), self.visit(expr.args[1]))) | ||
|
||
|
||
class transform_or2xor(SympyTransformer): | ||
# Or(And(a,b), And(!a,!b)) = !Xor(a,b) | ||
def visit_Or(self, expr): | ||
if ( | ||
len(expr.args) == 2 | ||
and isinstance(expr.args[0], And) | ||
and isinstance(expr.args[1], And) | ||
and ( | ||
( | ||
expr.args[1].args[0] == Not(expr.args[0].args[0]) | ||
and expr.args[1].args[1] == Not(expr.args[0].args[1]) | ||
) | ||
or ( | ||
Not(expr.args[1].args[0]) == expr.args[0].args[0] | ||
and Not(expr.args[1].args[1]) == expr.args[0].args[1] | ||
) | ||
) | ||
): | ||
a = self.visit(expr.args[0].args[0]) | ||
b = self.visit(expr.args[0].args[1]) | ||
return Not(Xor(a, b)) | ||
else: | ||
return super().visit_Or(expr) | ||
|
||
|
||
class transform_or2and(SympyTransformer): | ||
def visit_Or(self, expr): | ||
return Not(And(*[Not(self.visit(e)) for e in expr.args])) |
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,37 @@ | ||
from sympy.logic import ITE, And, Implies, Not, Or, Xor | ||
|
||
|
||
class SympyTransformer: | ||
def visit(self, e): | ||
if isinstance(e, And): | ||
return self.visit_And(e) | ||
elif isinstance(e, Or): | ||
return self.visit_Or(e) | ||
elif isinstance(e, Not): | ||
return self.visit_Not(e) | ||
elif isinstance(e, Implies): | ||
return self.visit_Implies(e) | ||
elif isinstance(e, ITE): | ||
return self.visit_ITE(e) | ||
elif isinstance(e, Xor): | ||
return self.visit_Xor(e) | ||
else: | ||
return e | ||
|
||
def visit_And(self, e): | ||
return And(*[self.visit(a) for a in e.args]) | ||
|
||
def visit_Or(self, e): | ||
return Or(*[self.visit(a) for a in e.args]) | ||
|
||
def visit_Not(self, e): | ||
return Not(self.visit(e.args[0])) | ||
|
||
def visit_ITE(self, e): | ||
return ITE(*[self.visit(a) for a in e.args]) | ||
|
||
def visit_Implies(self, e): | ||
return Implies(*[self.visit(a) for a in e.args]) | ||
|
||
def visit_Xor(self, e): | ||
return Xor(*[self.visit(a) for a in e.args]) |
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 |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
author_email="[email protected]", | ||
packages=[ | ||
"qlasskit", | ||
"qlasskit.boolopt", | ||
"qlasskit.types", | ||
"qlasskit.ast2logic", | ||
"qlasskit.qcircuit", | ||
|
Oops, something went wrong.