-
Notifications
You must be signed in to change notification settings - Fork 7
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(hugr-py): IndexDfg
builder for appending operations by index
#1256
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,210 @@ | ||||||||||||||||
"""Dfg builder that allows tracking a set of wires and appending operations by index.""" | ||||||||||||||||
|
||||||||||||||||
from collections.abc import Iterable | ||||||||||||||||
|
||||||||||||||||
from hugr import tys | ||||||||||||||||
from hugr.dfg import Dfg | ||||||||||||||||
from hugr.node_port import Node, Wire | ||||||||||||||||
from hugr.ops import Command, ComWire | ||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
class IndexDfg(Dfg): | ||||||||||||||||
"""Dfg builder to append operations to wires by index. | ||||||||||||||||
|
||||||||||||||||
Args: | ||||||||||||||||
*input_types: Input types of the Dfg. | ||||||||||||||||
track_inputs: Whether to track the input wires. | ||||||||||||||||
|
||||||||||||||||
Examples: | ||||||||||||||||
>>> dfg = IndexDfg(tys.Bool, tys.Unit, track_inputs=True) | ||||||||||||||||
>>> dfg.tracked | ||||||||||||||||
[OutPort(Node(1), 0), OutPort(Node(1), 1)] | ||||||||||||||||
""" | ||||||||||||||||
|
||||||||||||||||
#: Tracked wires. None if index is no longer tracked. | ||||||||||||||||
tracked: list[Wire | None] | ||||||||||||||||
|
||||||||||||||||
def __init__(self, *input_types: tys.Type, track_inputs: bool = False) -> None: | ||||||||||||||||
super().__init__(*input_types) | ||||||||||||||||
self.tracked = list(self.inputs()) if track_inputs else [] | ||||||||||||||||
|
||||||||||||||||
def track_wire(self, wire: Wire) -> int: | ||||||||||||||||
"""Add a wire from this DFG to the tracked wires, and return its index. | ||||||||||||||||
|
||||||||||||||||
Args: | ||||||||||||||||
wire: Wire to track. | ||||||||||||||||
|
||||||||||||||||
Returns: | ||||||||||||||||
Index of the tracked wire. | ||||||||||||||||
|
||||||||||||||||
Examples: | ||||||||||||||||
>>> dfg = IndexDfg(tys.Bool, tys.Unit) | ||||||||||||||||
>>> dfg.track_wire(dfg.inputs()[0]) | ||||||||||||||||
0 | ||||||||||||||||
""" | ||||||||||||||||
self.tracked.append(wire) | ||||||||||||||||
return len(self.tracked) - 1 | ||||||||||||||||
|
||||||||||||||||
def untrack_wire(self, index: int) -> Wire: | ||||||||||||||||
"""Untrack a wire by index and return it. | ||||||||||||||||
|
||||||||||||||||
Args: | ||||||||||||||||
index: Index of the wire to untrack. | ||||||||||||||||
|
||||||||||||||||
Returns: | ||||||||||||||||
Wire that was untracked. | ||||||||||||||||
|
||||||||||||||||
Raises: | ||||||||||||||||
IndexError: If the index is not a tracked wire. | ||||||||||||||||
|
||||||||||||||||
Examples: | ||||||||||||||||
>>> dfg = IndexDfg(tys.Bool, tys.Unit) | ||||||||||||||||
>>> w = dfg.inputs()[0] | ||||||||||||||||
>>> idx = dfg.track_wire(w) | ||||||||||||||||
>>> dfg.untrack_wire(idx) == w | ||||||||||||||||
True | ||||||||||||||||
""" | ||||||||||||||||
w = self.get_wire(index) | ||||||||||||||||
self.tracked[index] = None | ||||||||||||||||
return w | ||||||||||||||||
|
||||||||||||||||
def track_wires(self, wires: Iterable[Wire]) -> list[int]: | ||||||||||||||||
"""Set a list of wires to be tracked and return their indices. | ||||||||||||||||
|
||||||||||||||||
Args: | ||||||||||||||||
wires: Wires to track. | ||||||||||||||||
|
||||||||||||||||
Returns: | ||||||||||||||||
List of indices of the tracked wires. | ||||||||||||||||
|
||||||||||||||||
Examples: | ||||||||||||||||
>>> dfg = IndexDfg(tys.Bool, tys.Unit) | ||||||||||||||||
>>> dfg.track_wires(dfg.inputs()) | ||||||||||||||||
[0, 1] | ||||||||||||||||
""" | ||||||||||||||||
return [self.track_wire(w) for w in wires] | ||||||||||||||||
|
||||||||||||||||
def track_inputs(self) -> list[int]: | ||||||||||||||||
"""Track all input wires and return their indices. | ||||||||||||||||
|
||||||||||||||||
Returns: | ||||||||||||||||
List of indices of the tracked input wires. | ||||||||||||||||
|
||||||||||||||||
Examples: | ||||||||||||||||
>>> dfg = IndexDfg(tys.Bool, tys.Unit) | ||||||||||||||||
>>> dfg.track_inputs() | ||||||||||||||||
[0, 1] | ||||||||||||||||
""" | ||||||||||||||||
return self.track_wires(self.inputs()) | ||||||||||||||||
|
||||||||||||||||
def get_wire(self, index: int) -> Wire: | ||||||||||||||||
"""Get the tracked wire at the given index. | ||||||||||||||||
|
||||||||||||||||
Args: | ||||||||||||||||
index: Index of the tracked wire. | ||||||||||||||||
|
||||||||||||||||
Raises: | ||||||||||||||||
IndexError: If the index is not a tracked wire. | ||||||||||||||||
|
||||||||||||||||
Returns: | ||||||||||||||||
Tracked wire | ||||||||||||||||
|
||||||||||||||||
Examples: | ||||||||||||||||
>>> dfg = IndexDfg(tys.Bool, tys.Unit, track_inputs=True) | ||||||||||||||||
>>> dfg.get_wire(0) == dfg.inputs()[0] | ||||||||||||||||
True | ||||||||||||||||
""" | ||||||||||||||||
try: | ||||||||||||||||
tracked = self.tracked[index] | ||||||||||||||||
except IndexError: | ||||||||||||||||
tracked = None | ||||||||||||||||
if tracked is None: | ||||||||||||||||
msg = f"Index {index} not a tracked wire." | ||||||||||||||||
raise IndexError(msg) | ||||||||||||||||
return tracked | ||||||||||||||||
|
||||||||||||||||
def append(self, com: Command) -> Node: | ||||||||||||||||
"""Add a command to the DFG. | ||||||||||||||||
|
||||||||||||||||
Any incoming :class:`Wire <hugr.node_port.Wire>` will | ||||||||||||||||
be connected directly, while any integer will be treated as a reference | ||||||||||||||||
to the tracked wire at that index. | ||||||||||||||||
|
||||||||||||||||
Any tracked wires will be updated to the output of the new node at the same port | ||||||||||||||||
as the incoming index. | ||||||||||||||||
|
||||||||||||||||
Args: | ||||||||||||||||
com: Command to append. | ||||||||||||||||
|
||||||||||||||||
Returns: | ||||||||||||||||
The new node. | ||||||||||||||||
|
||||||||||||||||
Examples: | ||||||||||||||||
>>> dfg = IndexDfg(tys.Bool, track_inputs=True) | ||||||||||||||||
>>> dfg.tracked | ||||||||||||||||
[OutPort(Node(1), 0)] | ||||||||||||||||
>>> dfg.append(ops.Noop()(0)) | ||||||||||||||||
Node(3) | ||||||||||||||||
>>> dfg.tracked | ||||||||||||||||
[OutPort(Node(3), 0)] | ||||||||||||||||
""" | ||||||||||||||||
wires = self._to_wires(com.incoming) | ||||||||||||||||
n = self.add_op(com.op, *wires) | ||||||||||||||||
|
||||||||||||||||
for port_offset, com_wire in enumerate(com.incoming): | ||||||||||||||||
if isinstance(com_wire, int): | ||||||||||||||||
tracked_idx = com_wire | ||||||||||||||||
else: | ||||||||||||||||
continue | ||||||||||||||||
# update tracked wires to matching port outputs of new node | ||||||||||||||||
self.tracked[tracked_idx] = n.out(port_offset) | ||||||||||||||||
|
||||||||||||||||
return n | ||||||||||||||||
|
||||||||||||||||
def _to_wires(self, in_wires: Iterable[ComWire]) -> Iterable[Wire]: | ||||||||||||||||
return (self.get_wire(inc) if isinstance(inc, int) else inc for inc in in_wires) | ||||||||||||||||
|
||||||||||||||||
def extend(self, coms: Iterable[Command]) -> list[Node]: | ||||||||||||||||
"""Add a series of commands to the DFG. | ||||||||||||||||
|
||||||||||||||||
Shorthand for calling :meth:`append` on each command in `coms`. | ||||||||||||||||
|
||||||||||||||||
Args: | ||||||||||||||||
coms: Commands to append. | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
|
||||||||||||||||
Returns: | ||||||||||||||||
List of the new nodes in the same order as the commands. | ||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
Examples: | ||||||||||||||||
>>> dfg = IndexDfg(tys.Bool, tys.Unit, track_inputs=True) | ||||||||||||||||
>>> dfg.extend([ops.Noop()(0), ops.Noop()(1)]) | ||||||||||||||||
[Node(3), Node(4)] | ||||||||||||||||
""" | ||||||||||||||||
return [self.append(com) for com in coms] | ||||||||||||||||
|
||||||||||||||||
def set_indexed_outputs(self, *in_wires: ComWire) -> None: | ||||||||||||||||
"""Set the Dfg outputs, using either :class:`Wire <hugr.node_port.Wire>` or | ||||||||||||||||
indices to tracked wires. | ||||||||||||||||
|
||||||||||||||||
Args: | ||||||||||||||||
*in_wires: Wires/indices to set as outputs. | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
|
||||||||||||||||
Examples: | ||||||||||||||||
>>> dfg = IndexDfg(tys.Bool, tys.Unit) | ||||||||||||||||
>>> (b, i) = dfg.inputs() | ||||||||||||||||
>>> dfg.track_wire(b) | ||||||||||||||||
0 | ||||||||||||||||
>>> dfg.set_indexed_outputs(0, i) | ||||||||||||||||
""" | ||||||||||||||||
self.set_outputs(*self._to_wires(in_wires)) | ||||||||||||||||
|
||||||||||||||||
def set_tracked_outputs(self) -> None: | ||||||||||||||||
"""Set the Dfg outputs to the tracked wires. | ||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
Examples: | ||||||||||||||||
>>> dfg = IndexDfg(tys.Bool, tys.Unit, track_inputs=True) | ||||||||||||||||
>>> dfg.set_tracked_outputs() | ||||||||||||||||
""" | ||||||||||||||||
self.set_outputs(*(w for w in self.tracked if w is not None)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.