CQCL / tket

Source code for the TKET quantum compiler, Python bindings and utilities
https://docs.quantinuum.com/tket/
Apache License 2.0
257 stars 48 forks source link

`ClExprOp` and friends #1628

Closed cqc-alec closed 3 weeks ago

cqc-alec commented 4 weeks ago

This is the first stage of ClassicalExpBox refactoring: defining a new operation type (ClExprOp) to replace it. This is defined in C++ with Python bindings, and is independent of bit and register names (information about which variable corresponds to which input etc. is encoded in the Op).

This PR also implements QASM conversion to and from the new op, but for QASM-to-circuit conversion this is not the default: a new boolean argument for circuit_from_qasm and friends is added to control this. This is so as not to break current workflows that rely on pytket-to-X conversions that don't currently handle the ClExprOp. After this is in a release we will update pytket-qir, pytket-phir and any other relevant extensions to handle the new type; after that we can update tket to use it everywhere by default, write a new decomposer pass for it, and deprecate ClassicalExpBox.

PS The Nix build failure is expected because it is using an older mypy version.

cqc-alec commented 4 weeks ago

I've only left a couple of comments - looks nice and clean. Could it be worth adding tests covering some of the strange behaviour from ClassicalExpBox, such as having classical expressions in CircBox that are copied, or python circuits that have classical expressions added, the units relabelled and then converted to qasm to confirm that the expressions are as desired?

Good idea, I'll add some such tests.

cqc-alec commented 3 weeks ago

Exciting, LGTM! So I could submit jobs to QuantinuumBackend with qasm conversion and ClExpr now and it should work?

Yes I believe that should work. Note that there is not yet a version of DecomposeClassicalExp that works with ClExpr, but that should not be needed in many cases.