Skip to content

Commit

Permalink
🚨 address a couple more warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
burgholzer committed Sep 6, 2024
1 parent fea8365 commit f4369ea
Show file tree
Hide file tree
Showing 26 changed files with 76 additions and 114 deletions.
1 change: 0 additions & 1 deletion src/mqt/qudits/compiler/dit_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ def compile_O0(self, backend: Backend, circuit: QuantumCircuit) -> QuantumCircui

@staticmethod
def compile_O1(backend: Backend, circuit: QuantumCircuit) -> QuantumCircuit: # noqa: N802
phyloc = PhyLocAdaPass(backend)
phyent = PhyEntQRCEXPass(backend)

lanes = Lanes(circuit)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import contextlib
import gc
import itertools
from typing import TYPE_CHECKING, cast
Expand Down Expand Up @@ -101,23 +102,22 @@ def execute(self) -> tuple[list[Gate], tuple[float, float], LevelGraph]:
self.cost_limit,
[],
)
try:

with contextlib.suppress(SequenceFoundError):
self.dfs(self.TREE.root)
except SequenceFoundError:

matrices_decomposed, best_cost, final_graph = self.TREE.retrieve_decomposition(self.TREE.root)
matrices_decomposed_m: list[Gate] = []
if matrices_decomposed:
matrices_decomposed_m, final_graph = self.z_extraction(
matrices_decomposed, final_graph, self.phase_propagation
)
else:
pass
finally:
matrices_decomposed, best_cost, final_graph = self.TREE.retrieve_decomposition(self.TREE.root)
matrices_decomposed_m: list[Gate] = []
if matrices_decomposed != []:
matrices_decomposed_m, final_graph = self.z_extraction(
matrices_decomposed, final_graph, self.phase_propagation
)
else:
pass

self.TREE.print_tree(self.TREE.root, "TREE: ")
self.TREE.print_tree(self.TREE.root, "TREE: ")

return matrices_decomposed_m, best_cost, final_graph # noqa: B012
return matrices_decomposed_m, best_cost, final_graph

def z_extraction(
self, decomposition: list[TreeNode], placement: LevelGraph, phase_propagation: bool
Expand Down Expand Up @@ -209,21 +209,12 @@ def dfs(self, current_root: TreeNode, level: int = 0) -> None:

raise SequenceFoundError(current_root.key)

################################################
###############
#########

# BEGIN SEARCH

u_ = current_root.u_of_level

dimension = u_.shape[0]
for c, r, r2 in itertools.product(range(dimension), range(dimension), range(dimension)):
if r < c or r2 <= r:
continue
# for c in range(dimension):
# for r in range(c, dimension):
# for r2 in range(r + 1, dimension):
if abs(u_[r2, c]) > 1.0e-8 and (abs(u_[r, c]) > 1.0e-18 or abs(u_[r, c]) == 0):
theta = 2 * np.arctan2(abs(u_[r2, c]), abs(u_[r, c]))

Expand All @@ -249,12 +240,8 @@ def dfs(self, current_root: TreeNode, level: int = 0) -> None:
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
# branch_condition_2 = current_root.max_cost[0] - next_step_cost
# deprecated: FIRST IS ALGORITHMIC 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

Expand All @@ -267,25 +254,9 @@ def dfs(self, current_root: TreeNode, level: int = 0) -> None:
[new_placement.nodes[r]["lpmap"], new_placement.nodes[r2]["lpmap"], theta, phi],
self.dimension,
)
# R(theta, phi, new_placement.nodes[r]['lpmap'],
# new_placement.nodes[r2]['lpmap'], dimension)
#
physical_rotation = gate_chain_condition(pi_pulses_routing, physical_rotation)
physical_rotation = graph_rule_ongate(physical_rotation, new_placement)

""""# take care of phases accumulated by not pi-pulsing back
p_backs = []
for ppulse in pi_pulses_routing:
p_backs.append(
gates.R(
self.circuit,
"R",
self.qudit_index,
[ppulse.lev_a, ppulse.lev_b, ppulse.theta, -ppulse.phi],
self.dimension,
)
)
"""
p_backs = [
gates.R(
self.circuit,
Expand All @@ -296,7 +267,6 @@ def dfs(self, current_root: TreeNode, level: int = 0) -> None:
)
for ppulse in pi_pulses_routing
]
# p_backs.append(R(ppulse.theta, -ppulse.phi, ppulse.lev_a, ppulse.lev_b, dimension))

for p_back in p_backs:
graph_rule_update(p_back, new_placement)
Expand All @@ -312,10 +282,6 @@ def dfs(self, current_root: TreeNode, level: int = 0) -> None:
pi_pulses_routing,
)

# ===============CONTINUE SEARCH ON CHILDREN========================================
if current_root.children is not None:
for child in current_root.children:
self.dfs(child, level + 1)
# ===================================================================================

