Skip to content

feat: Capture pytket's output permutation explicitly in the hugr connectivity#1075

Merged
aborgna-q merged 2 commits intomainfrom
ab/explicit-output-permutation
Aug 26, 2025
Merged

feat: Capture pytket's output permutation explicitly in the hugr connectivity#1075
aborgna-q merged 2 commits intomainfrom
ab/explicit-output-permutation

Conversation

@aborgna-q
Copy link
Collaborator

@aborgna-q aborgna-q commented Aug 26, 2025

Now the permutation gets reflected in the wire structure.

A (circ := Circuit(2).SWAP(0,1)).replace_SWAPs() now gets encoded directly as

graph LR
    subgraph 0 ["(0) Module"]
        direction LR
        subgraph 1 ["(1) [**FuncDefn: #quot;#quot;**]"]
            direction LR
            style 1 stroke:#832561,stroke-width:3px
            2["(2) Input"]
            3["(3) Output"]
            2--"0:1<br>qubit"-->3
            2--"1:0<br>qubit"-->3
        end
    end
Loading

Before we only stored the permutation in the circuit's metadata

@aborgna-q aborgna-q requested a review from a team as a code owner August 26, 2025 11:23
@aborgna-q aborgna-q requested a review from lmondada August 26, 2025 11:23
@codecov
Copy link

codecov bot commented Aug 26, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.09%. Comparing base (5cb6293) to head (cef2a80).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1075      +/-   ##
==========================================
+ Coverage   79.01%   79.09%   +0.07%     
==========================================
  Files         115      115              
  Lines       13416    13390      -26     
  Branches    12634    12608      -26     
==========================================
- Hits        10601    10591      -10     
+ Misses       2158     2144      -14     
+ Partials      657      655       -2     
Flag Coverage Δ
python 92.83% <ø> (ø)
rust 78.24% <100.00%> (+0.08%) ⬆️

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 force-pushed the ab/explicit-output-permutation branch from e9fc8b9 to b1891f4 Compare August 26, 2025 12:14
@aborgna-q aborgna-q changed the title feat: Capture pytket's output permutation explicitly in the hugr structure feat: Capture pytket's output permutation explicitly in the hugr connectivity Aug 26, 2025
Copy link
Contributor

@lmondada lmondada left a comment

Choose a reason for hiding this comment

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

I think there is an error in your docs, but otherwise I like it. This makes more sense to me than the metadata trick you had before.

One question: does this represent a breaking change in serialisation? I.e. are there circuits that users may have stored for which the ordering of the outputs will not be parsed properly?

/// This originates from pytket's `implicit_permutation` field.
///
/// For a circuit with three qubit registers [q0, q1, q2] and an implicit
/// permutation {q0 -> q1, q1 -> q2, q2 -> q0}, `output_to_input` will be {1
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think output_to_input refers to anything anymore. Update?

/// A map from pytket register hashes to the latest up-to-date [`TrackedQubit`] referencing it.
///
/// The map keys are kept in the order they were defined in the circuit.
/// The map keys are kept in the order we expect to see them at the circuits output.
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm confused. In fn known_pytket_qubits you reorder your outputs using output_qubit_permutation, so that would indicate that they are not ordered by output order?

Furthermore, this is an IndexMap, so I'm not even sure what the order would be.

Copy link
Contributor

Choose a reason for hiding this comment

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

same comment 4 lines below.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah, I forgot to rollback this comment after trying a change here.

IndexMap is a HashMap that keeps the insertion order of its keys, so we can traverse it / find elements by index, etc.

pub const METADATA_Q_REGISTERS: &str = "TKET1.qubit_registers";
/// The reordered qubit registers in the output, if an implicit permutation was applied.
#[deprecated(
note = "Output registers now always match the input register order.",
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't find this message very clear. Do you mean something like

Suggested change
note = "Output registers now always match the input register order.",
note = "Qubit permutations are now represented explicitly in DFG output ordering .",

Comment on lines +40 to +43
#[deprecated(
note = "TKET.tk1op payload is stored as a op parameter, not a metadata field.",
since = "0.14.1"
)]
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand how this deprecation relates to the rest of the PR, but that might not be important.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sorry, it's a drive-by added while deprecating the other constants.

@aborgna-q
Copy link
Collaborator Author

One question: does this represent a breaking change in serialisation? I.e. are there circuits that users may have stored for which the ordering of the outputs will not be parsed properly?

