.. _program_listing_file_blackbird_cpp_BlackbirdVariables.h: Program Listing for File BlackbirdVariables.h ============================================= |exhale_lsh| :ref:`Return to documentation for file ` (``blackbird_cpp/BlackbirdVariables.h``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp // Copyright 2019 Xanadu Quantum Technologies Inc. // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // http://www.apache.org/licenses/LICENSE-2.0 // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // =========================== // Define the variable maps // =========================== namespace blackbird { template struct variable_map; template<> struct variable_map { static int getVal(Visitor *V, std::string key) { auto iter_int = V->int_vars.find(key); auto iter_float = V->float_vars.find(key); auto iter_complex = V->complex_vars.find(key); auto iter_str = V->str_vars.find(key); auto iter_bool = V->bool_vars.find(key); int val; if (iter_int != V->int_vars.end()) { val = iter_int->second; } else if (iter_float != V->float_vars.end()) { val = iter_float->second; } else if (iter_complex != V->complex_vars.end()) { throw std::invalid_argument("Cannot convert a complex variable to an int"); } else if (iter_str != V->str_vars.end()) { throw std::invalid_argument("Cannot convert a string variable to an int"); } else if (iter_bool != V->bool_vars.end()) { throw std::invalid_argument("Cannot convert a bool variable to an int"); } else { throw std::invalid_argument("Variable " + key + " not defined"); } return val; } static void setVal(Visitor *V, std::string key, int val) { V->int_vars[key] = val; } }; template<> struct variable_map { static double getVal(Visitor *V, std::string key) { auto iter_int = V->int_vars.find(key); auto iter_float = V->float_vars.find(key); auto iter_complex = V->complex_vars.find(key); auto iter_str = V->str_vars.find(key); auto iter_bool = V->bool_vars.find(key); double val; if (iter_float != V->float_vars.end()) { val = iter_float->second; } else if (iter_int != V->int_vars.end()) { val = iter_int->second; } else if (iter_complex != V->complex_vars.end()) { throw std::invalid_argument("Cannot convert a complex variable to a float"); } else if (iter_str != V->str_vars.end()) { throw std::invalid_argument("Cannot convert a string variable to a float"); } else if (iter_bool != V->bool_vars.end()) { throw std::invalid_argument("Cannot convert a bool variable to a float"); } else { throw std::invalid_argument("Variable " + key + " not defined"); } return val; } static void setVal(Visitor *V, std::string key, double val) { V->float_vars[key] = val; } }; template<> struct variable_map> { static std::complex getVal(Visitor *V, std::string key) { auto iter_int = V->int_vars.find(key); auto iter_float = V->float_vars.find(key); auto iter_complex = V->complex_vars.find(key); auto iter_str = V->str_vars.find(key); auto iter_bool = V->bool_vars.find(key); std::complex val; if (iter_complex != V->complex_vars.end()) { val = iter_complex->second; } else if (iter_int != V->int_vars.end()) { val = iter_int->second; } else if (iter_float != V->float_vars.end()) { val = iter_float->second; } else if (iter_str != V->str_vars.end()) { throw std::invalid_argument("Cannot convert a string variable to a complex"); } else if (iter_bool != V->bool_vars.end()) { throw std::invalid_argument("Cannot convert a bool variable to a complex"); } else { throw std::invalid_argument("Variable " + key + " not defined"); } return val; } static void setVal(Visitor *V, std::string key, std::complex val) { V->complex_vars[key] = val; } }; template<> struct variable_map { static std::string getVal(Visitor *V, std::string key) { auto iter_int = V->int_vars.find(key); auto iter_float = V->float_vars.find(key); auto iter_complex = V->complex_vars.find(key); auto iter_str = V->str_vars.find(key); auto iter_bool = V->bool_vars.find(key); if (iter_str != V->str_vars.end()) { return iter_str->second; } else if (iter_complex != V->complex_vars.end()) { throw std::invalid_argument("Cannot convert a complex variable to a string"); } else if (iter_int != V->int_vars.end()) { throw std::invalid_argument("Cannot convert an int variable to a string"); } else if (iter_float != V->float_vars.end()) { throw std::invalid_argument("Cannot convert a float variable to a string"); } else if (iter_bool != V->bool_vars.end()) { throw std::invalid_argument("Cannot convert a bool variable to a string"); } else { throw std::invalid_argument("Variable " + key + " not defined"); } } static void setVal(Visitor *V, std::string key, std::string val) { if (val.front() == '"') { val.erase(0, 1); // erase the first character val.erase(val.size() - 1); // erase the last character } V->str_vars[key] = val; } }; template<> struct variable_map { static bool getVal(Visitor *V, std::string key) { auto iter_int = V->int_vars.find(key); auto iter_float = V->float_vars.find(key); auto iter_complex = V->complex_vars.find(key); auto iter_str = V->str_vars.find(key); auto iter_bool = V->bool_vars.find(key); if (iter_bool != V->bool_vars.end()) { return iter_bool->second; } else if (iter_complex != V->complex_vars.end()) { throw std::invalid_argument("Cannot convert a complex variable to a bool"); } else if (iter_int != V->int_vars.end()) { throw std::invalid_argument("Cannot convert an int variable to a bool"); } else if (iter_float != V->float_vars.end()) { throw std::invalid_argument("Cannot convert a float variable to a bool"); } else if (iter_str != V->str_vars.end()) { throw std::invalid_argument("Cannot convert a string variable to a bool"); } else { throw std::invalid_argument("Variable " + key + " not defined"); } } static void setVal(Visitor *V, std::string key, bool val) { V->bool_vars[key] = val; } static void setVal(Visitor *V, std::string key, std::string val) { if (val == "True") { V->bool_vars[key] = true; } else if (val == "False") { V->bool_vars[key] = false; } else { throw std::invalid_argument(val + "is not a valid boolean variable"); } } }; }