Skip to content

fix(pytket decoder): Panic on repeated registers in pytket decoded output#1445

Open
aborgna-q wants to merge 2 commits intomainfrom
ab/decoder-fixes
Open

fix(pytket decoder): Panic on repeated registers in pytket decoded output#1445
aborgna-q wants to merge 2 commits intomainfrom
ab/decoder-fixes

Conversation

@aborgna-q
Copy link
Collaborator

Fix an edge case where we extracted a hugr from a pytket circuit, and declared multiple output ports to have the same bit register.

Also cleans up a bit of the logic that handled consumed bit wires, to avoid marking them as outdated if they can still be used by other operations.

@aborgna-q aborgna-q requested a review from a team as a code owner March 13, 2026 14:59
@aborgna-q aborgna-q requested a review from acl-cqc March 13, 2026 14:59
Comment on lines +321 to +322
let mut qubits: Vec<TrackedQubit> = Vec::new();
let mut bits: Vec<TrackedBit> = Vec::new();
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is the main fix.

We populated this with the registers declared at the Hugr output.

If the output said e.g. b[0], b[0], b[1] we ignored the second entry, and ended up outputting b[0], b[1], new_allocated_bit

node,
hugr,
// Output bits use new registers, so don't reuse any input bits.
EmitCommandOptions::new().reuse_bits(|_| vec![]),
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Drive-by: The classical expression emitter was re-using the input bits to the node instead of using a separate output bit register.

@hugrbot
Copy link
Collaborator

hugrbot commented Mar 13, 2026

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.17.0 (current)
     Built [  45.562s] (current)
   Parsing tket v0.17.0 (current)
    Parsed [   0.086s] (current)
  Building tket v0.17.0 (baseline)
     Built [  42.689s] (baseline)
   Parsing tket v0.17.0 (baseline)
    Parsed [   0.080s] (baseline)
  Checking tket v0.17.0 -> v0.17.0 (assume minor change)
   Checked [   0.110s] 196 checks: 196 pass, 56 skip
   Summary no semver update required
  Finished [  90.389s] tket
  Building tket-qsystem v0.23.0 (current)
