# Copyright (c) 2023 - 2026 Chair for Design Automation, TUM
# Copyright (c) 2025 - 2026 Munich Quantum Software Company GmbH
# All rights reserved.
#
# SPDX-License-Identifier: MIT
#
# Licensed under the MIT License
from __future__ import annotations
import contextlib
import gc
import itertools
from typing import TYPE_CHECKING, cast
import numpy as np
from ....core import NAryTree
from ....exceptions import SequenceFoundError
from ....quantum_circuit import gates
from ....quantum_circuit.components.extensions.gate_types import GateTypes
from ... import CompilerPass
from ...compilation_minitools import new_mod
from ..local_operation_swap import (
cost_calculator,
gate_chain_condition,
graph_rule_ongate,
graph_rule_update,
)
from ..mapping_aware_transpilation.phy_local_qr_decomp import PhyQrDecomp
if TYPE_CHECKING:
from collections.abc import Iterator
from numpy.typing import NDArray
from ....core import LevelGraph
from ....core.dfs_tree import Node as TreeNode
from ....quantum_circuit import QuantumCircuit
from ....quantum_circuit.gate import Gate
from ....simulation.backends.backendv2 import Backend
np.seterr(all="ignore")
[docs]
class PhyLocAdaPass(CompilerPass):
def __init__(self, backend: Backend, vrz_prop: bool = False) -> None:
super().__init__(backend)
self.vrz_prop = vrz_prop
[docs]
def transpile_gate(self, gate: Gate) -> list[Gate]:
energy_graph_i = self.backend.energy_level_graphs[cast("int", gate.target_qudits)]
dimension = cast("int", gate.dimensions)
phases = np.array([energy_graph_i.nodes[level].get("phase_storage", 0) for level in range(dimension)])
# Fold pending phases into the adjoint so the decomposition follows execution order.
gate = gates.CustomOne(
gate.parent_circuit,
"CUo",
cast("int", gate.target_qudits),
np.exp(1j * phases)[:, None] * gate.to_matrix(identities=0).conj().T,
dimension,
)
for node in energy_graph_i.nodes:
energy_graph_i.nodes[node]["phase_storage"] = 0
qr = PhyQrDecomp(gate, energy_graph_i)
qr_decomp, algorithmic_cost, total_cost = qr.execute()
adaptive = PhyAdaptiveDecomposition(
gate, energy_graph_i, (algorithmic_cost, total_cost), cast("int", gate.dimensions), z_prop=self.vrz_prop
)
matrices_decomposed, best_cost, new_energy_level_graph = adaptive.execute()
if not np.isfinite(best_cost[1]):
matrices_decomposed = qr_decomp
new_energy_level_graph = energy_graph_i
for node in new_energy_level_graph.nodes:
new_energy_level_graph.nodes[node]["phase_storage"] = 0
self.backend.energy_level_graphs[cast("int", gate.target_qudits)] = new_energy_level_graph
return matrices_decomposed
[docs]
def transpile(self, circuit: QuantumCircuit) -> QuantumCircuit:
self.circuit: QuantumCircuit = circuit
instructions: list[Gate] = circuit.instructions
new_instructions = []
for gate in instructions:
if gate.gate_type == GateTypes.SINGLE:
gate_trans = self.transpile_gate(gate)
new_instructions.extend(gate_trans)
gc.collect()
else:
new_instructions.append(gate)
transpiled_circuit = self.circuit.copy()
return transpiled_circuit.set_instructions(new_instructions)
[docs]
class PhyAdaptiveDecomposition:
def __init__(
self,
gate: Gate,
graph_orig: LevelGraph,
cost_limit: tuple[float, float] | None = (0, 0),
dimension: int | None = -1,
z_prop: bool | None = False,
max_nodes: int = 1000,
) -> None:
"""Initialize a search capped at max_nodes generated nodes, excluding the root.
A zero budget only checks whether the input is already diagonal. If no
solution is found within the node limit, execute returns an empty sequence
and infinite costs so the compiler pass can use its QR decomposition.
"""
if max_nodes < 0:
msg = "max_nodes must be non-negative."
raise ValueError(msg)
self.max_nodes = max_nodes
self.circuit: QuantumCircuit = gate.parent_circuit
self.U: NDArray[np.complex128] = gate.to_matrix(identities=0)
self.qudit_index: int = cast("int", gate.target_qudits)
self.graph: LevelGraph = graph_orig
self.graph.phase_storing_setup()
self.cost_limit: tuple[float, float] = cast("tuple[float, float]", cost_limit)
self.dimension: int = cast("int", dimension)
self.phase_propagation: bool = cast("bool", z_prop)
self.TREE: NAryTree = NAryTree()
[docs]
def execute(self) -> tuple[list[Gate], tuple[float, float], LevelGraph]:
self.TREE.global_id_counter = 0
self.TREE.add(
0,
gates.CustomOne(
self.circuit, "CUo", self.qudit_index, np.identity(self.dimension, dtype=np.complex128), self.dimension
),
self.U,
self.graph,
0,
0,
self.cost_limit,
[],
)
with contextlib.suppress(SequenceFoundError):
self.dfs(self.TREE.root)
matrices_decomposed, best_cost, final_graph = self.TREE.retrieve_decomposition(self.TREE.root)
assert final_graph is not None
matrices_decomposed_m: list[Gate] = []
if matrices_decomposed:
matrices_decomposed_m, final_graph = self.z_extraction(
matrices_decomposed, final_graph, self.phase_propagation
)
return matrices_decomposed_m, best_cost, final_graph
[docs]
def dfs(self, current_root: TreeNode) -> None:
lower_triangle = np.tri(current_root.u_of_level.shape[0], k=-1, dtype=np.bool_)
stack = [self._children(current_root, lower_triangle)]
while stack:
child = next(stack[-1], None)
if child is None:
stack.pop()
else:
stack.append(self._children(child, lower_triangle))
def _children(self, current_root: TreeNode, lower_triangle: NDArray[np.bool_]) -> Iterator[TreeNode]:
# check if close to diagonal
ucopy = current_root.u_of_level.copy()
current_placement = current_root.graph
assert current_placement is not None
# is the diagonal noisy?
valid_diag = any(abs(np.diag(ucopy)) > 1.0e-4)
# are the non diagonal entries zeroed-out?
filtered_ucopy = abs(ucopy) > 1.0e-4
np.fill_diagonal(filtered_ucopy, 0)
not_diag = filtered_ucopy.any()
# if is diagonal enough then somehow signal end of algorithm
if (not not_diag) and valid_diag:
current_root.finished = True
raise SequenceFoundError(current_root.key)
u_ = current_root.u_of_level
dimension = u_.shape[0]
subdiagonal_support = (np.abs(u_) > 1.0e-8) & lower_triangle
column_support_sizes = np.count_nonzero(subdiagonal_support, axis=0)
for c in range(dimension - 1):
if not column_support_sizes[c]:
continue
for r, r2 in itertools.combinations(range(c, dimension), 2):
if self.TREE.global_id_counter >= self.max_nodes:
break
if not subdiagonal_support[r2, c]:
continue
theta = 2 * np.arctan2(abs(u_[r2, c]), abs(u_[r, c]))
phi = -(np.pi / 2 + np.angle(u_[r, c]) - np.angle(u_[r2, c]))
rotation_involved = gates.R(
self.circuit, "R", self.qudit_index, [r, r2, theta, phi], self.dimension
) # R(theta, phi, r, r2, dimension)
u_temp = rotation_involved.to_matrix(identities=0) @ u_ # matmul(rotation_involved.matrix, U_)
# Reduce this column without reopening zeros in earlier columns.
# Later columns may gain entries, as in a cyclic permutation.
next_support = (np.abs(u_temp) > 1.0e-8) & lower_triangle
if np.any(next_support[:, :c] & ~subdiagonal_support[:, :c]) or (
np.count_nonzero(next_support[:, c]) >= column_support_sizes[c]
):
continue
non_zeros = int(np.count_nonzero(abs(u_temp) > 1.0e-4))
(
estimated_cost,
pi_pulses_routing,
new_placement,
cost_of_pi_pulses,
gate_cost,
) = cost_calculator(rotation_involved, current_placement, non_zeros)
next_step_cost = estimated_cost + current_root.current_cost
decomp_next_step_cost = cost_of_pi_pulses + gate_cost + current_root.current_decomp_cost
branch_condition = current_root.max_cost[1] - decomp_next_step_cost # SECOND POSITION IS PHYSICAL COST
if branch_condition > 0 or abs(branch_condition) < 1.0e-12:
# if cost is better can be only candidate otherwise try them all
self.TREE.global_id_counter += 1
new_key = self.TREE.global_id_counter
if new_placement.nodes[r]["lpmap"] > new_placement.nodes[r2]["lpmap"]:
phi *= -1
physical_rotation = gates.R(
self.circuit,
"R",
self.qudit_index,
[new_placement.nodes[r]["lpmap"], new_placement.nodes[r2]["lpmap"], theta, phi],
self.dimension,
)
physical_rotation = gate_chain_condition(pi_pulses_routing, physical_rotation)
physical_rotation = graph_rule_ongate(physical_rotation, new_placement)
p_backs = [
gates.R(
self.circuit,
"R",
self.qudit_index,
[ppulse.lev_a, ppulse.lev_b, ppulse.theta, -ppulse.phi],
self.dimension,
)
for ppulse in pi_pulses_routing
]
for p_back in p_backs:
graph_rule_update(p_back, new_placement)
current_root.add(
new_key,
physical_rotation,
u_temp,
new_placement,
next_step_cost,
decomp_next_step_cost,
current_root.max_cost,
pi_pulses_routing,
)
# Explore before generating siblings that consume the node budget.
yield current_root.children[-1]