-
-
Notifications
You must be signed in to change notification settings - Fork 12
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
Add py2bexp CLI tool to convert qlassf functions to boolean expressions #56
Conversation
- Implemented `py2bexp.py` in the `qlasskit/tools` package. - Added functionality to receive a Python script (file or stdin) as input and output boolean expressions. - Default input is stdin if not specified with `--input-file/-i`. - Default entrypoint function is the last qlassf defined if not specified with `--entrypoint/-e`. - Default output is stdout if not specified with `--output/-o`. - Added parameter `-f/--form` for specifying the expression form (anf, cnf, dnf, nnf). - Added parameter `-t/--format` for specifying the output format (sympy string or dimacs format for cnf). - Added `-h/--help` for usage and `-v/--version` for qlasskit version. - Added unit tests in `test_tools.py` to verify functionality: - Tested help and version output. - Tested default behavior and specific entrypoint function handling. - Tested different expression forms (anf, cnf, dnf, nnf). - Verified DIMACS format output for CNF.
Thank you, I will review tomorrow UTC; in the meanwhile, can you please take care of failing tests on CI? Anyway, in tests you can assert the string you are printing 👍 |
Yes, I'm working on it. |
- Added satisfactory assertions for all the unit tests except two (in test_anf_form, is_anf returns an error... ; Haven't found a satisfactory way to test the dimacs format) - Modify py2bexp.py to use sympy logic functions directly instead of sympy.to_anf, sympy.to_cnf, sympy.to_dnf, sympy.to_nnf functions - Refactor output_result function in py2bexp.py to handle DIMACS format only for CNF expressions
As the order of the symbols when converting to DIMACS is not guaranteed, and all the permutations of the names of the symbols give equivalent expressions, the test compares to all the possible permutations of the 3 symbols.
I added satisfactory assertions to all the unit tests except one, by comparing the results against sympy expressions and testing that the form is the right one with sympy too. This didn't work for anf where calling For the DIMACS test, as the order of the symbols when converting to DIMACS is not guaranteed, and all the permutations of the names of the symbols give equivalent expressions, the test compares all the possible permutations of the 3 symbols. |
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.
Excellent work, I propose you some minor changes and then I can merge it.
test/test_tools.py
Outdated
expr = load(result.stdout) | ||
symbols = expr.free_symbols | ||
x, y, z = symbols | ||
expected1 = And(Or(x, y, Not(z)), Or(z, Not(y))) |
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.
here it is better to use itertools.permutations:
from itertools import permutations
assert_val = False
for sl in permutations(symbols):
exp = And(Or(sl[0], sl[2], Not(sl[1])), Or(sl[1], Not(sl[2])))
assert_val = assert_val or expr.equals(exp)
self.assertTrue(assert_val)
something like this
qlasskit/tools/py2bexp.py
Outdated
return file.read() | ||
|
||
|
||
def find_last_qlassf(qlassf_list): |
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.
move this function to tools.py
test/test_tools.py
Outdated
result = self.run_command( | ||
["python", "-m", "qlasskit.tools.py2bexp", "--version"] | ||
) | ||
print(result.stdout) |
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.
remove this and other prints from test
Removed prints from unit test. Used itertools to handle permutations for the DIMACS test.
@dakk I made the requested changes :) |
Summary:
This pull request introduces the
py2bexp
command-line tool to theqlasskit
package. The tool converts qlassf functions in a Python script to boolean expressions. It also includes initial tests for the functionality.Changes:
Added
py2bexp.py
toqlasskit/tools
package:argparse
.Updated
setup.py
:py2bexp
as a console script entry point for seamless installation and usage.Initial Tests in
test_tools.py
:Related Issue:
This PR resolves issue #28
@dakk, I'm looking forward to your feedback.