error: running cargo-doc on crate 'tket-qsystem' failed with output:
-----
 Compiling proc-macro2 v1.0.106
 Compiling quote v1.0.45
 Compiling unicode-ident v1.0.24
 Compiling libc v0.2.183
 Compiling serde_core v1.0.228
 Compiling memchr v2.8.0
 Compiling find-msvc-tools v0.1.9
 Compiling syn v2.0.117
  Checking once_cell v1.21.4
 Compiling jobserver v0.1.34
 Compiling shlex v1.3.0
  Checking cfg-if v1.0.4
 Compiling cc v1.2.56
 Compiling autocfg v1.5.0
 Compiling serde v1.0.228
 Compiling heck v0.5.0
 Compiling num-traits v0.2.19
 Compiling version_check v0.9.5
  Checking either v1.15.0
 Compiling semver v1.0.27
 Compiling crossbeam-utils v0.8.21
 Compiling aho-corasick v1.1.4
 Compiling equivalent v1.0.2
 Compiling hashbrown v0.16.1
 Compiling regex-syntax v0.8.10
 Compiling indexmap v2.13.0
 Compiling lazy_static v1.5.0
 Compiling pkg-config v0.3.32
 Compiling regex-automata v0.4.14
 Compiling zstd-sys v2.0.16+zstd.1.5.7
  Checking unicode-width v0.2.2
 Compiling zmij v1.0.21
 Compiling thiserror v2.0.18
 Compiling fnv v1.0.7
 Compiling serde_derive v1.0.228
 Compiling getrandom v0.4.2
 Compiling strsim v0.11.1
 Compiling ucd-trie v0.1.7
  Checking smallvec v1.15.1
 Compiling ident_case v1.0.1
 Compiling unicode-segmentation v1.12.0
 Compiling darling_core v0.21.3
 Compiling convert_case v0.10.0
 Compiling pest v2.8.6
 Compiling regex v1.12.3
 Compiling thiserror-impl v2.0.18
  Checking crossbeam-epoch v0.9.18
 Compiling unicode-xid v0.2.6
 Compiling zerocopy v0.8.42
  Checking itoa v1.0.17
 Compiling radium v0.7.0
  Checking foldhash v0.1.5
 Compiling rayon-core v1.13.0
 Compiling serde_json v1.0.149
 Compiling paste v1.0.15
 Compiling typeid v1.0.3
  Checking hashbrown v0.15.5
 Compiling derive_more-impl v2.1.1
  Checking crossbeam-deque v0.8.6
 Compiling darling_macro v0.21.3
 Compiling pest_meta v2.8.6
 Compiling ahash v0.8.12
 Compiling slotmap_fork_lmondada v1.0.8
  Checking tracing-core v0.1.36
 Compiling rustix v1.1.4
 Compiling ref-cast v1.0.25
 Compiling erased-serde v0.4.10
  Checking fixedbitset v0.5.7
 Compiling zstd-safe v7.2.4
  Checking pin-project-lite v0.2.17
 Compiling thiserror v1.0.69
 Compiling winnow v0.7.15
  Checking tap v1.0.1
  Checking wyz v0.5.1
  Checking petgraph v0.8.3
  Checking derive_more v2.1.1
 Compiling pest_generator v2.8.6
 Compiling toml_parser v1.0.9+spec-1.1.0
 Compiling darling v0.21.3
 Compiling llvm-sys v140.1.3
 Compiling thiserror-impl v1.0.69
 Compiling ref-cast-impl v1.0.25
 Compiling serde_derive_internals v0.29.1
 Compiling rustc_version v0.4.1
  Checking itertools v0.14.0
  Checking funty v2.0.0
 Compiling toml_datetime v1.0.0+spec-1.1.0
  Checking arrayvec v0.5.2
  Checking linux-raw-sys v0.12.1
  Checking rustc-hash v2.1.1
  Checking byteorder v1.5.0
  Checking bitflags v2.11.0
 Compiling typetag v0.2.21
 Compiling convert_case v0.4.0
 Compiling parking_lot_core v0.9.12
 Compiling cgmath v0.18.0
  Checking typed-arena v2.0.2
  Checking pretty v0.12.5
 Compiling derive_more v0.99.20
  Checking fxhash v0.2.1
 Compiling toml_edit v0.25.4+spec-1.1.0
  Checking bitvec v1.0.1
 Compiling schemars_derive v1.2.1
 Compiling rstest_macros v0.26.1
  Checking rayon v1.11.0
 Compiling serde_with_macros v3.17.0
 Compiling pest_derive v2.8.6
  Checking ordered-float v5.1.0
 Compiling tracing-attributes v0.1.31
 Compiling delegate v0.13.5
 Compiling typetag-impl v0.2.21
 Compiling strum_macros v0.28.0
 Compiling derive-where v1.6.0
  Checking approx v0.4.0
  Checking itertools v0.13.0
  Checking smol_str v0.3.6
  Checking fastrand v2.3.0
  Checking scopeguard v1.2.0
  Checking bumpalo v3.20.2
  Checking dyn-clone v1.0.20
  Checking utf8-width v0.1.8
  Checking inventory v0.3.22
  Checking base64 v0.22.1
  Checking capnp v0.25.2
 Compiling fixedbitset v0.4.2
  Checking allocator-api2 v0.2.21
  Checking utf8parse v0.2.2
  Checking anstyle-parse v1.0.0
  Checking strum v0.28.0
  Checking hashbrown v0.14.5
  Checking relrc v0.5.0
 Compiling petgraph v0.6.5
  Checking hugr-model v0.25.7
  Checking html-escape v0.2.13
  Checking schemars v1.2.1
 Compiling enum_dispatch v0.3.13
  Checking lock_api v0.4.14
  Checking tempfile v3.27.0
  Checking zstd v0.13.3
  Checking tracing v0.1.44
  Checking portgraph v0.15.3
  Checking serde_with v3.17.0
 Compiling proc-macro-crate v3.5.0
 Compiling ascent_base v0.8.0
 Compiling derive-syn-parse v0.2.0
 Compiling futures-macro v0.3.32
  Checking anstyle v1.0.13
 Compiling duplicate v2.0.1
 Compiling relative-path v1.9.3
 Compiling glob v0.3.3
  Checking static_assertions v1.1.0
 Compiling inkwell v0.8.0
  Checking is_terminal_polyfill v1.70.2
  Checking slab v0.4.12
  Checking downcast-rs v2.0.2
  Checking futures-task v0.3.32
 Compiling anyhow v1.0.102
 Compiling pastey v0.2.1
  Checking futures-core v0.3.32
  Checking colorchoice v1.0.4
  Checking anstyle-query v1.1.5
 Compiling ascent_macro v0.8.0
  Checking anstream v1.0.0
  Checking futures-util v0.3.32
  Checking hugr-core v0.25.7
  Checking dashmap v5.5.3
 Compiling inkwell_internals v0.13.0
 Compiling hugr-llvm v0.25.7
  Checking console v0.15.11
  Checking instant v0.1.13
  Checking clap_lex v1.1.0
  Checking futures-timer v3.0.3
  Checking boxcar v0.1.0
  Checking similar v2.7.0
  Checking clap_builder v4.6.0
  Checking ascent v0.8.0
  Checking rstest v0.26.1
  Checking insta v1.46.3
 Compiling clap_derive v4.6.0
 Compiling proc-macro-error-attr2 v2.0.0
  Checking log v0.4.29
 Compiling proc-macro-error2 v2.0.1
 Compiling strum_macros v0.27.2
  Checking num-integer v0.1.46
  Checking same-file v1.0.6
  Checking bytecount v0.6.9
  Checking papergrid v0.17.0
  Checking clap v4.6.0
  Checking walkdir v2.5.0
  Checking num-bigint v0.4.6
  Checking hugr-passes v0.25.7
  Checking strum v0.27.2
 Compiling tabled_derive v0.11.0
  Checking tracing-log v0.2.0
  Checking sharded-slab v0.1.7
  Checking uuid v1.22.0
  Checking testing_table v0.3.0
  Checking csv-core v0.1.13
  Checking is-terminal v0.4.17
  Checking thread_local v1.1.9
  Checking ryu v1.0.23
  Checking iana-time-zone v0.1.65
  Checking nu-ansi-term v0.50.3
  Checking chrono v0.4.44
  Checking tracing-subscriber v0.3.23
  Checking csv v1.4.0
  Checking clio v0.3.5
  Checking tabled v0.20.0
  Checking tket-json-rs v0.8.1
  Checking num-rational v0.4.2
  Checking clap-verbosity-flag v3.0.4
  Checking priority-queue v2.7.0
  Checking crossbeam-channel v0.5.15
  Checking bytemuck v1.25.0
  Checking hugr v0.25.7
  Checking hugr-cli v0.25.7
  Checking tket v0.17.0 (/home/runner/work/tket2/tket2/PR_BRANCH/tket)
