Standard autodifferentiation toolkits can be used to compute gradients of the energy with respect to the output once the output space has been relaxed.
one challenge is maintaining constraints on the variables being optimized.