-
Notifications
You must be signed in to change notification settings - Fork 59
feat: visualize static operators and measurements as nodes #2218
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
Merged
andrijapau
merged 476 commits into
feature/graph-visualization
from
feature/visualize-nodes
Dec 10, 2025
Merged
Changes from 128 commits
Commits
Show all changes
476 commits
Select commit
Hold shift + click to select a range
6cda3bf
format
andrijapau 86b5662
Update frontend/catalyst/python_interface/visualization/dag_builder.py
andrijapau 9c26efd
Update frontend/catalyst/python_interface/visualization/pydot_dag_bui…
andrijapau 81a9aa6
Update frontend/catalyst/python_interface/visualization/pydot_dag_bui…
andrijapau ab06276
Update frontend/test/pytest/python_interface/visualization/test_pydot…
andrijapau 3e4102b
Update frontend/catalyst/python_interface/visualization/pydot_dag_bui…
andrijapau 194f14a
Update frontend/catalyst/python_interface/visualization/dag_builder.py
andrijapau 199ab70
add reset
andrijapau e28a1a9
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau 24d3dd3
Apply suggestion from @andrijapau
andrijapau 685842c
fix fakebackend
andrijapau 986fb3f
Merge branch 'fix/upgrade-dag-builders' into feature/mlir-to-dag-pass
andrijapau 07b7655
Merge branch 'feature/mlir-to-dag-pass' into feature/visualize-qnode
andrijapau 11f5864
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau 8c64d81
isort
andrijapau 606f5f5
Merge branch 'fix/upgrade-dag-builders' into feature/mlir-to-dag-pass
andrijapau edfcb93
Merge branch 'feature/mlir-to-dag-pass' into feature/visualize-qnode
andrijapau 0370886
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau 9847b39
try moving cluster stack to dag builder
andrijapau 57ce573
add test
andrijapau 805d21b
Revert "add test"
andrijapau c0e0283
Revert "try moving cluster stack to dag builder"
andrijapau e1a6c64
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau 2a960cf
improve device test
andrijapau a21e6dd
fix black isort
andrijapau b06585b
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau f666a9f
add exceptions check
andrijapau e28b2b7
add better documentation
andrijapau af5e52c
fix
andrijapau 952fd7f
fix typo
andrijapau 99f9fc6
Merge branch 'fix/upgrade-dag-builders' into feature/mlir-to-dag-pass
andrijapau 492276d
Merge branch 'feature/mlir-to-dag-pass' into feature/visualize-qnode
andrijapau 8f2dc98
Apply suggestion from @andrijapau
andrijapau eac25d5
Merge branch 'fix/upgrade-dag-builders' into feature/mlir-to-dag-pass
andrijapau b06e92e
Merge branch 'feature/mlir-to-dag-pass' into feature/visualize-qnode
andrijapau 53497ba
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau 83225b3
Merge branch 'feature/graph-visualization' into feature/mlir-to-dag-pass
andrijapau 7d04249
fix tests
andrijapau fbb0c5a
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau 941675f
Merge branch 'feature/mlir-to-dag-pass' into feature/visualize-qnode
andrijapau bb9549d
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau 2bad538
add reminder comments
andrijapau 683768e
add back comment
andrijapau 2adb3b2
format
andrijapau f7bbb6b
fix test name
andrijapau f7f4b5a
fix test
andrijapau a21f878
fix tests
andrijapau ae0ff32
format
andrijapau 26ae968
Apply suggestion from @andrijapau
andrijapau 1d4f1f1
more explanation
andrijapau 70ec17a
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau ed8bae0
more fixes
andrijapau 25e8051
fix up testing
andrijapau ffc9726
fix naming
andrijapau a243614
Merge branch 'feature/mlir-to-dag-pass' into feature/visualize-qnode
andrijapau 0659cdf
Merge branch 'feature/mlir-to-dag-pass' into feature/visualize-nodes
andrijapau e58891a
new testing approach
andrijapau 6d1ab99
format
andrijapau 710e6a8
test the fakebackend
andrijapau cb931d1
whoops forgot test
andrijapau 049a683
improve test
andrijapau 9ea917e
add new feature of jit_ prefix removal and single qnode
andrijapau fd41bc5
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau fd26a37
whoops
andrijapau 1ad2ec0
format
andrijapau 7fdbac0
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau 8fc4a30
improve some tests
andrijapau bfd28b2
shouldn't have conditional label
andrijapau 4753071
Merge branch 'feature/graph-visualization' into feature/visualize-qnode
andrijapau 6200c76
Merge branch 'feature/graph-visualization' into feature/visualize-qnode
andrijapau 1f90c6d
Merge branch 'feature/graph-visualization' into feature/visualize-qnode
andrijapau 9aef399
id to uid rename
andrijapau e87dba2
format
andrijapau 14ed4bc
Merge branch 'feature/graph-visualization' into feature/visualize-qnode
andrijapau be1961c
rename inner stack
andrijapau c92736a
format
andrijapau ca44e59
Apply suggestion from @andrijapau
andrijapau f84a6f1
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau 3a8da55
Apply suggestion from @andrijapau
andrijapau 26e95f9
format
andrijapau d7f21bd
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau c9594ee
fix
andrijapau d76fcca
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau c0554c5
fix
andrijapau a80141c
format
andrijapau 529cace
rename cluster stack
andrijapau 4a172dd
return op doesnt have regions
andrijapau a54de21
Apply suggestion from @andrijapau
andrijapau 5a731c0
Merge branch 'feature/graph-visualization' into feature/visualize-qnode
andrijapau 53f80b3
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau 2bb5bde
update testing and add for loop label upgrade
andrijapau 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 3a2ab18
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau e9c69d8
format
andrijapau f47b1a5
fix id to uid
andrijapau 5e83fe4
fix
andrijapau d5a0611
Apply suggestion from @andrijapau
andrijapau 98bf7ea
clean-up
andrijapau 818448c
clean up
andrijapau b924c43
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 70b0d93
fix test
andrijapau d983e97
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
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 5641e68
fix test
andrijapau 4ffc9d3
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
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 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 3175efa
add tests
andrijapau 194b357
fix get_nodes -> nodes
andrijapau 506f839
fix labels
andrijapau f67802f
fix labels
andrijapau ad35efc
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau fac6150
fix measure
andrijapau 9bdf8da
fix globalphase test
andrijapau b9479b1
fix sample test
andrijapau 60e83c4
fix sample
andrijapau ebd108e
format
andrijapau bfd1d10
add dev comment
andrijapau aec9f83
fix code
andrijapau c36a43e
fix naming
andrijapau 6c1d825
add expected error messages
andrijapau 7b56f89
fix test for probs
andrijapau 4f83355
format
andrijapau 9f6a131
add compileerror
andrijapau 9855985
update
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 27ff971
remove random import
andrijapau 1a0ac68
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau 5e3a6d2
re-work node labels to be records with ports
andrijapau a25f7b4
format
andrijapau 02d4a55
fix tests
andrijapau 7237a3f
move mid measure to an operator
andrijapau 6410aae
fix test
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 d8c1b7a
improve logic
andrijapau bafd278
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau 284f56b
Update frontend/catalyst/python_interface/visualization/construct_cir…
andrijapau 8b2ec3c
remove redundant label
andrijapau 488de58
add dev comment
andrijapau 490949b
format
andrijapau 3494f17
improve function naming
andrijapau fee7e1b
fix labels for measurements
andrijapau e15bdc0
add record
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 ea8c9b7
add labeljust
andrijapau d224c0c
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau 8392c2b
remove comment
andrijapau 80798f9
fix cluster
andrijapau cb1e531
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau 1f48498
fix
andrijapau ac5ffe7
Apply suggestion from @andrijapau
andrijapau 27cc772
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau 90e6588
fix
andrijapau 2983d67
fix
andrijapau 24ffa25
Merge branch 'feature/graph-visualization' into feature/visualize-nodes
andrijapau c9ec867
fix
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
Some comments aren't visible on the classic Files Changed page.
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
153 changes: 153 additions & 0 deletions
153
frontend/catalyst/python_interface/visualization/construct_circuit_dag.py
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 |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| # Copyright 2025 Xanadu Quantum Technologies Inc. | ||
|
|
||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
|
|
||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Contains the ConstructCircuitDAG tool for constructing a DAG from an xDSL module.""" | ||
|
|
||
| from functools import singledispatchmethod | ||
| from typing import Any | ||
|
|
||
| from xdsl.dialects import builtin | ||
| from xdsl.ir import Block, Operation, Region | ||
|
|
||
| from catalyst.python_interface.dialects import quantum | ||
| from catalyst.python_interface.visualization.dag_builder import DAGBuilder | ||
| from catalyst.python_interface.visualization.xdsl_conversion import ( | ||
andrijapau marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| xdsl_to_qml_measurement, | ||
| xdsl_to_qml_op, | ||
| ) | ||
|
|
||
|
|
||
| class ConstructCircuitDAG: | ||
| """A tool that analyzes an xDSL module and constructs a Directed Acyclic Graph (DAG) | ||
| using an injected DAGBuilder instance. This tool does not mutate the xDSL module.""" | ||
|
|
||
| def __init__(self, dag_builder: DAGBuilder) -> None: | ||
| """Initialize the analysis pass by injecting the DAG builder dependency. | ||
|
|
||
| Args: | ||
| dag_builder (DAGBuilder): The concrete builder instance used for graph construction. | ||
| """ | ||
| self.dag_builder: DAGBuilder = dag_builder | ||
|
|
||
| # Record clusters seen as a stack | ||
| # beginning with the base graph (None) | ||
| self._cluster_stack: list[str | None] = [None] | ||
|
|
||
| def _reset(self) -> None: | ||
| """Resets the instance.""" | ||
| self._cluster_stack: list[str | None] = [None] | ||
|
|
||
| # ================================= | ||
| # 1. CORE DISPATCH AND ENTRY POINT | ||
| # ================================= | ||
|
|
||
| @singledispatchmethod | ||
| def _visit(self, op: Any) -> None: | ||
| """Central dispatch method (Visitor Pattern). Routes the operation 'op' | ||
| to the specialized handler registered for its type.""" | ||
|
|
||
| def construct(self, module: builtin.ModuleOp) -> None: | ||
| """Constructs the DAG from the module.""" | ||
| self._reset() | ||
|
|
||
| for op in module.ops: | ||
| self._visit(op) | ||
|
|
||
| # ======================= | ||
| # 2. IR TRAVERSAL | ||
| # ======================= | ||
|
|
||
| @_visit.register | ||
| def _operation(self, operation: Operation) -> None: | ||
| """Visit an xDSL Operation.""" | ||
| for region in operation.regions: | ||
| self._visit(region) | ||
|
|
||
| @_visit.register | ||
| def _region(self, region: Region) -> None: | ||
| """Visit an xDSL Region operation.""" | ||
| for block in region.blocks: | ||
| self._visit(block) | ||
|
|
||
| @_visit.register | ||
| def _block(self, block: Block) -> None: | ||
| """Visit an xDSL Block operation, dispatching handling for each contained Operation.""" | ||
| for op in block.ops: | ||
| self._visit(op) | ||
|
|
||
| # ====================== | ||
| # 3. QUANTUM OPERATIONS | ||
| # ====================== | ||
|
|
||
| @_visit.register | ||
| def _unitary( | ||
andrijapau marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| self, | ||
| op: quantum.CustomOp | ||
| | quantum.GlobalPhaseOp | ||
| | quantum.QubitUnitaryOp | ||
| | quantum.MultiRZOp, | ||
| ) -> None: | ||
| """Generic handler for unitary gates and quantum state preparation operations.""" | ||
andrijapau marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| qml_op = xdsl_to_qml_op(op) | ||
| # Build node on graph | ||
| self.dag_builder.add_node( | ||
| id=f"node_{id(op)}", | ||
| label=str(qml_op), | ||
| cluster_id=self._cluster_stack[-1], | ||
| ) | ||
|
|
||
| # ======================== | ||
| # 4. QUANTUM MEASUREMENTS | ||
| # ======================== | ||
|
|
||
| @_visit.register | ||
| def _state_op(self, op: quantum.StateOp) -> None: | ||
| """Handler for the terminal state measurement operation.""" | ||
|
|
||
| meas = xdsl_to_qml_measurement(op) | ||
| # Build node on graph | ||
| self.dag_builder.add_node( | ||
| id=f"node_{id(op)}", | ||
| label=str(meas), | ||
| cluster_id=self._cluster_stack[-1], | ||
| ) | ||
|
|
||
| @_visit.register | ||
| def _statistical_measurement_ops( | ||
andrijapau marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| self, | ||
| op: quantum.ExpvalOp | quantum.VarianceOp | quantum.ProbsOp | quantum.SampleOp, | ||
| ) -> None: | ||
| """Handler for statistical measurement operations.""" | ||
|
|
||
| obs_op = op.obs.owner | ||
| meas = xdsl_to_qml_measurement(op, xdsl_to_qml_measurement(obs_op)) | ||
| # Build node on graph | ||
| self.dag_builder.add_node( | ||
| id=f"node_{id(op)}", | ||
| label=str(meas), | ||
| cluster_id=self._cluster_stack[-1], | ||
| ) | ||
|
|
||
| @_visit.register | ||
| def _projective_measure_op(self, op: quantum.MeasureOp) -> None: | ||
| """Handler for the single-qubit projective measurement operation.""" | ||
|
|
||
| meas = xdsl_to_qml_measurement(op) | ||
| # Build node on graph | ||
| self.dag_builder.add_node( | ||
| id=f"node_{id(op)}", | ||
| label=str(meas), | ||
| cluster_id=self._cluster_stack[-1], | ||
| ) | ||
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
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.