Skip to content
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

Merged
merged 7 commits into from
Jun 12, 2024

Conversation

tomv42
Copy link
Contributor

@tomv42 tomv42 commented Jun 11, 2024

Summary:

This pull request introduces the py2bexp command-line tool to the qlasskit package. The tool converts qlassf functions in a Python script to boolean expressions. It also includes initial tests for the functionality.

Changes:

  1. Added py2bexp.py to qlasskit/tools package:

    • Implements the conversion of qlassf functions to boolean expressions.
    • Supports reading from a file or stdin.
    • Allows specifying entrypoint function, output file, expression form, and output format.
    • Includes command-line arguments parsing with argparse.
  2. Updated setup.py:

    • Added py2bexp as a console script entry point for seamless installation and usage.
  3. Initial Tests in test_tools.py:

    • Included tests for help and version commands.
    • Added tests for default behavior, specific entrypoint, and different expression forms.
    • Verified DIMACS format output for CNF.
    • Note: Tests currently do not have assertions but printing results looks good.
      • I don't know what kind of assertion would be appropriate...

Related Issue:

This PR resolves issue #28

@dakk, I'm looking forward to your feedback.

- 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.
@dakk
Copy link
Owner

dakk commented Jun 11, 2024

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 👍

@tomv42
Copy link
Contributor Author

tomv42 commented Jun 11, 2024

Yes, I'm working on it.

tomv42 added 4 commits June 11, 2024 20:32
- 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.
@tomv42
Copy link
Contributor Author

tomv42 commented Jun 11, 2024

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 is_anf resulted in a type error. Which is weird because it didn't for the others.

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.

Copy link
Owner

@dakk dakk left a 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.

expr = load(result.stdout)
symbols = expr.free_symbols
x, y, z = symbols
expected1 = And(Or(x, y, Not(z)), Or(z, Not(y)))
Copy link
Owner

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

return file.read()


def find_last_qlassf(qlassf_list):
Copy link
Owner

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

result = self.run_command(
["python", "-m", "qlasskit.tools.py2bexp", "--version"]
)
print(result.stdout)
Copy link
Owner

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

@dakk dakk linked an issue Jun 12, 2024 that may be closed by this pull request
@dakk
Copy link
Owner

dakk commented Jun 12, 2024

@tomv42 with very few modification, you can also write the code for this: #26 , if you have time. You need to open a PR today, and then we have a week to review / request changes / edits.

tomv42 added 2 commits June 12, 2024 10:28
Removed prints from unit test.
Used itertools to handle permutations for the DIMACS test.
@tomv42
Copy link
Contributor Author

tomv42 commented Jun 12, 2024

@dakk I made the requested changes :)

@dakk dakk merged commit fe64388 into dakk:master Jun 12, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

py2bexp bool expressions exporter
2 participants