Skip to content

Commit

Permalink
Update code
Browse files Browse the repository at this point in the history
  • Loading branch information
DeborahVolpe committed Jun 18, 2024
1 parent 3883120 commit 83891d0
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 58 deletions.
72 changes: 36 additions & 36 deletions src/mqt/qao/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,13 @@ def _not_constraint_check(self, constraint: str, variables: Variables, solution:
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:
for var in solution:
for var, values in solution.items():

Check warning on line 243 in src/mqt/qao/constraints.py

View check run for this annotation

Codecov / codecov/patch

src/mqt/qao/constraints.py#L243

Added line #L243 was not covered by tests
if var in variables.variables_dict:
if isinstance(solution[var], float):
if isinstance(values, float):

Check warning on line 245 in src/mqt/qao/constraints.py

View check run for this annotation

Codecov / codecov/patch

src/mqt/qao/constraints.py#L245

Added line #L245 was not covered by tests
expr1, expr2 = self._constraint_check_sub_single_var(
expr1_to_sub, expr2_to_sub, expr1, expr2, solution[var], variables.variables_dict[var]
expr1_to_sub, expr2_to_sub, expr1, expr2, values, variables.variables_dict[var]
)
elif isinstance(solution[var], list):
elif isinstance(values, list):

Check warning on line 249 in src/mqt/qao/constraints.py

View check run for this annotation

Codecov / codecov/patch

src/mqt/qao/constraints.py#L249

Added line #L249 was not covered by tests
expr1, expr2 = self._constraint_check_sub_list_var(
solution,
variables,
Expand Down Expand Up @@ -380,20 +380,20 @@ def _and_constraint_check(self, constraint: str, variables: Variables, solution:
expr2_to_sub = bool(not str(expr2).replace(".", "").isnumeric())
expr3_to_sub = bool(not str(expr3).replace(".", "").isnumeric())
if expr2_to_sub or expr1_to_sub or expr3_to_sub:
for var in solution:
for var, value in solution.items():

Check warning on line 383 in src/mqt/qao/constraints.py

View check run for this annotation

Codecov / codecov/patch

src/mqt/qao/constraints.py#L383

Added line #L383 was not covered by tests
if var in variables.variables_dict:
if isinstance(solution[var], float):
if isinstance(value, float):

Check warning on line 385 in src/mqt/qao/constraints.py

View check run for this annotation

Codecov / codecov/patch

src/mqt/qao/constraints.py#L385

Added line #L385 was not covered by tests
expr1, expr2, expr3 = self._constraint_check_three_sub_single_var(
expr1_to_sub,
expr2_to_sub,
expr3_to_sub,
expr1,
expr2,
expr3,
solution[var],
value,
variables.variables_dict[var],
)
elif isinstance(solution[var], list):
elif isinstance(value, list):

Check warning on line 396 in src/mqt/qao/constraints.py

View check run for this annotation

Codecov / codecov/patch

src/mqt/qao/constraints.py#L396

Added line #L396 was not covered by tests
expr1, expr2, expr3 = self._constraint_check_three_sub_list_var(
solution, variables, expr1, expr2, expr3, var, expr1_to_sub, expr2_to_sub, expr3_to_sub
)
Expand Down Expand Up @@ -535,20 +535,20 @@ def _or_constraint_check(self, constraint: str, variables: Variables, solution:
expr2_to_sub = bool(not str(expr2).replace(".", "").isnumeric())
expr3_to_sub = bool(not str(expr3).replace(".", "").isnumeric())
if expr2_to_sub or expr1_to_sub or expr3_to_sub:
for var in solution:
for var, value in solution.items():

Check warning on line 538 in src/mqt/qao/constraints.py

View check run for this annotation

Codecov / codecov/patch

src/mqt/qao/constraints.py#L538

Added line #L538 was not covered by tests
if var in variables.variables_dict:
if isinstance(solution[var], float):
if isinstance(value, float):

Check warning on line 540 in src/mqt/qao/constraints.py

View check run for this annotation

Codecov / codecov/patch

src/mqt/qao/constraints.py#L540

Added line #L540 was not covered by tests
expr1, expr2, expr3 = self._constraint_check_three_sub_single_var(
expr1_to_sub,
expr2_to_sub,
expr3_to_sub,
expr1,
expr2,
expr3,
solution[var],
value,
variables.variables_dict[var],
)
elif isinstance(solution[var], list):
elif isinstance(value, list):

Check warning on line 551 in src/mqt/qao/constraints.py

View check run for this annotation

Codecov / codecov/patch

src/mqt/qao/constraints.py#L551

Added line #L551 was not covered by tests
expr1, expr2, expr3 = self._constraint_check_three_sub_list_var(
solution, variables, expr1, expr2, expr3, var, expr1_to_sub, expr2_to_sub, expr3_to_sub
)
Expand Down Expand Up @@ -765,20 +765,20 @@ def _xor_constraint_check(self, constraint: str, variables: Variables, solution:
expr2_to_sub = bool(not str(expr2).replace(".", "").isnumeric())
expr3_to_sub = bool(not str(expr3).replace(".", "").isnumeric())
if expr2_to_sub or expr1_to_sub or expr3_to_sub:
for var in solution:
for var, value in solution.items():

Check warning on line 768 in src/mqt/qao/constraints.py

View check run for this annotation

Codecov / codecov/patch

src/mqt/qao/constraints.py#L768

Added line #L768 was not covered by tests
if var in variables.variables_dict:
if isinstance(solution[var], float):
if isinstance(value, float):

Check warning on line 770 in src/mqt/qao/constraints.py

View check run for this annotation

Codecov / codecov/patch

src/mqt/qao/constraints.py#L770

Added line #L770 was not covered by tests
expr1, expr2, expr3 = self._constraint_check_three_sub_single_var(
expr1_to_sub,
expr2_to_sub,
expr3_to_sub,
expr1,
expr2,
expr3,
solution[var],
value,
variables.variables_dict[var],
)
elif isinstance(solution[var], list):
elif isinstance(value, list):

Check warning on line 781 in src/mqt/qao/constraints.py

View check run for this annotation

Codecov / codecov/patch

src/mqt/qao/constraints.py#L781

Added line #L781 was not covered by tests
expr1, expr2, expr3 = self._constraint_check_three_sub_list_var(
solution, variables, expr1, expr2, expr3, var, expr1_to_sub, expr2_to_sub, expr3_to_sub
)
Expand Down Expand Up @@ -1008,13 +1008,13 @@ def _greq_constraint_check(self, constraint: str, variables: Variables, solution
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:
for var in solution:
for var, value in solution.items():
if var in variables.variables_dict:
if isinstance(solution[var], float):
if isinstance(value, float):
expr1, expr2 = self._constraint_check_sub_single_var(
expr1_to_sub, expr2_to_sub, expr1, expr2, solution[var], variables.variables_dict[var]
expr1_to_sub, expr2_to_sub, expr1, expr2, value, variables.variables_dict[var]
)
elif isinstance(solution[var], list):
elif isinstance(value, list):
expr1, expr2 = self._constraint_check_sub_list_var(
solution, variables, expr1, expr2, var, expr1_to_sub, expr2_to_sub
)
Expand Down Expand Up @@ -1175,13 +1175,13 @@ def _lteq_constraint_check(self, constraint: str, variables: Variables, solution
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:
for var in solution:
for var, value in solution.items():
if var in variables.variables_dict:
if isinstance(solution[var], float):
if isinstance(value, float):
expr1, expr2 = self._constraint_check_sub_single_var(
expr1_to_sub, expr2_to_sub, expr1, expr2, solution[var], variables.variables_dict[var]
expr1_to_sub, expr2_to_sub, expr1, expr2, value, variables.variables_dict[var]
)
elif isinstance(solution[var], list):
elif isinstance(value, list):

Check warning on line 1184 in src/mqt/qao/constraints.py

View check run for this annotation

Codecov / codecov/patch

src/mqt/qao/constraints.py#L1184

Added line #L1184 was not covered by tests
expr1, expr2 = self._constraint_check_sub_list_var(
solution, variables, expr1, expr2, var, expr1_to_sub, expr2_to_sub
)
Expand Down Expand Up @@ -1352,13 +1352,13 @@ def _grt_constraint_check(self, constraint: str, variables: Variables, solution:
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:
for var in solution:
for var, value in solution.items():
if var in variables.variables_dict:
if isinstance(solution[var], float):
if isinstance(value, float):
expr1, expr2 = self._constraint_check_sub_single_var(
expr1_to_sub, expr2_to_sub, expr1, expr2, solution[var], variables.variables_dict[var]
expr1_to_sub, expr2_to_sub, expr1, expr2, value, variables.variables_dict[var]
)
elif isinstance(solution[var], list):
elif isinstance(value, list):

Check warning on line 1361 in src/mqt/qao/constraints.py

View check run for this annotation

Codecov / codecov/patch

src/mqt/qao/constraints.py#L1361

Added line #L1361 was not covered by tests
expr1, expr2 = self._constraint_check_sub_list_var(
solution, variables, expr1, expr2, var, expr1_to_sub, expr2_to_sub
)
Expand Down Expand Up @@ -1532,13 +1532,13 @@ def _lt_constraint_check(self, constraint: str, variables: Variables, solution:
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:
for var in solution:
for var, value in solution.items():
if var in variables.variables_dict:
if isinstance(solution[var], float):
if isinstance(value, float):
expr1, expr2 = self._constraint_check_sub_single_var(
expr1_to_sub, expr2_to_sub, expr1, expr2, solution[var], variables.variables_dict[var]
expr1_to_sub, expr2_to_sub, expr1, expr2, value, variables.variables_dict[var]
)
elif isinstance(solution[var], list):
elif isinstance(value, list):

Check warning on line 1541 in src/mqt/qao/constraints.py

View check run for this annotation

Codecov / codecov/patch

src/mqt/qao/constraints.py#L1541

Added line #L1541 was not covered by tests
expr1, expr2 = self._constraint_check_sub_list_var(
solution, variables, expr1, expr2, var, expr1_to_sub, expr2_to_sub
)
Expand Down Expand Up @@ -1635,13 +1635,13 @@ def _eq_constraint_check(self, constraint: str, variables: Variables, solution:
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:
for var in solution:
for var, value in solution.items():
if var in variables.variables_dict:
if isinstance(solution[var], float):
if isinstance(value, float):
expr1, expr2 = self._constraint_check_sub_single_var(
expr1_to_sub, expr2_to_sub, expr1, expr2, solution[var], variables.variables_dict[var]
expr1_to_sub, expr2_to_sub, expr1, expr2, value, variables.variables_dict[var]
)
elif isinstance(solution[var], list):
elif isinstance(value, list):

Check warning on line 1644 in src/mqt/qao/constraints.py

View check run for this annotation

Codecov / codecov/patch

src/mqt/qao/constraints.py#L1644

Added line #L1644 was not covered by tests
expr1, expr2 = self._constraint_check_sub_list_var(
solution, variables, expr1, expr2, var, expr1_to_sub, expr2_to_sub
)
Expand Down
10 changes: 5 additions & 5 deletions src/mqt/qao/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,9 @@ def _moc(cost_function: PUBO, constraint_function: PUBO) -> float:

val = 0.0
first = True
for key in p_sum:
for key, value in p_sum.items():
if key in p_sum_constrained and p_sum_constrained[key] != 0:
v = p_sum[key] / p_sum_constrained[key]
v = value / p_sum_constrained[key]
if v != 0 and first:
val = v
first = False
Expand Down Expand Up @@ -394,9 +394,9 @@ def upper_lower_bound_posiform_and_negaform_method(cost_function: PUBO) -> float
n_sum[elem] += cost_function[key] / len(key)
lowerbound = 0.0
upperbound = 0.0
for key in p_sum:
if p_sum[key] < 0:
lowerbound += p_sum[key]
for key, value in p_sum.items():
if value < 0:
lowerbound += value
if n_sum[key] > 0:
upperbound += n_sum[key]
return upperbound - lowerbound
Expand Down
6 changes: 3 additions & 3 deletions src/mqt/qao/solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1259,9 +1259,9 @@ def wring_json_reports(
elems_v = {}
coeff_m = sum(coeff) / len(coeff)
coeff_v = np.var(coeff)
for key in elems:
elems_m[key] = sum(elems[key]) / len(elems[key])
elems_v[key] = np.var(elems[key])
for key, value in elems.items():
elems_m[key] = sum(value) / len(value)
elems_v[key] = np.var(value)

Check warning on line 1264 in src/mqt/qao/solvers.py

View check run for this annotation

Codecov / codecov/patch

src/mqt/qao/solvers.py#L1262-L1264

Added lines #L1262 - L1264 were not covered by tests
data["qubo contributions"] = num_elm
data["qubo contributions average"] = elems_m
data["qubo contributions variance"] = elems_v
Expand Down
28 changes: 14 additions & 14 deletions tests/test_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,8 @@ def test_cost_function() -> None:
for key in qubo:
qubo_re[tuple(sorted(key))] = qubo[key]
reference_qubo_dict_re = {}
for key in reference_qubo_dict:
reference_qubo_dict_re[tuple(sorted(key))] = reference_qubo_dict[key]
for key, value in reference_qubo_dict.items():
reference_qubo_dict_re[tuple(sorted(key))] = value
assert dict(sorted(qubo_re.items())) == dict(sorted(reference_qubo_dict_re.items()))


Expand Down Expand Up @@ -392,8 +392,8 @@ def test_cost_function_matrix() -> None:
for key in qubo:
qubo_re[tuple(sorted(key))] = qubo[key]
reference_qubo_dict_re = {}
for key in reference_qubo_dict:
reference_qubo_dict_re[tuple(sorted(key))] = reference_qubo_dict[key]
for key, value in reference_qubo_dict.items():
reference_qubo_dict_re[tuple(sorted(key))] = value
assert dict(sorted(qubo_re.items())) == dict(sorted(reference_qubo_dict_re.items()))


Expand Down Expand Up @@ -449,13 +449,13 @@ def test_constraint(expression: str, var_precision: bool) -> None:
for key in qubo_second:
qubo_second_re[tuple(sorted(key))] = qubo_second[key]
dictionary_constraints_qubo_re = {}
for key in dictionary_constraints_qubo:
dictionary_constraints_qubo_re[tuple(sorted(key))] = dictionary_constraints_qubo[key]
for key, value in dictionary_constraints_qubo.items():
dictionary_constraints_qubo_re[tuple(sorted(key))] = value
if expression == "~a = b":
dictionary_constraints_qubo_2 = {("b0",): -1.0, ("b1",): -1.0, ("b0", "b1"): 2.0, (): 1.0}
dictionary_constraints_qubo_2_re = {}
for key in dictionary_constraints_qubo_2:
dictionary_constraints_qubo_2_re[tuple(sorted(key))] = dictionary_constraints_qubo_2[key]
for key, value in dictionary_constraints_qubo_2.items():
dictionary_constraints_qubo_2_re[tuple(sorted(key))] = value
assert dict(sorted(qubo_second_re.items())) == dict(sorted(dictionary_constraints_qubo_2_re.items()))
assert dict(sorted(qubo_first_re.items())) == dict(sorted(dictionary_constraints_qubo_re.items()))
elif expression == "a & b = c":
Expand Down Expand Up @@ -855,13 +855,13 @@ def test_constraint_no_sub(expression: str) -> None:
for key in qubo_second:
qubo_second_re[tuple(sorted(key))] = qubo_second[key]
dictionary_constraints_qubo_re = {}
for key in dictionary_constraints_qubo:
dictionary_constraints_qubo_re[tuple(sorted(key))] = dictionary_constraints_qubo[key]
for key, value in dictionary_constraints_qubo.items():
dictionary_constraints_qubo_re[tuple(sorted(key))] = value
if expression == "~b0 = b1":
dictionary_constraints_qubo_2 = {("b0",): -1, ("b1",): -1, ("b0", "b1"): 2, (): 1}
dictionary_constraints_qubo_2_re = {}
for key in dictionary_constraints_qubo_2:
dictionary_constraints_qubo_2_re[tuple(sorted(key))] = dictionary_constraints_qubo_2[key]
for key, value in dictionary_constraints_qubo_2.items():
dictionary_constraints_qubo_2_re[tuple(sorted(key))] = value
assert dict(sorted(qubo_second_re.items())) == dict(sorted(dictionary_constraints_qubo_2_re.items()))
assert dict(sorted(qubo_first_re.items())) == dict(sorted(dictionary_constraints_qubo_re.items()))

Expand Down Expand Up @@ -945,8 +945,8 @@ def test_problem(lambda_strategy: str) -> None:
for key in qubo:
qubo_re[tuple(sorted(key))] = qubo[key]
reference_qubo_dict_re = {}
for key in reference_qubo_dict:
reference_qubo_dict_re[tuple(sorted(key))] = reference_qubo_dict[key]
for key, value in reference_qubo_dict.items():
reference_qubo_dict_re[tuple(sorted(key))] = value
if lambda_strategy in {"upper_bound_only_positive", "upper lower bound naive"}:
assert lambdas == [52.25 * 1.1] * 2
assert dict(sorted(qubo_re.items())) == dict(sorted(reference_qubo_dict_re.items()))
Expand Down

0 comments on commit 83891d0

Please sign in to comment.