/******************************************************************************* * Copyright (c) 2019 UT-Battelle, LLC. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * and Eclipse Distribution License v1.0 which accompanies this * distribution. The Eclipse Public License is available at * http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution *License is available at https://eclipse.org/org/documents/edl-v10.php * * Contributors: * Alexander J. McCaskey - initial API and implementation *******************************************************************************/ #ifndef QUANTUM_GATE_ACCELERATORS_IBMACCELERATOR_QOBJECT_HPP_ #define QUANTUM_GATE_ACCELERATORS_IBMACCELERATOR_QOBJECT_HPP_ #include "variant.hpp" #include "json.hpp" // #include #include #include #include #include #ifndef NLOHMANN_OPT_HELPER #define NLOHMANN_OPT_HELPER namespace nlohmann { template struct adl_serializer> { static void to_json(json &j, const std::shared_ptr &opt) { if (!opt) j = nullptr; else j = *opt; } static std::shared_ptr from_json(const json &j) { if (j.is_null()) return std::unique_ptr(); else return std::unique_ptr(new T(j.get())); } }; } // namespace nlohmann #endif namespace xacc { namespace ibm { using nlohmann::json; inline json get_untyped(const json &j, const char *property) { if (j.find(property) != j.end()) { return j.at(property).get(); } return json(); } inline json get_untyped(const json &j, std::string property) { return get_untyped(j, property.data()); } template inline std::shared_ptr get_optional(const json &j, const char *property) { if (j.find(property) != j.end()) { return j.at(property).get>(); } return std::shared_ptr(); } template inline std::shared_ptr get_optional(const json &j, std::string property) { return get_optional(j, property.data()); } class Backend { public: Backend() = default; virtual ~Backend() = default; private: std::string name; public: const std::string &get_name() const { return name; } std::string &get_mutable_name() { return name; } void set_name(const std::string &value) { this->name = value; } }; class QObjectConfig { public: QObjectConfig() = default; virtual ~QObjectConfig() = default; private: int64_t shots; int64_t n_qubits; int64_t memory_slots; bool memory; std::vector parameter_binds; std::vector meas_lo_freq; std::vector schedule_los; std::vector qubit_lo_freq; std::vector qubit_lo_range; std::vector meas_lo_range; std::string meas_return; int64_t meas_level; int64_t memory_slot_size; public: const int64_t &get_shots() const { return shots; } int64_t &get_mutable_shots() { return shots; } void set_shots(const int64_t &value) { this->shots = value; } const int64_t &get_n_qubits() const { return n_qubits; } int64_t &get_mutable_n_qubits() { return n_qubits; } void set_n_qubits(const int64_t &value) { this->n_qubits = value; } const int64_t &get_memory_slots() const { return memory_slots; } int64_t &get_mutable_memory_slots() { return memory_slots; } void set_memory_slots(const int64_t &value) { this->memory_slots = value; } const bool &get_memory() const { return memory; } bool &get_mutable_memory() { return memory; } void set_memory(const bool &value) { this->memory = value; } const std::vector &get_parameter_binds() const { return parameter_binds; } std::vector &get_mutable_parameter_binds() { return parameter_binds; } void set_parameter_binds(const std::vector &value) { this->parameter_binds = value; } const std::vector &get_meas_lo_freq() const { return meas_lo_freq; } std::vector &get_mutable_meas_lo_freq() { return meas_lo_freq; } void set_meas_lo_freq(const std::vector &value) { this->meas_lo_freq = value; } const std::vector &get_schedule_los() const { return schedule_los; } std::vector &get_mutable_schedule_los() { return schedule_los; } void set_schedule_los(const std::vector &value) { this->schedule_los = value; } const std::vector &get_qubit_lo_freq() const { return qubit_lo_freq; } std::vector &get_mutable_qubit_lo_freq() { return qubit_lo_freq; } void set_qubit_lo_freq(const std::vector &value) { this->qubit_lo_freq = value; } const std::vector &get_qubit_lo_range() const { return qubit_lo_range; } std::vector &get_mutable_qubit_lo_range() { return qubit_lo_range; } void set_qubit_lo_range(const std::vector &value) { this->qubit_lo_range = value; } const std::vector &get_meas_lo_range() const { return meas_lo_range; } std::vector &get_mutable_meas_lo_range() { return meas_lo_range; } void set_meas_lo_range(const std::vector &value) { this->meas_lo_range = value; } const std::string &get_meas_return() const { return meas_return; } std::string &get_mutable_meas_return() { return meas_return; } void set_meas_return(const std::string &value) { this->meas_return = value; } const int64_t &get_meas_level() const { return meas_level; } int64_t &get_mutable_meas_level() { return meas_level; } void set_meas_level(const int64_t &value) { this->meas_level = value; } const int64_t &get_memory_slot_size() const { return memory_slot_size; } int64_t &get_mutable_memory_slot_size() { return memory_slot_size; } void set_memory_slot_size(const int64_t &value) { this->memory_slot_size = value; } }; class ExperimentConfig { public: ExperimentConfig() = default; virtual ~ExperimentConfig() = default; private: int64_t n_qubits; int64_t memory_slots; public: const int64_t &get_n_qubits() const { return n_qubits; } int64_t &get_mutable_n_qubits() { return n_qubits; } void set_n_qubits(const int64_t &value) { this->n_qubits = value; } const int64_t &get_memory_slots() const { return memory_slots; } int64_t &get_mutable_memory_slots() { return memory_slots; } void set_memory_slots(const int64_t &value) { this->memory_slots = value; } }; using ClbitLabel = mpark::variant; enum class QregSizeEnum : int { Q }; using QregSizeElement = mpark::variant; class ExperimentHeader { public: ExperimentHeader() = default; virtual ~ExperimentHeader() = default; private: std::string name; std::vector> qreg_sizes; int64_t n_qubits; std::vector> qubit_labels; int64_t memory_slots; std::vector> creg_sizes; std::vector> clbit_labels; public: const std::string &get_name() const { return name; } std::string &get_mutable_name() { return name; } void set_name(const std::string &value) { this->name = value; } const std::vector> &get_qreg_sizes() const { return qreg_sizes; } std::vector> &get_mutable_qreg_sizes() { return qreg_sizes; } void set_qreg_sizes(const std::vector> &value) { this->qreg_sizes = value; } const int64_t &get_n_qubits() const { return n_qubits; } int64_t &get_mutable_n_qubits() { return n_qubits; } void set_n_qubits(const int64_t &value) { this->n_qubits = value; } const std::vector> &get_qubit_labels() const { return qubit_labels; } std::vector> &get_mutable_qubit_labels() { return qubit_labels; } void set_qubit_labels(const std::vector> &value) { this->qubit_labels = value; } const int64_t &get_memory_slots() const { return memory_slots; } int64_t &get_mutable_memory_slots() { return memory_slots; } void set_memory_slots(const int64_t &value) { this->memory_slots = value; } const std::vector> &get_creg_sizes() const { return creg_sizes; } std::vector> &get_mutable_creg_sizes() { return creg_sizes; } void set_creg_sizes(const std::vector> &value) { this->creg_sizes = value; } const std::vector> &get_clbit_labels() const { return clbit_labels; } std::vector> &get_mutable_clbit_labels() { return clbit_labels; } void set_clbit_labels(const std::vector> &value) { this->clbit_labels = value; } }; struct Bfunc { Bfunc(int64_t in_regId, const std::string &in_hexMask, const std::string &in_relation = "==", const std::string &in_val = "") : registerId(in_regId), hex_mask(in_hexMask), relation(in_relation), hex_val(in_val.empty() ? in_hexMask : in_val) {} int64_t registerId; std::string hex_mask; std::string relation; std::string hex_val; }; class RegisterAllocator { static inline RegisterAllocator *instance = nullptr; std::unordered_map memoryToRegister; int64_t registerId; RegisterAllocator() { registerId = 0; } public: static RegisterAllocator *getInstance() { if (!instance) { instance = new RegisterAllocator; } return instance; } std::optional getRegisterId(int64_t in_memoryId) { if (memoryToRegister.find(in_memoryId) != memoryToRegister.end()) { return memoryToRegister[in_memoryId]; } return std::nullopt; } int64_t mapMemory(int64_t in_memoryId) { // Don't need to generate new mapping; // e.g. multiple if conditioned on the same measurement. auto iter = memoryToRegister.find(in_memoryId); if (iter != memoryToRegister.end()) { return iter->second; } memoryToRegister[in_memoryId] = registerId; registerId++; return memoryToRegister[in_memoryId]; } int64_t getNextRegister() { const auto result = registerId; ++registerId; return result; } void reset() { memoryToRegister.clear(); registerId = 0; } }; class Instruction { public: Instruction() = default; virtual ~Instruction() = default; // Construct a binary function instruction // Returns the Bfunc instruction and the register Id to condition the // sub-circuit. static Instruction createConditionalInst(int64_t memoryId) { // Map the memory Id to a register: const auto measRegisterId = RegisterAllocator::getInstance()->mapMemory(memoryId); const int64_t maskVal = 1ULL << measRegisterId; const std::string hexMaskStr = "0x" + std::to_string(maskVal); const int64_t resultRegisterId = RegisterAllocator::getInstance()->getNextRegister(); Bfunc bFuncObj(resultRegisterId, hexMaskStr); Instruction newInst; newInst.bfunc = bFuncObj; return newInst; } private: std::vector qubits; std::string name; std::vector params; std::vector memory; // Conditional on a register value std::optional conditional; // If this is a Bfunc to compute a register value std::optional bfunc; public: const std::vector &get_qubits() const { return qubits; } std::vector &get_mutable_qubits() { return qubits; } void set_qubits(const std::vector &value) { this->qubits = value; } const std::string &get_name() const { return name; } std::string &get_mutable_name() { return name; } void set_name(const std::string &value) { this->name = value; } std::vector get_params() const { return params; } void set_params(std::vector value) { this->params = value; } std::vector get_memory() const { return memory; } void set_memory(std::vector value) { this->memory = value; } std::optional get_bFunc() const { return bfunc; } void set_bFunc(Bfunc value) { this->bfunc = value; } bool isBfuc() const { return get_bFunc().has_value(); } std::optional get_condition_reg_id() const { return conditional; } void set_condition_reg_id(int64_t value) { this->conditional = value; } std::string toString() const { std::stringstream ss; ss << name; if (!params.empty()) { ss << "("; for (int i = 0; i < params.size(); ++i) { ss << params[i]; if (i != params.size() - 1) { ss << ", "; } } ss << ")"; } for (int i = 0; i < qubits.size(); ++i) { ss << " q[" << qubits[i] << "]"; if (i != qubits.size() - 1) { ss << ", "; } } if (!get_memory().empty()) { ss << " -> c[" << get_memory()[0] << "]"; } ss << ";"; return ss.str(); } }; class Experiment { public: Experiment() = default; virtual ~Experiment() = default; private: ExperimentConfig config; ExperimentHeader header; std::vector instructions; public: const ExperimentConfig &get_config() const { return config; } ExperimentConfig &get_mutable_config() { return config; } void set_config(const ExperimentConfig &value) { this->config = value; } const ExperimentHeader &get_header() const { return header; } ExperimentHeader &get_mutable_header() { return header; } void set_header(const ExperimentHeader &value) { this->header = value; } const std::vector &get_instructions() const { return instructions; } std::vector &get_mutable_instructions() { return instructions; } void set_instructions(const std::vector &value) { this->instructions = value; } }; class QObjectHeader { public: QObjectHeader() = default; virtual ~QObjectHeader() = default; private: std::string backend_version; std::string backend_name; public: const std::string &get_backend_version() const { return backend_version; } std::string &get_mutable_backend_version() { return backend_version; } void set_backend_version(const std::string &value) { this->backend_version = value; } const std::string &get_backend_name() const { return backend_name; } std::string &get_mutable_backend_name() { return backend_name; } void set_backend_name(const std::string &value) { this->backend_name = value; } }; class QObject { public: QObject() = default; virtual ~QObject() = default; private: std::string qobj_id; std::vector experiments; QObjectConfig config; std::string type; std::string schema_version; QObjectHeader header; public: const std::string &get_qobj_id() const { return qobj_id; } std::string &get_mutable_qobj_id() { return qobj_id; } void set_qobj_id(const std::string &value) { this->qobj_id = value; } const std::vector &get_experiments() const { return experiments; } std::vector &get_mutable_experiments() { return experiments; } void set_experiments(const std::vector &value) { this->experiments = value; } const QObjectConfig &get_config() const { return config; } QObjectConfig &get_mutable_config() { return config; } void set_config(const QObjectConfig &value) { this->config = value; } const std::string &get_type() const { return type; } std::string &get_mutable_type() { return type; } void set_type(const std::string &value) { this->type = value; } const std::string &get_schema_version() const { return schema_version; } std::string &get_mutable_schema_version() { return schema_version; } void set_schema_version(const std::string &value) { this->schema_version = value; } const QObjectHeader &get_header() const { return header; } QObjectHeader &get_mutable_header() { return header; } void set_header(const QObjectHeader &value) { this->header = value; } }; class QObjectRoot { public: QObjectRoot() = default; virtual ~QObjectRoot() = default; private: QObject q_object; Backend backend; int64_t shots; public: const QObject &get_q_object() const { return q_object; } QObject &get_mutable_q_object() { return q_object; } void set_q_object(const QObject &value) { this->q_object = value; } const Backend &get_backend() const { return backend; } Backend &get_mutable_backend() { return backend; } void set_backend(const Backend &value) { this->backend = value; } const int64_t &get_shots() const { return shots; } int64_t &get_mutable_shots() { return shots; } void set_shots(const int64_t &value) { this->shots = value; } }; class Data { public: Data() = default; virtual ~Data() = default; private: std::map counts; public: const std::map &get_counts() const { return counts; } std::map &get_mutable_counts() { return counts; } void set_counts(const std::map &value) { this->counts = value; } }; class Result { public: Result() = default; virtual ~Result() = default; private: Data data; ExperimentHeader header; int64_t meas_level; int64_t shots; bool success; public: const Data &get_data() const { return data; } Data &get_mutable_data() { return data; } void set_data(const Data &value) { this->data = value; } const ExperimentHeader &get_header() const { return header; } ExperimentHeader &get_mutable_header() { return header; } void set_header(const ExperimentHeader &value) { this->header = value; } const int64_t &get_meas_level() const { return meas_level; } int64_t &get_mutable_meas_level() { return meas_level; } void set_meas_level(const int64_t &value) { this->meas_level = value; } const int64_t &get_shots() const { return shots; } int64_t &get_mutable_shots() { return shots; } void set_shots(const int64_t &value) { this->shots = value; } const bool &get_success() const { return success; } bool &get_mutable_success() { return success; } void set_success(const bool &value) { this->success = value; } }; class QObjectResult { public: QObjectResult() = default; virtual ~QObjectResult() = default; private: std::string backend_name; std::string backend_version; std::string date; std::string execution_id; QObjectHeader header; std::string job_id; std::string qobj_id; std::vector results; std::string status; bool success; public: const std::string &get_backend_name() const { return backend_name; } std::string &get_mutable_backend_name() { return backend_name; } void set_backend_name(const std::string &value) { this->backend_name = value; } const std::string &get_backend_version() const { return backend_version; } std::string &get_mutable_backend_version() { return backend_version; } void set_backend_version(const std::string &value) { this->backend_version = value; } const std::string &get_date() const { return date; } std::string &get_mutable_date() { return date; } void set_date(const std::string &value) { this->date = value; } const std::string &get_execution_id() const { return execution_id; } std::string &get_mutable_execution_id() { return execution_id; } void set_execution_id(const std::string &value) { this->execution_id = value; } const QObjectHeader &get_header() const { return header; } QObjectHeader &get_mutable_header() { return header; } void set_header(const QObjectHeader &value) { this->header = value; } const std::string &get_job_id() const { return job_id; } std::string &get_mutable_job_id() { return job_id; } void set_job_id(const std::string &value) { this->job_id = value; } const std::string &get_qobj_id() const { return qobj_id; } std::string &get_mutable_qobj_id() { return qobj_id; } void set_qobj_id(const std::string &value) { this->qobj_id = value; } const std::vector &get_results() const { return results; } std::vector &get_mutable_results() { return results; } void set_results(const std::vector &value) { this->results = value; } const std::string &get_status() const { return status; } std::string &get_mutable_status() { return status; } void set_status(const std::string &value) { this->status = value; } const bool &get_success() const { return success; } bool &get_mutable_success() { return success; } void set_success(const bool &value) { this->success = value; } }; } // namespace ibm } // namespace xacc namespace nlohmann { void from_json(const json &j, xacc::ibm::Backend &x); void to_json(json &j, const xacc::ibm::Backend &x); void from_json(const json &j, xacc::ibm::QObjectConfig &x); void to_json(json &j, const xacc::ibm::QObjectConfig &x); void from_json(const json &j, xacc::ibm::ExperimentConfig &x); void to_json(json &j, const xacc::ibm::ExperimentConfig &x); void from_json(const json &j, xacc::ibm::ExperimentHeader &x); void to_json(json &j, const xacc::ibm::ExperimentHeader &x); void from_json(const json &j, xacc::ibm::Instruction &x); void to_json(json &j, const xacc::ibm::Instruction &x); void from_json(const json &j, xacc::ibm::Experiment &x); void to_json(json &j, const xacc::ibm::Experiment &x); void from_json(const json &j, xacc::ibm::QObjectHeader &x); void to_json(json &j, const xacc::ibm::QObjectHeader &x); void from_json(const json &j, xacc::ibm::QObject &x); void to_json(json &j, const xacc::ibm::QObject &x); void from_json(const json &j, xacc::ibm::QObjectRoot &x); void to_json(json &j, const xacc::ibm::QObjectRoot &x); void from_json(const json &j, xacc::ibm::QregSizeEnum &x); void to_json(json &j, const xacc::ibm::QregSizeEnum &x); void from_json(const json &j, mpark::variant &x); void to_json(json &j, const mpark::variant &x); void from_json(const json &j, mpark::variant &x); void to_json(json &j, const mpark::variant &x); void from_json(const json &j, xacc::ibm::Data &x); void to_json(json &j, const xacc::ibm::Data &x); void from_json(const json &j, xacc::ibm::Result &x); void to_json(json &j, const xacc::ibm::Result &x); void from_json(const json &j, xacc::ibm::QObjectResult &x); void to_json(json &j, const xacc::ibm::QObjectResult &x); inline void from_json(const json &j, xacc::ibm::Backend &x) { x.set_name(j.at("name").get()); } inline void to_json(json &j, const xacc::ibm::Backend &x) { j = json::object(); j["name"] = x.get_name(); } inline void from_json(const json &j, xacc::ibm::QObjectConfig &x) { x.set_shots(j.at("shots").get()); x.set_n_qubits(j.at("n_qubits").get()); x.set_memory_slots(j.at("memory_slots").get()); x.set_memory(j.at("memory").get()); x.set_parameter_binds(j.at("parameter_binds").get>()); if (j.find("meas_lo_freq") != j.end()) { x.set_meas_lo_freq(j.at("meas_lo_freq").get>()); } x.set_schedule_los(j.at("schedule_los").get>()); if (j.find("qubit_lo_freq") != j.end()) { x.set_qubit_lo_freq(j.at("qubit_lo_freq").get>()); } if (j.find("qubit_lo_range") != j.end()) { x.set_qubit_lo_range(j.at("qubit_lo_range").get>()); } if (j.find("meas_lo_range") != j.end()) { x.set_meas_lo_range(j.at("meas_lo_range").get>()); } x.set_meas_return(j.at("meas_return").get()); x.set_meas_level(j.at("meas_level").get()); x.set_memory_slot_size(j.at("memory_slot_size").get()); } inline void to_json(json &j, const xacc::ibm::QObjectConfig &x) { j = json::object(); j["shots"] = x.get_shots(); j["n_qubits"] = x.get_n_qubits(); j["memory_slots"] = x.get_memory_slots(); j["memory"] = x.get_memory(); j["parameter_binds"] = x.get_parameter_binds(); if (!x.get_meas_lo_freq().empty()) { j["meas_lo_freq"] = x.get_meas_lo_freq(); } if (!x.get_schedule_los().empty()) { j["schedule_los"] = x.get_schedule_los(); } if (!x.get_qubit_lo_freq().empty()) { j["qubit_lo_freq"] = x.get_qubit_lo_freq(); } if (!x.get_qubit_lo_range().empty()) { j["qubit_lo_range"] = x.get_qubit_lo_range(); } if (!x.get_meas_lo_range().empty()) { j["meas_lo_range"] = x.get_meas_lo_range(); } j["meas_return"] = x.get_meas_return(); j["meas_level"] = x.get_meas_level(); j["memory_slot_size"] = x.get_memory_slot_size(); } inline void from_json(const json &j, xacc::ibm::ExperimentConfig &x) { x.set_n_qubits(j.at("n_qubits").get()); x.set_memory_slots(j.at("memory_slots").get()); } inline void to_json(json &j, const xacc::ibm::ExperimentConfig &x) { j = json::object(); j["n_qubits"] = x.get_n_qubits(); j["memory_slots"] = x.get_memory_slots(); } inline void from_json(const json &j, xacc::ibm::ExperimentHeader &x) { x.set_name(j.at("name").get()); x.set_qreg_sizes( j.at("qreg_sizes") .get>>()); x.set_n_qubits(j.at("n_qubits").get()); x.set_qubit_labels( j.at("qubit_labels") .get>>()); x.set_memory_slots(j.at("memory_slots").get()); x.set_creg_sizes(j.at("creg_sizes") .get>>()); x.set_clbit_labels( j.at("clbit_labels") .get>>()); } inline void to_json(json &j, const xacc::ibm::ExperimentHeader &x) { j = json::object(); j["name"] = x.get_name(); j["qreg_sizes"] = x.get_qreg_sizes(); j["n_qubits"] = x.get_n_qubits(); j["qubit_labels"] = x.get_qubit_labels(); j["memory_slots"] = x.get_memory_slots(); j["creg_sizes"] = x.get_creg_sizes(); j["clbit_labels"] = x.get_clbit_labels(); } inline void from_json(const json &j, xacc::ibm::Instruction &x) { x.set_qubits(j.at("qubits").get>()); x.set_name(j.at("name").get()); if (j.find("params") != j.end()) { x.set_params(j.at("params").get>()); } if (j.find("memory") != j.end()) { x.set_memory(j.at("memory").get>()); } } inline void to_json(json &j, const xacc::ibm::Instruction &x) { j = json::object(); if (x.isBfuc()) { j["name"] = "bfunc"; j["register"] = x.get_bFunc()->registerId; j["mask"] = x.get_bFunc()->hex_mask; j["relation"] = x.get_bFunc()->relation; j["val"] = x.get_bFunc()->hex_val; return; } j["qubits"] = x.get_qubits(); j["name"] = x.get_name(); if (!x.get_params().empty()) { j["params"] = x.get_params(); } if (!x.get_memory().empty()) { j["memory"] = x.get_memory(); // Technically, we only use one memory slot in each Measure Op: if (x.get_memory().size() == 1) { const auto memoryId = x.get_memory()[0]; const auto measRegisterId = xacc::ibm::RegisterAllocator::getInstance()->getRegisterId(memoryId); if (measRegisterId.has_value()) { std::vector registerIds; registerIds.emplace_back(measRegisterId.value()); j["register"] = registerIds; } } } if (x.get_condition_reg_id().has_value()) { j["conditional"] = x.get_condition_reg_id().value(); } } inline void from_json(const json &j, xacc::ibm::Experiment &x) { x.set_config(j.at("config").get()); x.set_header(j.at("header").get()); x.set_instructions( j.at("instructions").get>()); } inline void to_json(json &j, const xacc::ibm::Experiment &x) { j = json::object(); j["config"] = x.get_config(); j["header"] = x.get_header(); j["instructions"] = x.get_instructions(); } inline void from_json(const json &j, xacc::ibm::QObjectHeader &x) { x.set_backend_version(j.at("backend_version").get()); x.set_backend_name(j.at("backend_name").get()); } inline void to_json(json &j, const xacc::ibm::QObjectHeader &x) { j = json::object(); j["backend_version"] = x.get_backend_version(); j["backend_name"] = x.get_backend_name(); } inline void from_json(const json &j, xacc::ibm::QObject &x) { x.set_qobj_id(j.at("qobj_id").get()); x.set_experiments( j.at("experiments").get>()); x.set_config(j.at("config").get()); x.set_type(j.at("type").get()); x.set_schema_version(j.at("schema_version").get()); x.set_header(j.at("header").get()); } inline void to_json(json &j, const xacc::ibm::QObject &x) { j = json::object(); j["qobj_id"] = x.get_qobj_id(); j["experiments"] = x.get_experiments(); j["config"] = x.get_config(); j["type"] = x.get_type(); j["schema_version"] = x.get_schema_version(); j["header"] = x.get_header(); } inline void from_json(const json &j, xacc::ibm::QObjectRoot &x) { x.set_q_object(j.at("qObject").get()); if (j.find("backend") != j.end()) { x.set_backend(j.at("backend").get()); } x.set_shots(j.at("shots").get()); } inline void to_json(json &j, const xacc::ibm::QObjectRoot &x) { j = json::object(); j["qObject"] = x.get_q_object(); j["backend"] = x.get_backend(); j["shots"] = x.get_shots(); } inline void from_json(const json &j, xacc::ibm::QregSizeEnum &x) { if (j == "q") x = xacc::ibm::QregSizeEnum::Q; else throw "Input JSON does not conform to schema"; } inline void to_json(json &j, const xacc::ibm::QregSizeEnum &x) { switch (x) { case xacc::ibm::QregSizeEnum::Q: j = "q"; break; default: throw "This should not happen"; } } inline void from_json(const json &j, mpark::variant &x) { if (j.is_number_integer()) x = j.get(); else if (j.is_string()) x = j.get(); else throw "Could not deserialize"; } inline void to_json(json &j, const mpark::variant &x) { switch (x.index()) { case 0: j = mpark::get(x); break; case 1: j = mpark::get(x); break; default: throw "Input JSON does not conform to schema"; } } inline void from_json(const json &j, mpark::variant &x) { if (j.is_number_integer()) x = j.get(); else if (j.is_string()) x = j.get(); else throw "Could not deserialize"; } inline void to_json(json &j, const mpark::variant &x) { switch (x.index()) { case 0: j = mpark::get(x); break; case 1: j = mpark::get(x); break; default: throw "Input JSON does not conform to schema"; } } inline void from_json(const json &j, xacc::ibm::Data &x) { x.set_counts(j.at("counts").get>()); } inline void to_json(json &j, const xacc::ibm::Data &x) { j = json::object(); j["counts"] = x.get_counts(); } inline void from_json(const json &j, xacc::ibm::Result &x) { x.set_data(j.at("data").get()); x.set_header(j.at("header").get()); if (j.find("meas_level") != j.end()) { x.set_meas_level(j.at("meas_level").get()); } x.set_shots(j.at("shots").get()); x.set_success(j.at("success").get()); } inline void to_json(json &j, const xacc::ibm::Result &x) { j = json::object(); j["data"] = x.get_data(); j["header"] = x.get_header(); j["meas_level"] = x.get_meas_level(); j["shots"] = x.get_shots(); j["success"] = x.get_success(); } inline void from_json(const json &j, xacc::ibm::QObjectResult &x) { x.set_backend_name(j.at("backend_name").get()); x.set_backend_version(j.at("backend_version").get()); x.set_date(j.at("date").get()); if (j.find("execution_id") != j.end()) { x.set_execution_id(j.at("execution_id").get()); } if (j.find("header") != j.end()) { x.set_header(j.at("header").get()); } x.set_job_id(j.at("job_id").get()); x.set_qobj_id(j.at("qobj_id").get()); x.set_results(j.at("results").get>()); x.set_status(j.at("status").get()); x.set_success(j.at("success").get()); } inline void to_json(json &j, const xacc::ibm::QObjectResult &x) { j = json::object(); j["backend_name"] = x.get_backend_name(); j["backend_version"] = x.get_backend_version(); j["date"] = x.get_date(); j["execution_id"] = x.get_execution_id(); j["header"] = x.get_header(); j["job_id"] = x.get_job_id(); j["qobj_id"] = x.get_qobj_id(); j["results"] = x.get_results(); j["status"] = x.get_status(); j["success"] = x.get_success(); } } // namespace nlohmann #endif