-
Notifications
You must be signed in to change notification settings - Fork 59
feat: add static wire connectivity between operators and measurements #2260
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
Open
andrijapau
wants to merge
567
commits into
feature/graph-visualization
Choose a base branch
from
feature/connect-nodes
base: feature/graph-visualization
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+459
−60
Open
Changes from 250 commits
Commits
Show all changes
567 commits
Select commit
Hold shift + click to select a range
434c500
fix
andrijapau cfa5f3b
add more tests
andrijapau a62f2b1
add dev comments
andrijapau d9026ef
more testing
andrijapau faf338f
move things around
andrijapau 5d03a81
add more qol
andrijapau 56e4756
Update frontend/catalyst/python_interface/visualization/construct_cir…
andrijapau c95c45f
Update frontend/catalyst/python_interface/visualization/construct_cir…
andrijapau 18aa30b
Update frontend/catalyst/python_interface/visualization/construct_cir…
andrijapau 929bd23
add flattened ifop ability
andrijapau 709e961
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau b51bb7b
fix
andrijapau 89f45f3
fix
andrijapau b45b2f0
fix
andrijapau 2028fa7
fix
andrijapau 4e994e6
simplify PR by making it for ...
andrijapau e590847
format
andrijapau eb6a0c3
fix tests
andrijapau 7eb31b6
format
andrijapau 8e787c9
fix logic in flatten if op
andrijapau 8b6287f
format
andrijapau 83ebb18
fix type hinting
andrijapau 6d84d36
add dev comment
andrijapau 1d8ac14
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau 3c42841
more deviceop tests
andrijapau e319aae
Apply suggestion from @andrijapau
andrijapau 1c9c857
re-order changelog
andrijapau 093a5d5
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau 2ca7bb8
add cl entry
andrijapau 9d724bf
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau 61b70d9
add smaller fontsize for the conditional bit
andrijapau 25aa04b
cl
andrijapau d613c1b
Apply suggestion from @andrijapau
andrijapau 3a2ab18
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau e9c69d8
format
andrijapau 73c8a0f
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau f47b1a5
fix id to uid
andrijapau 98c9175
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau 5e83fe4
fix
andrijapau 3d80d1a
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau d5a0611
Apply suggestion from @andrijapau
andrijapau 0cd2f7d
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau 858f40e
add good static connectivity
andrijapau 3d0af6a
rename
andrijapau 98bf7ea
clean-up
andrijapau 818448c
clean up
andrijapau b924c43
format
andrijapau 046196f
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau 08082d9
clean-up
andrijapau f23b550
refactor
andrijapau 437c7d8
make sure data is not carried over between qnodes
andrijapau 39d6ea6
format
andrijapau 089cb14
both single and multi qnode have qjit bounding box
andrijapau 2a2d55c
Apply suggestion from @andrijapau
andrijapau 220c6d5
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau f7261a9
add more detail to the for loop label
andrijapau a8cd49c
format
andrijapau 44bf9d3
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau 9c907c4
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau 70b0d93
fix test
andrijapau d983e97
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau 68c6ec0
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau f25dd2a
Apply suggestion from @andrijapau
andrijapau cb05921
use counter instead of builtin id function
andrijapau 1883fb3
whoops
andrijapau 5e2d456
Apply suggestion from @andrijapau
andrijapau 36b5f7d
Apply suggestion from @andrijapau
andrijapau ee15b67
Apply suggestion from @andrijapau
andrijapau 16e003d
add two counters
andrijapau 23561f7
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau d5ccaf7
use the counters for uid
andrijapau 1dd9f38
update tests to be simpler
andrijapau 6a9b01d
fix tests
andrijapau a2ac9ff
minor fix
andrijapau 0dbe4fe
foramt
andrijapau d92aea4
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau 8afc67a
Apply suggestion from @andrijapau
andrijapau c0d4d67
update tests
andrijapau 9706967
update tests
andrijapau 5e62c31
format
andrijapau 1474a66
whoops fix test
andrijapau 82430af
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau ced6f75
format
andrijapau 649aed2
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau 9c74b6a
update id
andrijapau 6d46b94
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau 56c70c9
update logic
andrijapau 5641e68
fix test
andrijapau 4ffc9d3
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau a544727
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau 22c8ae1
add more nested testing
andrijapau ab7c471
format
andrijapau dd1df44
adjust test
andrijapau 57409bd
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau 336d2e5
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau 9996684
fix test
andrijapau 303fb03
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau c262df9
format
andrijapau 05df88c
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau d66ff68
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau 3175efa
add tests
andrijapau 194b357
fix get_nodes -> nodes
andrijapau a9125f5
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau aea60d0
add test skeeltons
andrijapau 506f839
fix labels
andrijapau f67802f
fix labels
andrijapau ad35efc
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau 75bdd32
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau fac6150
fix measure
andrijapau 6d3c256
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau 9bdf8da
fix globalphase test
andrijapau df222e2
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau b9479b1
fix sample test
andrijapau e38737c
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau 60e83c4
fix sample
andrijapau ebd108e
format
andrijapau bfd1d10
add dev comment
andrijapau aec9f83
fix code
andrijapau aa7dd72
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau c36a43e
fix naming
andrijapau 6c1d825
add expected error messages
andrijapau 4dddd74
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau 7b56f89
fix test for probs
andrijapau 1712a27
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau 4f83355
format
andrijapau 67759dd
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau 9f6a131
add compileerror
andrijapau 9855985
update
andrijapau 4f0a8f4
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau 30db67e
Merge branch 'feature/graph-visualization' into feature/control-flow-…
andrijapau 2fe2074
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau d069fef
fix
andrijapau b65ae16
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau e29f546
bring back ssavalue
andrijapau b34d8e2
format
andrijapau aa1d90c
format
andrijapau d3fdfea
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau 5d9f21c
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau 27ff971
remove random import
andrijapau 1a0ac68
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau 19b326a
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau 5e3a6d2
re-work node labels to be records with ports
andrijapau a25f7b4
format
andrijapau 02d4a55
fix tests
andrijapau a9a89a9
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau b58602d
format
andrijapau 68c2488
whoops
andrijapau 7237a3f
move mid measure to an operator
andrijapau d28eb63
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau fc3ea2c
add connectivity to MCMs
andrijapau 6410aae
fix test
andrijapau 7cbfe6a
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau 20efb54
get rid of unnecessary function
andrijapau 81e9fe5
Update frontend/catalyst/python_interface/visualization/construct_cir…
andrijapau dcf59ae
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau 7eb1da4
Update frontend/test/pytest/python_interface/visualization/test_const…
andrijapau f7daa8a
Update doc/releases/changelog-dev.md
andrijapau 80da43c
Update frontend/test/pytest/python_interface/visualization/test_const…
andrijapau 5fc9cf1
fix bug and add test coverage
andrijapau ef5550f
fix test and format
andrijapau 5476e6d
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau dfde76d
format
andrijapau ecddff7
fix
andrijapau c2bd854
remove ssa condition, not needed for now
andrijapau 4176d03
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau f8b2922
refactor a bit
andrijapau 89dad36
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau e58cc4e
add another test
andrijapau 1877cf9
whoops
andrijapau 8643a24
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau c35b8c1
Apply suggestion from @andrijapau
andrijapau 5728859
Apply suggestion from @andrijapau
andrijapau 5345dfc
format
andrijapau 04ee8e2
update how the node labels look
andrijapau 74e8358
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau 77e63d9
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau 63ea212
Apply suggestion from @andrijapau
andrijapau be6a32b
Apply suggestion from @andrijapau
andrijapau 3e455bb
whoops
andrijapau d143ed0
fix test
andrijapau d8c1b7a
improve logic
andrijapau bafd278
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau 44643f7
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau 284f56b
Update frontend/catalyst/python_interface/visualization/construct_cir…
andrijapau 8b2ec3c
remove redundant label
andrijapau 488de58
add dev comment
andrijapau 490949b
format
andrijapau 4430396
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau 3494f17
improve function naming
andrijapau 0f49fb3
add more tests
andrijapau d682702
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau 9e41561
Apply suggestion from @andrijapau
andrijapau 7c4c552
fix tests
andrijapau 5451853
fix test
andrijapau c8f39fb
adjust pydotdagbuilder to have orthogonal edges
andrijapau 1d755ad
revert dag builder changes
andrijapau 3a8628b
revert
andrijapau a128861
revert
andrijapau 435aabb
add basic probs state test
andrijapau fee7e1b
fix labels for measurements
andrijapau c5c6e28
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau e15bdc0
add record
andrijapau 895105d
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau e735e37
format
andrijapau 2071360
Merge branch 'feature/graph-visualization' into feature/control-flow-…
andrijapau c86043f
remove node label
andrijapau 5aeba5e
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau d542968
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau ea8c9b7
add labeljust
andrijapau d224c0c
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau 5d27922
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau 8392c2b
remove comment
andrijapau 2fc5b7c
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau 80798f9
fix cluster
andrijapau cb1e531
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau bf3607f
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau 1f48498
fix
andrijapau f1b6a6f
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau ac5ffe7
Apply suggestion from @andrijapau
andrijapau 5f3a208
fix test
andrijapau 27cc772
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau 90e6588
fix
andrijapau 2983d67
fix
andrijapau d914ed0
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau 24ffa25
Merge branch 'feature/graph-visualization' into feature/visualize-nodes
andrijapau 822f929
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau c9ec867
fix
andrijapau 37a4f18
Merge branch 'feature/visualize-nodes' into feature/connect-nodes
andrijapau bd178e1
Merge branch 'feature/graph-visualization' into feature/connect-nodes
andrijapau c6e9eb0
Merge branch 'feature/graph-visualization' into feature/connect-nodes
andrijapau a094ca1
Merge branch 'feature/graph-visualization' into feature/connect-nodes
andrijapau e18cd6f
fix test
andrijapau d8c2ab8
Merge branch 'feature/graph-visualization' into feature/connect-nodes
andrijapau 3620b5d
add more test
andrijapau 5dc5437
format
andrijapau becf7ee
merge all measurements into one handler
andrijapau 9216c4b
minor clean-up
andrijapau ccd9c65
Add back comment
andrijapau 17d2f5d
Apply suggestion from @andrijapau
andrijapau e05f354
format
andrijapau c591c3a
add more tests
andrijapau 94ec4af
Apply suggestion from @andrijapau
andrijapau 9de7f94
add operator connectivity for multi wire
andrijapau 6ba5541
multi wire connectivity for measurements
andrijapau b0e665e
add basic test
andrijapau d54a42a
clean up
andrijapau e203b15
whoops
andrijapau 188dc06
fix
andrijapau ce238c4
fix
andrijapau 2563a88
Trigger CI
andrijapau 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -14,19 +14,24 @@ | |
|
|
||
| """Contains the ConstructCircuitDAG tool for constructing a DAG from an xDSL module.""" | ||
|
|
||
| from collections import defaultdict | ||
| from functools import singledispatch, singledispatchmethod | ||
|
|
||
| from pennylane.measurements import ExpectationMP, MeasurementProcess, ProbabilityMP, VarianceMP | ||
| from pennylane.operation import Operator | ||
| from xdsl.dialects import builtin, func, scf | ||
| from xdsl.ir import Block, Operation, Region, SSAValue | ||
|
|
||
| from catalyst.python_interface.dialects import quantum | ||
| from catalyst.python_interface.inspection.xdsl_conversion import ( | ||
| xdsl_to_qml_measurement, | ||
| xdsl_to_qml_op, | ||
| ) | ||
| from catalyst.python_interface.visualization.dag_builder import DAGBuilder | ||
| from pennylane.measurements import ( | ||
| ExpectationMP, | ||
| MeasurementProcess, | ||
| ProbabilityMP, | ||
| VarianceMP, | ||
| ) | ||
| from pennylane.operation import Operator | ||
| from xdsl.dialects import builtin, func, scf | ||
| from xdsl.ir import Block, Operation, Region, SSAValue | ||
|
|
||
|
|
||
| class ConstructCircuitDAG: | ||
|
|
@@ -50,6 +55,11 @@ def __init__(self, dag_builder: DAGBuilder) -> None: | |
| # Keep track of nesting clusters using a stack | ||
| self._cluster_uid_stack: list[str] = [] | ||
|
|
||
| # Create a map of wire to node uid | ||
| # Keys represent static (int) or dynamic wires (str) | ||
| # Values represent the set of all node uids that are on that wire. | ||
| self._wire_to_node_uids: dict[str | int, set[str]] = defaultdict(set) | ||
|
|
||
| # Use counter internally for UID | ||
| self._node_uid_counter: int = 0 | ||
| self._cluster_uid_counter: int = 0 | ||
|
|
@@ -59,6 +69,7 @@ def _reset(self) -> None: | |
| self._cluster_uid_stack: list[str] = [] | ||
| self._node_uid_counter: int = 0 | ||
| self._cluster_uid_counter: int = 0 | ||
| self._wire_to_node_uids: dict[str | int, set[str]] = defaultdict(set) | ||
|
|
||
| def construct(self, module: builtin.ModuleOp) -> None: | ||
| """Constructs the DAG from the module. | ||
|
|
@@ -98,7 +109,10 @@ def _visit_block(self, block: Block) -> None: | |
| @_visit_operation.register | ||
| def _gate_op( | ||
| self, | ||
| op: quantum.CustomOp | quantum.GlobalPhaseOp | quantum.QubitUnitaryOp | quantum.MultiRZOp, | ||
| op: quantum.CustomOp | ||
| | quantum.GlobalPhaseOp | ||
| | quantum.QubitUnitaryOp | ||
| | quantum.MultiRZOp, | ||
| ) -> None: | ||
| """Generic handler for unitary gates.""" | ||
|
|
||
|
|
@@ -116,6 +130,17 @@ def _gate_op( | |
| ) | ||
| self._node_uid_counter += 1 | ||
|
|
||
| # Search through previous ops found on current wires and connect | ||
| prev_ops = set.union( | ||
| set(), *(self._wire_to_node_uids[wire] for wire in qml_op.wires) | ||
| ) | ||
| for prev_op in prev_ops: | ||
| self.dag_builder.add_edge(prev_op, node_uid) | ||
|
|
||
| # Update affected wires to source from this node UID | ||
| for wire in qml_op.wires: | ||
| self._wire_to_node_uids[wire] = {node_uid} | ||
|
|
||
| @_visit_operation.register | ||
| def _projective_measure_op(self, op: quantum.MeasureOp) -> None: | ||
| """Handler for the single-qubit projective measurement operation.""" | ||
|
|
@@ -134,82 +159,80 @@ def _projective_measure_op(self, op: quantum.MeasureOp) -> None: | |
| ) | ||
| self._node_uid_counter += 1 | ||
|
|
||
| # Search through previous ops found on current wires and connect | ||
| prev_node_uids = set.union( | ||
| set(), *(self._wire_to_node_uids[wire] for wire in meas.wires) | ||
| ) | ||
| for prev_node_uid in prev_node_uids: | ||
| self.dag_builder.add_edge(prev_node_uid, node_uid) | ||
|
|
||
| # Update affected wires to source from this node UID | ||
| for wire in meas.wires: | ||
| self._wire_to_node_uids[wire] = {node_uid} | ||
|
|
||
| # ===================== | ||
| # QUANTUM MEASUREMENTS | ||
| # ===================== | ||
|
|
||
| @_visit_operation.register | ||
| def _state_op(self, op: quantum.StateOp) -> None: | ||
| """Handler for the terminal state measurement operation.""" | ||
|
|
||
| # Create PennyLane instance | ||
| meas = xdsl_to_qml_measurement(op) | ||
|
|
||
| # Add node to current cluster | ||
| node_uid = f"node{self._node_uid_counter}" | ||
| self.dag_builder.add_node( | ||
| uid=node_uid, | ||
| label=get_label(meas), | ||
| cluster_uid=self._cluster_uid_stack[-1], | ||
| fillcolor="lightpink", | ||
| color="lightpink3", | ||
| # NOTE: "record" allows us to use ports (https://graphviz.org/doc/info/shapes.html#record) | ||
| shape="record", | ||
| ) | ||
| self._node_uid_counter += 1 | ||
|
|
||
| @_visit_operation.register | ||
| def _expval_and_var_ops( | ||
| def _handle_measurements( | ||
| self, | ||
| op: quantum.ExpvalOp | quantum.VarianceOp, | ||
| op: quantum.StateOp | ||
| | quantum.ExpvalOp | ||
| | quantum.VarianceOp | ||
| | quantum.SampleOp | ||
| | quantum.ProbsOp, | ||
| ) -> None: | ||
| """Handler for statistical measurement operations.""" | ||
| """Handler for all quantum measurement operations.""" | ||
|
|
||
| # Create PennyLane instance | ||
| obs_op = op.obs.owner | ||
| meas = xdsl_to_qml_measurement(op, xdsl_to_qml_measurement(obs_op)) | ||
| prev_wires = [] | ||
| meas = None | ||
|
|
||
| # Add node to current cluster | ||
| node_uid = f"node{self._node_uid_counter}" | ||
| self.dag_builder.add_node( | ||
| uid=node_uid, | ||
| label=get_label(meas), | ||
| cluster_uid=self._cluster_uid_stack[-1], | ||
| fillcolor="lightpink", | ||
| color="lightpink3", | ||
| # NOTE: "record" allows us to use ports (https://graphviz.org/doc/info/shapes.html#record) | ||
| shape="record", | ||
| ) | ||
| self._node_uid_counter += 1 | ||
| match op: | ||
| case quantum.StateOp(): | ||
| meas = xdsl_to_qml_measurement(op) | ||
| # NOTE: state can only handle all wires | ||
| prev_wires = self._wire_to_node_uids.keys() | ||
|
|
||
| @_visit_operation.register | ||
| def _sample_counts_probs_ops( | ||
| self, | ||
| op: quantum.SampleOp | quantum.ProbsOp, | ||
| ) -> None: | ||
| """Handler for sample operations.""" | ||
| case quantum.ExpvalOp() | quantum.VarianceOp(): | ||
| obs_op = op.obs.owner | ||
| meas = xdsl_to_qml_measurement(op, xdsl_to_qml_measurement(obs_op)) | ||
| prev_wires = meas.wires.labels | ||
|
|
||
| # Create PennyLane instance | ||
| obs_op = op.obs.owner | ||
| case quantum.SampleOp() | quantum.ProbsOp(): | ||
| obs_op = op.obs.owner | ||
|
|
||
| # TODO: This doesn't logically make sense, but quantum.compbasis | ||
| # is obs_op and function below just pulls out the static wires | ||
| wires = xdsl_to_qml_measurement(obs_op) | ||
| meas = xdsl_to_qml_measurement(op, wires=None if wires == [] else wires) | ||
|
Contributor
Author
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. Bit of a rough diff but I essentially merged all handlers into one. |
||
| # TODO: This doesn't logically make sense, but quantum.compbasis | ||
| # is obs_op and function below just pulls out the static wires | ||
| wires = xdsl_to_qml_measurement(obs_op) | ||
| meas = xdsl_to_qml_measurement(op, wires=None if wires == [] else wires) | ||
|
|
||
| if wires == []: | ||
| # If no wires specified, connect to all seen current wires | ||
| prev_wires = self._wire_to_node_uids.keys() | ||
| else: | ||
| # Use the specific wires from the observable | ||
| prev_wires = wires | ||
|
|
||
| case _: | ||
| return | ||
|
|
||
| # Add node to current cluster | ||
| node_uid = f"node{self._node_uid_counter}" | ||
| self.dag_builder.add_node( | ||
| uid=node_uid, | ||
| label=get_label(meas), | ||
| cluster_uid=self._cluster_uid_stack[-1], | ||
| fillcolor="lightpink", | ||
| color="lightpink3", | ||
| # NOTE: "record" allows us to use ports (https://graphviz.org/doc/info/shapes.html#record) | ||
| shape="record", | ||
| ) | ||
| self._node_uid_counter += 1 | ||
|
|
||
| for wire in prev_wires: | ||
| if wire in self._wire_to_node_uids: | ||
| for seen_node in self._wire_to_node_uids[wire]: | ||
| self.dag_builder.add_edge(seen_node, node_uid, color="lightpink3") | ||
|
|
||
| # ============= | ||
| # CONTROL FLOW | ||
| # ============= | ||
|
|
@@ -263,6 +286,10 @@ def _if_op(self, operation: scf.IfOp): | |
| self._cluster_uid_stack.append(uid) | ||
| self._cluster_uid_counter += 1 | ||
|
|
||
| # Save wires state before all of the branches | ||
| wire_map_before = self._wire_to_node_uids.copy() | ||
| region_wire_maps: list[dict[int | str, set[str]]] = [] | ||
|
|
||
| # Loop through each branch and visualize as a cluster | ||
| flattened_if_op: list[Region] = _flatten_if_op(operation) | ||
| num_regions = len(flattened_if_op) | ||
|
|
@@ -284,16 +311,42 @@ def _if_op(self, operation: scf.IfOp): | |
| self._cluster_uid_stack.append(uid) | ||
| self._cluster_uid_counter += 1 | ||
|
|
||
| # Make fresh wire map before going into region | ||
| self._wire_to_node_uids = wire_map_before.copy() | ||
|
|
||
| # Go recursively into the branch to process internals | ||
| self._visit_region(region) | ||
|
|
||
| # Update branch wire maps | ||
| if self._wire_to_node_uids != wire_map_before: | ||
| region_wire_maps.append(self._wire_to_node_uids) | ||
|
|
||
| # Pop branch cluster after processing to ensure | ||
| # logical branches are treated as 'parallel' | ||
| self._cluster_uid_stack.pop() | ||
|
|
||
| # Pop IfOp cluster before leaving this handler | ||
| self._cluster_uid_stack.pop() | ||
|
|
||
| # Check what wires were affected | ||
| affected_wires: set[str | int] = set(wire_map_before.keys()) | ||
| for region_wire_map in region_wire_maps: | ||
| affected_wires.update(region_wire_map.keys()) | ||
|
|
||
| # Update state to be the union of all branch wire maps | ||
| final_wire_map = defaultdict(set) | ||
| for wire in affected_wires: | ||
| all_nodes: set = set() | ||
| for region_wire_map in region_wire_maps: | ||
| if not wire in region_wire_map: | ||
| # IfOp region didn't apply anything on this wire | ||
| # so default to node before the IfOp | ||
| all_nodes.update(wire_map_before.get(wire, set())) | ||
| else: | ||
| all_nodes.update(region_wire_map.get(wire, set())) | ||
| final_wire_map[wire] = all_nodes | ||
| self._wire_to_node_uids = final_wire_map | ||
|
|
||
| # ============ | ||
| # DEVICE NODE | ||
| # ============ | ||
|
|
@@ -326,7 +379,9 @@ def _func_op(self, operation: func.FuncOp) -> None: | |
| label = "qjit" | ||
|
|
||
| uid = f"cluster{self._cluster_uid_counter}" | ||
| parent_cluster_uid = None if self._cluster_uid_stack == [] else self._cluster_uid_stack[-1] | ||
| parent_cluster_uid = ( | ||
| None if self._cluster_uid_stack == [] else self._cluster_uid_stack[-1] | ||
| ) | ||
| self.dag_builder.add_cluster( | ||
| uid, | ||
| label=label, | ||
|
|
@@ -348,6 +403,9 @@ def _func_return(self, operation: func.ReturnOp) -> None: | |
| # the FuncOp's scope and so we can pop the ID off the stack. | ||
| self._cluster_uid_stack.pop() | ||
|
|
||
| # Clear seen wires as we are exiting a FuncOp (qnode) | ||
| self._wire_to_node_uids = defaultdict(set) | ||
|
|
||
|
|
||
| def _flatten_if_op(op: scf.IfOp) -> list[Region]: | ||
| """Recursively flattens a nested IfOp (if/elif/else chains).""" | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.