12#include <unordered_map>
33 std::optional<std::vector<size_t>>
nlevels;
36 std::optional<double>
dt;
40 std::optional<std::vector<double>>
Jkl;
41 std::optional<std::vector<double>>
rotfreq;
89struct is_vector : std::false_type {};
91struct is_vector<std::vector<T>> : std::true_type {};
93inline constexpr bool is_vector_v = is_vector<T>::value;
97inline constexpr bool always_false_v =
false;
109 std::unordered_map<std::string, std::function<void(
const std::string&)>> setters;
110 std::unordered_map<std::string, std::function<void(
int,
const std::string&)>>
117 std::optional<bool> optim_regul_interpolate;
125 std::vector<std::string> split(
const std::string& str,
char delimiter =
',');
126 void applyConfigLine(
const std::string& line);
127 bool handleIndexedSetting(
const std::string& key,
const std::string& value);
129 template <
typename StreamType>
130 void loadFromStream(StreamType& stream) {
132 while (getline(stream, line)) {
133 applyConfigLine(line);
137 std::vector<std::vector<double>> convertIndexedToVectorVector(
const std::map<
int, std::vector<double>>& indexed_map,
size_t num_oscillators);
139 template <
typename T>
140 void registerConfig(
const std::string& key, std::optional<T>& member) {
141 setters[key] = [
this, &member](
const std::string& value) { member = convertFromString<T>(value); };
144 template <
typename T>
145 void registerIndexedConfig(
const std::string& base_key, std::optional<std::map<int, T>>& storage) {
146 indexed_setters[base_key] = [
this, &storage](
int index,
const std::string& value) {
147 if (!storage.has_value()) {
148 storage = std::map<int, T>{};
150 (*storage)[index] = convertFromString<T>(value);
154 template <
typename T>
155 T convertFromString(
const std::string& str) {
156 if constexpr (std::is_same_v<T, std::string>) {
158 }
else if constexpr (std::is_same_v<T, bool>) {
159 const std::set<std::string> trueValues = {
"true",
"yes",
"1"};
160 std::string lowerStr =
toLower(str);
161 return trueValues.find(lowerStr) != trueValues.end();
162 }
else if constexpr (std::is_same_v<T, int>) {
163 return std::stoi(str);
164 }
else if constexpr (std::is_same_v<T, size_t>) {
165 return static_cast<size_t>(std::stoul(str));
166 }
else if constexpr (std::is_same_v<T, double>) {
167 return std::stod(str);
168 }
else if constexpr (is_vector_v<T>) {
169 return parseVector<T>(str);
171 static_assert(always_false_v<T>,
"Unsupported type for convertFromString");
175 template <
typename VectorType>
176 VectorType parseVector(
const std::string& str) {
177 using ElementType =
typename VectorType::value_type;
179 auto parts = split(str);
180 vec.reserve(parts.size());
181 for (
const auto& part : parts) {
182 vec.push_back(convertFromString<ElementType>(part));
Configuration parser that converts raw config to validated Config.
Definition cfgparser.hpp:107
ParsedConfigData parseFile(const std::string &filename)
Definition cfgparser.cpp:165
ParsedConfigData parseString(const std::string &config_content)
Definition cfgparser.cpp:176
MPI-aware logger that handles rank filtering and quiet mode.
Definition mpi_logger.hpp:12
Core type definitions and enumerations for Quandary quantum optimal control.
ControlType
Types of control parameterizations.
Definition defs.hpp:149
Definition cfgparser.hpp:20
std::vector< double > parameters
Parameters for control parameterization.
Definition cfgparser.hpp:22
ControlType control_type
Type of control parameterization.
Definition cfgparser.hpp:21
Configuration settings passed to Config constructor.
Definition cfgparser.hpp:31
std::optional< double > optim_tol_grad_abs
Definition cfgparser.hpp:59
std::optional< OptimTargetSettings > optim_target
Definition cfgparser.hpp:55
std::optional< LinearSolverType > linearsolver_type
Definition cfgparser.hpp:80
std::optional< size_t > ntime
Definition cfgparser.hpp:35
std::optional< std::map< int, ControlParameterizationData > > indexed_control_parameterizations
Definition cfgparser.hpp:50
std::optional< bool > control_zero_boundary_condition
Definition cfgparser.hpp:51
std::optional< std::map< int, ControlInitializationSettings > > indexed_control_init
Definition cfgparser.hpp:52
std::optional< bool > usematfree
Definition cfgparser.hpp:79
std::optional< std::vector< double > > Jkl
Definition cfgparser.hpp:40
std::optional< size_t > linearsolver_maxiter
Definition cfgparser.hpp:81
std::optional< std::map< int, std::vector< double > > > indexed_carrier_frequencies
Definition cfgparser.hpp:54
std::optional< std::vector< double > > optim_weights
Definition cfgparser.hpp:58
std::optional< ObjectiveType > optim_objective
Definition cfgparser.hpp:57
std::optional< size_t > output_timestep_stride
Definition cfgparser.hpp:76
std::optional< std::vector< double > > decay_time
Definition cfgparser.hpp:43
std::optional< std::vector< double > > dephase_time
Definition cfgparser.hpp:44
std::optional< double > optim_penalty_variation
Definition cfgparser.hpp:69
std::optional< double > optim_tol_infidelity
Definition cfgparser.hpp:62
std::optional< double > optim_penalty_energy
Definition cfgparser.hpp:68
std::optional< std::vector< double > > crosskerr
Definition cfgparser.hpp:39
std::optional< double > optim_tol_grad_rel
Definition cfgparser.hpp:60
std::optional< InitialConditionSettings > initialcondition
Definition cfgparser.hpp:45
std::optional< RunType > runtype
Definition cfgparser.hpp:78
std::optional< double > optim_penalty
Definition cfgparser.hpp:65
std::optional< std::string > datadir
Definition cfgparser.hpp:74
std::optional< size_t > output_optimization_stride
Definition cfgparser.hpp:77
std::optional< std::vector< double > > gate_rot_freq
Definition cfgparser.hpp:56
std::optional< bool > optim_regul_interpolate
Definition cfgparser.hpp:71
std::optional< double > dt
Definition cfgparser.hpp:36
std::optional< double > optim_penalty_dpdm
Definition cfgparser.hpp:67
std::optional< std::vector< size_t > > nessential
Definition cfgparser.hpp:34
std::optional< std::string > hamiltonian_file_Hsys
Definition cfgparser.hpp:46
std::optional< std::vector< size_t > > nlevels
Definition cfgparser.hpp:33
std::optional< double > optim_penalty_param
Definition cfgparser.hpp:66
std::optional< std::vector< double > > transfreq
Definition cfgparser.hpp:37
std::optional< size_t > optim_maxiter
Definition cfgparser.hpp:63
std::optional< std::map< int, std::vector< OutputType > > > indexed_output
Definition cfgparser.hpp:75
std::optional< std::vector< double > > selfkerr
Definition cfgparser.hpp:38
std::optional< double > optim_tol_final_cost
Definition cfgparser.hpp:61
std::optional< TimeStepperType > timestepper_type
Definition cfgparser.hpp:82
std::optional< DecoherenceType > decoherence_type
Definition cfgparser.hpp:42
std::optional< bool > optim_regul_tik0
Definition cfgparser.hpp:70
std::optional< std::string > hamiltonian_file_Hc
Definition cfgparser.hpp:47
std::optional< double > optim_regul
Definition cfgparser.hpp:64
std::optional< std::vector< double > > rotfreq
Definition cfgparser.hpp:41
std::optional< std::map< int, std::vector< double > > > indexed_control_amplitude_bounds
Definition cfgparser.hpp:53
std::optional< int > rand_seed
Definition cfgparser.hpp:83
std::string toLower(std::string str)
Returns a lowercase version of the input string.
Definition util.cpp:737