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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,19 @@ repos:
- id: check-yaml
- id: detect-private-key
- id: end-of-file-fixer
exclude: ^specification/schema/
exclude: |
(?x)^(
specification/schema/.*|
.*.snap|
.*.snap.new
)$
- id: trailing-whitespace
exclude: |
(?x)^(
specification/schema/.*|
.*.snap|
.*.snap.new
)$
- id: fix-byte-order-marker
- id: mixed-line-ending
# Python-specific
Expand Down
7 changes: 2 additions & 5 deletions quantinuum-hugr-py/src/quantinuum_hugr/serialization/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,9 @@ def insert_child_dfg_signature(self, inputs: TypeRow, outputs: TypeRow) -> None:
if isinstance(pred, tys.UnitSum):
self.sum_rows = [[] for _ in range(cast(tys.UnitSum, pred).size)]
else:
assert isinstance(pred, tys.GeneralSum)
self.sum_rows = []
for variant in pred.rows:
assert isinstance(variant, tys.TupleType)
self.sum_rows.append(variant.inner)
self.sum_rows.append(variant)
self.other_outputs = outputs[1:]

class Config:
Expand Down Expand Up @@ -346,8 +344,7 @@ def insert_port_types(self, in_types: TypeRow, out_types: TypeRow) -> None:
assert isinstance(pred, tys.GeneralSum)
self.sum_rows = []
for ty in pred.rows:
assert isinstance(ty, tys.TupleType)
self.sum_rows.append(ty.inner)
self.sum_rows.append(ty)
self.other_inputs = list(in_types[1:])
self.outputs = list(out_types)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,6 @@ class Array(MultiContainer):
len: int


class TupleType(BaseModel):
"""Product type, known-size tuple over elements of type row."""

t: Literal["Tuple"] = "Tuple"
inner: "TypeRow"


class UnitSum(BaseModel):
"""Simple predicate where all variants are empty tuples."""

Expand Down Expand Up @@ -286,7 +279,7 @@ class Type(RootModel):
"""A HUGR type."""