Technically yes. Stored hugrs extracted from pytket circuits with implicit_permutations won't preserve the reordering if re-encoded into pytket.

The HUGR -> pytket is not currently used externally though, so I don't think we have to worry about the break.

@aborgna-q aborgna-q added this pull request to the merge queue Aug 26, 2025
@aborgna-q aborgna-q removed this pull request from the merge queue due to a manual request Aug 26, 2025
@aborgna-q aborgna-q enabled auto-merge August 26, 2025 13:50
@aborgna-q aborgna-q added this pull request to the merge queue Aug 26, 2025
Merged via the queue into main with commit be48088 Aug 26, 2025
20 checks passed
@aborgna-q aborgna-q deleted the ab/explicit-output-permutation branch August 26, 2025 13:59
@hugrbot hugrbot mentioned this pull request Aug 26, 2025
github-merge-queue bot pushed a commit that referenced this pull request Aug 26, 2025
So we can make a release with #1075.

BEGIN_COMMIT_OVERRIDE

feat: Capture pytket's output permutation explicitly in the hugr
connectivity (#1075)

END_COMMIT_OVERRIDE
github-merge-queue bot pushed a commit that referenced this pull request Aug 26, 2025
So we can make a release with #1075.

BEGIN_COMMIT_OVERRIDE

feat: Capture pytket's output permutation explicitly in the hugr
connectivity (#1075)

END_COMMIT_OVERRIDE


...#1076 didn't actually touch the python files -.-
I swear I'm not farming github stats.
github-merge-queue bot pushed a commit that referenced this pull request Aug 26, 2025
🤖 I have created a release *beep* *boop*
---


##
[0.12.5](tket-py-v0.12.4...tket-py-v0.12.5)
(2025-08-26)


### Features

* Capture pytket's output permutation explicitly in the hugr
connectivity ([#1075](#1075))
([5fc9614](5fc9614))

---
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>
@hugrbot hugrbot mentioned this pull request Sep 15, 2025
github-merge-queue bot pushed a commit that referenced this pull request Sep 15, 2025
## 🤖 New release

* `tket`: 0.14.0 -> 0.15.0 (✓ API compatible changes)
* `tket-qsystem`: 0.20.1 -> 0.21.0 (✓ API compatible changes)

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

## `tket`

<blockquote>

##
[0.15.0](tket-v0.14.0...tket-v0.15.0)
- 2025-09-15

### Bug Fixes

- [**breaking**] Fix rotation -> float param type conversion
([#1061](#1061))
- Pytket barrier operations not being decoded
([#1069](#1069))
- Always load parameter expressions as half turns in the decoder
([#1083](#1083))
- Move attribute to come after all the cases.
([#1112](#1112))

### New Features

- Capture pytket's output permutation explicitly in the hugr
connectivity ([#1075](#1075))
- Add ResourceScope ([#1052](#1052))
- [**breaking**] Remove unnecessary Arc from PytketDecoder method
([#1114](#1114))
- [**breaking**] Remove deprecated definitions
([#1113](#1113))
</blockquote>

## `tket-qsystem`

<blockquote>

##
[0.21.0](tket-qsystem-v0.20.1...tket-qsystem-v0.21.0)
- 2025-09-15

### Bug Fixes

- [**breaking**] Fix rotation -> float param type conversion
([#1061](#1061))
- Pytket barrier operations not being decoded
([#1069](#1069))
- *(qystem)* fix angle bug in CZ decomposition
([#1080](#1080))
- Always load parameter expressions as half turns in the decoder
([#1083](#1083))

### New Features

- Add a `borrow_array` type replacement pass
([#975](#975))
- Add gpu module ([#1090](#1090))
- [**breaking**] Remove unnecessary Arc from PytketDecoder method
([#1114](#1114))
- [**breaking**] Remove deprecated definitions
([#1113](#1113))

### Refactor

- [**breaking**] Factor out wasm extension code into compute module
([#1089](#1089))
</blockquote>


</p></details>

---
This PR was generated with
[release-plz](https://github.com/release-plz/release-plz/).

---------

Co-authored-by: Agustín Borgna <121866228+aborgna-q@users.noreply.github.com>
Co-authored-by: Alec Edgington <54802828+cqc-alec@users.noreply.github.com>
@hugrbot hugrbot mentioned this pull request Sep 16, 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.

2 participants