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 Dec 2, 2024
1 parent 3ff6189 commit 85f6790
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/mqt/qubomaker/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def __eq__(self, value: object) -> bool:
"""
if not isinstance(value, Graph):
return False
return cast(bool, np.array_equal(self.adjacency_matrix, value.adjacency_matrix))
return cast("bool", np.array_equal(self.adjacency_matrix, value.adjacency_matrix))

def __hash__(self) -> int:
"""Returns the hash of the graph.
Expand Down
26 changes: 16 additions & 10 deletions src/mqt/qubomaker/pathfinder/cost_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def doit(self, **_hints: Any) -> sp.Expr:
n, i = self.args
if not isinstance(n, sp.Integer) or not isinstance(i, sp.Integer):
return self
return cast(sp.Expr, sp.Integer(int(n) >> (int(i) - 1) & 1))
return cast("sp.Expr", sp.Integer(int(n) >> (int(i) - 1) & 1))


class ExpandingSum(sp.Sum):
Expand Down Expand Up @@ -288,7 +288,7 @@ def sum_set(expression: sp.Expr, variables: list[str], latex: str, callback: Set
expr = functools.reduce(
lambda total, new: total + expression.subs(dict(zip(variable_symbols, new))),
assignments,
cast(sp.Expr, sp.Integer(0)),
cast("sp.Expr", sp.Integer(0)),
)

if len(assignments) <= 1:
Expand Down Expand Up @@ -318,7 +318,7 @@ def adjacency(v: int | str | sp.Expr, w: int | str | sp.Expr) -> sp.Expr:
v = FormulaHelpers.variable(v)
if isinstance(w, str):
w = FormulaHelpers.variable(w)
return cast(sp.Expr, A(v, w))
return cast("sp.Expr", A(v, w))

@staticmethod
def variable(name: str) -> sp.Symbol:
Expand Down Expand Up @@ -354,7 +354,7 @@ def get_encoding_variable_one_hot(path: Any, vertex: Any, position: Any, _num_ve
vertex = FormulaHelpers.variable(vertex)
if isinstance(position, str):
position = FormulaHelpers.variable(position)
return cast(sp.Expr, X(path, vertex, position))
return cast("sp.Expr", X(path, vertex, position))

@staticmethod
def get_encoding_variable_domain_wall(path: Any, vertex: Any, position: Any, _num_vertices: int = 0) -> sp.Expr:
Expand Down Expand Up @@ -409,7 +409,7 @@ def get_encoding_variable_binary(path: Any, vertex: Any, position: Any, num_vert
index_symbol = FormulaHelpers.variable("w")
max_index = int(np.ceil(np.log2(num_vertices + 1)))
return cast(
sp.Expr,
"sp.Expr",
sp.Product(
Decompose(vertex, index_symbol)
* FormulaHelpers.get_encoding_variable_one_hot(path, index_symbol, position)
Expand Down Expand Up @@ -1223,7 +1223,7 @@ def get_formula_general(
),
["v", "w"],
"\\in E",
lambda: cast(list[Union[sp.Expr, int, float, tuple[Union[sp.Expr, int, float], ...]]], graph.all_edges),
lambda: cast("list[Union[sp.Expr, int, float, tuple[Union[sp.Expr, int, float], ...]]]", graph.all_edges),
)


Expand Down Expand Up @@ -1261,7 +1261,9 @@ def get_formula_general(
),
["v", "w"],
"\\not\\in E",
lambda: cast(list[Union[sp.Expr, int, float, tuple[Union[sp.Expr, int, float], ...]]], graph.non_edges),
lambda: cast(
"list[Union[sp.Expr, int, float, tuple[Union[sp.Expr, int, float], ...]]]", graph.non_edges
),
),
self.path_ids,
)
Expand Down Expand Up @@ -1296,7 +1298,7 @@ def get_formula_domain_wall(self, graph: Graph, settings: pathfinder.PathFinding
* FormulaHelpers.get_encoding_variable_one_hot("p", FormulaHelpers.variable("v") + 1, "i"),
["v"],
"\\in V",
cast(SetCallback, lambda: graph.all_vertices),
cast("SetCallback", lambda: graph.all_vertices),
),
settings.max_path_length,
),
Expand Down Expand Up @@ -1339,7 +1341,9 @@ def get_formula_general(
),
["v", "w"],
"\\in E",
lambda: cast(list[Union[sp.Expr, int, float, tuple[Union[sp.Expr, int, float], ...]]], graph.all_edges),
lambda: cast(
"list[Union[sp.Expr, int, float, tuple[Union[sp.Expr, int, float], ...]]]", graph.all_edges
),
),
self.path_ids,
)
Expand Down Expand Up @@ -1376,7 +1380,9 @@ def get_formula_general(
),
["v", "w"],
"\\in E",
lambda: cast(list[Union[sp.Expr, int, float, tuple[Union[sp.Expr, int, float], ...]]], graph.all_edges),
lambda: cast(
"list[Union[sp.Expr, int, float, tuple[Union[sp.Expr, int, float], ...]]]", graph.all_edges
),
),
self.path_ids,
)
Expand Down
10 changes: 5 additions & 5 deletions src/mqt/qubomaker/pathfinder/pathfinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def get_vertices_possibly_all(constraint: dict[str, Any]) -> list[int]:
vertices = constraint.get("vertices", [])
if len(vertices) == 0:
vertices = graph.all_vertices
return cast(list[int], vertices)
return cast("list[int]", vertices)

