-
Notifications
You must be signed in to change notification settings - Fork 15
/
solution.jl
69 lines (56 loc) · 1.86 KB
/
solution.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
""
function build_QCModel_result(qcm::QuantumCircuitModel, solve_time::Number)
# try-catch is needed until solvers reliably support ResultCount()
result_count = 1
try
result_count = JuMP.result_count(qcm.model)
catch
Memento.warn(_LOGGER, "the given optimizer does not provide the ResultCount() attribute, assuming the solver returned a solution which may be incorrect.");
end
solution = Dict{String,Any}()
if result_count > 0
solution = QCO.build_QCModel_solution(qcm)
else
Memento.warn(_LOGGER, "Quantum circuit model has no results, solution cannot be built")
end
result = Dict{String,Any}(
"optimizer" => JuMP.solver_name(qcm.model),
"termination_status" => JuMP.termination_status(qcm.model),
"primal_status" => JuMP.primal_status(qcm.model),
"objective" => QCO.get_objective_value(qcm.model),
"objective_lb" => QCO.get_objective_bound(qcm.model),
"solve_time" => solve_time,
"solution" => solution,
)
# Objective slack Penalty
if qcm.data["decomposition_type"] == "approximate"
result["objective_slack_penalty"] = qcm.options.objective_slack_penalty
end
return result
end
""
function get_objective_value(model::JuMP.Model)
obj_val = NaN
try
obj_val = JuMP.objective_value(model)
catch
Memento.warn(_LOGGER, "Objective value is unbounded. Problem may be infeasible or not constrained properly");
end
return obj_val
end
""
function get_objective_bound(model::JuMP.Model)
obj_lb = -Inf
try
obj_lb = JuMP.objective_bound(model)
catch
end
return obj_lb
end
function build_QCModel_solution(qcm::QuantumCircuitModel)
solution = Dict{String,Any}()
for i in keys(qcm.variables)
solution[String(i)] = JuMP.value.(qcm.variables[i])
end
return solution
end