quizx icon indicating copy to clipboard operation
quizx copied to clipboard

refactor: simplified and updated Python/PyZX bindings

Open akissinger opened this issue 9 months ago • 1 comments

This is a WiP, where I've been working on revamping the Python bindings. Currently, the bindings are a bit outdated and incomplete. They also "double wrap" the main rust classes. For example, VecGraph is wrapped in a PyO3 class written in rust, which in turn is wrapped in a subclass of BaseGraph written in python.

I originally did this to overcome a limitation in PyO3 that you can't extend classes written in python directly with rust bindings. However, this means you have to write twice as much boilerplate to extend the bindings, and you have to deal with this "double-wrapping" not only for graphs but any other rust functions or classes that consume graphs.

I think the simplest way around this is to just re-implement the helper methods provided by BaseGraph directly in rust (or throw "Not implemented" exceptions for those we don't care too much about), then "pretend" that the VecGraph wrapper class provided by the rust bindings extends BaseGraph, just by saying so in the mypy type stubs.

While I haven't tested this approach extensively, this will certainly do the right thing in code that relies on duck-typing, and should satisfy any python type checker.

akissinger avatar Apr 08 '25 14:04 akissinger

This PR contains breaking changes to the public Rust API. Please deprecate the old API instead (if possible), or mark the PR with a ! following the conventional commits format to indicate a breaking change.

cargo-semver-checks summary

--- failure enum_variant_added: enum variant added on exhaustive enum ---

Description:
A publicly-visible enum without #[non_exhaustive] has a new variant.
      ref: https://doc.rust-lang.org/cargo/reference/semver.html#enum-variant-new
     impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.41.0/src/lints/enum_variant_added.ron

Failed in:
variant BasisElem:SKIP in /home/runner/work/quizx/quizx/PR_BRANCH/quizx/src/graph.rs:105
variant BasisElem:SKIP in /home/runner/work/quizx/quizx/PR_BRANCH/quizx/src/graph.rs:105
variant BasisElem:SKIP in /home/runner/work/quizx/quizx/PR_BRANCH/quizx/src/graph.rs:105

--- failure inherent_method_missing: pub method removed or renamed ---

Description:
A publicly-visible method or associated fn is no longer available under its prior name. It may have been renamed or removed entirely.
      ref: https://doc.rust-lang.org/cargo/reference/semver.html#item-remove
     impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.41.0/src/lints/inherent_method_missing.ron

Failed in:
Scalar::complex_value, previously in file /home/runner/work/quizx/quizx/BASELINE_BRANCH/pybindings/src/scalar.rs:119
Scalar::mul_sqrt2_pow, previously in file /home/runner/work/quizx/quizx/BASELINE_BRANCH/pybindings/src/scalar.rs:124
Scalar::mul_phase, previously in file /home/runner/work/quizx/quizx/BASELINE_BRANCH/pybindings/src/scalar.rs:134

github-actions[bot] avatar May 13 '25 07:05 github-actions[bot]