diff --git a/quantum/plugins/optimizers/nlopt-optimizers/nlopt_optimizer.cpp b/quantum/plugins/optimizers/nlopt-optimizers/nlopt_optimizer.cpp index c91aec2ba..3f99ae033 100644 --- a/quantum/plugins/optimizers/nlopt-optimizers/nlopt_optimizer.cpp +++ b/quantum/plugins/optimizers/nlopt-optimizers/nlopt_optimizer.cpp @@ -145,6 +145,12 @@ OptResult NLOptimizer::optimize(OptFunction &function) { upperBounds = options.get>("nlopt-upper-bounds"); } + // if fails to find mininum should not throw error + bool throwError = true; + if (options.keyExists("throw-error")) { + throwError = options.get("throw-error"); + } + _opt.set_lower_bounds(lowerBounds); _opt.set_upper_bounds(upperBounds); _opt.set_maxeval(maxeval); @@ -171,10 +177,16 @@ OptResult NLOptimizer::optimize(OptFunction &function) { try { r = _opt.optimize(x, optF); } catch (std::exception &e) { - xacc::error("NLOpt failed with error code = " + std::to_string(r) + ", " + + + if (throwError) { + xacc::error("NLOpt failed with error code = " + std::to_string(r) + ", " + std::string(e.what())); - } + } else { + xacc::warning("NLOpt failed with error code = " + std::to_string(r) + ", " + + std::string(e.what())); + } + } return OptResult{optF, x}; } diff --git a/quantum/plugins/optimizers/scipy/scipy_optimizer.cpp b/quantum/plugins/optimizers/scipy/scipy_optimizer.cpp index 38e4760fe..b7fc7cbb4 100644 --- a/quantum/plugins/optimizers/scipy/scipy_optimizer.cpp +++ b/quantum/plugins/optimizers/scipy/scipy_optimizer.cpp @@ -101,6 +101,12 @@ OptResult ScipyOptimizer::optimize(OptFunction &function) { x = std::vector(tmpx.begin(), tmpx.end()); } + // if fails to find mininum should not throw error + bool throwError = true; + if (options.keyExists("throw-error")) { + throwError = options.get("throw-error"); + } + // here the python stuff starts py::list pyInitialParams; for (const auto ¶m : x) { @@ -152,11 +158,20 @@ OptResult ScipyOptimizer::optimize(OptFunction &function) { return {optimalValue, optimizedParams}; } catch (const py::error_already_set &e) { - std::cerr << "Python error: " << e.what() << std::endl; - throw; + + if (throwError) { + xacc::error("Python error: " + std::string(e.what())); + throw; + } + return {}; + } catch (const std::exception &e) { - std::cerr << "Error: " << e.what() << std::endl; - throw; + + if (throwError) { + xacc::error("Error: " + std::string(e.what())); + throw; + } + return {}; } } } // namespace xacc