Skip to content

Conversation

@aborgna-q
Copy link
Collaborator

@aborgna-q aborgna-q commented Nov 5, 2025

Adds some pass definitions to tket.passes:

  • normalize_guppy(Tk2Circuit, ...) -> Tk2Circuit
  • clifford_simp(Tk2Circuit, *, allow_swaps = True, traverse_subcircuits = True) -> Tk2Circuit
  • squash_phased_rz(Tk2Circuit, *, traverse_subcircuits = True) -> Tk2Circuit

The last two use the new bridge to the old tket C++ codebase to run optimisation on regions of the hugr.

The API here not too ergonomic, we just expose it as a MVP while working on the API refresh.

To call this from a guppy program output, we'll need to convert the hugr to the rust representation and back,

from guppylang import guppy
from guppylang.std.angles import angle
from guppylang.std.builtins import result
from guppylang.std.quantum import h, rx, rz, measure, qubit
from hugr import Hugr
from tket.circuit import Tk2Circuit
from tket.passes import normalize_guppy, squash_phasedx_rz

@guppy
def main() -> None:
    q = qubit()

    rz(q, angle(0.1))
    h(q)
    rx(q, angle(-0.1))
    h(q)

    b = measure(q)
    result("b", b)

program = main.compile()
compiler_state = Tk2Circuit.from_bytes(program.to_bytes())
compiler_state = normalize_guppy(compiler_state)
compiler_state = squash_phasedx_rz(compiler_state)
hugr = Hugr.from_str(compiler_state.to_str())
print(hugr.render_dot())

drive-by: Add Tk2Circuit.render_mermaid method

BEGIN_COMMIT_OVERRIDE

feat: Add bindings for tket1-passes from python (#1225)

END_COMMIT_OVERRIDE

@aborgna-q aborgna-q requested a review from cqc-alec November 5, 2025 16:08
@codecov
Copy link

codecov bot commented Nov 5, 2025

Codecov Report

❌ Patch coverage is 0% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.58%. Comparing base (5a458cf) to head (6c99879).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
tket1-passes/src/lib.rs 0.00% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1225      +/-   ##
==========================================
- Coverage   78.61%   78.58%   -0.03%     
==========================================
  Files         159      159              
  Lines       20184    20191       +7     
  Branches    19082    19089       +7     
==========================================
  Hits        15868    15868              
- Misses       3345     3352       +7     
  Partials      971      971              
Flag Coverage Δ
python 92.65% <ø> (ø)
qis-compiler 68.40% <ø> (ø)
rust 78.20% <0.00%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@aborgna-q aborgna-q marked this pull request as ready for review November 5, 2025 16:19
@aborgna-q aborgna-q requested a review from a team as a code owner November 5, 2025 16:19
try_with_circ(circ, |mut circ, typ| {
let mut pass = tket::passes::NormalizeGuppy::default();

pass.simplify_cfgs(simplify_cfgs)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest making all the parameter names match these names? If we extend them in future it could make things a bit easier.

Comment on lines +24 to +25
// TODO: We should also expose `target_gate` here, but the most appropriate
// parameter type [`crate::ops::PyTketOp`] doesn't include `TK2` -.-
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only call clifford_simp with target_gate = CX for now.

We don't have a tket optype with both CX and TK2 variants, but if we really wanted to have this we could either accept a string or a pytket optype and convert it.

@aborgna-q aborgna-q force-pushed the ab/python-rust-c-passes branch from a02312d to 245ee9f Compare November 5, 2025 16:56
@aborgna-q aborgna-q enabled auto-merge November 5, 2025 17:19
@aborgna-q aborgna-q added this pull request to the merge queue Nov 5, 2025
Merged via the queue into main with commit fce8084 Nov 5, 2025
23 of 24 checks passed
@aborgna-q aborgna-q deleted the ab/python-rust-c-passes branch November 5, 2025 17:27
github-merge-queue bot pushed a commit that referenced this pull request Nov 11, 2025
🤖 I have created a release *beep* *boop*
---


##
[0.12.9](tket-py-v0.12.8...tket-py-v0.12.9)
(2025-11-11)


### Features

* Add bindings for tket1-passes from python
([[#1225](https://github.com/CQCL/tket2/issues/1225)](https://github.com/CQCL/tket2/pull/1225))
([fce8084](fce8084))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

---------

Co-authored-by: Agustín Borgna <[email protected]>
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.

3 participants