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: Mermaid renderer for hugrs #852

Merged
merged 9 commits into from
Mar 5, 2024
Merged

feat: Mermaid renderer for hugrs #852

merged 9 commits into from
Mar 5, 2024

Conversation

aborgna-q
Copy link
Collaborator

Adds a HugrView::mermaid_string which produces things like

graph LR
    subgraph 0 ["(0) DFG"]
        direction LR
        1["(1) Input"]
        1--0:0-->3
        1--1:1-->3
        2["(2) Output"]
        3["(3) test.quantum.CX"]
        3--0:1-->4
        3--1:0-->4
        3-.2:2.-4
        4["(4) test.quantum.CX"]
        4--0:0-->2
        4--1:1-->2
    end
Loading

Note that edges in mermaid are unordered, so I had to add the port indices explicitly.

The new code in src/hugr/views/render.rs is just moved from src/hugr/views.rs.

Closes #696

Requires CQCL/portgraph#125

@aborgna-q aborgna-q requested review from ss2165 and removed request for ss2165 February 28, 2024 19:23
@aborgna-q
Copy link
Collaborator Author

aborgna-q commented Feb 29, 2024

Here's a render for a CFG:

graph LR
    subgraph 0 ["(0) CFG"]
        direction LR
        subgraph 2 ["(2) DataflowBlock"]
            direction LR
            3["(3) Input"]
            3--0:0-->5
            4["(4) Output"]
            5["(5) MakeTuple"]
            5--0:0-->6
            6["(6) Tag"]
            6--0:0-->4
        end
        2--0:0-->7
        2--1:0-->1
        1["(1) ExitBlock"]
        subgraph 7 ["(7) DataflowBlock"]
            direction LR
            8["(8) Input"]
            8--0:1-->9
            9["(9) Output"]
            10["(10) const:sum:{tag:0, val:const:seq:{}}"]
            10--0:0-->11
            11["(11) LoadConstant"]
            11--0:0-->9
        end
        7--0:0-->1
    end
Loading

I also tried adding the type to the edge labels. It's a bit more noisy, but probably more useful too.

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) MakeTuple"]
            5--"0:0<br>Tuple([usize])"-->6
            6["(6) Tag"]
            6--"0:0<br>Sum([Tuple([usize]), Tuple([usize])])"-->4
        end
        2--"0:0"-->7
        2--"1:0"-->1
        1["(1) ExitBlock"]
        subgraph 7 ["(7) DataflowBlock"]
            direction LR
            8["(8) Input"]
            8--"0:1<br>usize"-->9
            9["(9) Output"]
            10["(10) const:sum:{tag:0, val:const:seq:{}}"]
            10--"0:0<br>Sum(UnitSum(1))"-->11
            11["(11) LoadConstant"]
            11--"0:0<br>Sum(UnitSum(1))"-->9
        end
        7--"0:0"-->1
    end
Loading

And this is now the first example, with type info:

graph LR
    subgraph 0 ["(0) DFG"]
        direction LR
        1["(1) Input"]
        1--"0:0<br>qubit([])"-->3
        1--"1:1<br>qubit([])"-->3
        2["(2) Output"]
        3["(3) test.quantum.CX"]
        3--"0:1<br>qubit([])"-->4
        3--"1:0<br>qubit([])"-->4
        3-."2:2".->4
        4["(4) test.quantum.CX"]
        4--"0:0<br>qubit([])"-->2
        4--"1:1<br>qubit([])"-->2
    end
Loading

drive-by: Flatten TOML section (my linter was complaining...)
Copy link

codecov bot commented Mar 1, 2024

Codecov Report

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

Project coverage is 84.13%. Comparing base (697e7d7) to head (deabfd7).
Report is 2 commits behind head on main.

❗ Current head deabfd7 differs from pull request most recent head f39a14e. Consider uploading reports for the commit f39a14e to get more accurate results

Files Patch % Lines
src/hugr/views/render.rs 98.27% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #852      +/-   ##
==========================================
+ Coverage   84.04%   84.13%   +0.09%     
==========================================
  Files          77       78       +1     
  Lines       14234    14261      +27     
  Branches    14234    14261      +27     
==========================================
+ Hits        11963    11999      +36     
+ Misses       1435     1427       -8     
+ Partials      836      835       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@aborgna-q aborgna-q marked this pull request as ready for review March 1, 2024 16:27
@aborgna-q aborgna-q requested a review from ss2165 March 1, 2024 16:27
@aborgna-q aborgna-q requested a review from ss2165 March 5, 2024 14:52
@aborgna-q
Copy link
Collaborator Author

aborgna-q commented Mar 5, 2024

Here's the CFG rendered in the new test:

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) MakeTuple"]
            5--"0:0<br>Tuple([usize])"-->6
            6["(6) Tag"]
            6--"0:0<br>Sum([Tuple([usize]), Tuple([usize])])"-->4
        end
        2-."0:0".->7
        2-."1:0".->1
        1["(1) ExitBlock"]
        subgraph 7 ["(7) DataflowBlock"]
            direction LR
            8["(8) Input"]
            8--"0:1<br>usize"-->9
            9["(9) Output"]
            10["(10) const:sum:{tag:0, val:const:seq:{}}"]
            10--"0:0<br>Sum(UnitSum(1))"-->11
            11["(11) LoadConstant"]
            11--"0:0<br>Sum(UnitSum(1))"-->9
        end
        7-."0:0".->1
    end
Loading

you can optionally hide the node indices, types, and ports now;

graph LR
    subgraph 0 ["CFG"]
        direction LR
        subgraph 2 ["DataflowBlock"]
            direction LR
            3["Input"]
            3-->5
            4["Output"]
            5["MakeTuple"]
            5-->6
            6["Tag"]
            6-->4
        end
        2-.->7
        2-.->1
        1["ExitBlock"]
        subgraph 7 ["DataflowBlock"]
            direction LR
            8["Input"]
            8-->9
            9["Output"]
            10["const:sum:{tag:0, val:const:seq:{}}"]
            10-->11
            11["LoadConstant"]
            11-->9
        end
        7-.->1
    end
Loading

@aborgna-q aborgna-q enabled auto-merge March 5, 2024 15:33
@aborgna-q aborgna-q added this pull request to the merge queue Mar 5, 2024
Merged via the queue into main with commit 3fb833a Mar 5, 2024
8 checks passed
@aborgna-q aborgna-q deleted the feat/mermaid-render branch March 5, 2024 15:37
@github-actions github-actions bot mentioned this pull request Mar 5, 2024
github-merge-queue bot pushed a commit that referenced this pull request Mar 7, 2024
- Fixes the failing unsoundness check in `main`
  https://github.com/CQCL/hugr/actions/runs/8158956607/job/22302072598
  
The test change in #852 moved the miri ignore flag, and it seems it has
to be placed after `rstest`'s cases.
  
- Adds a concurrency group for the unsoundness check.
The check currently takes around 15mins (and it will only grow as we add
more tests).
This makes it so old jobs get cancelled when pushing new changes to
main, and a new check is started.
@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.

feat: automatic mermaid diagram generation
2 participants