Skip to content

feat: Define a wire tracker for the new pytket decoder #1036

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Aug 15, 2025

Conversation

aborgna-q
Copy link
Collaborator

Defines a WireTracker struct for the pytket decoder framework.

It keeps track of wires that have been added to the HUGR being created along with which qubit/bit/parameter elements each of them carries (wires may carry multiple, see PytketTypeTranslator in ::serialize::pytket::extensions.
The tracker lets us associate new wires with registers/parameters, and updates previous references so they know they don't have the latest pytket register value.

In the future we'll expand this tracker to let us do automatic type translations (e.g. unpacking a tuple), as defined by the extension decoders.

Also adds a new Tk1DecodeError that will be used by the decoder framework.

Depends on #1035

@aborgna-q aborgna-q requested a review from ss2165 August 13, 2025 16:44
@aborgna-q aborgna-q requested a review from a team as a code owner August 13, 2025 16:44
Copy link

codecov bot commented Aug 13, 2025

Codecov Report

❌ Patch coverage is 39.70037% with 322 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.03%. Comparing base (99f568b) to head (ae40c62).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
tket/src/serialize/pytket/decoder/wires.rs 28.39% 292 Missing and 3 partials ⚠️
tket/src/serialize/pytket.rs 52.77% 17 Missing ⚠️
tket/src/serialize/pytket/decoder/tracked_elem.rs 88.37% 10 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1036      +/-   ##
==========================================
- Coverage   79.74%   78.03%   -1.72%     
==========================================
  Files          97       99       +2     
  Lines       11674    12208     +534     
  Branches    11395    11929     +534     
==========================================
+ Hits         9309     9526     +217     
- Misses       1734     2049     +315     
- Partials      631      633       +2     
Flag Coverage Δ
python 81.00% <ø> (ø)
rust 77.96% <39.70%> (-1.75%) ⬇️

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 requested a review from Copilot August 13, 2025 16:48
Copilot

This comment was marked as resolved.

@aborgna-q aborgna-q force-pushed the ab/decoder-wire-tracker branch from 420e3ac to fbb74ac Compare August 13, 2025 17:03
Base automatically changed from ab/decoder-lopaded-param to main August 14, 2025 11:07
}
}

/// Tracked wires to a pytket operation.
Copy link
Member

Choose a reason for hiding this comment

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

not sure what this docstring means

/// Return an iterator over the wires and their types.
///
/// This returns the wires as-is, without any additional conversions.
/// If you need to retrieve a specific wire type, use TODO
Copy link
Member

Choose a reason for hiding this comment

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

TODO?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Leftover comment from the automated type translation work. Removed.


