From b45655a74c10517b8ef2b9bd706c498d7c7d88f2 Mon Sep 17 00:00:00 2001 From: Ryo Wakizaka Date: Fri, 6 Mar 2026 20:01:28 +0900 Subject: [PATCH 1/2] Fix collecting creg data from the result JSON --- .../containers/sampler_pub_result.hpp | 178 ++++++++---------- 1 file changed, 80 insertions(+), 98 deletions(-) diff --git a/src/primitives/containers/sampler_pub_result.hpp b/src/primitives/containers/sampler_pub_result.hpp index d257219..1fc9df8 100644 --- a/src/primitives/containers/sampler_pub_result.hpp +++ b/src/primitives/containers/sampler_pub_result.hpp @@ -16,7 +16,6 @@ #include - #ifndef __qiskitcpp_primitives_sampler_pub_result_hpp__ #define __qiskitcpp_primitives_sampler_pub_result_hpp__ @@ -31,118 +30,101 @@ namespace primitives { /// @brief Result of Sampler Pub(Primitive Unified Bloc). class SamplerPubResult { protected: - std::unordered_map data_; // in pair of creg name and bitstrings - SamplerPub pub_; // + std::unordered_map + data_; // in pair of creg name and bitstrings + SamplerPub pub_; // public: - /// @brief Create a new SamplerPubResult - SamplerPubResult() {} - - /// @brief Create a new SamplerPubResult - /// @param pub a pub for this result - SamplerPubResult(SamplerPub& pub) - { - pub_ = pub; - for (auto creg : pub_.circuit().cregs()) { - data_[creg.name()] = BitArray(); - data_[creg.name()].set_bits(creg.size()); - } + /// @brief Create a new SamplerPubResult + SamplerPubResult() {} + + /// @brief Create a new SamplerPubResult + /// @param pub a pub for this result + SamplerPubResult(SamplerPub &pub) { + pub_ = pub; + for (auto creg : pub_.circuit().cregs()) { + data_[creg.name()] = BitArray(); + data_[creg.name()].set_bits(creg.size()); } - - /// @brief Create a new SamplerPubResult as a copy of src. - /// @param src copy source. - SamplerPubResult(const SamplerPubResult& src) - { - data_ = src.data_; - pub_ = src.pub_; + } + + /// @brief Create a new SamplerPubResult as a copy of src. + /// @param src copy source. + SamplerPubResult(const SamplerPubResult &src) { + data_ = src.data_; + pub_ = src.pub_; + } + + /// @brief Result data for the pub. + /// @return the bitarray for the first creg in the pub + BitArray &data(void) { return data_[pub_.circuit().cregs()[0].name()]; } + + /// @brief Result data for the pub. + /// @param name the name of the creg + /// @return the bitarray for the creg name in the pub + BitArray &data(const std::string &name) { return data_[name]; } + + /// @brief Result data for the pub. + /// @param creg the creg to be returned + /// @return the bitarray for the creg in the pub + BitArray &data(const circuit::ClassicalRegister &creg) { + return data_[creg.name()]; + } + + /// @brief get pub for this result + /// @return pub + const SamplerPub &pub(void) const { return pub_; } + + /// @brief set pub for this result + /// @param pub to be set + void set_pub(const SamplerPub &pub) { + pub_ = pub; + for (auto creg : pub_.circuit().cregs()) { + data_[creg.name()] = BitArray(); + data_[creg.name()].set_bits(creg.size()); } - - /// @brief Result data for the pub. - /// @return the bitarray for the first creg in the pub - BitArray& data(void) - { - return data_[pub_.circuit().cregs()[0].name()]; + } + + /// @brief Set pub result from json + bool from_json(nlohmann::ordered_json &input) { + if (!input.contains("data")) { + std::cerr << " SamplerPubResult Error : JSON result does not contain " + "data section " + << std::endl; + return false; } - /// @brief Result data for the pub. - /// @param name the name of the creg - /// @return the bitarray for the creg name in the pub - BitArray& data(const std::string& name) - { - return data_[name]; - } + auto data = input["data"]; - /// @brief Result data for the pub. - /// @param creg the creg to be returned - /// @return the bitarray for the creg in the pub - BitArray& data(const circuit::ClassicalRegister& creg) - { - return data_[creg.name()]; + for(auto creg : pub_.circuit().cregs()) { + if(!data.contains(creg.name())) { + std::cerr << " SamplerPubResult Error : JSON result does not contain " + "creg section " + << std::endl; + return false; + } } - /// @brief get pub for this result - /// @return pub - const SamplerPub& pub(void) const - { - return pub_; + for(auto creg : pub_.circuit().cregs()) { + BitArray bits; + bits.set_bits(creg.size()); + bits.from_json(data[creg.name()]); + data_[creg.name()] = bits; } - /// @brief set pub for this result - /// @param pub to be set - void set_pub(const SamplerPub& pub) - { - pub_ = pub; - for (auto creg : pub_.circuit().cregs()) { - data_[creg.name()] = BitArray(); - data_[creg.name()].set_bits(creg.size()); - } - } + return true; + } - /// @brief Set pub reuslt from json - bool from_json(nlohmann::ordered_json& input) - { - if (!input.contains("data")) { - std::cerr << " SamplerPubResult Error : JSON result does not contain data section " << std::endl; - return false; - } - if (!input["data"].contains("c")) { - std::cerr << " SamplerPubResult Error : JSON result does not contain creg section " << std::endl; - return false; - } - - auto data = input["data"]["c"]; - - uint_t total_bits = 0; - for (auto creg : pub_.circuit().cregs()) { - total_bits += creg.size(); - } - // read all bits - BitArray allbits; - allbits.set_bits(total_bits); - allbits.from_json(data); - - total_bits = 0; - for (auto creg : pub_.circuit().cregs()) { - data_[creg.name()] = allbits.get_subset(total_bits, creg.size()); - total_bits += creg.size(); - } - - return true; + /// @brief allocate bit array data + /// @param num_samples number of samples to be allocated + void allocate(uint_t num_samples) { + for (auto creg : pub_.circuit().cregs()) { + data_[creg.name()] = BitArray(); + data_[creg.name()].allocate(num_samples, creg.size()); } - - /// @brief allocate bit array data - /// @param num_samples number of samples to be allocated - void allocate(uint_t num_samples) - { - for (auto creg : pub_.circuit().cregs()) { - data_[creg.name()] = BitArray(); - data_[creg.name()].allocate(num_samples, creg.size()); - } - } - + } }; } // namespace primitives } // namespace Qiskit - #endif //__qiskitcpp_primitives_sampler_pub_result_hpp__ From 1e71610f6bc05b94feedf28939938d0169ce52c4 Mon Sep 17 00:00:00 2001 From: Ryo Wakizaka Date: Fri, 6 Mar 2026 20:13:43 +0900 Subject: [PATCH 2/2] Revert format --- .../containers/sampler_pub_result.hpp | 176 ++++++++++-------- 1 file changed, 96 insertions(+), 80 deletions(-) diff --git a/src/primitives/containers/sampler_pub_result.hpp b/src/primitives/containers/sampler_pub_result.hpp index 1fc9df8..f0faeff 100644 --- a/src/primitives/containers/sampler_pub_result.hpp +++ b/src/primitives/containers/sampler_pub_result.hpp @@ -16,6 +16,7 @@ #include + #ifndef __qiskitcpp_primitives_sampler_pub_result_hpp__ #define __qiskitcpp_primitives_sampler_pub_result_hpp__ @@ -30,101 +31,116 @@ namespace primitives { /// @brief Result of Sampler Pub(Primitive Unified Bloc). class SamplerPubResult { protected: - std::unordered_map - data_; // in pair of creg name and bitstrings - SamplerPub pub_; // + std::unordered_map data_; // in pair of creg name and bitstrings + SamplerPub pub_; // public: - /// @brief Create a new SamplerPubResult - SamplerPubResult() {} - - /// @brief Create a new SamplerPubResult - /// @param pub a pub for this result - SamplerPubResult(SamplerPub &pub) { - pub_ = pub; - for (auto creg : pub_.circuit().cregs()) { - data_[creg.name()] = BitArray(); - data_[creg.name()].set_bits(creg.size()); + /// @brief Create a new SamplerPubResult + SamplerPubResult() {} + + /// @brief Create a new SamplerPubResult + /// @param pub a pub for this result + SamplerPubResult(SamplerPub& pub) + { + pub_ = pub; + for (auto creg : pub_.circuit().cregs()) { + data_[creg.name()] = BitArray(); + data_[creg.name()].set_bits(creg.size()); + } } - } - - /// @brief Create a new SamplerPubResult as a copy of src. - /// @param src copy source. - SamplerPubResult(const SamplerPubResult &src) { - data_ = src.data_; - pub_ = src.pub_; - } - - /// @brief Result data for the pub. - /// @return the bitarray for the first creg in the pub - BitArray &data(void) { return data_[pub_.circuit().cregs()[0].name()]; } - - /// @brief Result data for the pub. - /// @param name the name of the creg - /// @return the bitarray for the creg name in the pub - BitArray &data(const std::string &name) { return data_[name]; } - - /// @brief Result data for the pub. - /// @param creg the creg to be returned - /// @return the bitarray for the creg in the pub - BitArray &data(const circuit::ClassicalRegister &creg) { - return data_[creg.name()]; - } - - /// @brief get pub for this result - /// @return pub - const SamplerPub &pub(void) const { return pub_; } - - /// @brief set pub for this result - /// @param pub to be set - void set_pub(const SamplerPub &pub) { - pub_ = pub; - for (auto creg : pub_.circuit().cregs()) { - data_[creg.name()] = BitArray(); - data_[creg.name()].set_bits(creg.size()); + + /// @brief Create a new SamplerPubResult as a copy of src. + /// @param src copy source. + SamplerPubResult(const SamplerPubResult& src) + { + data_ = src.data_; + pub_ = src.pub_; } - } - - /// @brief Set pub result from json - bool from_json(nlohmann::ordered_json &input) { - if (!input.contains("data")) { - std::cerr << " SamplerPubResult Error : JSON result does not contain " - "data section " - << std::endl; - return false; + + /// @brief Result data for the pub. + /// @return the bitarray for the first creg in the pub + BitArray& data(void) + { + return data_[pub_.circuit().cregs()[0].name()]; } - auto data = input["data"]; + /// @brief Result data for the pub. + /// @param name the name of the creg + /// @return the bitarray for the creg name in the pub + BitArray& data(const std::string& name) + { + return data_[name]; + } - for(auto creg : pub_.circuit().cregs()) { - if(!data.contains(creg.name())) { - std::cerr << " SamplerPubResult Error : JSON result does not contain " - "creg section " - << std::endl; - return false; - } + /// @brief Result data for the pub. + /// @param creg the creg to be returned + /// @return the bitarray for the creg in the pub + BitArray& data(const circuit::ClassicalRegister& creg) + { + return data_[creg.name()]; } - for(auto creg : pub_.circuit().cregs()) { - BitArray bits; - bits.set_bits(creg.size()); - bits.from_json(data[creg.name()]); - data_[creg.name()] = bits; + /// @brief get pub for this result + /// @return pub + const SamplerPub& pub(void) const + { + return pub_; } - return true; - } + /// @brief set pub for this result + /// @param pub to be set + void set_pub(const SamplerPub& pub) + { + pub_ = pub; + for (auto creg : pub_.circuit().cregs()) { + data_[creg.name()] = BitArray(); + data_[creg.name()].set_bits(creg.size()); + } + } - /// @brief allocate bit array data - /// @param num_samples number of samples to be allocated - void allocate(uint_t num_samples) { - for (auto creg : pub_.circuit().cregs()) { - data_[creg.name()] = BitArray(); - data_[creg.name()].allocate(num_samples, creg.size()); + /// @brief Set pub reuslt from json + bool from_json(nlohmann::ordered_json& input) + { + if (!input.contains("data")) { + std::cerr << " SamplerPubResult Error : JSON result does not contain data section " << std::endl; + return false; + } + + auto data = input["data"]; + + for(auto creg : pub_.circuit().cregs()) { + if(!data.contains(creg.name())) { + std::cerr << " SamplerPubResult Error : JSON result does not contain " + "creg section for " + << creg.name() + << std::endl; + return false; + } + } + + for(auto creg : pub_.circuit().cregs()) { + BitArray bits; + bits.set_bits(creg.size()); + bits.from_json(data[creg.name()]); + data_[creg.name()] = bits; + } + + return true; } - } + + /// @brief allocate bit array data + /// @param num_samples number of samples to be allocated + void allocate(uint_t num_samples) + { + for (auto creg : pub_.circuit().cregs()) { + data_[creg.name()] = BitArray(); + data_[creg.name()].allocate(num_samples, creg.size()); + } + } + }; } // namespace primitives } // namespace Qiskit + #endif //__qiskitcpp_primitives_sampler_pub_result_hpp__