Skip to content

Commit dc44b81

Browse files
authored
fix: handle order port case for missing dataflow ops (#2238)
Fixes #1968 drive-by `Call` now inherits from `DataflowOp`
1 parent acf201f commit dc44b81

File tree

4 files changed

+319
-23
lines changed

4 files changed

+319
-23
lines changed

hugr-py/src/hugr/ops.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,6 @@ def name(self) -> str:
7979
return str(self)
8080

8181

82-
def _sig_port_type(sig: tys.FunctionType, port: InPort | OutPort) -> tys.Type:
83-
"""Get the type of the given dataflow port given the signature of the operation."""
84-
if port.offset == -1:
85-
# Order port
86-
msg = "Order port has no type."
87-
raise ValueError(msg)
88-
if port.direction == Direction.INCOMING:
89-
return sig.input[port.offset]
90-
return sig.output[port.offset]
91-
92-
9382
@runtime_checkable
9483
class DataflowOp(Op, Protocol):
9584
"""Abstract dataflow operation. Can be assumed to have a signature and Value-
@@ -142,7 +131,17 @@ def port_type(self, port: InPort | OutPort) -> tys.Type:
142131
Bool
143132
144133
"""
145-
return _sig_port_type(self.outer_signature(), port)
134+
sig = self.outer_signature()
135+
if port.offset == -1:
136+
# Order port
137+
msg = "Order port has no type."
138+
raise ValueError(msg)
139+
try:
140+
if port.direction == Direction.INCOMING:
141+
return sig.input[port.offset]
142+
return sig.output[port.offset]
143+
except IndexError as e:
144+
raise self._invalid_port(port) from e
146145

147146
def __call__(self, *args) -> Command:
148147
"""Calling with incoming :class:`Wire` arguments returns a
@@ -973,10 +972,8 @@ def port_kind(self, port: InPort | OutPort) -> tys.Kind:
973972
match port:
974973
case InPort(_, 0):
975974
return tys.ConstKind(self.type_)
976-
case OutPort(_, 0):
977-
return tys.ValueKind(self.type_)
978975
case _:
979-
raise self._invalid_port(port)
976+
return DataflowOp.port_kind(self, port)
980977

981978
def __repr__(self) -> str:
982979
return "LoadConst" + (f"({self._typ})" if self._typ is not None else "")
@@ -1284,7 +1281,7 @@ def __init__(
12841281
self.type_args = list(type_args)
12851282

12861283

1287-
class Call(_CallOrLoad, Op):
1284+
class Call(_CallOrLoad, DataflowOp):
12881285
"""Call a function inside a dataflow graph. Connects to :class:`FuncDefn` or
12891286
:class:`FuncDecl` operations.
12901287
@@ -1318,11 +1315,17 @@ def port_kind(self, port: InPort | OutPort) -> tys.Kind:
13181315
case InPort(_, offset) if offset == self._function_port_offset():
13191316
return tys.FunctionKind(self.signature)
13201317
case _:
1321-
return tys.ValueKind(_sig_port_type(self.instantiation, port))
1318+
return DataflowOp.port_kind(self, port)
13221319

13231320
def name(self) -> str:
13241321
return "Call"
13251322

1323+
def _inputs(self) -> tys.TypeRow:
1324+
return self.instantiation.input
1325+
1326+
def outer_signature(self) -> tys.FunctionType:
1327+
return self.instantiation
1328+
13261329

13271330
@dataclass()
13281331
class CallIndirect(DataflowOp, _PartialOp):
@@ -1408,10 +1411,8 @@ def port_kind(self, port: InPort | OutPort) -> tys.Kind:
14081411
match port:
14091412
case InPort(_, 0):
14101413
return tys.FunctionKind(self.signature)
1411-
case OutPort(_, 0):
1412-
return tys.ValueKind(self.instantiation)
14131414
case _:
1414-
raise self._invalid_port(port)
1415+
return DataflowOp.port_kind(self, port)
14151416

14161417
def name(self) -> str:
14171418
return "LoadFunc"

hugr-py/tests/__snapshots__/test_hugr_build.ambr

Lines changed: 284 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,290 @@
825825

826826
'''
827827
# ---
828+
# name: test_higher_order
829+
'''
830+
digraph {
831+
bgcolor=white margin=0 nodesep=0.15 rankdir="" ranksep=0.1
832+
subgraph cluster0 {
833+
subgraph cluster1 {
834+
2 [label=<
835+
<TABLE BORDER="1" CELLBORDER="0" CELLSPACING="1" CELLPADDING="1"
836+
BGCOLOR="#ACCBF9" COLOR="white">
837+
838+
<TR>
839+
<TD>
840+
<TABLE BORDER="0" CELLBORDER="0">
841+
<TR><TD><FONT POINT-SIZE="11.0" FACE="monospace"
842+
COLOR="black"><B>Input</B></FONT></TD></TR>
843+
</TABLE>
844+
</TD>
845+
</TR>
846+
847+
<TR>
848+
<TD>
849+
<TABLE BORDER="0" CELLBORDER="0" CELLSPACING="3" CELLPADDING="2">
850+
<TR>
851+
<TD BGCOLOR="white" COLOR="#1CADE4" PORT="out.0" BORDER="1"><FONT POINT-SIZE="10.0" FACE="monospace" COLOR="black">0</FONT></TD>
852+
</TR>
853+
</TABLE>
854+
</TD>
855+
</TR>
856+
857+
</TABLE>
858+
> shape=plain]
859+
3 [label=<
860+
<TABLE BORDER="1" CELLBORDER="0" CELLSPACING="1" CELLPADDING="1"
861+
BGCOLOR="#ACCBF9" COLOR="white">
862+
863+
<TR>
864+
<TD>
865+
<TABLE BORDER="0" CELLBORDER="0" CELLSPACING="3" CELLPADDING="2">
866+
<TR>
867+
<TD BGCOLOR="white" COLOR="#1CADE4" PORT="in.0" BORDER="1"><FONT POINT-SIZE="10.0" FACE="monospace" COLOR="black">0</FONT></TD>
868+
</TR>
869+
</TABLE>
870+
</TD>
871+
</TR>
872+
873+
<TR>
874+
<TD>
875+
<TABLE BORDER="0" CELLBORDER="0">
876+
<TR><TD><FONT POINT-SIZE="11.0" FACE="monospace"
877+
COLOR="black"><B>Output</B></FONT></TD></TR>
878+
</TABLE>
879+
</TD>
880+
</TR>
881+
882+
</TABLE>
883+
> shape=plain]
884+
subgraph cluster4 {
885+
5 [label=<
886+
<TABLE BORDER="1" CELLBORDER="0" CELLSPACING="1" CELLPADDING="1"
887+
BGCOLOR="#ACCBF9" COLOR="white">
888+
889+
<TR>
890+
<TD>
891+
<TABLE BORDER="0" CELLBORDER="0">
892+
<TR><TD><FONT POINT-SIZE="11.0" FACE="monospace"
893+
COLOR="black"><B>Input</B></FONT></TD></TR>
894+
</TABLE>
895+
</TD>
896+
</TR>
897+
898+
<TR>
899+
<TD>
900+
<TABLE BORDER="0" CELLBORDER="0" CELLSPACING="3" CELLPADDING="2">
901+
<TR>
902+
<TD BGCOLOR="white" COLOR="#1CADE4" PORT="out.0" BORDER="1"><FONT POINT-SIZE="10.0" FACE="monospace" COLOR="black">0</FONT></TD>
903+
</TR>
904+
</TABLE>
905+
</TD>
906+
</TR>
907+
908+
</TABLE>
909+
> shape=plain]
910+
6 [label=<
911+
<TABLE BORDER="1" CELLBORDER="0" CELLSPACING="1" CELLPADDING="1"
912+
BGCOLOR="#ACCBF9" COLOR="white">
913+
914+
<TR>
915+
<TD>
916+
<TABLE BORDER="0" CELLBORDER="0" CELLSPACING="3" CELLPADDING="2">
917+
<TR>
918+
<TD BGCOLOR="white" COLOR="#1CADE4" PORT="in.0" BORDER="1"><FONT POINT-SIZE="10.0" FACE="monospace" COLOR="black">0</FONT></TD>
919+
</TR>
920+
</TABLE>
921+
</TD>
922+
</TR>
923+
924+
<TR>
925+
<TD>
926+
<TABLE BORDER="0" CELLBORDER="0">
927+
<TR><TD><FONT POINT-SIZE="11.0" FACE="monospace"
928+
COLOR="black"><B>Output</B></FONT></TD></TR>
929+
</TABLE>
930+
</TD>
931+
</TR>
932+
933+
</TABLE>
934+
> shape=plain]
935+
7 [label=<
936+
<TABLE BORDER="1" CELLBORDER="0" CELLSPACING="1" CELLPADDING="1"
937+
BGCOLOR="#ACCBF9" COLOR="white">
938+
939+
<TR>
940+
<TD>
941+
<TABLE BORDER="0" CELLBORDER="0">
942+
<TR><TD><FONT POINT-SIZE="11.0" FACE="monospace"
943+
COLOR="black"><B>Const(Function(body=Hugr(module_root=Node(0), entrypoint=Node(4), _nodes=[NodeData(op=Module(), parent=None, metadata={}), NodeData(op=FuncDefn(f_name='main', inputs=[Qubit], params=[]), parent=Node(0), metadata={}), NodeData(op=Input(types=[Qubit]), parent=Node(1), metadata={}), NodeData(op=Output(), parent=Node(1), metadata={}), NodeData(op=DFG(inputs=[Qubit]), parent=Node(1), metadata={}), NodeData(op=Input(types=[Qubit]), parent=Node(4), metadata={}), NodeData(op=Output(), parent=Node(4), metadata={}), NodeData(op=Noop(Qubit), parent=Node(4), metadata={})], _links=BiMap({_SubPort(port=OutPort(Node(2), 0), sub_offset=0): _SubPort(port=InPort(Node(4), 0), sub_offset=0), _SubPort(port=OutPort(Node(5), 0), sub_offset=0): _SubPort(port=InPort(Node(7), 0), sub_offset=0), _SubPort(port=OutPort(Node(7), 0), sub_offset=0): _SubPort(port=InPort(Node(6), 0), sub_offset=0), _SubPort(port=OutPort(Node(4), 0), sub_offset=0): _SubPort(port=InPort(Node(3), 0), sub_offset=0)}), _free_nodes=[])))</B></FONT></TD></TR>
944+
</TABLE>
945+
</TD>
946+
</TR>
947+
948+
<TR>
949+
<TD>
950+
<TABLE BORDER="0" CELLBORDER="0" CELLSPACING="3" CELLPADDING="2">
951+
<TR>
952+
<TD BGCOLOR="white" COLOR="#1CADE4" PORT="out.0" BORDER="1"><FONT POINT-SIZE="10.0" FACE="monospace" COLOR="black">0</FONT></TD>
953+
</TR>
954+
</TABLE>
955+
</TD>
956+
</TR>
957+
958+
</TABLE>
959+
> shape=plain]
960+
8 [label=<
961+
<TABLE BORDER="1" CELLBORDER="0" CELLSPACING="1" CELLPADDING="1"
962+
BGCOLOR="#ACCBF9" COLOR="white">
963+
964+
<TR>
965+
<TD>
966+
<TABLE BORDER="0" CELLBORDER="0" CELLSPACING="3" CELLPADDING="2">
967+
<TR>
968+
<TD BGCOLOR="white" COLOR="#1CADE4" PORT="in.0" BORDER="1"><FONT POINT-SIZE="10.0" FACE="monospace" COLOR="black">0</FONT></TD>
969+
</TR>
970+
</TABLE>
971+
</TD>
972+
</TR>
973+
974+
<TR>
975+
<TD>
976+
<TABLE BORDER="0" CELLBORDER="0">
977+
<TR><TD><FONT POINT-SIZE="11.0" FACE="monospace"
978+
COLOR="black"><B>LoadConst</B></FONT></TD></TR>
979+
</TABLE>
980+
</TD>
981+
</TR>
982+
983+
<TR>
984+
<TD>
985+
<TABLE BORDER="0" CELLBORDER="0" CELLSPACING="3" CELLPADDING="2">
986+
<TR>
987+
<TD BGCOLOR="white" COLOR="#1CADE4" PORT="out.0" BORDER="1"><FONT POINT-SIZE="10.0" FACE="monospace" COLOR="black">0</FONT></TD>
988+
</TR>
989+
</TABLE>
990+
</TD>
991+
</TR>
992+
993+
</TABLE>
994+
> shape=plain]
995+
9 [label=<
996+
<TABLE BORDER="1" CELLBORDER="0" CELLSPACING="1" CELLPADDING="1"
997+
BGCOLOR="#ACCBF9" COLOR="white">
998+
999+
<TR>
1000+
<TD>
1001+
<TABLE BORDER="0" CELLBORDER="0" CELLSPACING="3" CELLPADDING="2">
1002+
<TR>
1003+
<TD BGCOLOR="white" COLOR="#1CADE4" PORT="in.0" BORDER="1"><FONT POINT-SIZE="10.0" FACE="monospace" COLOR="black">0</FONT></TD><TD BGCOLOR="white" COLOR="#1CADE4" PORT="in.1" BORDER="1"><FONT POINT-SIZE="10.0" FACE="monospace" COLOR="black">1</FONT></TD>
1004+
</TR>
1005+
</TABLE>
1006+
</TD>
1007+
</TR>
1008+
1009+
<TR>
1010+
<TD>
1011+
<TABLE BORDER="0" CELLBORDER="0">
1012+
<TR><TD><FONT POINT-SIZE="11.0" FACE="monospace"
1013+
COLOR="black"><B>CallIndirect</B></FONT></TD></TR>
1014+
</TABLE>
1015+
</TD>
1016+
</TR>
1017+
1018+
<TR>
1019+
<TD>
1020+
<TABLE BORDER="0" CELLBORDER="0" CELLSPACING="3" CELLPADDING="2">
1021+
<TR>
1022+
<TD BGCOLOR="white" COLOR="#1CADE4" PORT="out.0" BORDER="1"><FONT POINT-SIZE="10.0" FACE="monospace" COLOR="black">0</FONT></TD>
1023+
</TR>
1024+
</TABLE>
1025+
</TD>
1026+
</TR>
1027+
1028+
</TABLE>
1029+
> shape=plain]
1030+
4 [label=<
1031+
<TABLE BORDER="2" CELLBORDER="0" CELLSPACING="1" CELLPADDING="1"
1032+
BGCOLOR="#1CADE4" COLOR="#F4A261">
1033+
1034+
<TR>
1035+
<TD>
1036+
<TABLE BORDER="0" CELLBORDER="0" CELLSPACING="3" CELLPADDING="2">
1037+
<TR>
1038+
<TD BGCOLOR="white" COLOR="#1CADE4" PORT="in.0" BORDER="1"><FONT POINT-SIZE="10.0" FACE="monospace" COLOR="black">0</FONT></TD>
1039+
</TR>
1040+
</TABLE>
1041+
</TD>
1042+
</TR>
1043+
1044+
<TR>
1045+
<TD>
1046+
<TABLE BORDER="0" CELLBORDER="0">
1047+
<TR><TD><FONT POINT-SIZE="11.0" FACE="monospace"
1048+
COLOR="black"><B><b>[DFG]</b></B></FONT></TD></TR>
1049+
</TABLE>
1050+
</TD>
1051+
</TR>
1052+
1053+
<TR>
1054+
<TD>
1055+
<TABLE BORDER="0" CELLBORDER="0" CELLSPACING="3" CELLPADDING="2">
1056+
<TR>
1057+
<TD BGCOLOR="white" COLOR="#1CADE4" PORT="out.0" BORDER="1"><FONT POINT-SIZE="10.0" FACE="monospace" COLOR="black">0</FONT></TD>
1058+
</TR>
1059+
</TABLE>
1060+
</TD>
1061+
</TR>
1062+
1063+
</TABLE>
1064+
> shape=plain]
1065+
color="#F4A261" label="" margin=10 penwidth=2
1066+
}
1067+
1 [label=<
1068+
<TABLE BORDER="1" CELLBORDER="0" CELLSPACING="1" CELLPADDING="1"
1069+
BGCOLOR="#1CADE4" COLOR="#1CADE4">
1070+
1071+
<TR>
1072+
<TD>
1073+
<TABLE BORDER="0" CELLBORDER="0">
1074+
<TR><TD><FONT POINT-SIZE="11.0" FACE="monospace"
1075+
COLOR="black"><B>FuncDefn(main)</B></FONT></TD></TR>
1076+
</TABLE>
1077+
</TD>
1078+
</TR>
1079+
1080+
</TABLE>
1081+
> shape=plain]
1082+
color="#1CADE4" label="" margin=10 penwidth=1
1083+
}
1084+
0 [label=<
1085+
<TABLE BORDER="1" CELLBORDER="0" CELLSPACING="1" CELLPADDING="1"
1086+
BGCOLOR="#1CADE4" COLOR="#1CADE4">
1087+
1088+
<TR>
1089+
<TD>
1090+
<TABLE BORDER="0" CELLBORDER="0">
1091+
<TR><TD><FONT POINT-SIZE="11.0" FACE="monospace"
1092+
COLOR="black"><B>Module</B></FONT></TD></TR>
1093+
</TABLE>
1094+
</TD>
1095+
</TR>
1096+
1097+
</TABLE>
1098+
> shape=plain]
1099+
color="#1CADE4" label="" margin=10 penwidth=1
1100+
}
1101+
2:"out.0" -> 4:"in.0" [label=Qubit arrowhead=none arrowsize=1.0 color="#1CADE4" fontcolor=black fontname=monospace fontsize=9 penwidth=1.5]
1102+
7:"out.0" -> 8:"in.0" [label="" arrowhead=none arrowsize=1.0 color="#77CEEF" fontcolor=black fontname=monospace fontsize=9 penwidth=1.5]
1103+
8:"out.0" -> 9:"in.0" [label="Qubit -> Qubit" arrowhead=none arrowsize=1.0 color="#1CADE4" fontcolor=black fontname=monospace fontsize=9 penwidth=1.5]
1104+
5:"out.0" -> 9:"in.1" [label=Qubit arrowhead=none arrowsize=1.0 color="#1CADE4" fontcolor=black fontname=monospace fontsize=9 penwidth=1.5]
1105+
5:"out.-1" -> 8:"in.-1" [label="" arrowhead=none arrowsize=1.0 color=black fontcolor=black fontname=monospace fontsize=9 penwidth=1.5]
1106+
9:"out.0" -> 6:"in.0" [label=Qubit arrowhead=none arrowsize=1.0 color="#1CADE4" fontcolor=black fontname=monospace fontsize=9 penwidth=1.5]
1107+
4:"out.0" -> 3:"in.0" [label=Qubit arrowhead=none arrowsize=1.0 color="#1CADE4" fontcolor=black fontname=monospace fontsize=9 penwidth=1.5]
1108+
}
1109+
1110+
'''
1111+
# ---
8281112
# name: test_insert_nested
8291113
'''
8301114
digraph {

0 commit comments

Comments
 (0)