12#ifndef _SIMULATOR_CONFIGURATION_H_
13#define _SIMULATOR_CONFIGURATION_H_
17#include <unordered_map>
83 const auto result = std::from_chars(value.data(), value.data() + value.size(), device);
84 if (result.ec != std::errc() || result.ptr != value.data() + value.size() || device < 0)
85 throw std::invalid_argument(
"gpu_device must be a nonnegative integer");
93 for (
const char *group : {
"matrix_product_state_use_gesvd",
"matrix_product_operator_use_gesvd",
"tensor_network_use_gesvd"})
95 const size_t length = std::char_traits<char>::length(group);
98 if (key.size() == length + 1 && key.compare(0, length, group) == 0 && (key.back() ==
'j' || key.back() ==
'p' || key.back() ==
'r'))
106 if (value ==
"true" || value ==
"1")
108 if (value ==
"false" || value ==
"0")
110 throw std::invalid_argument(
"GPU SVD flags must be true, false, 1 or 0");
115 if (key ==
"precision")
117 if (value ==
"double")
119 if (value ==
"single")
121 throw std::invalid_argument(
"precision must be single or double");
123 if (value ==
"true" || value ==
"1")
125 if (value ==
"false" || value ==
"0")
127 throw std::invalid_argument(
"use_double_precision must be true, false, 1 or 0");
138 if (key ==
"precision" || key ==
"use_double_precision")
141 configMap[
"precision"] = useDouble ?
"double" :
"single";
142 configMap[
"use_double_precision"] = useDouble ?
"1" :
"0";
145 if (key ==
"gpu_device")
148 if (!svdGroup.empty())
151 configMap[key] = enabled ?
"true" :
"false";
154 for (
const char *algorithm : {
"",
"j",
"p",
"r"})
156 const auto otherKey = svdGroup + algorithm;
157 const auto other = configMap.find(otherKey);
160 if (otherKey != key && other != configMap.end())
161 other->second =
"false";
166 if ((key ==
"matrix_product_state_truncation_mode" || key ==
"matrix_product_operator_truncation_mode") && value !=
"relative_max" &&
167 value !=
"discarded_weight")
168 throw std::invalid_argument(
"Invalid truncation mode: expected relative_max or "
174 configMap[key] = value;
201 bool IsSet(
const std::string &key)
const
203 return configMap.find(key) != configMap.end();
216 auto it = configMap.find(key);
217 if (it != configMap.end())
226 auto it = configMap.find(key);
227 if (it != configMap.end())
229 return std::stoll(it->second);
238 auto it = configMap.find(key);
239 if (it != configMap.end())
241 return std::stoull(it->second);
248 auto it = configMap.find(key);
249 if (it != configMap.end())
251 return std::stod(it->second);
258 if (key ==
"gpu_device" || key ==
"method" || key ==
"use_double_precision" || key ==
"precision" || key ==
"max_parallel_threads" ||
259 key ==
"parallel_state_update" || key ==
"statevector_parallel_threshold")
267 if (key ==
"max_simulators" || key ==
"method")
275 for (
const auto &[key, value] : configMap)
294 simulator->SetSeed(std::stoull(value));
297 catch (
const std::exception &)
303 simulator->Configure(key.c_str(), value.c_str());
316 for (
const auto &[key, value] : config)
320 const std::unordered_map<std::string, std::string> &
GetConfigMap()
const
325 bool WasApplied(
const std::string &key,
const std::string &value)
const
327 auto it = configMap.find(key);
328 if (it != configMap.end())
329 return it->second == value;
335 std::unordered_map<std::string, std::string> configMap;
static std::string GpuSvdSettingGroup(const std::string &key)
bool IsSet(const std::string &key) const
Check if a configuration value is set.
static int ParseGpuDevice(const std::string &value)
Set a configuration value.
static bool ParsePrecision(const std::string &key, const std::string &value)
Configuration(const Configuration &)=default
The copy constructor.
Configuration & operator=(const Configuration &)=default
The copy assignment operator.
static bool CanBeAppliedOnInitializedSimulator(const std::string &key)
bool WasApplied(const std::string &key, const std::string &value) const
static bool ParseGpuSvdFlag(const std::string &value)
const std::unordered_map< std::string, std::string > & GetConfigMap() const
void ApplyConfigurationFromSimulator(const std::shared_ptr< Simulators::IState > &simulator)
void ApplyConfigurationFromMap(const std::unordered_map< std::string, std::string > &config)
void SetConfiguration(const std::string &key, double value)
double GetConfigurationAsDouble(const std::string &key) const
void SetConfiguration(const std::string &key, const std::string &value)
Configuration(Configuration &&)=default
The move constructor.
void ApplyConfigurationToSimulator(const std::shared_ptr< Simulators::IState > &simulator) const
unsigned long long int GetConfigurationAsUnsigned(const std::string &key) const
long long int GetConfigurationAsInt(const std::string &key) const
static bool IgnoredSetting(const std::string &key)
Configuration & operator=(Configuration &&)=default
The move assignment operator.
Configuration()=default
The constructor.
virtual ~Configuration()=default
The destructor.
void SetConfiguration(const std::string &key, long long int value)
std::string GetConfiguration(const std::string &key) const
Get a configuration value.
constexpr const char * kLegacyCap
constexpr const char * kTruncationWeight
constexpr const char * kVirtualBonds
double ParseWeight(const std::string &value)
bool ParseFlag(const std::string &value)
constexpr const char * kStrategy
Strategy ParseStrategy(const std::string &value)
Bounded online routing based on current physical bond dimensions.
@ kCompositeQCSim
composite qcsim simulator type
@ kQCSim
qcsim simulator type
bool IsGpuSimulator(SimulatorType type)