Skip to content

Commit

Permalink
Fix the mypy errors
Browse files Browse the repository at this point in the history
  • Loading branch information
DeborahVolpe committed May 20, 2024
1 parent e99c614 commit 43fadf5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
9 changes: 7 additions & 2 deletions src/mqt/qao/solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@
from qiskit.circuit import Parameter, QuantumCircuit
from qiskit.circuit.library import TwoLocal
from qiskit.exceptions import QiskitError
from qiskit_optimization.algorithms import GroverOptimizer, MinimumEigenOptimizer, OptimizationResult
from qiskit_optimization.algorithms import (
GroverOptimizer,
MinimumEigenOptimizationResult,
MinimumEigenOptimizer,
OptimizationResult,
)
from qiskit_optimization.translators import from_docplex_mp
from qubovert import PUBO, QUBO

Expand Down Expand Up @@ -1053,7 +1058,7 @@ def create_dwave_annealing_solution(

def create_qiskit_qubo_solution(
self,
res: list[OptimizationResult],
res: list[MinimumEigenOptimizationResult] | list[OptimizationResult],
pubo: PUBO,
qubo: QUBO,
time: float = -1.0,
Expand Down
24 changes: 12 additions & 12 deletions tests/test_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ def test_simulated_annealer_solver_constrained(lambda_strategy: str, constraint_
problem.create_problem(variables, constraint, objective_function)
solver = Solver()
solution = solver.solve_simulated_annealing(problem, lambda_strategy=lambda_strategy)
if not isinstance(solution, bool):
if isinstance(solution, Solution):
all_satisfy, each_satisfy = solution.check_constraint_optimal_solution()
if constraint_expr == "c >= 1":
assert solution.best_solution == {"a": 0.0, "b": -1.0, "c": 1.0} or not all_satisfy
Expand Down Expand Up @@ -1167,7 +1167,7 @@ def test_simulated_annealer_solver_constrained_lambda_update_mechanism(
problem, max_lambda_update=10, lambda_update_mechanism=lambda_update, lambda_strategy="manual", lambda_value=5.0
)
solver.get_lambda_updates()
if not isinstance(solution, bool):
if isinstance(solution, Solution):
all_satisfy, each_satisfy = solution.check_constraint_optimal_solution()
if constraint_expr == "c >= 1":
assert solution.best_solution == {"a": 0.0, "b": -1.0, "c": 1.0} or not all_satisfy
Expand Down Expand Up @@ -1369,7 +1369,7 @@ def test_simulated_annealer_solver_constrained_lambda_update_mechanism_and_strat
problem, max_lambda_update=10, lambda_update_mechanism=lambda_update, lambda_strategy=lambda_strategy
)
solver.get_lambda_updates()
if not isinstance(solution, bool):
if isinstance(solution, Solution):
all_satisfy, each_satisfy = solution.check_constraint_optimal_solution()
if constraint_expr == "c >= 1":
assert solution.best_solution == {"a": 0.0, "b": -1.0, "c": 1.0} or not all_satisfy
Expand Down Expand Up @@ -1465,7 +1465,7 @@ def test_simulated_annealing_cost_function_matrix(
problem, max_lambda_update=10, lambda_update_mechanism=lambda_update, lambda_strategy=lambda_strategy
)
solver.get_lambda_updates()
if not isinstance(solution, bool):
if isinstance(solution, Solution):
all_satisfy, each_satisfy = solution.check_constraint_optimal_solution()
if constraint_expr == "M1_0_1 >= 1":
assert (
Expand Down Expand Up @@ -1564,7 +1564,7 @@ def test_gas_solver_basic() -> None:
problem.create_problem(variables, constraint, objective_function)
solver = Solver()
solution = solver.solve_grover_adaptive_search_qubo(problem, qubit_values=6, num_runs=10)
if not isinstance(solution, bool):
if isinstance(solution, Solution):
all_satisfy, each_satisfy = solution.check_constraint_optimal_solution()
assert solution.best_solution == {"a": 1.0, "b": 0.0, "c": 1.0}
print(solution.best_solution)
Expand Down Expand Up @@ -1594,7 +1594,7 @@ def test_qaoa_solver_qubo_basic() -> None:
problem,
num_runs=10,
)
if not isinstance(solution, bool):
if isinstance(solution, Solution):
all_satisfy, each_satisfy = solution.check_constraint_optimal_solution()
assert solution.best_solution == {"a": 1.0, "b": 0.0, "c": 1.0}
print(solution.best_solution)
Expand Down Expand Up @@ -1624,7 +1624,7 @@ def test_vqe_solver_qubo_basic() -> None:
problem,
num_runs=10,
)
if not isinstance(solution, bool):
if isinstance(solution, Solution):
all_satisfy, each_satisfy = solution.check_constraint_optimal_solution()
assert solution.best_solution == {"a": 1.0, "b": 0.0, "c": 1.0}
print(solution.best_solution)
Expand Down Expand Up @@ -1657,7 +1657,7 @@ def test_qaoa_solver(lambda_strategy: str) -> None:
problem.create_problem(variables, constraint, objective_function)
solver = Solver()
solution = solver.solve_qaoa_qubo(problem, num_runs=10, lambda_strategy=lambda_strategy)
if not isinstance(solution, bool):
if isinstance(solution, Solution):
all_satisfy, each_satisfy = solution.check_constraint_optimal_solution()
assert solution.best_solution == {"a": 0.0, "b": 3.0, "c": -1.5}
assert solution.best_energy < -2.24 # (the range if for having no issues with numerical errors)
Expand Down Expand Up @@ -1688,7 +1688,7 @@ def test_vqe_solver(lambda_strategy: str) -> None:
problem.create_problem(variables, constraint, objective_function)
solver = Solver()
solution = solver.solve_vqe_qubo(problem, num_runs=10, lambda_strategy=lambda_strategy)
if not isinstance(solution, bool):
if isinstance(solution, Solution):
all_satisfy, each_satisfy = solution.check_constraint_optimal_solution()
assert solution.best_solution == {"a": 0.0, "b": 3.0, "c": -1.5}
assert solution.best_energy < -2.24 # (the range if for having no issues with numerical errors)
Expand Down Expand Up @@ -1853,7 +1853,7 @@ def test_qaoa_constrained_lambda_update_mechanism_and_strategy(
lambda_strategy=lambda_strategy,
)
solver.get_lambda_updates()
if not isinstance(solution, bool):
if isinstance(solution, Solution):
all_satisfy, each_satisfy = solution.check_constraint_optimal_solution()
if constraint_expr == "c >= 1":
assert solution.best_solution == {"a": 0.0, "b": -1.0, "c": 1.0} or not all_satisfy
Expand Down Expand Up @@ -2059,7 +2059,7 @@ def test_vqe_constrained_lambda_update_mechanism_and_strategy(
lambda_strategy=lambda_strategy,
)
solver.get_lambda_updates()
if not isinstance(solution, bool):
if isinstance(solution, Solution):
all_satisfy, each_satisfy = solution.check_constraint_optimal_solution()
if constraint_expr == "c >= 1":
assert solution.best_solution == {"a": 0.0, "b": -1.0, "c": 1.0} or not all_satisfy
Expand Down Expand Up @@ -2133,7 +2133,7 @@ def test_gas_solver(lambda_strategy: str) -> None:
solution = solver.solve_grover_adaptive_search_qubo(
problem, qubit_values=7, num_runs=10, coeff_precision=0.5, lambda_strategy=lambda_strategy
)
if not isinstance(solution, bool):
if isinstance(solution, Solution):
all_satisfy, each_satisfy = solution.check_constraint_optimal_solution()
assert solution.best_solution == {"b": 3.0, "c": -1.0}
assert solution.best_energy < -1.9 # (the range if for having no issues with numerical errors)
Expand Down

0 comments on commit 43fadf5

Please sign in to comment.