Maestro 0.3.11
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
RoutingConfig.h
Go to the documentation of this file.
1#pragma once
2#include <cmath>
3#include <stdexcept>
4#include <string>
5
7{
8inline constexpr const char *kStrategy = "mps_routing_strategy";
9inline constexpr const char *kVirtualBonds = "mps_routing_virtual_bonds";
10inline constexpr const char *kLegacyCap = "mps_routing_legacy_cap";
11inline constexpr const char *kTruncationWeight = "mps_routing_truncation_weight";
12
13enum class Strategy
14{
17};
18
19inline Strategy ParseStrategy(const std::string &value)
20{
21 if (value == "combined")
22 return Strategy::Combined;
23 if (value == "legacy")
24 return Strategy::Legacy;
25 throw std::invalid_argument("mps_routing_strategy must be combined or legacy");
26}
27
28template <class Config> Strategy GetStrategy(const Config &config)
29{
30 return config.IsSet(kStrategy) ? ParseStrategy(config.GetConfiguration(kStrategy)) : Strategy::Legacy;
31}
32
33inline bool ParseFlag(const std::string &value)
34{
35 if (value == "true" || value == "1")
36 return true;
37 if (value == "false" || value == "0")
38 return false;
39 throw std::invalid_argument("Routing flags must be true, false, 1 or 0");
40}
41
42inline double ParseWeight(const std::string &value)
43{
44 size_t consumed = 0;
45 const double weight = std::stod(value, &consumed);
46 if (consumed != value.size() || !std::isfinite(weight) || weight < 0.)
47 throw std::invalid_argument("mps_routing_truncation_weight must be finite and nonnegative");
48 return weight;
49}
50
51template <class Config> bool VirtualBondsEnabled(const Config &config)
52{
53 // The old opt-out remains an override for existing C++ callers.
54 if (config.IsSet(kLegacyCap) && ParseFlag(config.GetConfiguration(kLegacyCap)))
55 return false;
56 return config.IsSet(kVirtualBonds) && ParseFlag(config.GetConfiguration(kVirtualBonds));
57}
58
59template <class Config> double TruncationWeight(const Config &config)
60{
61 return config.IsSet(kTruncationWeight) ? ParseWeight(config.GetConfiguration(kTruncationWeight)) : 1.;
62}
63} // namespace Simulators::RoutingConfig
constexpr const char * kLegacyCap
constexpr const char * kTruncationWeight
constexpr const char * kVirtualBonds
bool VirtualBondsEnabled(const Config &config)
double ParseWeight(const std::string &value)
bool ParseFlag(const std::string &value)
Strategy GetStrategy(const Config &config)
constexpr const char * kStrategy
Strategy ParseStrategy(const std::string &value)
double TruncationWeight(const Config &config)