Skip to content

Commit

Permalink
🎨 pre-commit fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pre-commit-ci[bot] committed Aug 25, 2024
1 parent 389c672 commit 7086c6b
Show file tree
Hide file tree
Showing 29 changed files with 334 additions and 311 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def __init__(
self.permutation_matrix_initial = None
self.permutation_matrix_final = None

def get_perm_matrix(self, nodes: list[int], mapping:list[int]) -> NDArray:
def get_perm_matrix(self, nodes: list[int], mapping: list[int]) -> NDArray:
# sum ( |phy> <log| )
perm = np.zeros((self.dimension, self.dimension))

Expand Down
22 changes: 11 additions & 11 deletions src/mqt/qudits/compiler/dit_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@

class QuditCompiler:
passes_enabled: typing.ClassVar = {
"PhyLocQRPass": PhyLocQRPass,
"PhyLocAdaPass": PhyLocAdaPass,
"LocQRPass": PhyLocQRPass,
"LocAdaPass": PhyLocAdaPass,
"LogLocQRPass": LogLocQRPass,
"ZPropagationOptPass": ZPropagationOptPass,
"ZRemovalOptPass": ZRemovalOptPass,
"LogEntQRCEXPass": LogEntQRCEXPass,
"PhyEntQRCEXPass": PhyEntQRCEXPass,
"PhyLocQRPass": PhyLocQRPass,
"PhyLocAdaPass": PhyLocAdaPass,
"LocQRPass": PhyLocQRPass,
"LocAdaPass": PhyLocAdaPass,
"LogLocQRPass": LogLocQRPass,
"ZPropagationOptPass": ZPropagationOptPass,
"ZRemovalOptPass": ZRemovalOptPass,
"LogEntQRCEXPass": LogEntQRCEXPass,
"PhyEntQRCEXPass": PhyEntQRCEXPass,
"NaiveLocResynthOptPass": NaiveLocResynthOptPass,
}

Expand Down Expand Up @@ -55,7 +55,7 @@ def compile(self, backend: Backend, circuit: QuantumCircuit, passes_names: list[

return circuit

def compile_O0(self, backend: Backend, circuit: QuantumCircuit) -> QuantumCircuit: #noqa: N802
def compile_O0(self, backend: Backend, circuit: QuantumCircuit) -> QuantumCircuit: # noqa: N802
passes = ["PhyLocQRPass", "PhyEntQRCEXPass"]
compiled = self.compile(backend, circuit, passes)

Expand All @@ -66,7 +66,7 @@ def compile_O0(self, backend: Backend, circuit: QuantumCircuit) -> QuantumCircui
return compiled

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

Expand Down
31 changes: 14 additions & 17 deletions src/mqt/qudits/compiler/onedit/local_operation_swap/swap_routine.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def graph_rule_ongate(gate: gates.R, graph: LevelGraph) -> gates.R:
new_g_phi += graph.nodes[logic_nodes[1]]["phase_storage"]

return gates.R(
gate.parent_circuit, "R", gate.target_qudits, [g_lev_a, g_lev_b, gate.theta, new_g_phi], gate.dimensions
gate.parent_circuit, "R", gate.target_qudits, [g_lev_a, g_lev_b, gate.theta, new_g_phi], gate.dimensions
)
# R(gate_matrix.theta, new_g_phi, g_lev_a, g_lev_b, gate_matrix.dimension)

Expand Down Expand Up @@ -111,16 +111,15 @@ def gate_chain_condition(previous_gates: R, current: R) -> R:
pass

return gates.R(
current.parent_circuit,
"R",
current.target_qudits,
[current.lev_a, current.lev_b, theta, phi],
current.dimensions,
current.parent_circuit,
"R",
current.target_qudits,
[current.lev_a, current.lev_b, theta, phi],
current.dimensions,
) # R(theta, phi, current.lev_a, current.lev_b, current.dimension)


def route_states2rotate_basic(gate: R,
orig_placement: LevelGraph) -> tuple[float, list[R], LevelGraph]:
def route_states2rotate_basic(gate: R, orig_placement: LevelGraph) -> tuple[float, list[R], LevelGraph]:
placement = orig_placement
dimension = gate.dimensions

Expand All @@ -139,19 +138,19 @@ def route_states2rotate_basic(gate: R,
phy_n_ip1 = placement.nodes[path[i + 1]]["lpmap"]

pi_gate_phy = gates.R(
gate.parent_circuit, "R", gate.target_qudits, [phy_n_i, phy_n_ip1, np.pi, -np.pi / 2], dimension
gate.parent_circuit, "R", gate.target_qudits, [phy_n_i, phy_n_ip1, np.pi, -np.pi / 2], dimension
) # R(np.pi, -np.pi / 2, phy_n_i, phy_n_ip1, dimension)

pi_gate_phy = gate_chain_condition(pi_pulses_routing, pi_gate_phy)
pi_gate_phy = graph_rule_ongate(pi_gate_phy, placement)

# -- COSTING based only on the position of the pi pulse and angle phase is neglected ----------------
pi_gate_logic = gates.R(
gate.parent_circuit,
"R",
gate.target_qudits,
[path[i], path[i + 1], pi_gate_phy.theta, pi_gate_phy.phi / 2],
dimension,
gate.parent_circuit,
"R",
gate.target_qudits,
[path[i], path[i + 1], pi_gate_phy.theta, pi_gate_phy.phi / 2],
dimension,
) # R(pi_gate_phy.theta, pi_gate_phy.phi, path[i], path[i + 1], dimension)
cost_of_pi_pulses += rotation_cost_calc(pi_gate_logic, placement)
# -----------------------------------------------------------------------------------------------------
Expand All @@ -165,9 +164,7 @@ def route_states2rotate_basic(gate: R,
return cost_of_pi_pulses, pi_pulses_routing, placement


def cost_calculator(gate: R,
placement: LevelGraph,
non_zeros: int) -> tuple[float, list[R], LevelGraph, float, float]:
def cost_calculator(gate: R, placement: LevelGraph, non_zeros: int) -> tuple[float, list[R], LevelGraph, float, float]:
cost_of_pi_pulses, pi_pulses_routing, new_placement = route_states2rotate_basic(gate, placement)
gate_cost = rotation_cost_calc(gate, new_placement)
total_costing = (gate_cost + cost_of_pi_pulses) * non_zeros
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,21 @@ def propagate_z(circuit: QuantumCircuit, line: list[Gate], back: bool) -> tuple[
# object is R
if back:
new_phi = pi_mod(
line[gate_index].phi + z_angles[line[gate_index].lev_a] - z_angles[line[gate_index].lev_b]
line[gate_index].phi + z_angles[line[gate_index].lev_a] - z_angles[line[gate_index].lev_b]
)
else:
new_phi = pi_mod(
line[gate_index].phi - z_angles[line[gate_index].lev_a] + z_angles[line[gate_index].lev_b]
line[gate_index].phi - z_angles[line[gate_index].lev_a] + z_angles[line[gate_index].lev_b]
)

list_of_x_yrots.append(
gates.R(
circuit,
"R",
qudit_index,
[line[gate_index].lev_a, line[gate_index].lev_b, line[gate_index].theta, new_phi],
dimension,
)
gates.R(
circuit,
"R",
qudit_index,
[line[gate_index].lev_a, line[gate_index].lev_b, line[gate_index].theta, new_phi],
dimension,
)
)
# list_of_XYrots.append(R(line[gate_index].theta, new_phi,
# line[gate_index].lev_a, line[gate_index].lev_b, line[gate_index].dimension))
Expand Down Expand Up @@ -113,7 +113,7 @@ def remove_z(self, original_circuit: QuantumCircuit, back: bool | None = True) -

for interval in intervals:
if len(interval) > 1:
sequence: list[Gate] = circuit.instructions[interval[0]: interval[-1] + 1]
sequence: list[Gate] = circuit.instructions[interval[0] : interval[-1] + 1]
fixed_seq: list[R]
z_tail: list[VirtRz]
fixed_seq, z_tail = self.propagate_z(circuit, sequence, back)
Expand All @@ -122,7 +122,7 @@ def remove_z(self, original_circuit: QuantumCircuit, back: bool | None = True) -
else:
new_instructions[interval[0]: interval[-1] + 1] = fixed_seq + z_tail"""
combined_seq = z_tail + fixed_seq if back else fixed_seq + z_tail
new_instructions[interval[0]: interval[-1] + 1] = []
new_instructions[interval[0] : interval[-1] + 1] = []
new_instructions.extend(combined_seq)

return circuit.set_instructions(new_instructions)
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def execute(self) -> tuple[list[gates.R | gates.VirtRz], tuple[int, int], LevelG

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

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

Check warning

Code scanning / CodeQL

'break' or 'return' statement in finally Warning

'return' in a finally block will swallow any exceptions raised.

def z_extraction(
self, decomposition: list[TreeNode], placement: LevelGraph, phase_propagation: bool
Expand Down Expand Up @@ -170,11 +170,11 @@ def z_extraction(
theta_z = new_mod(placement.nodes[i]["phase_storage"])
if abs(theta_z) > 1.0e-4:
phase_gate = gates.VirtRz(
self.circuit,
"VRz",
self.qudit_index,
[placement.nodes[i]["lpmap"], theta_z],
self.dimension,
self.circuit,
"VRz",
self.qudit_index,
[placement.nodes[i]["lpmap"], theta_z],
self.dimension,
) # VirtRz(thetaZ, placement.nodes[i]['lpmap'],
# dimension)
matrices.append(phase_gate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,11 @@ def execute(self) -> tuple[list[gates.R | gates.VirtRz], tuple[int, int], LevelG

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

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

Check warning

Code scanning / CodeQL

'break' or 'return' statement in finally Warning

'return' in a finally block will swallow any exceptions raised.

def z_extraction(
self, decomposition: list[TreeNode], placement: LevelGraph) -> tuple[list[Gate], LevelGraph]: # phase_propagation: bool
self, decomposition: list[TreeNode], placement: LevelGraph
) -> tuple[list[Gate], LevelGraph]: # phase_propagation: bool
matrices = []

for d in decomposition[1:]:
Expand Down
37 changes: 18 additions & 19 deletions src/mqt/qudits/compiler/state_compilation/state_preparation.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def find_complex_number(x: complex, c: complex) -> complex:
b = x.imag # Imaginary part of x

# Calculate z
real_part = (c.real - b * c.imag) / (a ** 2 + b ** 2)
imag_part = (c.imag + b * c.real) / (a ** 2 + b ** 2)
real_part = (c.real - b * c.imag) / (a**2 + b**2)
imag_part = (c.imag + b * c.real) / (a**2 + b**2)
return complex(real_part, imag_part)


Expand All @@ -44,7 +44,7 @@ def get_angles(from_: complex, to_: complex) -> tuple[float, float]:

class Operation:
def __init__(
self, controls: list[tuple[int, int]], qudit: int, levels: tuple[int, int], angles: tuple[float, float]
self, controls: list[tuple[int, int]], qudit: int, levels: tuple[int, int], angles: tuple[float, float]
) -> None:
self._controls = controls
self._qudit = qudit
Expand Down Expand Up @@ -109,7 +109,7 @@ def __init__(self, quantum_circuit: QuantumCircuit, state: NDArray[np.complex128
self.approximation = approx

def retrieve_local_sequence(
self, fweight: complex, children: list[TreeNode]
self, fweight: complex, children: list[TreeNode]
) -> dict[tuple[int, int], tuple[float, float]]:
size = len(children)
qudit = children[0].value
Expand All @@ -129,13 +129,13 @@ def retrieve_local_sequence(
return aplog

def synthesis(
self,
labels: list[int],
cardinalities: list[int],
node: TreeNode,
circuit_meta: list[Operation],
controls: list[tuple[int, int]] | None = None,
depth: int = 0,
self,
labels: list[int],
cardinalities: list[int],
node: TreeNode,
circuit_meta: list[Operation],
controls: list[tuple[int, int]] | None = None,
depth: int = 0,
) -> None:
if controls is None:
controls = []
Expand All @@ -155,18 +155,17 @@ def synthesis(
self.synthesis(labels, cardinalities, node.children[i], circuit_meta, controls_track, depth + 1)
else:
self.synthesis(
labels,
cardinalities,
node.children[node.children_index[i]],
circuit_meta,
controls_track,
depth + 1,
labels,
cardinalities,
node.children[node.children_index[i]],
circuit_meta,
controls_track,
depth + 1,
)
else:
controls_track = copy.deepcopy(controls)
self.synthesis(
labels, cardinalities, node.children[node.children_index[0]], circuit_meta, controls_track,
depth + 1
labels, cardinalities, node.children[node.children_index[0]], circuit_meta, controls_track, depth + 1
)

def compile_state(self) -> QuantumCircuit:
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#!/usr/bin/env python3
#!/usr/bin/env python3
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from mqt.qudits.compiler.twodit.variational_twodit_compilation.opt import Optimizer

if typing.TYPE_CHECKING:

from mqt.qudits.quantum_circuit import QuantumCircuit
from mqt.qudits.quantum_circuit.gate import Gate

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ class Optimizer:
FUN_SOLUTION: typing.ClassVar = []

@classmethod
def set_class_variables(cls, target: NDArray[np.complex128] | None = None,
obj_fid: float = 1e-4,
dim_0: int | None = None,
dim_1: int | None = None,
layers: int | None = None) -> None:
def set_class_variables(
cls,
target: NDArray[np.complex128] | None = None,
obj_fid: float = 1e-4,
dim_0: int | None = None,
dim_1: int | None = None,
layers: int | None = None,
) -> None:
cls.OBJ_FIDELITY = obj_fid
cls.SINGLE_DIM_0 = dim_0
cls.SINGLE_DIM_1 = dim_1
Expand All @@ -46,8 +49,9 @@ def set_class_variables(cls, target: NDArray[np.complex128] | None = None,
cls.FUN_SOLUTION = []

@staticmethod
def bounds_assigner(b1: tuple[float, float], b2: tuple[float, float], b3: tuple[float, float],
num_params_single: int, d: int) -> list[tuple[float, float]]:
def bounds_assigner(
b1: tuple[float, float], b2: tuple[float, float], b3: tuple[float, float], num_params_single: int, d: int
) -> list[tuple[float, float]]:
assignment = [None] * (num_params_single + 1)

for m in range(d):
Expand All @@ -63,14 +67,14 @@ def bounds_assigner(b1: tuple[float, float], b2: tuple[float, float], b3: tuple[

@classmethod
def return_bounds(cls, num_layer_search: int = 1) -> list[tuple[float, float]]:
num_params_single_unitary_line_0 = -1 + Optimizer.SINGLE_DIM_0 ** 2
num_params_single_unitary_line_1 = -1 + Optimizer.SINGLE_DIM_1 ** 2
num_params_single_unitary_line_0 = -1 + Optimizer.SINGLE_DIM_0**2
num_params_single_unitary_line_1 = -1 + Optimizer.SINGLE_DIM_1**2

bounds_line_0 = Optimizer.bounds_assigner(
bound_1, bound_2, bound_3, num_params_single_unitary_line_0, Optimizer.SINGLE_DIM_0
bound_1, bound_2, bound_3, num_params_single_unitary_line_0, Optimizer.SINGLE_DIM_0
)
bounds_line_1 = Optimizer.bounds_assigner(
bound_1, bound_2, bound_3, num_params_single_unitary_line_1, Optimizer.SINGLE_DIM_1
bound_1, bound_2, bound_3, num_params_single_unitary_line_1, Optimizer.SINGLE_DIM_1
)

# Determine the length of the longest bounds list
Expand Down Expand Up @@ -113,9 +117,9 @@ def objective_fnc_cu(cls, lambdas: list[float]) -> list[float]:
return cls.obj_fun_core(ansatz, lambdas)

@classmethod
def solve_anneal(cls, bounds: list[tuple[float, float]],
ansatz_type: str,
result_queue: multiprocessing.Queue) -> None:
def solve_anneal(
cls, bounds: list[tuple[float, float]], ansatz_type: str, result_queue: multiprocessing.Queue
) -> None:
try:
if ansatz_type == "MS": # MS is 0
opt = dual_annealing(cls.objective_fnc_ms, bounds=bounds)
Expand Down
Loading

0 comments on commit 7086c6b

Please sign in to comment.