# END OF RECURSION#
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
from mqt.qudits.core.micro_dd import MicroDDNode, NodeContribution
from mqt.qudits.quantum_circuit import QuantumCircuit

complex_array = NDArray[np.complex128]


def find_complex_number(x: complex, c: complex) -> complex:
a = x.real # Real part of x
Expand Down Expand Up @@ -142,9 +140,9 @@ def synthesis(
return
if node.weight is not None:
rotations = self.retrieve_local_sequence(node.weight, node.children)

for key in sorted(rotations.keys()):
circuit_meta.append(Operation(controls, labels[depth], key, rotations[key])) # noqa: PERF401
circuit_meta.extend([
Operation(controls, labels[depth], key, rotations[key]) for key in sorted(rotations.keys())
])

if not node.reduced:
for i in range(cardinalities[depth]):
Expand Down
3 changes: 3 additions & 0 deletions src/mqt/qudits/quantum_circuit/components/classic_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ def __getitem__(self, key: int | slice) -> int | list[int]:
return [self.local_sitemap[i] for i in range(start, stop)]

return self.local_sitemap[key]


__all__ = ["ClSitemap", "ClassicRegister"]
15 changes: 10 additions & 5 deletions src/mqt/qudits/quantum_circuit/gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ def __init__(
params: Parameter = None,
control_set: ControlData | None = None,
label: str | None = None,
lev_a: int = 0,
lev_b: int = 0,
theta: float = 0.0,
phi: float = 0.0,
qasm_tag: str = "",
) -> None:
self.dagger = False
self.parent_circuit = circuit
Expand All @@ -55,11 +60,11 @@ def __init__(

self.is_long_range = self.check_long_range()
# inheritable parameters
self.lev_a: int = 0
self.lev_b: int = 0
self.theta: float = 0.0
self.phi: float = 0.0
self.qasm_tag = ""
self.lev_a: int = lev_a
self.lev_b: int = lev_b
self.theta: float = theta
self.phi: float = phi
self.qasm_tag = qasm_tag

@property
def reference_lines(self) -> list[int]:
Expand Down
2 changes: 1 addition & 1 deletion src/mqt/qudits/quantum_circuit/gates/csum.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def __init__(
target_qudits=target_qudits,
dimensions=dimensions,
control_set=controls,
qasm_tag="csum",
)
self.qasm_tag = "csum"

def __array__(self) -> NDArray[np.complex128, np.complex128]: # noqa: PLW3201
qudits_targeted = cast(list[int], self.target_qudits)
Expand Down
3 changes: 1 addition & 2 deletions src/mqt/qudits/quantum_circuit/gates/custom_multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@ def __init__(
dimensions=dimensions,
control_set=controls,
params=parameters,
qasm_tag="cumulti",
)
self.__array_storage: NDArray = None
if self.validate_parameter(parameters):
self.__array_storage = parameters

self.qasm_tag = "cumulti"

def __array__(self) -> NDArray: # noqa: PLW3201
return self.__array_storage

Expand Down
2 changes: 1 addition & 1 deletion src/mqt/qudits/quantum_circuit/gates/custom_one.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ def __init__(
dimensions=dimensions,
control_set=controls,
params=parameters,
qasm_tag="cuone",
)
self.__array_storage: NDArray = None
if self.validate_parameter(parameters):
self.__array_storage = parameters
self.qasm_tag = "cuone"

def __array__(self) -> NDArray: # noqa: PLW3201
return self.__array_storage
Expand Down
2 changes: 1 addition & 1 deletion src/mqt/qudits/quantum_circuit/gates/custom_two.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ def __init__(
dimensions=dimensions,
control_set=controls,
params=parameters,
qasm_tag="cutwo",
)
self.__array_storage: NDArray = None
if self.validate_parameter(parameters):
self.__array_storage = parameters
self.qasm_tag = "cutwo"

def __array__(self) -> NDArray: # noqa: PLW3201
return self.__array_storage
Expand Down
9 changes: 4 additions & 5 deletions src/mqt/qudits/quantum_circuit/gates/cx.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ def __init__(
target_qudits=target_qudits,
dimensions=dimensions,
control_set=controls,
qasm_tag="cx",
lev_b=1,
params=[0, 1, 1, 0.0],
)
# if customized
if parameters is not None and self.validate_parameter(parameters):
Expand All @@ -45,11 +48,7 @@ def __init__(
# self.lev_a, self.lev_b, self.ctrl_lev, self.phi = parameters
self._params: list[int | float] = parameters
else:
# Default
self.lev_a, self.lev_b, self.ctrl_lev, self.phi = (0, 1, 1, 0.0)
self._params = [0, 1, 1, 0.0]

self.qasm_tag = "cx"
self.ctrl_lev = 1

def __array__(self) -> NDArray: # noqa: PLW3201
levels_swap_low: int = cast(int, self._params[0])
Expand Down
12 changes: 6 additions & 6 deletions src/mqt/qudits/quantum_circuit/gates/gellmann.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ def __init__(
dimensions: int,
controls: ControlData | None = None,
) -> None:
assert self.validate_parameter(parameters), "Invalid parameters"
super().__init__(
circuit=circuit,
name=name,
gate_type=GateTypes.SINGLE,
target_qudits=target_qudits,
dimensions=dimensions,
control_set=controls,
qasm_tag="gell",
lev_a=cast(int, parameters[0]),
lev_b=cast(int, parameters[1]),
params=parameters,
)
if self.validate_parameter(parameters):
self.lev_a = cast(int, parameters[0])
self.lev_b = cast(int, parameters[1])
self.type_m = cast(str, parameters[2])
self._params = parameters
self.qasm_tag = "gell"
self.type_m = cast(str, parameters[2])

def __array__(self) -> NDArray: # noqa: PLW3201
d = self._dimensions
Expand Down
2 changes: 1 addition & 1 deletion src/mqt/qudits/quantum_circuit/gates/h.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def __init__(
target_qudits=target_qudits,
dimensions=dimensions,
control_set=controls,
qasm_tag="h",
)
self.qasm_tag = "h"

def __array__(self) -> NDArray: # noqa: PLW3201
matrix = np.zeros((self.dimensions, self.dimensions), dtype="complex")
Expand Down
4 changes: 3 additions & 1 deletion src/mqt/qudits/quantum_circuit/gates/ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ def __init__(
target_qudits=target_qudits,
dimensions=dimensions,
control_set=controls,
qasm_tag="ls",
params=parameters,
theta=parameters[0],
)
if self.validate_parameter(parameters):
self.theta = parameters[0]
self._params = parameters
self.qasm_tag = "ls"

def __array__(self) -> NDArray: # noqa: PLW3201
dimension_0 = self.dimensions[0]
Expand Down
4 changes: 3 additions & 1 deletion src/mqt/qudits/quantum_circuit/gates/ms.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ def __init__(
target_qudits=target_qudits,
dimensions=dimensions,
control_set=controls,
qasm_tag="ms",
params=parameters,
theta=parameters[0],
)
if self.validate_parameter(parameters):
self.theta = parameters[0]
self._params = parameters
self.qasm_tag = "ms"

def __array__(self) -> NDArray: # noqa: PLW3201
theta = self.theta
Expand Down
3 changes: 2 additions & 1 deletion src/mqt/qudits/quantum_circuit/gates/perm.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ def __init__(
target_qudits=target_qudits,
dimensions=dimensions,
control_set=controls,
params=parameters,
qasm_tag="pm",
)
if self.validate_parameter(parameters):
self.perm_data = parameters
self._params = parameters
self.qasm_tag = "pm"

def __array__(self) -> NDArray: # noqa: PLW3201
return np.eye(self.dimensions)[:, self.perm_data]
Expand Down
2 changes: 1 addition & 1 deletion src/mqt/qudits/quantum_circuit/gates/r.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def __init__(
target_qudits=target_qudits,
dimensions=dimensions,
control_set=controls,
qasm_tag="rxy",
)

if self.validate_parameter(parameters):
Expand All @@ -44,7 +45,6 @@ def __init__(
self.lev_a, self.lev_b = self.levels_setter(self.original_lev_a, self.original_lev_b)
self.theta = regulate_theta(self.theta)
self._params = parameters
self.qasm_tag = "rxy"

def __array__(self) -> NDArray: # noqa: PLW3201
dimension = self.dimensions
Expand Down
2 changes: 1 addition & 1 deletion src/mqt/qudits/quantum_circuit/gates/randu.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def __init__(
target_qudits=target_qudits,
dimensions=dimensions,
control_set=controls,
qasm_tag="rdu",
)
self.qasm_tag = "rdu"

def __array__(self) -> NDArray[np.complex128, np.complex128]: # noqa: PLW3201
dim = reduce(operator.mul, self.dimensions)
Expand Down
2 changes: 1 addition & 1 deletion src/mqt/qudits/quantum_circuit/gates/rh.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ def __init__(
target_qudits=target_qudits,
dimensions=dimensions,
control_set=controls,
qasm_tag="rh",
)
if self.validate_parameter(parameters):
self.original_lev_a = parameters[0]
self.original_lev_b = parameters[1]
self.lev_a, self.lev_b = self.levels_setter(self.original_lev_a, self.original_lev_b)
self._params = parameters
self.qasm_tag = "rh"

def __array__(self) -> NDArray: # noqa: PLW3201
# (R(-np.pi, 0, l1, l2, dim) * R(np.pi / 2, np.pi / 2, l1, l2, dim))
Expand Down
Loading

0 comments on commit f4369ea

Please sign in to comment.