root: Annotated[
Qubit | Variable | USize | PolyFuncType | Array | TupleType | SumType | Opaque,
Qubit | Variable | USize | PolyFuncType | Array | SumType | Opaque,
WrapValidator(_json_custom_error_validator),
] = Field(discriminator="t")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,46 @@
---
source: src/hugr/views/tests.rs
source: quantinuum-hugr/src/hugr/views/tests.rs
expression: h.dot_string()
---
"digraph {\n0 [shape=plain label=<<table border=\"1\"><tr><td align=\"text\" border=\"0\" colspan=\"1\">(0) CFG</td></tr></table>>]\n1 [shape=plain label=<<table border=\"1\"><tr><td port=\"in0\" align=\"text\" colspan=\"1\" cellpadding=\"1\" border=\"0\">0</td></tr><tr><td align=\"text\" border=\"0\" colspan=\"1\">(1) ExitBlock</td></tr></table>>]\n2 [shape=plain label=<<table border=\"1\"><tr><td port=\"in0\" align=\"text\" colspan=\"2\" cellpadding=\"1\" border=\"0\">0</td></tr><tr><td align=\"text\" border=\"0\" colspan=\"2\">(2) DataflowBlock</td></tr><tr><td port=\"out0\" align=\"text\" colspan=\"1\" cellpadding=\"1\" border=\"0\">0</td><td port=\"out1\" align=\"text\" colspan=\"1\" cellpadding=\"1\" border=\"0\">1</td></tr></table>>]\n2:out0 -> 6:in0 [style=\"dashed\"]\n2:out1 -> 1:in0 [style=\"dashed\"]\n3 [shape=plain label=<<table border=\"1\"><tr><td align=\"text\" border=\"0\" colspan=\"1\">(3) Input</td></tr><tr><td port=\"out0\" align=\"text\" colspan=\"1\" cellpadding=\"1\" >0: usize</td></tr></table>>]\n3:out0 -> 5:in0 [style=\"\"]\n4 [shape=plain label=<<table border=\"1\"><tr><td port=\"in0\" align=\"text\" colspan=\"1\" cellpadding=\"1\" >0: Sum(General([TypeRow { types: [Type(Extension(CustomType { extension: IdentList(\"prelude\"), id: \"usize\", args: [], bound: Eq }), Eq)] }, TypeRow { types: [Type(Extension(CustomType { extension: IdentList(\"prelude\"), id: \"usize\", args: [], bound: Eq }), Eq)] }]))</td></tr><tr><td align=\"text\" border=\"0\" colspan=\"1\">(4) Output</td></tr></table>>]\n5 [shape=plain label=<<table border=\"1\"><tr><td port=\"in0\" align=\"text\" colspan=\"1\" cellpadding=\"1\" >0: usize</td></tr><tr><td align=\"text\" border=\"0\" colspan=\"1\">(5) Tag</td></tr><tr><td port=\"out0\" align=\"text\" colspan=\"1\" cellpadding=\"1\" >0: Sum(General([TypeRow { types: [Type(Extension(CustomType { extension: IdentList(\"prelude\"), id: \"usize\", args: [], bound: Eq }), Eq)] }, TypeRow { types: [Type(Extension(CustomType { extension: IdentList(\"prelude\"), id: \"usize\", args: [], bound: Eq }), Eq)] }]))</td></tr></table>>]\n5:out0 -> 4:in0 [style=\"\"]\n6 [shape=plain label=<<table border=\"1\"><tr><td port=\"in0\" align=\"text\" colspan=\"1\" cellpadding=\"1\" border=\"0\">0</td></tr><tr><td align=\"text\" border=\"0\" colspan=\"1\">(6) DataflowBlock</td></tr><tr><td port=\"out0\" align=\"text\" colspan=\"1\" cellpadding=\"1\" border=\"0\">0</td></tr></table>>]\n6:out0 -> 1:in0 [style=\"dashed\"]\n7 [shape=plain label=<<table border=\"1\"><tr><td align=\"text\" border=\"0\" colspan=\"1\">(7) Input</td></tr><tr><td port=\"out0\" align=\"text\" colspan=\"1\" cellpadding=\"1\" >0: usize</td></tr></table>>]\n7:out0 -> 8:in1 [style=\"\"]\n8 [shape=plain label=<<table border=\"1\"><tr><td port=\"in0\" align=\"text\" colspan=\"1\" cellpadding=\"1\" >0: Sum(UnitSum(1))</td><td port=\"in1\" align=\"text\" colspan=\"1\" cellpadding=\"1\" >1: usize</td></tr><tr><td align=\"text\" border=\"0\" colspan=\"2\">(8) Output</td></tr></table>>]\n9 [shape=plain label=<<table border=\"1\"><tr><td align=\"text\" border=\"0\" colspan=\"1\">(9) const:sum:{tag:0, vals:[]}</td></tr><tr><td port=\"out0\" align=\"text\" colspan=\"1\" cellpadding=\"1\" >0: Sum(UnitSum(1))</td></tr></table>>]\n9:out0 -> 10:in0 [style=\"\"]\n10 [shape=plain label=<<table border=\"1\"><tr><td port=\"in0\" align=\"text\" colspan=\"1\" cellpadding=\"1\" >0: Sum(UnitSum(1))</td></tr><tr><td align=\"text\" border=\"0\" colspan=\"1\">(10) LoadConstant</td></tr><tr><td port=\"out0\" align=\"text\" colspan=\"1\" cellpadding=\"1\" >0: Sum(UnitSum(1))</td></tr></table>>]\n10:out0 -> 8:in0 [style=\"\"]\nhier0 [shape=plain label=\"0\"]\nhier0 -> hier2 [style = \"dashed\"] \nhier0 -> hier1 [style = \"dashed\"] \nhier0 -> hier6 [style = \"dashed\"] \nhier1 [shape=plain label=\"1\"]\nhier2 [shape=plain label=\"2\"]\nhier2 -> hier3 [style = \"dashed\"] \nhier2 -> hier4 [style = \"dashed\"] \nhier2 -> hier5 [style = \"dashed\"] \nhier3 [shape=plain label=\"3\"]\nhier4 [shape=plain label=\"4\"]\nhier5 [shape=plain label=\"5\"]\nhier6 [shape=plain label=\"6\"]\nhier6 -> hier7 [style = \"dashed\"] \nhier6 -> hier8 [style = \"dashed\"] \nhier6 -> hier9 [style = \"dashed\"] \nhier6 -> hier10 [style = \"dashed\"] \nhier7 [shape=plain label=\"7\"]\nhier8 [shape=plain label=\"8\"]\nhier9 [shape=plain label=\"9\"]\nhier10 [shape=plain label=\"10\"]\n}\n"
digraph {
0 [shape=plain label=<<table border="1"><tr><td align="text" border="0" colspan="1">(0) CFG</td></tr></table>>]
1 [shape=plain label=<<table border="1"><tr><td port="in0" align="text" colspan="1" cellpadding="1" border="0">0</td></tr><tr><td align="text" border="0" colspan="1">(1) ExitBlock</td></tr></table>>]
2 [shape=plain label=<<table border="1"><tr><td port="in0" align="text" colspan="2" cellpadding="1" border="0">0</td></tr><tr><td align="text" border="0" colspan="2">(2) DataflowBlock</td></tr><tr><td port="out0" align="text" colspan="1" cellpadding="1" border="0">0</td><td port="out1" align="text" colspan="1" cellpadding="1" border="0">1</td></tr></table>>]
2:out0 -> 6:in0 [style="dashed"]
2:out1 -> 1:in0 [style="dashed"]
3 [shape=plain label=<<table border="1"><tr><td align="text" border="0" colspan="1">(3) Input</td></tr><tr><td port="out0" align="text" colspan="1" cellpadding="1" >0: usize</td></tr></table>>]
3:out0 -> 5:in0 [style=""]
4 [shape=plain label=<<table border="1"><tr><td port="in0" align="text" colspan="1" cellpadding="1" >0: [usize]+[usize]</td></tr><tr><td align="text" border="0" colspan="1">(4) Output</td></tr></table>>]
5 [shape=plain label=<<table border="1"><tr><td port="in0" align="text" colspan="1" cellpadding="1" >0: usize</td></tr><tr><td align="text" border="0" colspan="1">(5) Tag</td></tr><tr><td port="out0" align="text" colspan="1" cellpadding="1" >0: [usize]+[usize]</td></tr></table>>]
5:out0 -> 4:in0 [style=""]
6 [shape=plain label=<<table border="1"><tr><td port="in0" align="text" colspan="1" cellpadding="1" border="0">0</td></tr><tr><td align="text" border="0" colspan="1">(6) DataflowBlock</td></tr><tr><td port="out0" align="text" colspan="1" cellpadding="1" border="0">0</td></tr></table>>]
6:out0 -> 1:in0 [style="dashed"]
7 [shape=plain label=<<table border="1"><tr><td align="text" border="0" colspan="1">(7) Input</td></tr><tr><td port="out0" align="text" colspan="1" cellpadding="1" >0: usize</td></tr></table>>]
7:out0 -> 8:in1 [style=""]
8 [shape=plain label=<<table border="1"><tr><td port="in0" align="text" colspan="1" cellpadding="1" >0: []</td><td port="in1" align="text" colspan="1" cellpadding="1" >1: usize</td></tr><tr><td align="text" border="0" colspan="2">(8) Output</td></tr></table>>]
9 [shape=plain label=<<table border="1"><tr><td align="text" border="0" colspan="1">(9) const:sum:{tag:0, vals:[]}</td></tr><tr><td port="out0" align="text" colspan="1" cellpadding="1" >0: []</td></tr></table>>]
9:out0 -> 10:in0 [style=""]
10 [shape=plain label=<<table border="1"><tr><td port="in0" align="text" colspan="1" cellpadding="1" >0: []</td></tr><tr><td align="text" border="0" colspan="1">(10) LoadConstant</td></tr><tr><td port="out0" align="text" colspan="1" cellpadding="1" >0: []</td></tr></table>>]
10:out0 -> 8:in0 [style=""]
hier0 [shape=plain label="0"]
hier0 -> hier2 [style = "dashed"]
hier0 -> hier1 [style = "dashed"]
hier0 -> hier6 [style = "dashed"]
hier1 [shape=plain label="1"]
hier2 [shape=plain label="2"]
hier2 -> hier3 [style = "dashed"]
hier2 -> hier4 [style = "dashed"]
hier2 -> hier5 [style = "dashed"]
hier3 [shape=plain label="3"]
hier4 [shape=plain label="4"]
hier5 [shape=plain label="5"]
hier6 [shape=plain label="6"]
hier6 -> hier7 [style = "dashed"]
hier6 -> hier8 [style = "dashed"]
hier6 -> hier9 [style = "dashed"]
hier6 -> hier10 [style = "dashed"]
hier7 [shape=plain label="7"]
hier8 [shape=plain label="8"]
hier9 [shape=plain label="9"]
hier10 [shape=plain label="10"]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
---
source: src/hugr/views/tests.rs
source: quantinuum-hugr/src/hugr/views/tests.rs
expression: h.dot_string()
---
"digraph {\n0 [shape=plain label=<<table border=\"1\"><tr><td align=\"text\" border=\"0\" colspan=\"1\">(0) DFG</td></tr></table>>]\n1 [shape=plain label=<<table border=\"1\"><tr><td align=\"text\" border=\"0\" colspan=\"2\">(1) Input</td></tr><tr><td port=\"out0\" align=\"text\" colspan=\"1\" cellpadding=\"1\" >0: qubit</td><td port=\"out1\" align=\"text\" colspan=\"1\" cellpadding=\"1\" >1: qubit</td></tr></table>>]\n1:out0 -> 3:in0 [style=\"\"]\n1:out1 -> 3:in1 [style=\"\"]\n2 [shape=plain label=<<table border=\"1\"><tr><td port=\"in0\" align=\"text\" colspan=\"1\" cellpadding=\"1\" >0: qubit</td><td port=\"in1\" align=\"text\" colspan=\"1\" cellpadding=\"1\" >1: qubit</td></tr><tr><td align=\"text\" border=\"0\" colspan=\"2\">(2) Output</td></tr></table>>]\n3 [shape=plain label=<<table border=\"1\"><tr><td port=\"in0\" align=\"text\" colspan=\"3\" cellpadding=\"1\" >0: qubit</td><td port=\"in1\" align=\"text\" colspan=\"3\" cellpadding=\"1\" >1: qubit</td></tr><tr><td align=\"text\" border=\"0\" colspan=\"6\">(3) test.quantum.CX</td></tr><tr><td port=\"out0\" align=\"text\" colspan=\"2\" cellpadding=\"1\" >0: qubit</td><td port=\"out1\" align=\"text\" colspan=\"2\" cellpadding=\"1\" >1: qubit</td><td port=\"out2\" align=\"text\" colspan=\"2\" cellpadding=\"1\" border=\"0\"></td></tr></table>>]\n3:out0 -> 4:in1 [style=\"\"]\n3:out1 -> 4:in0 [style=\"\"]\n3:out2 -> 4:in2 [style=\"dotted\"]\n4 [shape=plain label=<<table border=\"1\"><tr><td port=\"in0\" align=\"text\" colspan=\"2\" cellpadding=\"1\" >0: qubit</td><td port=\"in1\" align=\"text\" colspan=\"2\" cellpadding=\"1\" >1: qubit</td><td port=\"in2\" align=\"text\" colspan=\"2\" cellpadding=\"1\" border=\"0\"></td></tr><tr><td align=\"text\" border=\"0\" colspan=\"6\">(4) test.quantum.CX</td></tr><tr><td port=\"out0\" align=\"text\" colspan=\"3\" cellpadding=\"1\" >0: qubit</td><td port=\"out1\" align=\"text\" colspan=\"3\" cellpadding=\"1\" >1: qubit</td></tr></table>>]\n4:out0 -> 2:in0 [style=\"\"]\n4:out1 -> 2:in1 [style=\"\"]\nhier0 [shape=plain label=\"0\"]\nhier0 -> hier1 [style = \"dashed\"] \nhier0 -> hier2 [style = \"dashed\"] \nhier0 -> hier3 [style = \"dashed\"] \nhier0 -> hier4 [style = \"dashed\"] \nhier1 [shape=plain label=\"1\"]\nhier2 [shape=plain label=\"2\"]\nhier3 [shape=plain label=\"3\"]\nhier4 [shape=plain label=\"4\"]\n}\n"
digraph {
0 [shape=plain label=<<table border="1"><tr><td align="text" border="0" colspan="1">(0) DFG</td></tr></table>>]
1 [shape=plain label=<<table border="1"><tr><td align="text" border="0" colspan="2">(1) Input</td></tr><tr><td port="out0" align="text" colspan="1" cellpadding="1" >0: qubit</td><td port="out1" align="text" colspan="1" cellpadding="1" >1: qubit</td></tr></table>>]
1:out0 -> 3:in0 [style=""]
1:out1 -> 3:in1 [style=""]
2 [shape=plain label=<<table border="1"><tr><td port="in0" align="text" colspan="1" cellpadding="1" >0: qubit</td><td port="in1" align="text" colspan="1" cellpadding="1" >1: qubit</td></tr><tr><td align="text" border="0" colspan="2">(2) Output</td></tr></table>>]
3 [shape=plain label=<<table border="1"><tr><td port="in0" align="text" colspan="3" cellpadding="1" >0: qubit</td><td port="in1" align="text" colspan="3" cellpadding="1" >1: qubit</td></tr><tr><td align="text" border="0" colspan="6">(3) test.quantum.CX</td></tr><tr><td port="out0" align="text" colspan="2" cellpadding="1" >0: qubit</td><td port="out1" align="text" colspan="2" cellpadding="1" >1: qubit</td><td port="out2" align="text" colspan="2" cellpadding="1" border="0"></td></tr></table>>]
3:out0 -> 4:in1 [style=""]
3:out1 -> 4:in0 [style=""]
3:out2 -> 4:in2 [style="dotted"]
4 [shape=plain label=<<table border="1"><tr><td port="in0" align="text" colspan="2" cellpadding="1" >0: qubit</td><td port="in1" align="text" colspan="2" cellpadding="1" >1: qubit</td><td port="in2" align="text" colspan="2" cellpadding="1" border="0"></td></tr><tr><td align="text" border="0" colspan="6">(4) test.quantum.CX</td></tr><tr><td port="out0" align="text" colspan="3" cellpadding="1" >0: qubit</td><td port="out1" align="text" colspan="3" cellpadding="1" >1: qubit</td></tr></table>>]
4:out0 -> 2:in0 [style=""]
4:out1 -> 2:in1 [style=""]
hier0 [shape=plain label="0"]
hier0 -> hier1 [style = "dashed"]
hier0 -> hier2 [style = "dashed"]
hier0 -> hier3 [style = "dashed"]
hier0 -> hier4 [style = "dashed"]
hier1 [shape=plain label="1"]
hier2 [shape=plain label="2"]
hier3 [shape=plain label="3"]
hier4 [shape=plain label="4"]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
---
source: src/hugr/views/tests.rs
source: quantinuum-hugr/src/hugr/views/tests.rs
expression: h.dot_string()
---
"digraph {\n0 [shape=plain label=<<table border=\"1\"><tr><td align=\"text\" border=\"0\" colspan=\"1\">(0) DFG</td></tr></table>>]\n1 [shape=plain label=<<table border=\"1\"><tr><td align=\"text\" border=\"0\" colspan=\"1\">(1) Input</td></tr><tr><td port=\"out0\" align=\"text\" colspan=\"1\" cellpadding=\"1\" >0: Sum(UnitSum(2))</td></tr></table>>]\n1:out0 -> 2:in0 [style=\"\"]\n2 [shape=plain label=<<table border=\"1\"><tr><td port=\"in0\" align=\"text\" colspan=\"1\" cellpadding=\"1\" >0: Sum(UnitSum(2))</td></tr><tr><td align=\"text\" border=\"0\" colspan=\"1\">(2) Output</td></tr></table>>]\nhier0 [shape=plain label=\"0\"]\nhier0 -> hier1 [style = \"dashed\"] \nhier0 -> hier2 [style = \"dashed\"] \nhier1 [shape=plain label=\"1\"]\nhier2 [shape=plain label=\"2\"]\n}\n"
digraph {
0 [shape=plain label=<<table border="1"><tr><td align="text" border="0" colspan="1">(0) DFG</td></tr></table>>]
1 [shape=plain label=<<table border="1"><tr><td align="text" border="0" colspan="1">(1) Input</td></tr><tr><td port="out0" align="text" colspan="1" cellpadding="1" >0: []+[]</td></tr></table>>]
1:out0 -> 2:in0 [style=""]
2 [shape=plain label=<<table border="1"><tr><td port="in0" align="text" colspan="1" cellpadding="1" >0: []+[]</td></tr><tr><td align="text" border="0" colspan="1">(2) Output</td></tr></table>>]
hier0 [shape=plain label="0"]
hier0 -> hier1 [style = "dashed"]
hier0 -> hier2 [style = "dashed"]
hier1 [shape=plain label="1"]
hier2 [shape=plain label="2"]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: src/hugr/views/tests.rs
source: quantinuum-hugr/src/hugr/views/tests.rs
expression: h.mermaid_string()
---
graph LR
Expand All @@ -11,7 +11,7 @@ graph LR
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
5--"0:0<br>[usize]+[usize]"-->4
end
2-."0:0".->6
2-."1:0".->1
Expand All @@ -22,9 +22,9 @@ graph LR
7--"0:1<br>usize"-->8
8["(8) Output"]
9["(9) const:sum:{tag:0, vals:[]}"]
9--"0:0<br>Sum(UnitSum(1))"-->10
9--"0:0<br>[]"-->10
10["(10) LoadConstant"]
10--"0:0<br>Sum(UnitSum(1))"-->8
10--"0:0<br>[]"-->8
end
6-."0:0".->1
end
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
---
source: src/hugr/views/tests.rs
source: quantinuum-hugr/src/hugr/views/tests.rs
expression: h.mermaid_string()
---
graph LR
subgraph 0 ["(0) DFG"]
direction LR
1["(1) Input"]
1--"0:0<br>Sum(UnitSum(2))"-->2
1--"0:0<br>[]+[]"-->2
2["(2) Output"]
end
2 changes: 1 addition & 1 deletion quantinuum-hugr/src/hugr/views/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn node_connections(
#[case::empty_dfg("dot_empty_dfg", crate::builder::test::simple_dfg_hugr())]
#[cfg_attr(miri, ignore)] // Opening files is not supported in (isolated) miri
fn dot_string(#[case] test_name: &str, #[case] h: Hugr) {
insta::assert_yaml_snapshot!(test_name, h.dot_string());
insta::assert_snapshot!(test_name, h.dot_string());
}

/// Render some hugrs into mermaid format.
Expand Down
Loading
Loading