error[E0277]: the trait bound `BoolOp: strum::IntoEnumIterator` is not satisfied
 --> /home/runner/work/tket2/tket2/PR_BRANCH/tket/src/extension/bool.rs:32:13
  |
32 |             BoolOp::load_all_ops(ext, ext_ref).unwrap();
  |             ^^^^^^ unsatisfied trait bound
  |
help: the trait `strum::IntoEnumIterator` is not implemented for `BoolOp`
 --> /home/runner/work/tket2/tket2/PR_BRANCH/tket/src/extension/bool.rs:118:1
  |
118 | pub enum BoolOp {
  | ^^^^^^^^^^^^^^^
note: there are multiple different versions of crate `strum` in the dependency graph
 --> /home/runner/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/strum-0.28.0/src/lib.rs:99:1
  |
99 | pub trait IntoEnumIterator: Sized {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this is the expected trait
  |
 ::: /home/runner/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/strum-0.27.2/src/lib.rs:100:1
  |
100 | pub trait IntoEnumIterator: Sized {
  | --------------------------------- this is the found trait
  = help: you can use `cargo tree` to explore your dependency tree
  = help: the following other types implement trait `strum::IntoEnumIterator`:
            BArrayUnsafeOpDef
            ConvertOpDef
            FloatOps
            GenericArrayOpDef<AK>
            IntOpDef
            ListOp
            LogicOp
            PtrOpDef
          and 2 others
note: required by a bound in `load_all_ops`
 --> /home/runner/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/hugr-core-0.25.7/src/extension/simple_op.rs:109:15
  |
104 |     fn load_all_ops(
  |        ------------ required by a bound in this associated function
...
109 |         Self: IntoEnumIterator,
  |               ^^^^^^^^^^^^^^^^ required by this bound in `MakeOpDef::load_all_ops`

error[E0277]: the trait bound `RotationOp: strum::IntoEnumIterator` is not satisfied
 --> /home/runner/work/tket2/tket2/PR_BRANCH/tket/src/extension/rotation.rs:215:5
  |
215 |     RotationOp::load_all_ops(extension, extension_ref).expect("add fail");
  |     ^^^^^^^^^^ unsatisfied trait bound
  |
help: the trait `strum::IntoEnumIterator` is not implemented for `RotationOp`
 --> /home/runner/work/tket2/tket2/PR_BRANCH/tket/src/extension/rotation.rs:119:1
  |
119 | pub enum RotationOp {
  | ^^^^^^^^^^^^^^^^^^^
note: there are multiple different versions of crate `strum` in the dependency graph
 --> /home/runner/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/strum-0.28.0/src/lib.rs:99:1
  |
99 | pub trait IntoEnumIterator: Sized {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this is the expected trait
  |
 ::: /home/runner/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/strum-0.27.2/src/lib.rs:100:1
  |
100 | pub trait IntoEnumIterator: Sized {
  | --------------------------------- this is the found trait
  = help: you can use `cargo tree` to explore your dependency tree
  = help: the following other types implement trait `strum::IntoEnumIterator`:
            BArrayUnsafeOpDef
            ConvertOpDef
            FloatOps
            GenericArrayOpDef<AK>
            IntOpDef
            ListOp
            LogicOp
            PtrOpDef
          and 2 others
note: required by a bound in `load_all_ops`
 --> /home/runner/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/hugr-core-0.25.7/src/extension/simple_op.rs:109:15
  |
104 |     fn load_all_ops(
  |        ------------ required by a bound in this associated function
...
109 |         Self: IntoEnumIterator,
  |               ^^^^^^^^^^^^^^^^ required by this bound in `MakeOpDef::load_all_ops`

error[E0277]: the trait bound `TketOp: strum::IntoEnumIterator` is not satisfied
 --> /home/runner/work/tket2/tket2/PR_BRANCH/tket/src/extension.rs:102:13
  |
102 |             TketOp::load_all_ops(res, ext_ref).expect("add_fail");
  |             ^^^^^^ unsatisfied trait bound
  |
help: the trait `strum::IntoEnumIterator` is not implemented for `TketOp`
 --> /home/runner/work/tket2/tket2/PR_BRANCH/tket/src/ops.rs:43:1
  |
43 | pub enum TketOp {
  | ^^^^^^^^^^^^^^^
note: there are multiple different versions of crate `strum` in the dependency graph
 --> /home/runner/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/strum-0.28.0/src/lib.rs:99:1
  |
99 | pub trait IntoEnumIterator: Sized {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this is the expected trait
  |
 ::: /home/runner/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/strum-0.27.2/src/lib.rs:100:1
  |
100 | pub trait IntoEnumIterator: Sized {
  | --------------------------------- this is the found trait
  = help: you can use `cargo tree` to explore your dependency tree
  = help: the following other types implement trait `strum::IntoEnumIterator`:
            BArrayUnsafeOpDef
            ConvertOpDef
            FloatOps
            GenericArrayOpDef<AK>
            IntOpDef
            ListOp
            LogicOp
            PtrOpDef
          and 2 others
note: required by a bound in `load_all_ops`
 --> /home/runner/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/hugr-core-0.25.7/src/extension/simple_op.rs:109:15
  |
104 |     fn load_all_ops(
  |        ------------ required by a bound in this associated function
...
109 |         Self: IntoEnumIterator,
  |               ^^^^^^^^^^^^^^^^ required by this bound in `MakeOpDef::load_all_ops`

error[E0277]: the trait bound `BoolOp: strum::IntoEnumIterator` is not satisfied
 --> /home/runner/work/tket2/tket2/PR_BRANCH/tket/src/llvm/bool.rs:113:14
  |
113 |             .simple_extension_op(move |context, args, op| self.emit_bool_op(context, args, op))
  |              ^^^^^^^^^^^^^^^^^^^ unsatisfied trait bound
  |
help: the trait `strum::IntoEnumIterator` is not implemented for `BoolOp`
 --> /home/runner/work/tket2/tket2/PR_BRANCH/tket/src/extension/bool.rs:118:1
  |
118 | pub enum BoolOp {
  | ^^^^^^^^^^^^^^^
note: there are multiple different versions of crate `strum` in the dependency graph
 --> /home/runner/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/strum-0.28.0/src/lib.rs:99:1
  |
99 | pub trait IntoEnumIterator: Sized {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this is the expected trait
  |
 ::: /home/runner/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/strum-0.27.2/src/lib.rs:100:1
  |
100 | pub trait IntoEnumIterator: Sized {
  | --------------------------------- this is the found trait
  = help: you can use `cargo tree` to explore your dependency tree
  = help: the following other types implement trait `strum::IntoEnumIterator`:
            BArrayUnsafeOpDef
            ConvertOpDef
            FloatOps
            GenericArrayOpDef<AK>
            IntOpDef
            ListOp
            LogicOp
            PtrOpDef
          and 2 others
note: required by a bound in `CodegenExtsBuilder::<'a, H>::simple_extension_op`
 --> /home/runner/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/hugr-llvm-0.25.7/src/custom.rs:110:48
  |
110 |     pub fn simple_extension_op<Op: MakeOpDef + IntoEnumIterator>(
  |                                                ^^^^^^^^^^^^^^^^ required by this bound in `CodegenExtsBuilder::<'a, H>::simple_extension_op`

error[E0277]: the trait bound `RotationOp: strum::IntoEnumIterator` is not satisfied
 --> /home/runner/work/tket2/tket2/PR_BRANCH/tket/src/llvm/rotation.rs:208:14
  |
208 |             .simple_extension_op(move |context, args, op| self.emit_rotation_op(context, args, op))
  |              ^^^^^^^^^^^^^^^^^^^ unsatisfied trait bound
  |
help: the trait `strum::IntoEnumIterator` is not implemented for `RotationOp`
 --> /home/runner/work/tket2/tket2/PR_BRANCH/tket/src/extension/rotation.rs:119:1
  |
119 | pub enum RotationOp {
  | ^^^^^^^^^^^^^^^^^^^
note: there are multiple different versions of crate `strum` in the dependency graph
 --> /home/runner/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/strum-0.28.0/src/lib.rs:99:1
  |
99 | pub trait IntoEnumIterator: Sized {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this is the expected trait
  |
 ::: /home/runner/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/strum-0.27.2/src/lib.rs:100:1
  |
100 | pub trait IntoEnumIterator: Sized {
  | --------------------------------- this is the found trait
  = help: you can use `cargo tree` to explore your dependency tree
  = help: the following other types implement trait `strum::IntoEnumIterator`:
            BArrayUnsafeOpDef
            ConvertOpDef
            FloatOps
            GenericArrayOpDef<AK>
            IntOpDef
            ListOp
            LogicOp
            PtrOpDef
          and 2 others
note: required by a bound in `CodegenExtsBuilder::<'a, H>::simple_extension_op`
 --> /home/runner/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/hugr-llvm-0.25.7/src/custom.rs:110:48
  |
110 |     pub fn simple_extension_op<Op: MakeOpDef + IntoEnumIterator>(
  |                                                ^^^^^^^^^^^^^^^^ required by this bound in `CodegenExtsBuilder::<'a, H>::simple_extension_op`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `tket` (lib) due to 5 previous errors

-----

error: failed to build rustdoc for crate tket-qsystem v0.23.0
note: this is usually due to a compilation error in the crate,
    and is unlikely to be a bug in cargo-semver-checks
note: the following command can be used to reproduce the error:
    cargo new --lib example &&
        cd example &&
        echo '[workspace]' >> Cargo.toml &&
        cargo add --path /home/runner/work/tket2/tket2/PR_BRANCH/tket-qsystem --features cli,default,llvm &&
        cargo check &&
        cargo doc

error: aborting due to failure to build rustdoc for crate tket-qsystem v0.23.0

@codecov
Copy link

codecov bot commented Mar 13, 2026

Codecov Report

❌ Patch coverage is 89.65517% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.01%. Comparing base (aa3c780) to head (204bbf7).

Files with missing lines Patch % Lines
tket/src/serialize/pytket/decoder/wires.rs 77.77% 0 Missing and 2 partials ⚠️
tket/src/serialize/pytket/extension/bool.rs 83.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1445      +/-   ##
==========================================
+ Coverage   79.65%   80.01%   +0.36%     
==========================================
  Files         155      155              
  Lines       20335    20333       -2     
  Branches    19345    19343       -2     
==========================================
+ Hits        16197    16270      +73     
+ Misses       3180     3101      -79     
- Partials      958      962       +4     
Flag Coverage Δ
python 93.00% <ø> (ø)
qis-compiler 100.00% <ø> (ø)
rust 79.33% <89.65%> (+0.38%) ⬆️

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.

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.

2 participants