|
| 1 | +""" |
| 2 | +Abstract type for the result object returned by [`optimize`](@ref). Any |
| 3 | +optimization method implemented on top of `QuantumControl` should subtype |
| 4 | +from `AbstractOptimizationResult`. This enables conversion between the results |
| 5 | +of different methods, allowing one method to continue an optimization from |
| 6 | +another method. |
| 7 | +
|
| 8 | +In order for this to work seamlessly, result objects should use a common set of |
| 9 | +field names as much as a possible. When a result object requires fields that |
| 10 | +cannot be provided by all other result objects, it should have default values |
| 11 | +for these field, which can be defined in a custom `Base.convert` method, as, |
| 12 | +e.g., |
| 13 | +
|
| 14 | +```julia |
| 15 | +function Base.convert(::Type{MyResult}, result::AbstractOptimizationResult) |
| 16 | + defaults = Dict{Symbol,Any}( |
| 17 | + :f_calls => 0, |
| 18 | + :fg_calls => 0, |
| 19 | + ) |
| 20 | + return convert(MyResult, result, defaults) |
| 21 | +end |
| 22 | +``` |
| 23 | +
|
| 24 | +Where `f_calls` and `fg_calls` are fields of `MyResult` that are not present in |
| 25 | +a given `result` of a different type. The three-argument `convert` is defined |
| 26 | +internally for any `AbstractOptimizationResult`. |
| 27 | +""" |
| 28 | +abstract type AbstractOptimizationResult end |
| 29 | + |
| 30 | +function Base.convert( |
| 31 | + ::Type{Dict{Symbol,Any}}, |
| 32 | + result::R |
| 33 | +) where {R<:AbstractOptimizationResult} |
| 34 | + return Dict{Symbol,Any}(field => getfield(result, field) for field in fieldnames(R)) |
| 35 | +end |
| 36 | + |
| 37 | + |
| 38 | +struct MissingResultDataException{R} <: Exception |
| 39 | + missing_fields::Vector{Symbol} |
| 40 | +end |
| 41 | + |
| 42 | + |
| 43 | +function Base.showerror(io::IO, err::MissingResultDataException{R}) where {R} |
| 44 | + msg = "Missing data for fields $(err.missing_fields) to instantiate $R." |
| 45 | + print(io, msg) |
| 46 | +end |
| 47 | + |
| 48 | + |
| 49 | +struct IncompatibleResultsException{R1,R2} <: Exception |
| 50 | + missing_fields::Vector{Symbol} |
| 51 | +end |
| 52 | + |
| 53 | + |
| 54 | +function Base.showerror(io::IO, err::IncompatibleResultsException{R1,R2}) where {R1,R2} |
| 55 | + msg = "$R2 cannot be converted to $R1: $R2 does not provide required fields $(err.missing_fields). $R1 may need a custom implementation of `Base.convert` that sets values for any field names not provided by all results." |
| 56 | + print(io, msg) |
| 57 | +end |
| 58 | + |
| 59 | + |
| 60 | +function Base.convert( |
| 61 | + ::Type{R}, |
| 62 | + data::Dict{Symbol,<:Any}, |
| 63 | + defaults::Dict{Symbol,<:Any}=Dict{Symbol,Any}(), |
| 64 | +) where {R<:AbstractOptimizationResult} |
| 65 | + |
| 66 | + function _get(data, field, defaults) |
| 67 | + # Can't use `get`, because that would try to evaluate the non-existing |
| 68 | + # `defaults[field]` for `fields` that actually exist in `data`. |
| 69 | + if haskey(data, field) |
| 70 | + return data[field] |
| 71 | + else |
| 72 | + return defaults[field] |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + args = try |
| 77 | + [_get(data, field, defaults) for field in fieldnames(R)] |
| 78 | + catch exc |
| 79 | + if exc isa KeyError |
| 80 | + missing_fields = [ |
| 81 | + field for field in fieldnames(R) if |
| 82 | + !(haskey(data, field) || haskey(defaults, field)) |
| 83 | + ] |
| 84 | + throw(MissingResultDataException{R}(missing_fields)) |
| 85 | + else |
| 86 | + rethrow() |
| 87 | + end |
| 88 | + end |
| 89 | + return R(args...) |
| 90 | +end |
| 91 | + |
| 92 | + |
| 93 | +function Base.convert( |
| 94 | + ::Type{R1}, |
| 95 | + result::R2, |
| 96 | + defaults::Dict{Symbol,<:Any}=Dict{Symbol,Any}(), |
| 97 | +) where {R1<:AbstractOptimizationResult,R2<:AbstractOptimizationResult} |
| 98 | + data = convert(Dict{Symbol,Any}, result) |
| 99 | + try |
| 100 | + return convert(R1, data, defaults) |
| 101 | + catch exc |
| 102 | + if exc isa MissingResultDataException{R1} |
| 103 | + throw(IncompatibleResultsException{R1,R2}(exc.missing_fields)) |
| 104 | + else |
| 105 | + rethrow() |
| 106 | + end |
| 107 | + end |
| 108 | +end |
0 commit comments