12#ifndef _SIMULATOR_CONFIGURATION_H_
13#define _SIMULATOR_CONFIGURATION_H_
17#include <unordered_map>
81 const auto result = std::from_chars(value.data(), value.data() + value.size(), device);
82 if (result.ec != std::errc() || result.ptr != value.data() + value.size() || device < 0)
83 throw std::invalid_argument(
"gpu_device must be a nonnegative integer");
90 for (
const char* group : {
"matrix_product_state_use_gesvd",
91 "matrix_product_operator_use_gesvd",
92 "tensor_network_use_gesvd"}) {
93 const size_t length = std::char_traits<char>::length(group);
94 if (key == group)
return group;
95 if (key.size() == length + 1 && key.compare(0, length, group) == 0 &&
96 (key.back() ==
'j' || key.back() ==
'p' || key.back() ==
'r'))
103 if (value ==
"true" || value ==
"1")
return true;
104 if (value ==
"false" || value ==
"0")
return false;
105 throw std::invalid_argument(
"GPU SVD flags must be true, false, 1 or 0");
111 if (!svdGroup.empty()) {
113 configMap[key] = enabled ?
"true" :
"false";
115 for (
const char* algorithm : {
"",
"j",
"p",
"r"}) {
116 const auto otherKey = svdGroup + algorithm;
117 const auto other = configMap.find(otherKey);
120 if (otherKey != key && other != configMap.end()) other->second =
"false";
125 if ((key ==
"matrix_product_state_truncation_mode" ||
126 key ==
"matrix_product_operator_truncation_mode") &&
127 value !=
"relative_max" && value !=
"discarded_weight")
128 throw std::invalid_argument(
129 "Invalid truncation mode: expected relative_max or "
134 configMap[key] = value;
157 bool IsSet(
const std::string& key)
const {
158 return configMap.find(key) != configMap.end();
170 auto it = configMap.find(key);
171 if (it != configMap.end()) {
178 auto it = configMap.find(key);
179 if (it != configMap.end()) {
180 return std::stoll(it->second);
188 const std::string& key)
const {
189 auto it = configMap.find(key);
190 if (it != configMap.end()) {
191 return std::stoull(it->second);
197 auto it = configMap.find(key);
198 if (it != configMap.end()) {
199 return std::stod(it->second);
205 if (key ==
"gpu_device" || key ==
"method" || key ==
"use_double_precision" ||
206 key ==
"precision" ||
207 key ==
"max_parallel_threads" || key ==
"parallel_state_update" ||
208 key ==
"statevector_parallel_threshold")
215 if (key ==
"max_simulators" || key ==
"method")
222 for (
const auto& [key, value] : configMap)
223 simulator->Configure(key.c_str(), value.c_str());
227 if (!simulator)
return;
235 const std::unordered_map<std::string, std::string>&
GetConfigMap()
const {
239 bool WasApplied(
const std::string& key,
const std::string& value)
const {
240 auto it = configMap.find(key);
241 if (it != configMap.end())
242 return it->second == value;
248 std::unordered_map<std::string, std::string>
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.
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.