Skip to content

Commit

Permalink
🚨🎨 code quality fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
burgholzer committed Aug 12, 2024
1 parent bbf7af9 commit 405b62b
Showing 1 changed file with 51 additions and 57 deletions.
108 changes: 51 additions & 57 deletions src/mqt/qao/solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,7 @@ def solve_simulated_annealing(
time = -1.0
sol = Solution()
sol.create_problem(self.problem)
solver_info: dict[str, Any] = {}
solver_info["solver name"] = "Simulated annealer"
solver_info["num reads"] = num_reads
solver_info["annealing time"] = annealing_time
solver_info = {"solver name": "Simulated annealer", "num reads": num_reads, "annealing time": annealing_time}
if save_compilation_time:
solver_info["compilation time"] = compilation_time
sol.create_dwave_annealing_solution(samples, self.pubo, self.qubo, self.qubo.offset, time, solver_info)
Expand Down Expand Up @@ -193,11 +190,12 @@ def solve_simulated_annealing(
sol = Solution()
sol.create_problem(self.problem)
self._number_of_lambda_update += 1
solver_info = {}
solver_info["solver name"] = "Simulated annealer"
solver_info["lambda update"] = self._number_of_lambda_update
solver_info["num reads"] = num_reads
solver_info["annealing time"] = annealing_time
solver_info = {
"solver name": "Simulated annealer",
"lambda update": self._number_of_lambda_update,
"num reads": num_reads,
"annealing time": annealing_time,
}
if save_compilation_time:
solver_info["compilation time"] = compilation_time
sol.create_dwave_annealing_solution(samples, self.pubo, self.qubo, self.qubo.offset, time, solver_info)
Expand Down Expand Up @@ -335,11 +333,12 @@ def solve_dwave_quantum_annealer(

sol = Solution()
sol.create_problem(self.problem)
solver_info: dict[str, Any] = {}
solver_info["solver name"] = "Dwave annealer"
solver_info["num reads"] = num_reads
solver_info["annealing time"] = annealing_time_scheduling
solver_info["qpu annealing time"] = time
solver_info = {
"solver name": "Dwave annealer",
"num reads": num_reads,
"annealing time": annealing_time_scheduling,
"qpu annealing time": time,
}
if save_compilation_time:
solver_info["compilation time"] = compilation_time
sol.create_dwave_annealing_solution(samples, self.pubo, self.qubo, self.qubo.offset, time, solver_info)
Expand All @@ -364,12 +363,13 @@ def solve_dwave_quantum_annealer(
sol = Solution()
sol.create_problem(self.problem)
self._number_of_lambda_update += 1
solver_info = {}
solver_info["solver name"] = "Dwave annealer"
solver_info["lambda update"] = self._number_of_lambda_update
solver_info["num reads"] = num_reads
solver_info["annealing time"] = annealing_time_scheduling
solver_info["qpu annealing time"] = time
solver_info = {
"solver name": "Dwave annealer",
"lambda update": self._number_of_lambda_update,
"num reads": num_reads,
"annealing time": annealing_time_scheduling,
"qpu annealing time": time,
}
if save_compilation_time:
solver_info["compilation time"] = compilation_time
sol.create_dwave_annealing_solution(samples, self.pubo, self.qubo, self.qubo.offset, time, solver_info)
Expand Down Expand Up @@ -459,12 +459,13 @@ def solve_grover_adaptive_search_qubo(

sol = Solution()
sol.create_problem(self.problem)
solver_info: dict[str, Any] = {}
solver_info["solver name"] = "GAS qubo"
solver_info["qubit values"] = qubit_values
solver_info["num runs"] = num_runs
solver_info["threshold"] = threshold
solver_info["time"] = time
solver_info = {
"solver name": "GAS qubo",
"qubit values": qubit_values,
"num runs": num_runs,
"threshold": threshold,
"time": time,
}
if save_compilation_time:
solver_info["compilation time"] = compilation_time
sol.create_qiskit_qubo_solution(res, self.pubo, self.qubo, time, solver_info)
Expand Down Expand Up @@ -505,13 +506,14 @@ def solve_grover_adaptive_search_qubo(
sol = Solution()
sol.create_problem(self.problem)
self._number_of_lambda_update += 1
solver_info = {}
solver_info["solver name"] = "GAS qubo"
solver_info["qubit values"] = qubit_values
solver_info["lambda update"] = self._number_of_lambda_update
solver_info["num runs"] = num_runs
solver_info["threshold"] = threshold
solver_info["time"] = time
solver_info = {
"solver name": "GAS qubo",
"qubit values": qubit_values,
"lambda update": self._number_of_lambda_update,
"num runs": num_runs,
"threshold": threshold,
"time": time,
}
if save_compilation_time:
solver_info["compilation time"] = compilation_time
sol.create_qiskit_qubo_solution(res, self.pubo, self.qubo, time, solver_info)
Expand Down Expand Up @@ -613,11 +615,7 @@ def solve_qaoa_qubo(

sol = Solution()
sol.create_problem(self.problem)
solver_info: dict[str, Any] = {}
solver_info["solver name"] = "QAOA qubo"
solver_info["num runs"] = num_runs
solver_info["repetitions"] = reps
solver_info["time"] = time
solver_info = {"solver name": "QAOA qubo", "num runs": num_runs, "repetitions": reps, "time": time}
if save_compilation_time:
solver_info["compilation time"] = compilation_time
sol.create_qiskit_qubo_solution(res, self.pubo, self.qubo, time, solver_info)
Expand Down Expand Up @@ -646,12 +644,13 @@ def solve_qaoa_qubo(
sol = Solution()
sol.create_problem(self.problem)
self._number_of_lambda_update += 1
solver_info = {}
solver_info["solver name"] = "QAOA qubo"
solver_info["lambda update"] = self._number_of_lambda_update
solver_info["num runs"] = num_runs
solver_info["repetitions"] = reps
solver_info["time"] = time
solver_info = {
"solver name": "QAOA qubo",
"lambda update": self._number_of_lambda_update,
"num runs": num_runs,
"repetitions": reps,
"time": time,
}
if save_compilation_time:
solver_info["compilation time"] = compilation_time
sol.create_qiskit_qubo_solution(res, self.pubo, self.qubo, time, solver_info)
Expand Down Expand Up @@ -738,10 +737,7 @@ def solve_vqe_qubo(

sol = Solution()
sol.create_problem(self.problem)
solver_info: dict[str, Any] = {}
solver_info["solver name"] = "VQE qubo"
solver_info["num runs"] = num_runs
solver_info["time"] = time
solver_info = {"solver name": "VQE qubo", "num runs": num_runs, "time": time}
if save_compilation_time:
solver_info["compilation time"] = compilation_time
sol.create_qiskit_qubo_solution(res, self.pubo, self.qubo, time, solver_info)
Expand Down Expand Up @@ -770,11 +766,12 @@ def solve_vqe_qubo(
sol = Solution()
sol.create_problem(self.problem)
self._number_of_lambda_update += 1
solver_info = {}
solver_info["solver name"] = "VQE qubo"
solver_info["lambda update"] = self._number_of_lambda_update
solver_info["num runs"] = num_runs
solver_info["time"] = time
solver_info = {
"solver name": "VQE qubo",
"lambda update": self._number_of_lambda_update,
"num runs": num_runs,
"time": time,
}
if save_compilation_time:
solver_info["compilation time"] = compilation_time
sol.create_qiskit_qubo_solution(res, self.pubo, self.qubo, time, solver_info)
Expand Down Expand Up @@ -822,7 +819,6 @@ def _solve_with_the_predicted_solver(
# num_reads = 100
auto_scale = True
flux_drift_compensation = True
initial_state = None
programming_thermalization = 1000.0
readout_thermalization = 1000.0
reduce_intersample_correlation = True
Expand All @@ -836,7 +832,6 @@ def _solve_with_the_predicted_solver(
endpoint=endpoint,
token=token,
)
dwave_sampler_embedded = EmbeddingComposite(dwave_sampler)
except (RuntimeError, TypeError):
print("It is not possible to exploit quantum annealer\n")
return False, solver_info
Expand Down Expand Up @@ -957,7 +952,7 @@ def _solve_with_the_predicted_solver(

elif y == 3: # GAS
print("The best solver is the GAS. The problem will be solved with the GAS\n")
scaled_qubo_c = round((self.qubo.copy() - self.qubo.offset) / (coeff_precision))
scaled_qubo_c = round((self.qubo.copy() - self.qubo.offset) / coeff_precision)
min_coeff = abs(min(scaled_qubo_c.values(), key=abs))
scaled_qubo = round(scaled_qubo_c / min_coeff)
qp = self._from_qubovert_to_qiskit_model(scaled_qubo)
Expand Down Expand Up @@ -1077,7 +1072,6 @@ def solve(
self.problem = problem
self.pubo = self.problem.write_the_final_cost_function(lambda_strategy, lambda_value=lambda_value)
self.qubo = self.pubo.to_qubo()
compilation_time = -1

# estrapolate the features
x = np.zeros((1, 9))
Expand Down

0 comments on commit 405b62b

Please sign in to comment.