Skip to content

Conversation

@aborgna-q
Copy link
Collaborator

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

This PR is a compilation of fixes to the pytket encoder/decoder. With these implemented, the example from CQCL/hugr#2667 should work correctly.

The fixes include (one per commit):

  • Marking wires that carry no value as unsupported, since they cannot be attached to a pytket operation. (They will instead be included in an opaque subgraph / barrier op).
  • Don't use SiblingSubgraph to define the unsupported subgraphs. That structure is quite opinionated and brittle;
    • it doesn't support non-value edges in the boundaries
    • it has a complex definition for mapping between boundary ports and actual node ports (unnecessary in this case)
    • requires running an expensive convexity checker on each instantiation, even when we know the set of nodes is valid
    • and other annoyances
  • Mark any node with order edges as unsupported (so we don't lose those the edges the pytket roundtrip)
  • Fix issue with bits not appearing in the input/output missing from the pytket circuit's list
  • Add an initial hardcoded translator between native booleans and tket booleans, with a TODO to make the system extensible.
  • Avoid QAllocing qubits that don't get consumed afterwards.
  • Ensure we give loaded output parameters the correct type.
  • Don't try to encode wires with complex types that include parameters like [float] / [float, rotation].
  • Store input parameters to extra_subgraph (that cannot be encoded in the pytket circuit)

The main result is a bunch of tests now passing in tests.rs.

@codecov
Copy link

codecov bot commented Nov 6, 2025

Codecov Report

❌ Patch coverage is 78.99160% with 100 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.71%. Comparing base (b270af8) to head (9600a72).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...et/src/serialize/pytket/config/type_translators.rs 40.00% 39 Missing ⚠️
tket/src/serialize/pytket/decoder/wires.rs 51.94% 34 Missing and 3 partials ⚠️
tket/src/serialize/pytket/encoder.rs 88.57% 2 Missing and 10 partials ⚠️
tket/src/serialize/pytket/decoder.rs 90.69% 0 Missing and 4 partials ⚠️
tket/src/serialize/pytket/decoder/subgraph.rs 92.59% 1 Missing and 1 partial ⚠️
tket/src/serialize/pytket.rs 0.00% 0 Missing and 1 partial ⚠️
tket/src/serialize/pytket/circuit.rs 93.33% 0 Missing and 1 partial ⚠️
tket/src/serialize/pytket/extension/core.rs 0.00% 1 Missing ⚠️
tket/src/serialize/pytket/extension/prelude.rs 75.00% 1 Missing ⚠️
tket/src/serialize/pytket/opaque.rs 91.66% 0 Missing and 1 partial ⚠️
... and 1 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1226      +/-   ##
==========================================
- Coverage   78.78%   78.71%   -0.07%     
==========================================
  Files         159      160       +1     
  Lines       20324    20592     +268     
  Branches    19222    19490     +268     
==========================================
+ Hits        16012    16210     +198     
- Misses       3332     3392      +60     
- Partials      980      990      +10     
Flag Coverage Δ
python 92.65% <ø> (ø)
qis-compiler 68.40% <ø> (ø)
rust 78.35% <78.99%> (-0.07%) ⬇️

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.

@hugrbot
Copy link
Collaborator

hugrbot commented Nov 6, 2025

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

cargo-semver-checks summary
    Building tket v0.16.0 (current)
     Built [  40.538s] (current)
   Parsing tket v0.16.0 (current)
    Parsed [   0.097s] (current)
  Building tket v0.16.0 (baseline)
     Built [  40.579s] (baseline)
   Parsing tket v0.16.0 (baseline)
    Parsed [   0.080s] (baseline)
  Checking tket v0.16.0 -> v0.16.0 (assume minor change)
   Checked [   0.076s] 159 checks: 158 pass, 1 fail, 0 warn, 41 skip

--- failure auto_trait_impl_removed: auto trait no longer implemented ---

Description:
A public type has stopped implementing one or more auto traits. This can break downstream code that depends on the traits being implemented.
      ref: https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits
     impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.45.0/src/lints/auto_trait_impl_removed.ron

Failed in:
type EncodedCircuit is no longer UnwindSafe, in /home/runner/work/tket2/tket2/PR_BRANCH/tket/src/serialize/pytket/circuit.rs:39
type EncodedCircuit is no longer RefUnwindSafe, in /home/runner/work/tket2/tket2/PR_BRANCH/tket/src/serialize/pytket/circuit.rs:39

   Summary semver requires new major version: 1 major and 0 minor checks failed
  Finished [  82.869s] tket
  Building tket-qsystem v0.22.0 (current)
     Built [  42.169s] (current)
   Parsing tket-qsystem v0.22.0 (current)
    Parsed [   0.029s] (current)
  Building tket-qsystem v0.22.0 (baseline)
     Built [  42.252s] (baseline)
   Parsing tket-qsystem v0.22.0 (baseline)
    Parsed [   0.028s] (baseline)
  Checking tket-qsystem v0.22.0 -> v0.22.0 (assume minor change)
   Checked [   0.046s] 159 checks: 159 pass, 41 skip
   Summary no semver update required
  Finished [  86.199s] tket-qsystem

@aborgna-q aborgna-q requested a review from mark-koch November 6, 2025 14:57
@aborgna-q aborgna-q marked this pull request as ready for review November 6, 2025 14:57
@aborgna-q aborgna-q requested a review from a team as a code owner November 6, 2025 14:57
@aborgna-q aborgna-q requested review from ss2165 and removed request for ss2165 November 6, 2025 14:57
Copy link
Contributor

@mark-koch mark-koch left a comment

Choose a reason for hiding this comment

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

Nice! Not an expert on your pytket conversion work, but looks good from the surface 👍

Mostly just some doc nits and questions. Also I'm wondering why this doesn't count as a breaking change due to signature changes/the switch to OpaqueSubgraph?

/// of subgraphs it can represent; in particular const edges and order edge are
/// not allowed in the boundary.
#[derive(Debug, Clone)]
pub struct OpaqueSubgraph<N> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we consider upstreaming this? In general, SiblingSubgraph seems quite brittle/not production ready 😅

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, we could have a discussion on which bits of this are useful.

This structure is not specific to the encoder/decoder per-se, but some it's not always useful esp. if the plan is to do rewriting.

There are some plans to redefine the subgraph structures anyways.

Comment on lines +873 to +874
// Ignore nodes with order edges, as they cannot be represented in the pytket circuit.
if !self.has_order_edges(node, optype, circ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Coming from Guppy, this also includes ancilla allocation and measurement :/

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Those would have appeared as input/output qubits to pytket anyways.

We should thing about alternatives in the future, but at least this change makes things correct.
(We'd need to device some way to track relations between pytket commands, which seems hard)

return true;
}

// For now, we just hard-code this to the two kind of bits we support.
Copy link
Contributor

Choose a reason for hiding this comment

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

This only works if the sum-bool is used linearly, right? Is this a problem?

Copy link
Contributor

Choose a reason for hiding this comment

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

Or do we assume that pytket extraction only runs after bool linearisation?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sum bool is marked as unsupported, so we won't see them here.

Comment on lines 983 to 986
// TODO: fix edge case: non-local edge from an unsupported node inside a nested CircBox
// to/from the input of the head region being encoded...
#[should_panic(expected = "Unsupported edge kind")]
#[case::non_local(circ_non_local(), 1, CircuitRoundtripTestConfig::Default)]
#[should_panic(expected = "has an unconnected port")]
#[case::non_local(circ_non_local(), 2, CircuitRoundtripTestConfig::Default)]
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this still the same edge case? I don't understand why the message is now "has an unconnected port"?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thinking about it I'm not sure why it's still failing after some of these fixes 🤔
Let me double check :P

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It was a problem with non-local edges from input nodes... It's fixed now :)

@aborgna-q aborgna-q added this pull request to the merge queue Nov 6, 2025
Merged via the queue into main with commit e0771f8 Nov 6, 2025
23 of 24 checks passed
@aborgna-q aborgna-q deleted the ab/fixes branch November 6, 2025 19:04
@hugrbot hugrbot mentioned this pull request Nov 6, 2025
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.

5 participants