Skip to content
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

feat: Replace Tuple with unary sums #891

Merged
merged 5 commits into from
Mar 25, 2024
Merged

feat: Replace Tuple with unary sums #891

merged 5 commits into from
Mar 25, 2024

Conversation

aborgna-q
Copy link
Collaborator

@aborgna-q aborgna-q commented Mar 25, 2024

Closes #864.

Updates the Display impl of Sum as part of this change, so

graph LR
    subgraph 0 ["(0) CFG"]
        direction LR
        subgraph 2 ["(2) DataflowBlock"]
            direction LR
            3["(3) Input"]
            3--"0:0<br>usize"-->5
            4["(4) Output"]
            5["(5) Tag"]
            5--"0:0<br>Sum(General([TypeRow { types: [Type(Extension(CustomType { extension: IdentList(#quot;prelude#quot;), id: #quot;usize#quot;, args: [], bound: Eq }), Eq)] }, TypeRow { types: [Type(Extension(CustomType { extension: IdentList(#quot;prelude#quot;), id: #quot;usize#quot;, args: [], bound: Eq }), Eq)] }]))"-->4
        end
        2-."0:0".->6
        2-."1:0".->1
        1["(1) ExitBlock"]
        subgraph 6 ["(6) DataflowBlock"]
            direction LR
            7["(7) Input"]
            7--"0:1<br>usize"-->8
            8["(8) Output"]
            9["(9) const:sum:{tag:0, vals:[]}"]
            9--"0:0<br>Sum(UnitSum(1))"-->10
            10["(10) LoadConstant"]
            10--"0:0<br>Sum(UnitSum(1))"-->8
        end
        6-."0:0".->1
    end
Loading

is now rendered as

graph LR
    subgraph 0 ["(0) CFG"]
        direction LR
        subgraph 2 ["(2) DataflowBlock"]
            direction LR
            3["(3) Input"]
            3--"0:0<br>usize"-->5
            4["(4) Output"]
            5["(5) Tag"]
            5--"0:0<br>[usize]+[usize]"-->4
        end
        2-."0:0".->6
        2-."1:0".->1
        1["(1) ExitBlock"]
        subgraph 6 ["(6) DataflowBlock"]
            direction LR
            7["(7) Input"]
            7--"0:1<br>usize"-->8
            8["(8) Output"]
            9["(9) const:sum:{tag:0, vals:[]}"]
            9--"0:0<br>[]"-->10
            10["(10) LoadConstant"]
            10--"0:0<br>[]"-->8
        end
        6-."0:0".->1
    end
Loading

And tuples appear directly as [usize, usize, ...].
We were incorrectly using the debug formatter for the internal types.

drive-by: Add rust snapshots to the pre-commit ignores. We don't want it to modify whitespaces.

@aborgna-q aborgna-q requested a review from cqc-alec March 25, 2024 13:25
Copy link

codecov bot commented Mar 25, 2024

Codecov Report

Attention: Patch coverage is 94.11765% with 2 lines in your changes are missing coverage. Please review.

Project coverage is 85.68%. Comparing base (63afb03) to head (cfe31e2).

Files Patch % Lines
quantinuum-hugr/src/types.rs 94.11% 1 Missing ⚠️
quantinuum-hugr/src/utils.rs 92.30% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #891      +/-   ##
==========================================
+ Coverage   85.55%   85.68%   +0.12%     
==========================================
  Files          78       78              
  Lines       14396    14417      +21     
  Branches    14396    14417      +21     
==========================================
+ Hits        12317    12353      +36     
+ Misses       1445     1430      -15     
  Partials      634      634              
Flag Coverage Δ
python ∅ <ø> (∅)
rust 85.68% <94.11%> (+0.12%) ⬆️

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.

@aborgna-q
Copy link
Collaborator Author

aborgna-q commented Mar 25, 2024

This is the implementation side of the change, I'll update the spec too.

Edit: done

@aborgna-q
Copy link
Collaborator Author

We could also add an explicit single-element variant to SumType to optimize for tuples internally (this would be transparent to the user, as the current Unit|General variants).

Copy link
Collaborator

@cqc-alec cqc-alec left a comment

Choose a reason for hiding this comment

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

LGTM

@aborgna-q aborgna-q added this pull request to the merge queue Mar 25, 2024
Merged via the queue into main with commit f1e9600 Mar 25, 2024
17 checks passed
@aborgna-q aborgna-q deleted the feat/unit-as-sums branch March 25, 2024 13:55
@github-actions github-actions bot mentioned this pull request Mar 20, 2024
github-merge-queue bot pushed a commit that referenced this pull request Mar 25, 2024
Adds some actual benchmarks, mainly extracted from the unit tests:

- Construct a complex `Type`
- Construct a `DFG` hugr
- Construct a `CFG` hugr

I intended to include these in a PR implementing this optimisation
suggestion
#891 (comment), but the
actual tests showed a consistent 4% slowdown on the hugr builders.
So I'd say it's important to have benchmarks :)
@github-actions github-actions bot mentioned this pull request Apr 15, 2024
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.

Replace tuples with unary variants over type rows
2 participants