From a668c4976e00b96ac53edaf7851f5d0bce0e742f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 25 Nov 2024 17:38:39 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=E2=AC=86=EF=B8=8F=F0=9F=AA=9D=20update=20p?= =?UTF-8?q?re-commit=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/astral-sh/ruff-pre-commit: v0.7.4 → v0.8.0](https://github.com/astral-sh/ruff-pre-commit/compare/v0.7.4...v0.8.0) - [github.com/henryiii/validate-pyproject-schema-store: 2024.11.18 → 2024.11.22](https://github.com/henryiii/validate-pyproject-schema-store/compare/2024.11.18...2024.11.22) --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ee41734..59264af 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -58,7 +58,7 @@ repos: # Python linting using ruff - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.7.4 + rev: v0.8.0 hooks: - id: ruff args: ["--fix", "--show-fixes"] @@ -123,6 +123,6 @@ repos: # Check the pyproject.toml file - repo: https://github.com/henryiii/validate-pyproject-schema-store - rev: 2024.11.18 + rev: 2024.11.22 hooks: - id: validate-pyproject From 715d807b80b23064cb2b0ba9e4d9796a8e18b117 Mon Sep 17 00:00:00 2001 From: Nils Quetschlich Date: Sat, 30 Nov 2024 08:55:30 +0100 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=91=B7=20update=20precommit=20config?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .pre-commit-config.yaml | 10 +- pyproject.toml | 5 +- scripts/Knapsack/Knapsack.py | 14 +- scripts/linear_regression/LinearRegression.py | 8 +- src/mqt/qao/constraints.py | 180 +++++++++--------- src/mqt/qao/objectivefunction.py | 2 +- tests/test_evaluation.py | 28 +-- 7 files changed, 128 insertions(+), 119 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 59264af..6492cdf 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -58,7 +58,7 @@ repos: # Python linting using ruff - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.8.0 + rev: v0.8.1 hooks: - id: ruff args: ["--fix", "--show-fixes"] @@ -86,14 +86,14 @@ repos: # Format configuration files with prettier - repo: https://github.com/rbubley/mirrors-prettier - rev: v3.3.3 + rev: v3.4.1 hooks: - id: prettier types_or: [yaml, markdown, html, css, scss, javascript, json] # Check for spelling - repo: https://github.com/crate-ci/typos - rev: typos-dict-v0.11.35 + rev: v1.28.1 hooks: - id: typos @@ -115,7 +115,7 @@ repos: # Check JSON schemata - repo: https://github.com/python-jsonschema/check-jsonschema - rev: 0.29.4 + rev: 0.30.0 hooks: - id: check-dependabot - id: check-github-workflows @@ -123,6 +123,6 @@ repos: # Check the pyproject.toml file - repo: https://github.com/henryiii/validate-pyproject-schema-store - rev: 2024.11.22 + rev: 2024.11.25 hooks: - id: validate-pyproject diff --git a/pyproject.toml b/pyproject.toml index 70dc60b..932b696 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -186,20 +186,17 @@ extend-select = [ "SLF", # flake8-self "SLOT", # flake8-slots "SIM", # flake8-simplify - "TCH", # flake8-type-checking + "TC", # flake8-type-checking "TID251", # flake8-tidy-imports.banned-api "TRY", # tryceratops "UP", # pyupgrade "YTT", # flake8-2020 ] ignore = [ - "ANN101", # Missing type annotation for `self` in method - "ANN102", # Missing type annotation for `cls` in classmethod "ISC001", # Conflicts with formatter "PLR09", # Too many <...> "PLR2004", # Magic value used in comparison "PLC0415", # Import should be at top of file - "PT004", # Incorrect, just usefixtures instead. "S101", # Use of assert detected "S404", # `subprocess` module is possibly insecure ] diff --git a/scripts/Knapsack/Knapsack.py b/scripts/Knapsack/Knapsack.py index 78b7dd4..0949866 100644 --- a/scripts/Knapsack/Knapsack.py +++ b/scripts/Knapsack/Knapsack.py @@ -1,16 +1,17 @@ from __future__ import annotations import locale -import os from pathlib import Path -from typing import cast +from typing import TYPE_CHECKING, cast import numpy as np -from sympy import Expr # for managing symbols from mqt.qao import Constraints, ObjectiveFunction, Problem, Solver, Variables +if TYPE_CHECKING: + from sympy import Expr + lambdas_method = [ "upper_bound_only_positive", "maximum_coefficient", @@ -44,8 +45,7 @@ ("upper lower bound posiform and negaform method", "binary search penalty algorithm"), ] -files = os.listdir("Data/") -for file in files: +for file in Path("Data/").iterdir(): print(file) with Path("./Data/" + file).open("r", encoding=locale.getpreferredencoding(False)) as f: lines = f.readlines() @@ -65,7 +65,7 @@ variables = Variables() obj = variables.add_binary_variables_array("obj", [objects]) objective_function = ObjectiveFunction() - objective_function.add_objective_function(cast(Expr, np.dot(np.transpose(obj), p_arr)), minimization=False) + objective_function.add_objective_function(cast("Expr", np.dot(np.transpose(obj), p_arr)), minimization=False) constraint = Constraints() constraint.add_constraint(str(np.dot(np.transpose(obj), w_arr)) + " <= " + format(W_max)) problem = Problem() @@ -85,7 +85,7 @@ variables = Variables() obj = variables.add_binary_variables_array("obj", [objects]) objective_function = ObjectiveFunction() - objective_function.add_objective_function(cast(Expr, np.dot(np.transpose(obj), p_arr)), minimization=False) + objective_function.add_objective_function(cast("Expr", np.dot(np.transpose(obj), p_arr)), minimization=False) constraint = Constraints() constraint.add_constraint(str(np.dot(np.transpose(obj), w_arr)) + " <= " + format(W_max)) problem = Problem() diff --git a/scripts/linear_regression/LinearRegression.py b/scripts/linear_regression/LinearRegression.py index 724e03e..9f925c8 100644 --- a/scripts/linear_regression/LinearRegression.py +++ b/scripts/linear_regression/LinearRegression.py @@ -1,16 +1,18 @@ from __future__ import annotations -from typing import cast +from typing import TYPE_CHECKING, cast import numpy as np import pandas as pd from sklearn.decomposition import PCA from sklearn.utils import shuffle -from sympy import Expr # for managing symbols from mqt.qao import Constraints, ObjectiveFunction, Problem, Solver, Variables +if TYPE_CHECKING: + from sympy import Expr + df = pd.read_csv("iris_csv.csv") df = shuffle(df) d = len(df.columns) - 1 @@ -48,7 +50,7 @@ objective_function = ObjectiveFunction() objective_function.add_objective_function( cast( - Expr, + "Expr", ( np.dot(np.dot(np.dot(np.transpose(w), np.transpose(X_training)), X_training), w) - 2 * np.dot(np.dot(np.transpose(w), np.transpose(X_training)), Y_training) diff --git a/src/mqt/qao/constraints.py b/src/mqt/qao/constraints.py index 303f230..3d30554 100644 --- a/src/mqt/qao/constraints.py +++ b/src/mqt/qao/constraints.py @@ -205,11 +205,11 @@ def _not_constraint(self, elem: tuple[str, bool, bool, bool], var: Variables) -> print("Wrong constraint format\n") return False a = self._convert_expression_logic( - expand(cast(Expr, (el[0]))).evalf(), + expand(cast("Expr", (el[0]))).evalf(), var.binary_variables_name_weight, ) b = self._convert_expression_logic( - expand(cast(Expr, (el[1]))).evalf(), + expand(cast("Expr", (el[1]))).evalf(), var.binary_variables_name_weight, ) ret = 2 * a * b - a - b + 1 @@ -236,8 +236,8 @@ def _not_constraint_check(self, constraint: str, variables: Variables, solution: bool -- saying if the constraint is satisfied or not """ el = (constraint).replace("~", "").split("=") - expr1 = expand(cast(Expr, (el[0]))).evalf() - expr2 = expand(cast(Expr, (el[1]))).evalf() + expr1 = expand(cast("Expr", (el[0]))).evalf() + expr2 = expand(cast("Expr", (el[1]))).evalf() expr1_to_sub = bool(not str(expr1).replace(".", "").isnumeric()) expr2_to_sub = bool(not str(expr2).replace(".", "").isnumeric()) if expr2_to_sub or expr1_to_sub: @@ -274,8 +274,8 @@ def _not_constraint_check_no_sub(constraint: str, solution: dict[str, int]) -> b bool -- saying if the constraint is satisfied or not """ el = (constraint).replace("~", "").split("=") - expr1 = expand(cast(Expr, (el[0]))).evalf() - expr2 = expand(cast(Expr, (el[1]))).evalf() + expr1 = expand(cast("Expr", (el[0]))).evalf() + expr2 = expand(cast("Expr", (el[1]))).evalf() expr1_to_sub = True if not str(expr1).replace(".", "").isnumeric(): symbols_in_the_expression1 = expr1.free_symbols @@ -327,15 +327,15 @@ def _and_constraint(self, elem: tuple[str, bool, bool, bool], var: Variables) -> print("Wrong constraint format\n") return False a = self._convert_expression_logic( - expand(cast(Expr, (el2[0]))).evalf(), + expand(cast("Expr", (el2[0]))).evalf(), var.binary_variables_name_weight, ) b = self._convert_expression_logic( - expand(cast(Expr, (el2[1]))).evalf(), + expand(cast("Expr", (el2[1]))).evalf(), var.binary_variables_name_weight, ) c = self._convert_expression_logic( - expand(cast(Expr, (el[1]))).evalf(), + expand(cast("Expr", (el[1]))).evalf(), var.binary_variables_name_weight, ) ret = a * b - 2 * (a + b) * c + 3 * c @@ -374,9 +374,9 @@ def _and_constraint_check(self, constraint: str, variables: Variables, solution: if len(el2) != 2: print("Wrong constraint format\n") return False - expr1 = expand(cast(Expr, (el2[0]))).evalf() - expr2 = expand(cast(Expr, (el2[1]))).evalf() - expr3 = expand(cast(Expr, (el[1]))).evalf() + expr1 = expand(cast("Expr", (el2[0]))).evalf() + expr2 = expand(cast("Expr", (el2[1]))).evalf() + expr3 = expand(cast("Expr", (el[1]))).evalf() expr1_to_sub = bool(not str(expr1).replace(".", "").isnumeric()) expr2_to_sub = bool(not str(expr2).replace(".", "").isnumeric()) expr3_to_sub = bool(not str(expr3).replace(".", "").isnumeric()) @@ -422,9 +422,9 @@ def _and_constraint_check_no_sub(constraint: str, solution: dict[str, int]) -> b if len(el2) != 2: print("Wrong constraint format\n") return False - expr1 = expand(cast(Expr, (el2[0]))).evalf() - expr2 = expand(cast(Expr, (el2[1]))).evalf() - expr3 = expand(cast(Expr, (el[1]))).evalf() + expr1 = expand(cast("Expr", (el2[0]))).evalf() + expr2 = expand(cast("Expr", (el2[1]))).evalf() + expr3 = expand(cast("Expr", (el[1]))).evalf() expr1_to_sub = True if not str(expr1).replace(".", "").isnumeric(): symbols_in_the_expression1 = expr1.free_symbols @@ -483,15 +483,15 @@ def _or_constraint(self, elem: tuple[str, bool, bool, bool], var: Variables) -> print("Wrong constraint format\n") return False a = self._convert_expression_logic( - expand(cast(Expr, (el2[0]))).evalf(), + expand(cast("Expr", (el2[0]))).evalf(), var.binary_variables_name_weight, ) b = self._convert_expression_logic( - expand(cast(Expr, (el2[1]))).evalf(), + expand(cast("Expr", (el2[1]))).evalf(), var.binary_variables_name_weight, ) c = self._convert_expression_logic( - expand(cast(Expr, (el[1]))).evalf(), + expand(cast("Expr", (el[1]))).evalf(), var.binary_variables_name_weight, ) ret = a * b + (a + b) * (1 - 2 * c) + c @@ -529,9 +529,9 @@ def _or_constraint_check(self, constraint: str, variables: Variables, solution: if len(el2) != 2: print("Wrong constraint format\n") return False - expr1 = expand(cast(Expr, (el2[0]))).evalf() - expr2 = expand(cast(Expr, (el2[1]))).evalf() - expr3 = expand(cast(Expr, (el[1]))).evalf() + expr1 = expand(cast("Expr", (el2[0]))).evalf() + expr2 = expand(cast("Expr", (el2[1]))).evalf() + expr3 = expand(cast("Expr", (el[1]))).evalf() expr1_to_sub = bool(not str(expr1).replace(".", "").isnumeric()) expr2_to_sub = bool(not str(expr2).replace(".", "").isnumeric()) expr3_to_sub = bool(not str(expr3).replace(".", "").isnumeric()) @@ -577,9 +577,9 @@ def _or_constraint_check_no_sub(constraint: str, solution: dict[str, int]) -> bo if len(el2) != 2: print("Wrong constraint format\n") return False - expr1 = expand(cast(Expr, (el2[0]))).evalf() - expr2 = expand(cast(Expr, (el2[1]))).evalf() - expr3 = expand(cast(Expr, (el[1]))).evalf() + expr1 = expand(cast("Expr", (el2[0]))).evalf() + expr2 = expand(cast("Expr", (el2[1]))).evalf() + expr3 = expand(cast("Expr", (el[1]))).evalf() expr1_to_sub = True if not str(expr1).replace(".", "").isnumeric(): symbols_in_the_expression1 = expr1.free_symbols @@ -649,9 +649,9 @@ def _xor_constraint(self, elem: tuple[str, bool, bool, bool], var: Variables) -> if len(el2) != 2: print("Wrong constraint format\n") return False - a = self._convert_expression_logic(expand(cast(Expr, (el2[0]))).evalf(), var.binary_variables_name_weight) - b = self._convert_expression_logic(expand(cast(Expr, (el2[1]))).evalf(), var.binary_variables_name_weight) - c = self._convert_expression_logic(expand(cast(Expr, (el[1]))).evalf(), var.binary_variables_name_weight) + a = self._convert_expression_logic(expand(cast("Expr", (el2[0]))).evalf(), var.binary_variables_name_weight) + b = self._convert_expression_logic(expand(cast("Expr", (el2[1]))).evalf(), var.binary_variables_name_weight) + c = self._convert_expression_logic(expand(cast("Expr", (el[1]))).evalf(), var.binary_variables_name_weight) ret = a + b + c - 2 * a * c - 2 * b * c - 2 * a * b + 4 * a * b * c else: el = str(elem[0]).replace(" ", "").split("=") @@ -759,9 +759,9 @@ def _xor_constraint_check(self, constraint: str, variables: Variables, solution: if len(el2) != 2: print("Wrong constraint format\n") return False - expr1 = expand(cast(Expr, (el2[0]))).evalf() - expr2 = expand(cast(Expr, (el2[1]))).evalf() - expr3 = expand(cast(Expr, (el[1]))).evalf() + expr1 = expand(cast("Expr", (el2[0]))).evalf() + expr2 = expand(cast("Expr", (el2[1]))).evalf() + expr3 = expand(cast("Expr", (el[1]))).evalf() expr1_to_sub = bool(not str(expr1).replace(".", "").isnumeric()) expr2_to_sub = bool(not str(expr2).replace(".", "").isnumeric()) expr3_to_sub = bool(not str(expr3).replace(".", "").isnumeric()) @@ -807,9 +807,9 @@ def _xor_constraint_check_no_sub(constraint: str, solution: dict[str, int]) -> b if len(el2) != 2: print("Wrong constraint format\n") return False - expr1 = expand(cast(Expr, (el2[0]))).evalf() - expr2 = expand(cast(Expr, (el2[1]))).evalf() - expr3 = expand(cast(Expr, (el[1]))).evalf() + expr1 = expand(cast("Expr", (el2[0]))).evalf() + expr2 = expand(cast("Expr", (el2[1]))).evalf() + expr3 = expand(cast("Expr", (el[1]))).evalf() expr1_to_sub = True if not str(expr1).replace(".", "").isnumeric(): symbols_in_the_expression1 = expr1.free_symbols @@ -931,7 +931,7 @@ def _greq_constraint( exp = PUBO() if elem[3]: ret_func = self._convert_expression_prec( - expand(cast(Expr, (el[0] + "-" + el[1]))).evalf(), + expand(cast("Expr", (el[0] + "-" + el[1]))).evalf(), var.binary_variables_name_weight, var.variables_dict, ) @@ -940,7 +940,7 @@ def _greq_constraint( var_precision = ret_func[1] else: exp = self._convert_expression( - expand(cast(Expr, (el[0] + "-" + el[1]))).evalf(), + expand(cast("Expr", (el[0] + "-" + el[1]))).evalf(), var.binary_variables_name_weight, ) @@ -959,14 +959,14 @@ def _greq_constraint( precision = min(1, var_precision) if elem[3] else 1 aux_var = aux.add_continuous_variable("aux" + format(j), 0, max_val + const, precision) self.constraints, i = aux.move_to_binary(self.constraints, i, "__a") - exp_auxiliary = self._convert_expression(cast(Expr, (aux_var)), aux.binary_variables_name_weight) + exp_auxiliary = self._convert_expression(cast("Expr", (aux_var)), aux.binary_variables_name_weight) j += 1 ret = (exp - exp_auxiliary) ** 2, i, j, aux else: ret = (exp) ** 2, i, j, aux else: el = str(elem[0]).split(">=") - exp = expand(cast(Expr, (el[0] + "-" + el[1]))).evalf() + exp = expand(cast("Expr", (el[0] + "-" + el[1]))).evalf() _min_val, max_val, const = self._min_max_const_estimation(exp) if max_val + const > 0: if len(str(max_val + const).split(".")) == 2: @@ -978,15 +978,15 @@ def _greq_constraint( aux_var = aux.add_continuous_variable("aux" + format(j), 0, max_val + const, precision) j += 1 self.constraints, i = aux.move_to_binary(self.constraints, i, "__a") - exp_auxiliary = self._convert_expression(cast(Expr, (aux_var)), aux.binary_variables_name_weight) + exp_auxiliary = self._convert_expression(cast("Expr", (aux_var)), aux.binary_variables_name_weight) hamiltonian = self._expression_to_hamiltonian( - cast(Expr, exp - exp_auxiliary), + cast("Expr", exp - exp_auxiliary), list(var.binary_variables_name_weight.values()) + list(aux.binary_variables_name_weight.values()), ) ret = hamiltonian**2, i, j, aux else: hamiltonian = self._expression_to_hamiltonian( - cast(Expr, exp), list(var.binary_variables_name_weight.values()) + cast("Expr", exp), list(var.binary_variables_name_weight.values()) ) ret = hamiltonian**2, i, j, aux @@ -1004,8 +1004,8 @@ def _greq_constraint_check(self, constraint: str, variables: Variables, solution bool -- saying if the constraint is satisfied or not """ el = constraint.split(">=") - expr1 = expand(cast(Expr, (el[0]))).evalf() - expr2 = expand(cast(Expr, (el[1]))).evalf() + expr1 = expand(cast("Expr", (el[0]))).evalf() + expr2 = expand(cast("Expr", (el[1]))).evalf() expr1_to_sub = bool(not str(expr1).replace(".", "").isnumeric()) expr2_to_sub = bool(not str(expr2).replace(".", "").isnumeric()) if expr2_to_sub or expr1_to_sub: @@ -1036,8 +1036,8 @@ def _greq_constraint_check_no_sub(constraint: str, solution: dict[str, int]) -> bool -- saying if the constraint is satisfied or not """ el = (constraint).split(">=") - expr1 = expand(cast(Expr, (el[0]))).evalf() - expr2 = expand(cast(Expr, (el[1]))).evalf() + expr1 = expand(cast("Expr", (el[0]))).evalf() + expr2 = expand(cast("Expr", (el[1]))).evalf() expr1_to_sub = True if not str(expr1).replace(".", "").isnumeric(): symbols_in_the_expression1 = expr1.free_symbols @@ -1098,7 +1098,7 @@ def _lteq_constraint( exp = PUBO() if elem[3]: ret_func = self._convert_expression_prec( - expand(cast(Expr, (el[0] + "-" + el[1]))).evalf(), + expand(cast("Expr", (el[0] + "-" + el[1]))).evalf(), var.binary_variables_name_weight, var.variables_dict, ) @@ -1107,7 +1107,7 @@ def _lteq_constraint( var_precision = ret_func[1] else: exp = self._convert_expression( - expand(cast(Expr, (el[0] + "-" + el[1]))).evalf(), + expand(cast("Expr", (el[0] + "-" + el[1]))).evalf(), var.binary_variables_name_weight, ) min_val, _max_val, const = self._min_max_const_estimation(exp) @@ -1127,13 +1127,13 @@ def _lteq_constraint( aux_var = aux.add_continuous_variable("aux" + format(j), 0, const - min_val, precision) j += 1 self.constraints, i = aux.move_to_binary(self.constraints, i, "__a") - exp_auxiliary = self._convert_expression((cast(Expr, (aux_var))), aux.binary_variables_name_weight) + exp_auxiliary = self._convert_expression((cast("Expr", (aux_var))), aux.binary_variables_name_weight) ret = (exp + exp_auxiliary) ** 2, i, j, aux else: ret = (exp) ** 2, i, j, aux else: el = str(elem[0]).split("<=") - exp = expand(cast(Expr, (el[0] + "-" + el[1]))).evalf() + exp = expand(cast("Expr", (el[0] + "-" + el[1]))).evalf() min_val, _max_val, const = self._min_max_const_estimation(exp) const = -const if const - min_val > 0: @@ -1146,15 +1146,15 @@ def _lteq_constraint( aux_var = aux.add_continuous_variable("aux" + format(j), 0, const - min_val, precision) j += 1 self.constraints, i = aux.move_to_binary(self.constraints, i, "__a") - exp_auxiliary = self._convert_expression((cast(Expr, (aux_var))), aux.binary_variables_name_weight) + exp_auxiliary = self._convert_expression((cast("Expr", (aux_var))), aux.binary_variables_name_weight) hamiltonian = self._expression_to_hamiltonian( - cast(Expr, exp + exp_auxiliary), + cast("Expr", exp + exp_auxiliary), list(var.binary_variables_name_weight.values()) + list(aux.binary_variables_name_weight.values()), ) ret = hamiltonian**2, i, j, aux else: hamiltonian = self._expression_to_hamiltonian( - cast(Expr, exp), list(var.binary_variables_name_weight.values()) + cast("Expr", exp), list(var.binary_variables_name_weight.values()) ) ret = hamiltonian**2, i, j, aux return ret @@ -1171,8 +1171,8 @@ def _lteq_constraint_check(self, constraint: str, variables: Variables, solution bool -- saying if the constraint is satisfied or not """ el = constraint.split("<=") - expr1 = expand(cast(Expr, (el[0]))).evalf() - expr2 = expand(cast(Expr, (el[1]))).evalf() + expr1 = expand(cast("Expr", (el[0]))).evalf() + expr2 = expand(cast("Expr", (el[1]))).evalf() expr1_to_sub = bool(not str(expr1).replace(".", "").isnumeric()) expr2_to_sub = bool(not str(expr2).replace(".", "").isnumeric()) if expr2_to_sub or expr1_to_sub: @@ -1203,8 +1203,8 @@ def _lteq_constraint_check_no_sub(constraint: str, solution: dict[str, int]) -> bool -- saying if the constraint is satisfied or not """ el = (constraint).split("<=") - expr1 = expand(cast(Expr, (el[0]))).evalf() - expr2 = expand(cast(Expr, (el[1]))).evalf() + expr1 = expand(cast("Expr", (el[0]))).evalf() + expr2 = expand(cast("Expr", (el[1]))).evalf() expr1_to_sub = True if not str(expr1).replace(".", "").isnumeric(): symbols_in_the_expression1 = expr1.free_symbols @@ -1265,7 +1265,7 @@ def _grt_constraint( exp = PUBO() if elem[3]: ret_func = self._convert_expression_prec( - expand(cast(Expr, (el[0] + "-" + el[1]))).evalf(), + expand(cast("Expr", (el[0] + "-" + el[1]))).evalf(), var.binary_variables_name_weight, var.variables_dict, ) @@ -1274,7 +1274,7 @@ def _grt_constraint( var_precision = ret_func[1] else: exp = self._convert_expression( - expand(cast(Expr, (el[0] + "-" + el[1]))).evalf(), + expand(cast("Expr", (el[0] + "-" + el[1]))).evalf(), var.binary_variables_name_weight, ) _min_val, max_val, const = self._min_max_const_estimation(exp) @@ -1294,7 +1294,9 @@ def _grt_constraint( aux_var = aux.add_continuous_variable("aux" + format(j), precision, max_val + const, precision) j += 1 self.constraints, i = aux.move_to_binary(self.constraints, i, "__a") - exp_auxiliary = self._convert_expression((cast(Expr, (aux_var))), aux.binary_variables_name_weight) + exp_auxiliary = self._convert_expression( + (cast("Expr", (aux_var))), aux.binary_variables_name_weight + ) ret = (exp - exp_auxiliary) ** 2, i, j, aux else: ret = (exp - precision) ** 2, i, j, aux @@ -1302,7 +1304,7 @@ def _grt_constraint( ret = (exp) ** 2, i, j, aux else: el = str(elem[0]).split(">") - exp = expand(cast(Expr, (el[0] + "-" + el[1]))).evalf() + exp = expand(cast("Expr", (el[0] + "-" + el[1]))).evalf() _min_val, max_val, const = self._min_max_const_estimation(exp) if max_val + const > 0: if len(str(max_val + const).split(".")) == 2: @@ -1315,23 +1317,25 @@ def _grt_constraint( aux_var = aux.add_continuous_variable("aux" + format(j), precision, max_val + const, precision) j += 1 self.constraints, i = aux.move_to_binary(self.constraints, i, "__a") - exp_auxiliary = self._convert_expression((cast(Expr, (aux_var))), aux.binary_variables_name_weight) + exp_auxiliary = self._convert_expression( + (cast("Expr", (aux_var))), aux.binary_variables_name_weight + ) hamiltonian = self._expression_to_hamiltonian( - cast(Expr, exp - exp_auxiliary), + cast("Expr", exp - exp_auxiliary), list(var.binary_variables_name_weight.values()) + list(aux.binary_variables_name_weight.values()), ) ret = hamiltonian**2, i, j, aux else: hamiltonian = self._expression_to_hamiltonian( - cast(Expr, exp - precision), + cast("Expr", exp - precision), list(var.binary_variables_name_weight.values()) + list(aux.binary_variables_name_weight.values()), ) ret = hamiltonian**2, i, j, aux else: hamiltonian = self._expression_to_hamiltonian( - cast(Expr, exp), list(var.binary_variables_name_weight.values()) + cast("Expr", exp), list(var.binary_variables_name_weight.values()) ) ret = hamiltonian**2, i, j, aux return ret @@ -1348,8 +1352,8 @@ def _grt_constraint_check(self, constraint: str, variables: Variables, solution: bool -- saying if the constraint is satisfied or not """ el = constraint.split(">") - expr1 = expand(cast(Expr, (el[0]))).evalf() - expr2 = expand(cast(Expr, (el[1]))).evalf() + expr1 = expand(cast("Expr", (el[0]))).evalf() + expr2 = expand(cast("Expr", (el[1]))).evalf() expr1_to_sub = bool(not str(expr1).replace(".", "").isnumeric()) expr2_to_sub = bool(not str(expr2).replace(".", "").isnumeric()) if expr2_to_sub or expr1_to_sub: @@ -1380,8 +1384,8 @@ def _grt_constraint_check_no_sub(constraint: str, solution: dict[str, int]) -> b bool -- saying if the constraint is satisfied or not """ el = (constraint).split(">") - expr1 = expand(cast(Expr, (el[0]))).evalf() - expr2 = expand(cast(Expr, (el[1]))).evalf() + expr1 = expand(cast("Expr", (el[0]))).evalf() + expr2 = expand(cast("Expr", (el[1]))).evalf() expr1_to_sub = True if not str(expr1).replace(".", "").isnumeric(): symbols_in_the_expression1 = expr1.free_symbols @@ -1442,7 +1446,7 @@ def _lt_constraint( exp = PUBO() if elem[3]: ret_func = self._convert_expression_prec( - expand(cast(Expr, (el[0] + "-" + el[1]))).evalf(), + expand(cast("Expr", (el[0] + "-" + el[1]))).evalf(), var.binary_variables_name_weight, var.variables_dict, ) @@ -1451,7 +1455,7 @@ def _lt_constraint( var_precision = ret_func[1] else: exp = self._convert_expression( - expand(cast(Expr, (el[0] + "-" + el[1]))).evalf(), + expand(cast("Expr", (el[0] + "-" + el[1]))).evalf(), var.binary_variables_name_weight, ) min_val, _max_val, const = self._min_max_const_estimation(exp) @@ -1473,7 +1477,9 @@ def _lt_constraint( aux_var = aux.add_continuous_variable("aux" + format(j), precision, const - min_val, precision) j += 1 self.constraints, i = aux.move_to_binary(self.constraints, i, "__a") - exp_auxiliary = self._convert_expression((cast(Expr, (aux_var))), aux.binary_variables_name_weight) + exp_auxiliary = self._convert_expression( + (cast("Expr", (aux_var))), aux.binary_variables_name_weight + ) ret = (exp + exp_auxiliary) ** 2, i, j, aux else: ret = (exp + precision) ** 2, i, j, aux @@ -1481,7 +1487,7 @@ def _lt_constraint( ret = (exp) ** 2, i, j, aux else: el = str(elem[0]).split("<") - exp = expand(cast(Expr, (el[0] + "-" + el[1]))).evalf() + exp = expand(cast("Expr", (el[0] + "-" + el[1]))).evalf() min_val, _max_val, const = self._min_max_const_estimation(exp) const = -const if const - min_val > 0: @@ -1495,23 +1501,25 @@ def _lt_constraint( aux_var = aux.add_continuous_variable("aux" + format(j), precision, const - min_val, precision) j += 1 self.constraints, i = aux.move_to_binary(self.constraints, i, "__a") - exp_auxiliary = self._convert_expression((cast(Expr, (aux_var))), aux.binary_variables_name_weight) + exp_auxiliary = self._convert_expression( + (cast("Expr", (aux_var))), aux.binary_variables_name_weight + ) hamiltonian = self._expression_to_hamiltonian( - cast(Expr, exp + exp_auxiliary), + cast("Expr", exp + exp_auxiliary), list(var.binary_variables_name_weight.values()) + list(aux.binary_variables_name_weight.values()), ) ret = hamiltonian**2, i, j, aux else: hamiltonian = self._expression_to_hamiltonian( - cast(Expr, exp + precision), + cast("Expr", exp + precision), list(var.binary_variables_name_weight.values()) + list(aux.binary_variables_name_weight.values()), ) ret = hamiltonian**2, i, j, aux else: hamiltonian = self._expression_to_hamiltonian( - cast(Expr, exp), list(var.binary_variables_name_weight.values()) + cast("Expr", exp), list(var.binary_variables_name_weight.values()) ) ret = hamiltonian**2, i, j, aux return ret @@ -1528,8 +1536,8 @@ def _lt_constraint_check(self, constraint: str, variables: Variables, solution: bool -- saying if the constraint is satisfied or not """ el = constraint.split("<") - expr1 = expand(cast(Expr, (el[0]))).evalf() - expr2 = expand(cast(Expr, (el[1]))).evalf() + expr1 = expand(cast("Expr", (el[0]))).evalf() + expr2 = expand(cast("Expr", (el[1]))).evalf() expr1_to_sub = bool(not str(expr1).replace(".", "").isnumeric()) expr2_to_sub = bool(not str(expr2).replace(".", "").isnumeric()) if expr2_to_sub or expr1_to_sub: @@ -1560,8 +1568,8 @@ def _lt_constraint_check_no_sub(constraint: str, solution: dict[str, int]) -> bo bool -- saying if the constraint is satisfied or not """ el = (constraint).split("<") - expr1 = expand(cast(Expr, (el[0]))).evalf() - expr2 = expand(cast(Expr, (el[1]))).evalf() + expr1 = expand(cast("Expr", (el[0]))).evalf() + expr2 = expand(cast("Expr", (el[1]))).evalf() expr1_to_sub = True if not str(expr1).replace(".", "").isnumeric(): symbols_in_the_expression1 = expr1.free_symbols @@ -1606,15 +1614,15 @@ def _eq_constraint(self, elem: tuple[str, bool, bool, bool], var: Variables) -> if elem[2]: el = str(elem[0]).split("=") exp = self._convert_expression( - expand(cast(Expr, (el[0] + "-" + el[1]))).evalf(), + expand(cast("Expr", (el[0] + "-" + el[1]))).evalf(), var.binary_variables_name_weight, ) ret = (exp) ** 2 else: el = str(elem[0]).split("=") - exp = expand(cast(Expr, (el[0] + "-" + el[1]))).evalf() + exp = expand(cast("Expr", (el[0] + "-" + el[1]))).evalf() hamiltonian = self._expression_to_hamiltonian( - cast(Expr, exp), list(var.binary_variables_name_weight.values()) + cast("Expr", exp), list(var.binary_variables_name_weight.values()) ) ret = hamiltonian**2 return ret @@ -1631,8 +1639,8 @@ def _eq_constraint_check(self, constraint: str, variables: Variables, solution: bool -- saying if the constraint is satisfied or not """ el = constraint.split("=") - expr1 = expand(cast(Expr, (el[0]))).evalf() - expr2 = expand(cast(Expr, (el[1]))).evalf() + expr1 = expand(cast("Expr", (el[0]))).evalf() + expr2 = expand(cast("Expr", (el[1]))).evalf() expr1_to_sub = bool(not str(expr1).replace(".", "").isnumeric()) expr2_to_sub = bool(not str(expr2).replace(".", "").isnumeric()) if expr2_to_sub or expr1_to_sub: @@ -1663,8 +1671,8 @@ def _eq_constraint_check_no_sub(constraint: str, solution: dict[str, int]) -> bo bool -- saying if the constraint is satisfied or not """ el = (constraint).split("=") - expr1 = expand(cast(Expr, (el[0]))).evalf() - expr2 = expand(cast(Expr, (el[1]))).evalf() + expr1 = expand(cast("Expr", (el[0]))).evalf() + expr2 = expand(cast("Expr", (el[1]))).evalf() expr1_to_sub = True if not str(expr1).replace(".", "").isnumeric(): symbols_in_the_expression1 = expr1.free_symbols diff --git a/src/mqt/qao/objectivefunction.py b/src/mqt/qao/objectivefunction.py index f064fe0..f465d0e 100644 --- a/src/mqt/qao/objectivefunction.py +++ b/src/mqt/qao/objectivefunction.py @@ -32,7 +32,7 @@ def add_objective_function(self, objective_function: Expr, minimization: bool = """ if not minimization: objective_function = -objective_function - self.objective_functions.append((cast(Expr, expand(objective_function).evalf()), weight)) + self.objective_functions.append((cast("Expr", expand(objective_function).evalf()), weight)) @staticmethod def rewrite_powers(powers: list[str], var: Variables, to_add: float) -> float | bool: diff --git a/tests/test_evaluation.py b/tests/test_evaluation.py index ccc809b..280924e 100644 --- a/tests/test_evaluation.py +++ b/tests/test_evaluation.py @@ -3,14 +3,13 @@ from __future__ import annotations from pathlib import Path -from typing import cast +from typing import TYPE_CHECKING, cast import numpy as np import pytest # for managing symbols from qubovert import PUBO, boolean_var -from sympy import Expr from mqt.qao.constraints import Constraints from mqt.qao.objectivefunction import ObjectiveFunction @@ -18,6 +17,9 @@ from mqt.qao.solvers import Solution, Solver from mqt.qao.variables import Variables +if TYPE_CHECKING: + from sympy import Expr + def test_binary_only() -> None: """Test only the construction of binary variables""" @@ -293,7 +295,7 @@ def test_cost_function() -> None: a0 = variables.add_binary_variable("a") b0 = variables.add_discrete_variable("b", [-1, 1, 3]) c0 = variables.add_continuous_variable("c", -2, 2, 0.25, "", "") - cost_function = cast(Expr, a0 + b0 * c0 + c0**2) + cost_function = cast("Expr", a0 + b0 * c0 + c0**2) objective_function = ObjectiveFunction() objective_function.add_objective_function(cost_function) variables.move_to_binary(constraint.constraints) @@ -886,7 +888,7 @@ def test_problem(lambda_strategy: str) -> None: a0 = variables.add_binary_variable("a") b0 = variables.add_discrete_variable("b", [-1, 1, 3]) c0 = variables.add_continuous_variable("c", -2, 2, 0.25, "", "") - cost_function = cast(Expr, a0 + b0 * c0 + c0**2) + cost_function = cast("Expr", a0 + b0 * c0 + c0**2) objective_function = ObjectiveFunction() objective_function.add_objective_function(cost_function) constraint.add_constraint("c >= 1", True, True, False) @@ -984,7 +986,7 @@ def test_simulated_annealer_solver(lambda_strategy: str) -> None: a0 = variables.add_binary_variable("a") b0 = variables.add_discrete_variable("b", [-1, 1, 3]) c0 = variables.add_continuous_variable("c", -2, 2, 0.25, "", "") - cost_function = cast(Expr, a0 + b0 * c0 + c0**2) + cost_function = cast("Expr", a0 + b0 * c0 + c0**2) objective_function = ObjectiveFunction() objective_function.add_objective_function(cost_function) problem = Problem() @@ -1053,7 +1055,7 @@ def test_simulated_annealer_solver_constrained(lambda_strategy: str, constraint_ b0 = variables.add_discrete_variable("b", [-1, 1, 3]) c0 = variables.add_continuous_variable("c", -2, 2, 0.25) objective_function = ObjectiveFunction() - objective_function.add_objective_function(cast(Expr, a0 + b0 * c0 + c0**2)) + objective_function.add_objective_function(cast("Expr", a0 + b0 * c0 + c0**2)) constraint = Constraints() constraint.add_constraint(constraint_expr, variable_precision=True) problem = Problem() @@ -1145,7 +1147,7 @@ def test_simulated_annealer_solver_constrained_lambda_update_mechanism( b0 = variables.add_discrete_variable("b", [-1, 1, 3]) c0 = variables.add_continuous_variable("c", -2, 2, 0.25) objective_function = ObjectiveFunction() - objective_function.add_objective_function(cast(Expr, a0 + b0 * c0 + c0**2)) + objective_function.add_objective_function(cast("Expr", a0 + b0 * c0 + c0**2)) constraint = Constraints() constraint.add_constraint(constraint_expr, variable_precision=True) problem = Problem() @@ -1346,7 +1348,7 @@ def test_simulated_annealer_solver_constrained_lambda_update_mechanism_and_strat b0 = variables.add_discrete_variable("b", [-1, 1, 3]) c0 = variables.add_continuous_variable("c", -2, 2, 0.25) objective_function = ObjectiveFunction() - objective_function.add_objective_function(cast(Expr, a0 + b0 * c0 + c0**2)) + objective_function.add_objective_function(cast("Expr", a0 + b0 * c0 + c0**2)) constraint = Constraints() constraint.add_constraint(constraint_expr, variable_precision=True) problem = Problem() @@ -1475,7 +1477,7 @@ def test_predict_solver_basic() -> None: a0 = variables.add_binary_variable("a") b0 = variables.add_binary_variable("b") c0 = variables.add_binary_variable("c") - cost_function = cast(Expr, -a0 + 2 * b0 - 3 * c0 - 2 * a0 * c0 - 1 * b0 * c0) + cost_function = cast("Expr", -a0 + 2 * b0 - 3 * c0 - 2 * a0 * c0 - 1 * b0 * c0) objective_function = ObjectiveFunction() objective_function.add_objective_function(cost_function) problem = Problem() @@ -1503,7 +1505,7 @@ def test_gas_solver_basic() -> None: a0 = variables.add_binary_variable("a") b0 = variables.add_binary_variable("b") c0 = variables.add_binary_variable("c") - cost_function = cast(Expr, -a0 + 2 * b0 - 3 * c0 - 2 * a0 * c0 - 1 * b0 * c0) + cost_function = cast("Expr", -a0 + 2 * b0 - 3 * c0 - 2 * a0 * c0 - 1 * b0 * c0) objective_function = ObjectiveFunction() objective_function.add_objective_function(cost_function) problem = Problem() @@ -1531,7 +1533,7 @@ def test_qaoa_solver_qubo_basic() -> None: a0 = variables.add_binary_variable("a") b0 = variables.add_binary_variable("b") c0 = variables.add_binary_variable("c") - cost_function = cast(Expr, -a0 + 2 * b0 - 3 * c0 - 2 * a0 * c0 - 1 * b0 * c0) + cost_function = cast("Expr", -a0 + 2 * b0 - 3 * c0 - 2 * a0 * c0 - 1 * b0 * c0) objective_function = ObjectiveFunction() objective_function.add_objective_function(cost_function) problem = Problem() @@ -1561,7 +1563,7 @@ def test_vqe_solver_qubo_basic() -> None: a0 = variables.add_binary_variable("a") b0 = variables.add_binary_variable("b") c0 = variables.add_binary_variable("c") - cost_function = cast(Expr, -a0 + 2 * b0 - 3 * c0 - 2 * a0 * c0 - 1 * b0 * c0) + cost_function = cast("Expr", -a0 + 2 * b0 - 3 * c0 - 2 * a0 * c0 - 1 * b0 * c0) objective_function = ObjectiveFunction() objective_function.add_objective_function(cost_function) problem = Problem() @@ -1616,7 +1618,7 @@ def test_predict_solver_maxcut(problem_name: str) -> None: for i in range(nodes): for j in range(i + 1, nodes): cut += weight.item((i, j)) * (x[j] + x[i] - 2 * x[i] * x[j]) - objective_function.add_objective_function(cast(Expr, cut), minimization=False) + objective_function.add_objective_function(cast("Expr", cut), minimization=False) constraint = Constraints() problem = Problem() problem.create_problem(variables, constraint, objective_function)