def get_edges_possibly_all(constraint: dict[str, Any]) -> list[tuple[int, int]]:
edges = [tuple(edge) for edge in constraint.get("edges", [])]
Expand Down Expand Up @@ -266,7 +266,7 @@ def _select_lambdas(self) -> list[tuple[sp.Expr, float]]:

def __optimal_lambda(self) -> float:
"""Compute the optimal lambda value for all penalties."""
return cast(float, np.max(self.graph.adjacency_matrix) * self.settings.max_path_length + 1)
return cast("float", np.max(self.graph.adjacency_matrix) * self.settings.max_path_length + 1)

@override
def _construct_expansion(self, expression: sp.Expr) -> sp.Expr:
Expand Down Expand Up @@ -314,9 +314,9 @@ def get_variable_index(self, var: sp.Expr) -> int:
if self.settings.encoding_type == cf.EncodingType.BINARY:
max_v = int(np.ceil(np.log2(self.graph.n_vertices + 1)))

p = int(cast(int, parts[0]))
v = int(cast(int, parts[1]))
i = int(cast(int, parts[2]))
p = int(cast("int", parts[0]))
v = int(cast("int", parts[1]))
i = int(cast("int", parts[2]))

return int((v - 1) + (i - 1) * max_v + (p - 1) * self.settings.max_path_length * max_v + 1)

Expand Down
6 changes: 3 additions & 3 deletions src/mqt/qubomaker/qubo_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def construct(self) -> sp.Expr:
sp.Expr: The mathematical representation of the QUBO formulation.
"""
return cast(
sp.Expr,
"sp.Expr",
functools.reduce(
lambda current, new: current + new[1] * new[0],
self._select_lambdas(),
Expand Down Expand Up @@ -125,7 +125,7 @@ def expand_higher_order_terms(self, expression: sp.Expr) -> sp.Expr:
new_term = self.__decrease_order(unpowered, auxiliary_dict)
result += new_term * coeffs[term]
self.auxiliary_cache = auxiliary_dict
return cast(sp.Expr, result)
return cast("sp.Expr", result)

@staticmethod
def __simplify_auxiliary_variables(expression: sp.Expr, auxiliary_dict: dict[sp.Expr, sp.Expr]) -> sp.Expr:
Expand Down Expand Up @@ -269,7 +269,7 @@ def construct_qubo_matrix(self) -> npt.NDArray[np.int_ | np.float64]:
def get_index(variable: sp.Expr) -> int:
if variable in all_variables:
return all_variables[variable] - 1
return auxiliary_variables.index(cast(sp.Symbol, variable)) + self.get_encoding_variable_count()
return auxiliary_variables.index(cast("sp.Symbol", variable)) + self.get_encoding_variable_count()

Check warning on line 272 in src/mqt/qubomaker/qubo_generator.py

View check run for this annotation

Codecov / codecov/patch

src/mqt/qubomaker/qubo_generator.py#L272

Added line #L272 was not covered by tests

for term, value in coefficients.items():
if isinstance(term, sp.Mul):
Expand Down
6 changes: 1 addition & 5 deletions src/mqt/qubomaker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@

from __future__ import annotations

from typing import TYPE_CHECKING, no_type_check
from typing import no_type_check

import numpy as np
import numpy.typing as npt
from IPython.display import Math, clear_output, display
from ipywidgets import widgets

if TYPE_CHECKING:
from collections.abc import Iterable


@no_type_check
def print_matrix(array: Iterable[Iterable[float]]) -> None:
Expand Down

0 comments on commit 85f6790

Please sign in to comment.