Skip to content
Merged
Show file tree
Hide file tree
Changes from 128 commits
Commits
Show all changes
476 commits
Select commit Hold shift + click to select a range
6cda3bf
format
andrijapau Nov 26, 2025
86b5662
Update frontend/catalyst/python_interface/visualization/dag_builder.py
andrijapau Nov 26, 2025
9c26efd
Update frontend/catalyst/python_interface/visualization/pydot_dag_bui…
andrijapau Nov 26, 2025
81a9aa6
Update frontend/catalyst/python_interface/visualization/pydot_dag_bui…
andrijapau Nov 26, 2025
ab06276
Update frontend/test/pytest/python_interface/visualization/test_pydot…
andrijapau Nov 26, 2025
3e4102b
Update frontend/catalyst/python_interface/visualization/pydot_dag_bui…
andrijapau Nov 26, 2025
194f14a
Update frontend/catalyst/python_interface/visualization/dag_builder.py
andrijapau Nov 26, 2025
199ab70
add reset
andrijapau Nov 26, 2025
e28a1a9
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau Nov 26, 2025
24d3dd3
Apply suggestion from @andrijapau
andrijapau Nov 26, 2025
685842c
fix fakebackend
andrijapau Nov 26, 2025
986fb3f
Merge branch 'fix/upgrade-dag-builders' into feature/mlir-to-dag-pass
andrijapau Nov 26, 2025
07b7655
Merge branch 'feature/mlir-to-dag-pass' into feature/visualize-qnode
andrijapau Nov 26, 2025
11f5864
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau Nov 26, 2025
8c64d81
isort
andrijapau Nov 26, 2025
606f5f5
Merge branch 'fix/upgrade-dag-builders' into feature/mlir-to-dag-pass
andrijapau Nov 26, 2025
edfcb93
Merge branch 'feature/mlir-to-dag-pass' into feature/visualize-qnode
andrijapau Nov 26, 2025
0370886
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau Nov 26, 2025
9847b39
try moving cluster stack to dag builder
andrijapau Nov 26, 2025
57ce573
add test
andrijapau Nov 26, 2025
805d21b
Revert "add test"
andrijapau Nov 26, 2025
c0e0283
Revert "try moving cluster stack to dag builder"
andrijapau Nov 26, 2025
e1a6c64
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau Nov 26, 2025
2a960cf
improve device test
andrijapau Nov 26, 2025
a21e6dd
fix black isort
andrijapau Nov 26, 2025
b06585b
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau Nov 26, 2025
f666a9f
add exceptions check
andrijapau Nov 26, 2025
e28b2b7
add better documentation
andrijapau Nov 26, 2025
af5e52c
fix
andrijapau Nov 26, 2025
952fd7f
fix typo
andrijapau Nov 26, 2025
99f9fc6
Merge branch 'fix/upgrade-dag-builders' into feature/mlir-to-dag-pass
andrijapau Nov 26, 2025
492276d
Merge branch 'feature/mlir-to-dag-pass' into feature/visualize-qnode
andrijapau Nov 26, 2025
8f2dc98
Apply suggestion from @andrijapau
andrijapau Nov 26, 2025
eac25d5
Merge branch 'fix/upgrade-dag-builders' into feature/mlir-to-dag-pass
andrijapau Nov 26, 2025
b06e92e
Merge branch 'feature/mlir-to-dag-pass' into feature/visualize-qnode
andrijapau Nov 26, 2025
53497ba
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau Nov 26, 2025
83225b3
Merge branch 'feature/graph-visualization' into feature/mlir-to-dag-pass
andrijapau Nov 26, 2025
7d04249
fix tests
andrijapau Nov 26, 2025
fbb0c5a
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau Nov 26, 2025
941675f
Merge branch 'feature/mlir-to-dag-pass' into feature/visualize-qnode
andrijapau Nov 26, 2025
bb9549d
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau Nov 26, 2025
2bad538
add reminder comments
andrijapau Nov 26, 2025
683768e
add back comment
andrijapau Nov 26, 2025
2adb3b2
format
andrijapau Nov 26, 2025
f7bbb6b
fix test name
andrijapau Nov 26, 2025
f7f4b5a
fix test
andrijapau Nov 26, 2025
a21f878
fix tests
andrijapau Nov 26, 2025
ae0ff32
format
andrijapau Nov 26, 2025
26ae968
Apply suggestion from @andrijapau
andrijapau Nov 26, 2025
1d4f1f1
more explanation
andrijapau Nov 26, 2025
70ec17a
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau Nov 26, 2025
ed8bae0
more fixes
andrijapau Nov 26, 2025
25e8051
fix up testing
andrijapau Nov 26, 2025
ffc9726
fix naming
andrijapau Nov 26, 2025
a243614
Merge branch 'feature/mlir-to-dag-pass' into feature/visualize-qnode
andrijapau Nov 26, 2025
0659cdf
Merge branch 'feature/mlir-to-dag-pass' into feature/visualize-nodes
andrijapau Nov 26, 2025
e58891a
new testing approach
andrijapau Nov 27, 2025
6d1ab99
format
andrijapau Nov 27, 2025
710e6a8
test the fakebackend
andrijapau Nov 27, 2025
cb931d1
whoops forgot test
andrijapau Nov 27, 2025
049a683
improve test
andrijapau Nov 27, 2025
9ea917e
add new feature of jit_ prefix removal and single qnode
andrijapau Nov 27, 2025
fd41bc5
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau Nov 27, 2025
fd26a37
whoops
andrijapau Nov 27, 2025
1ad2ec0
format
andrijapau Nov 27, 2025
7fdbac0
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau Nov 27, 2025
8fc4a30
improve some tests
andrijapau Nov 27, 2025
bfd28b2
shouldn't have conditional label
andrijapau Nov 27, 2025
4753071
Merge branch 'feature/graph-visualization' into feature/visualize-qnode
andrijapau Nov 28, 2025
6200c76
Merge branch 'feature/graph-visualization' into feature/visualize-qnode
andrijapau Nov 28, 2025
1f90c6d
Merge branch 'feature/graph-visualization' into feature/visualize-qnode
andrijapau Nov 28, 2025
9aef399
id to uid rename
andrijapau Nov 28, 2025
e87dba2
format
andrijapau Nov 28, 2025
14ed4bc
Merge branch 'feature/graph-visualization' into feature/visualize-qnode
andrijapau Nov 28, 2025
be1961c
rename inner stack
andrijapau Nov 28, 2025
c92736a
format
andrijapau Nov 28, 2025
ca44e59
Apply suggestion from @andrijapau
andrijapau Nov 28, 2025
f84a6f1
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau Nov 28, 2025
3a8da55
Apply suggestion from @andrijapau
andrijapau Nov 28, 2025
26e95f9
format
andrijapau Nov 28, 2025
d7f21bd
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau Nov 28, 2025
c9594ee
fix
andrijapau Nov 28, 2025
d76fcca
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau Nov 28, 2025
c0554c5
fix
andrijapau Nov 28, 2025
a80141c
format
andrijapau Nov 28, 2025
529cace
rename cluster stack
andrijapau Nov 28, 2025
4a172dd
return op doesnt have regions
andrijapau Dec 1, 2025
a54de21
Apply suggestion from @andrijapau
andrijapau Dec 1, 2025
5a731c0
Merge branch 'feature/graph-visualization' into feature/visualize-qnode
andrijapau Dec 1, 2025
53f80b3
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau Dec 1, 2025
2bb5bde
update testing and add for loop label upgrade
andrijapau Dec 1, 2025
434c500
fix
andrijapau Dec 1, 2025
cfa5f3b
add more tests
andrijapau Dec 1, 2025
a62f2b1
add dev comments
andrijapau Dec 1, 2025
d9026ef
more testing
andrijapau Dec 1, 2025
faf338f
move things around
andrijapau Dec 1, 2025
5d03a81
add more qol
andrijapau Dec 2, 2025
56e4756
Update frontend/catalyst/python_interface/visualization/construct_cir…
andrijapau Dec 2, 2025
c95c45f
Update frontend/catalyst/python_interface/visualization/construct_cir…
andrijapau Dec 2, 2025
18aa30b
Update frontend/catalyst/python_interface/visualization/construct_cir…
andrijapau Dec 2, 2025
929bd23
add flattened ifop ability
andrijapau Dec 2, 2025
709e961
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau Dec 2, 2025
b51bb7b
fix
andrijapau Dec 2, 2025
89f45f3
fix
andrijapau Dec 2, 2025
b45b2f0
fix
andrijapau Dec 2, 2025
2028fa7
fix
andrijapau Dec 2, 2025
4e994e6
simplify PR by making it for ...
andrijapau Dec 2, 2025
e590847
format
andrijapau Dec 2, 2025
eb6a0c3
fix tests
andrijapau Dec 2, 2025
7eb31b6
format
andrijapau Dec 2, 2025
8e787c9
fix logic in flatten if op
andrijapau Dec 2, 2025
8b6287f
format
andrijapau Dec 2, 2025
83ebb18
fix type hinting
andrijapau Dec 2, 2025
6d84d36
add dev comment
andrijapau Dec 2, 2025
1d8ac14
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau Dec 2, 2025
3c42841
more deviceop tests
andrijapau Dec 2, 2025
e319aae
Apply suggestion from @andrijapau
andrijapau Dec 2, 2025
1c9c857
re-order changelog
andrijapau Dec 2, 2025
093a5d5
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau Dec 2, 2025
2ca7bb8
add cl entry
andrijapau Dec 2, 2025
9d724bf
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau Dec 2, 2025
61b70d9
add smaller fontsize for the conditional bit
andrijapau Dec 2, 2025
3a2ab18
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau Dec 2, 2025
e9c69d8
format
andrijapau Dec 2, 2025
f47b1a5
fix id to uid
andrijapau Dec 2, 2025
5e83fe4
fix
andrijapau Dec 2, 2025
d5a0611
Apply suggestion from @andrijapau
andrijapau Dec 2, 2025
98bf7ea
clean-up
andrijapau Dec 2, 2025
818448c
clean up
andrijapau Dec 2, 2025
b924c43
format
andrijapau Dec 2, 2025
089cb14
both single and multi qnode have qjit bounding box
andrijapau Dec 3, 2025
2a2d55c
Apply suggestion from @andrijapau
andrijapau Dec 3, 2025
220c6d5
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau Dec 3, 2025
f7261a9
add more detail to the for loop label
andrijapau Dec 3, 2025
a8cd49c
format
andrijapau Dec 3, 2025
44bf9d3
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau Dec 3, 2025
70b0d93
fix test
andrijapau Dec 3, 2025
d983e97
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau Dec 3, 2025
cb05921
use counter instead of builtin id function
andrijapau Dec 4, 2025
1883fb3
whoops
andrijapau Dec 4, 2025
5e2d456
Apply suggestion from @andrijapau
andrijapau Dec 4, 2025
36b5f7d
Apply suggestion from @andrijapau
andrijapau Dec 4, 2025
ee15b67
Apply suggestion from @andrijapau
andrijapau Dec 4, 2025
16e003d
add two counters
andrijapau Dec 4, 2025
23561f7
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau Dec 4, 2025
d5ccaf7
use the counters for uid
andrijapau Dec 4, 2025
1dd9f38
update tests to be simpler
andrijapau Dec 4, 2025
6a9b01d
fix tests
andrijapau Dec 4, 2025
a2ac9ff
minor fix
andrijapau Dec 4, 2025
0dbe4fe
foramt
andrijapau Dec 4, 2025
d92aea4
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau Dec 4, 2025
8afc67a
Apply suggestion from @andrijapau
andrijapau Dec 4, 2025
c0d4d67
update tests
andrijapau Dec 4, 2025
9706967
update tests
andrijapau Dec 4, 2025
5e62c31
format
andrijapau Dec 4, 2025
1474a66
whoops fix test
andrijapau Dec 4, 2025
82430af
Merge branch 'feature/visualize-qnode' into feature/control-flow-clus…
andrijapau Dec 4, 2025
ced6f75
format
andrijapau Dec 4, 2025
649aed2
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau Dec 4, 2025
9c74b6a
update id
andrijapau Dec 4, 2025
5641e68
fix test
andrijapau Dec 4, 2025
4ffc9d3
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau Dec 4, 2025
22c8ae1
add more nested testing
andrijapau Dec 4, 2025
ab7c471
format
andrijapau Dec 4, 2025
dd1df44
adjust test
andrijapau Dec 4, 2025
57409bd
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau Dec 4, 2025
9996684
fix test
andrijapau Dec 4, 2025
303fb03
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau Dec 4, 2025
c262df9
format
andrijapau Dec 4, 2025
05df88c
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau Dec 4, 2025
3175efa
add tests
andrijapau Dec 4, 2025
194b357
fix get_nodes -> nodes
andrijapau Dec 4, 2025
506f839
fix labels
andrijapau Dec 4, 2025
f67802f
fix labels
andrijapau Dec 4, 2025
ad35efc
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau Dec 4, 2025
fac6150
fix measure
andrijapau Dec 5, 2025
9bdf8da
fix globalphase test
andrijapau Dec 5, 2025
b9479b1
fix sample test
andrijapau Dec 5, 2025
60e83c4
fix sample
andrijapau Dec 5, 2025
ebd108e
format
andrijapau Dec 5, 2025
bfd1d10
add dev comment
andrijapau Dec 5, 2025
aec9f83
fix code
andrijapau Dec 5, 2025
c36a43e
fix naming
andrijapau Dec 5, 2025
6c1d825
add expected error messages
andrijapau Dec 5, 2025
7b56f89
fix test for probs
andrijapau Dec 5, 2025
4f83355
format
andrijapau Dec 5, 2025
9f6a131
add compileerror
andrijapau Dec 5, 2025
9855985
update
andrijapau Dec 5, 2025
30db67e
Merge branch 'feature/graph-visualization' into feature/control-flow-…
andrijapau Dec 5, 2025
2fe2074
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau Dec 5, 2025
d069fef
fix
andrijapau Dec 5, 2025
b65ae16
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau Dec 5, 2025
e29f546
bring back ssavalue
andrijapau Dec 5, 2025
b34d8e2
format
andrijapau Dec 5, 2025
aa1d90c
format
andrijapau Dec 5, 2025
d3fdfea
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau Dec 5, 2025
27ff971
remove random import
andrijapau Dec 5, 2025
1a0ac68
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau Dec 5, 2025
5e3a6d2
re-work node labels to be records with ports
andrijapau Dec 5, 2025
a25f7b4
format
andrijapau Dec 5, 2025
02d4a55
fix tests
andrijapau Dec 5, 2025
7237a3f
move mid measure to an operator
andrijapau Dec 5, 2025
6410aae
fix test
andrijapau Dec 6, 2025
20efb54
get rid of unnecessary function
andrijapau Dec 9, 2025
81e9fe5
Update frontend/catalyst/python_interface/visualization/construct_cir…
andrijapau Dec 9, 2025
dcf59ae
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau Dec 9, 2025
7eb1da4
Update frontend/test/pytest/python_interface/visualization/test_const…
andrijapau Dec 9, 2025
f7daa8a
Update doc/releases/changelog-dev.md
andrijapau Dec 9, 2025
80da43c
Update frontend/test/pytest/python_interface/visualization/test_const…
andrijapau Dec 9, 2025
5fc9cf1
fix bug and add test coverage
andrijapau Dec 9, 2025
ef5550f
fix test and format
andrijapau Dec 9, 2025
5476e6d
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau Dec 9, 2025
dfde76d
format
andrijapau Dec 9, 2025
ecddff7
fix
andrijapau Dec 9, 2025
c2bd854
remove ssa condition, not needed for now
andrijapau Dec 9, 2025
4176d03
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau Dec 9, 2025
f8b2922
refactor a bit
andrijapau Dec 9, 2025
89dad36
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau Dec 9, 2025
e58cc4e
add another test
andrijapau Dec 9, 2025
1877cf9
whoops
andrijapau Dec 9, 2025
8643a24
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau Dec 9, 2025
c35b8c1
Apply suggestion from @andrijapau
andrijapau Dec 9, 2025
5728859
Apply suggestion from @andrijapau
andrijapau Dec 9, 2025
5345dfc
format
andrijapau Dec 9, 2025
04ee8e2
update how the node labels look
andrijapau Dec 9, 2025
74e8358
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau Dec 9, 2025
d8c1b7a
improve logic
andrijapau Dec 9, 2025
bafd278
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau Dec 9, 2025
284f56b
Update frontend/catalyst/python_interface/visualization/construct_cir…
andrijapau Dec 9, 2025
8b2ec3c
remove redundant label
andrijapau Dec 9, 2025
488de58
add dev comment
andrijapau Dec 9, 2025
490949b
format
andrijapau Dec 9, 2025
3494f17
improve function naming
andrijapau Dec 9, 2025
fee7e1b
fix labels for measurements
andrijapau Dec 10, 2025
e15bdc0
add record
andrijapau Dec 10, 2025
2071360
Merge branch 'feature/graph-visualization' into feature/control-flow-…
andrijapau Dec 10, 2025
c86043f
remove node label
andrijapau Dec 10, 2025
5aeba5e
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau Dec 10, 2025
ea8c9b7
add labeljust
andrijapau Dec 10, 2025
d224c0c
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau Dec 10, 2025
8392c2b
remove comment
andrijapau Dec 10, 2025
80798f9
fix cluster
andrijapau Dec 10, 2025
cb1e531
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau Dec 10, 2025
1f48498
fix
andrijapau Dec 10, 2025
ac5ffe7
Apply suggestion from @andrijapau
andrijapau Dec 10, 2025
27cc772
Merge branch 'feature/control-flow-clusters' into feature/visualize-n…
andrijapau Dec 10, 2025
90e6588
fix
andrijapau Dec 10, 2025
2983d67
fix
andrijapau Dec 10, 2025
24ffa25
Merge branch 'feature/graph-visualization' into feature/visualize-nodes
andrijapau Dec 10, 2025
c9ec867
fix
andrijapau Dec 10, 2025
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
3 changes: 3 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