/// Returns the tracked qubit elements in the set of wires as an array.
#[inline]
pub fn qubits_arr<const N: usize>(
Copy link
Member

Choose a reason for hiding this comment

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

no array equivalent for bits?

do all of these need to be public?

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 object that implementors of decoders interact with when doing operation translations, so I tried to add some useful helpers.

This one (in contrast to wires_arr) is just a collect_arr wrapper, so I removed it.

/// operation, all previous references to it become "outdated".
#[derive(Debug, Clone, Default)]
pub struct WireTracker {
/// A list of tracked wires, with their type and list of
Copy link
Member

Choose a reason for hiding this comment

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

list or map? outdated docstring?

///
/// * `hugr` - The hugr to add the wires to.
/// * `args` - The list of pytket element ids to map to wires.
/// * `operation` - The name of the operation being decoded, used for error reporting.
Copy link
Member

Choose a reason for hiding this comment

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

if it is only used for error reporting shouldn't it be added as context by the calling function instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Made a change copying the model ImportError / ImportErrorInner structure,
so we can add the context later and remove these parameters.

@aborgna-q aborgna-q requested a review from ss2165 August 14, 2025 17:46
@aborgna-q
Copy link
Collaborator Author

@ss2165 I ended up going and adding a last commit simplifying the tracker logic after the review. Sorry 😬

@aborgna-q aborgna-q requested a review from Copilot August 15, 2025 08:55
Copilot

This comment was marked as resolved.

Comment on lines 40 to 43
reg: Arc<PytketRegister>,
/// The hash of the pytket register for this tracked element, used to
/// speed up hashing and equality checks.
reg_hash: RegisterHash,
Copy link
Member

Choose a reason for hiding this comment

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

ideally these two should be in a struct by themselves - they're not independent

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I originally intended to put this inside the Arc, but RegisterHash is a single byte so it's cheaper to just have it here.
I guess we could add an intermediary

pub struct PytketResource {
    element: Arc<tket_json_rs::register::ElementId>,
    hash: RegisterHash,
}

I'll add a TODO

@aborgna-q aborgna-q enabled auto-merge August 15, 2025 14:36
@aborgna-q aborgna-q added this pull request to the merge queue Aug 15, 2025
Merged via the queue into main with commit 2466ee2 Aug 15, 2025
19 of 20 checks passed
@aborgna-q aborgna-q deleted the ab/decoder-wire-tracker branch August 15, 2025 14:44
This was referenced Aug 14, 2025
github-merge-queue bot pushed a commit that referenced this pull request Aug 15, 2025
- Adds a `PytketDecoder` trait that let us extend the decoding framework
to external extensions.
- Translations are no longer 1-to-1 between pytket commands and hugr
ops, the decoding call has access to a hugr builder and its context.
- Allows tracking wires with complex/composite types. We don't provide
helpers to convert types yet, it's up to the extension emitter to look
at the incoming wire types. (Note: I splitted this change to a previous
PR, #1036)


TODO:
- [x] Implement the decoder trait for all standard extensions.
- [x] Fix remaining test errors.
- [ ] Add a nicer guide for writing extension traits in the module docs.

BREAKING CHANGE: Split pytket encoder/decoder errors into separate
structs.
github-merge-queue bot pushed a commit that referenced this pull request Aug 18, 2025
## 🤖 New release

* `tket`: 0.13.2 -> 0.14.0 (✓ API compatible changes)
* `tket-qsystem`: 0.18.1 -> 0.19.0 (✓ API compatible changes)

<details><summary><i><b>Changelog</b></i></summary><p>

## `tket`

<blockquote>

##
[0.14.0](tket-v0.13.2...tket-v0.14.0)
- 2025-08-18

### New Features

- [**breaking**] Allow PytketTypeTranslators to translate nested types
([#1038](#1038))
- Define a wire tracker for the new pytket decoder
([#1036](#1036))
- [**breaking**] Reworked pytket decoder framework
([#1030](#1030))
- [**breaking**] Use qsystem encoder/decoders in tket-py
([#1041](#1041))
- [**breaking**] Avoid eagerly cloning SerialCircuits when decoding from
pytket ([#1048](#1048))

### Refactor

- [**breaking**] Rename tk2 encoder names to tket
([#1037](#1037))
</blockquote>

## `tket-qsystem`

<blockquote>

##
[0.19.0](tket-qsystem-v0.18.1...tket-qsystem-v0.19.0)
- 2025-08-18

### New Features

- Add emitters for tket-qsystem
([#1039](#1039))
- [**breaking**] Avoid eagerly cloning SerialCircuits when decoding from
pytket ([#1048](#1048))
</blockquote>


</p></details>

---
This PR was generated with
[release-plz](https://github.com/release-plz/release-plz/).
github-merge-queue bot pushed a commit that referenced this pull request Aug 19, 2025
🤖 I have created a release *beep* *boop*
---


##
[0.12.2](tket-py-v0.12.1...tket-py-v0.12.2)
(2025-08-19)


### Features

* Define a wire tracker for the new pytket decoder
([#1036](#1036))
([2466ee2](2466ee2))
* Support qsystem native operations when loading pytket circuits
([#1041](#1041))
([88c5c79](88c5c79))
* **tket-py:** Create BadgerOptimiser from arbitrary Rewriters
([#1022](#1022))
([a975c1d](a975c1d)),
closes [#1021](#1021)


### Documentation

* Update README badges
([#1004](#1004))
([d609bf5](d609bf5))

---
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 <agustin.borgna@quantinuum.com>
croyzor pushed a commit that referenced this pull request Aug 20, 2025
🤖 I have created a release *beep* *boop*
---


##
[0.12.2](tket-py-v0.12.1...tket-py-v0.12.2)
(2025-08-19)


### Features

* Define a wire tracker for the new pytket decoder
([#1036](#1036))
([2466ee2](2466ee2))
* Support qsystem native operations when loading pytket circuits
([#1041](#1041))
([88c5c79](88c5c79))
* **tket-py:** Create BadgerOptimiser from arbitrary Rewriters
([#1022](#1022))
([a975c1d](a975c1d)),
closes [#1021](#1021)


### Documentation

* Update README badges
([#1004](#1004))
([d609bf5](d609bf5))

---
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 <agustin.borgna@quantinuum.com>
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