kestrelquantum / QuantumCollocation.jl

Quantum Optimal Control with Direct Collocation
MIT License
29 stars 7 forks source link

QuantumObjective function: Pass a symbol or function for Loss #17

Closed andgoldschmidt closed 10 months ago

andgoldschmidt commented 1 year ago

Currently, the QuantumObjective function in objectives.jl takes a loss::Symbol which is called internally using eval(loss). This makes it hard for the user to define a local loss function, which might be a common task. Can the quantum objectives function take a loss function instead of symbol? In this alternative, the user passes an existing loss function fromlosses.jl instead of a symbol.

What are the tradeoffs here?

aarontrowbridge commented 1 year ago

the reason for passing a symbol and not a function (currently) is to allow problems to be saved with the objective. julia does not allow saving of functions so the way around this was to make the argument a symbol that evals to a function defined in losses.jl that can be saved and used to reconstruct the objective during a load. modifying the code to include a user-defined loss function should be pretty straight forward -- basically we just need to define a new Objective constructor that accepts a function (or potentially a Loss <: AbstractLoss defined on line 61 of losses.jl; though this was primarily meant for quantum state (or unitary) losses though). saving and loading should be modified to handle this case. unfortunately tests don't yet exist for this part of the code.