* Compiled programs can be visualized.
[(#2213)](https://github.com/PennyLaneAI/catalyst/pull/2213)
[(#2229)](https://github.com/PennyLaneAI/catalyst/pull/2229)
[(#2214)](https://github.com/PennyLaneAI/catalyst/pull/2214)
[(#2118)](https://github.com/PennyLaneAI/catalyst/pull/2218)

* Added ``catalyst.switch``, a qjit compatible, index-switch style control flow decorator.
[(#2171)](https://github.com/PennyLaneAI/catalyst/pull/2171)
Expand Down
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 (
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(
self,
op: quantum.CustomOp
| quantum.GlobalPhaseOp
| quantum.QubitUnitaryOp
| quantum.MultiRZOp,
) -> None:
"""Generic handler for unitary gates and quantum state preparation operations."""

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(
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],
)
68 changes: 52 additions & 16 deletions frontend/catalyst/python_interface/visualization/dag_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
"""File that defines the DAGBuilder abstract base class."""

from abc import ABC, abstractmethod
from typing import Any
from typing import Any, TypeAlias

ClusterID: TypeAlias = str
NodeID: TypeAlias = str


class DAGBuilder(ABC):
Expand All @@ -28,50 +31,83 @@ class DAGBuilder(ABC):

@abstractmethod
def add_node(
self, node_id: str, node_label: str, parent_graph_id: str | None = None, **node_attrs: Any
self,
id: NodeID,
label: str,
cluster_id: ClusterID | None = None,
**attrs: Any,
) -> None:
"""Add a single node to the graph.

Args:
node_id (str): Unique node ID to identify this node.
node_label (str): The text to display on the node when rendered.
parent_graph_id (str | None): Optional ID of the cluster this node belongs to.
**node_attrs (Any): Any additional styling keyword arguments.
id (str): Unique node ID to identify this node.
label (str): The text to display on the node when rendered.
cluster_id (str | None): Optional ID of the cluster this node belongs to. If `None`, this node gets
added on the base graph.
**attrs (Any): Any additional styling keyword arguments.

"""
raise NotImplementedError

@abstractmethod
def add_edge(self, from_node_id: str, to_node_id: str, **edge_attrs: Any) -> None:
def add_edge(self, from_id: NodeID, to_id: NodeID, **attrs: Any) -> None:
"""Add a single directed edge between nodes in the graph.

Args:
from_node_id (str): The unique ID of the source node.
to_node_id (str): The unique ID of the destination node.
**edge_attrs (Any): Any additional styling keyword arguments.
from_id (str): The unique ID of the source node.
to_id (str): The unique ID of the destination node.
**attrs (Any): Any additional styling keyword arguments.

"""
raise NotImplementedError

@abstractmethod
def add_cluster(
self,
cluster_id: str,
id: ClusterID,
node_label: str | None = None,
parent_graph_id: str | None = None,
**cluster_attrs: Any,
cluster_id: ClusterID | None = None,
**attrs: Any,
) -> None:
"""Add a single cluster to the graph.

A cluster is a specific type of subgraph where the nodes and edges contained
within it are visually and logically grouped.

Args:
cluster_id (str): Unique cluster ID to identify this cluster.
id (str): Unique cluster ID to identify this cluster.
node_label (str | None): The text to display on an information node within the cluster when rendered.
parent_graph_id (str | None): Optional ID of the cluster this cluster belongs to.
**cluster_attrs (Any): Any additional styling keyword arguments.
cluster_id (str | None): Optional ID of the cluster this cluster belongs to. If `None`, the cluster will be
placed on the base graph.
**attrs (Any): Any additional styling keyword arguments.

"""
raise NotImplementedError

@abstractmethod
def get_nodes(self) -> dict[NodeID, dict[str, Any]]:
"""Retrieve the current set of nodes in the graph.

Returns:
nodes (dict[str, dict[str, Any]]): A dictionary that maps the node's ID to it's node information.
"""
raise NotImplementedError

@abstractmethod
def get_edges(self) -> list[dict[str, Any]]:
"""Retrieve the current set of edges in the graph.

Returns:
edges (list[dict[str, Any]]): A list of edges where each element in the list contains a dictionary of edge information.
"""
raise NotImplementedError

@abstractmethod
def get_clusters(self) -> dict[ClusterID, dict[str, Any]]:
"""Retrieve the current set of clusters in the graph.

Returns:
clusters (dict[str, dict[str, Any]]): A dictionary that maps the cluster's ID to it's cluster information.
"""
raise NotImplementedError

Expand Down
Loading
Loading