@@ -83,18 +83,39 @@ class BitArray {
8383 // from bitstring
8484 void from_bitstring (const std::vector<std::string>& samples);
8585
86+ // / @brief Return subsets of the BitArray
87+ // / @param start_bit start bit index of subset
88+ // / @param num_bits number of bits in a subset
89+ // / @return A new BitArray
90+ BitArray get_subset (const uint_t start_bit, const uint_t num_bits);
91+
92+ // / @brief Return a list of bitstrings.
93+ // / @return A list of bitstrings.
94+ std::vector<std::string> get_bitstrings (void );
95+
8696 // / @brief Return a list of bitstrings.
97+ // / @param index a list of index to be stored in the output list
8798 // / @return A list of bitstrings.
88- std::vector<std::string> get_bitstring ( void );
99+ std::vector<std::string> get_bitstrings ( reg_t & index );
89100
90101 // / @brief Return a list of hex string
91102 // / @return A list of hex string.
92- std::vector<std::string> get_hexstring (void );
103+ std::vector<std::string> get_hexstrings (void );
104+
105+ // / @brief Return a list of hex string.
106+ // / @param index a list of index to be stored in the output list
107+ // / @return A list of hex string.
108+ std::vector<std::string> get_hexstrings (reg_t & index);
93109
94110 // / @brief Return a counts dictionary with bitstring keys.
95111 // / @return A counts dictionary with bitstring keys.
96112 std::unordered_map<std::string, uint_t > get_counts (void );
97113
114+ // / @brief Return a counts dictionary with bitstring keys.
115+ // / @param index a list of index to be stored in the output map
116+ // / @return A counts dictionary with bitstring keys.
117+ std::unordered_map<std::string, uint_t > get_counts (reg_t & index);
118+
98119 // / @brief Set pub samples from json
99120 // / @param input JSON input
100121 void from_json (nlohmann::ordered_json& input);
@@ -103,6 +124,11 @@ class BitArray {
103124 // / @param index an index to be set
104125 // / @param input a sample in a hex string format
105126 void set_hexstring (uint_t index, std::string& input);
127+
128+ // / @brief Return a list of bit counts
129+ // / @return A list of interger counts of bits appears in each shot
130+ reg_t bitcount (void );
131+
106132};
107133
108134void BitArray::from_samples (const reg_t & samples, uint_t num_bits)
@@ -132,7 +158,7 @@ void BitArray::from_bitstring(const std::vector<std::string>& samples)
132158 num_bits_ = array_[0 ].size ();
133159}
134160
135- std::vector<std::string> BitArray::get_bitstring (void )
161+ std::vector<std::string> BitArray::get_bitstrings (void )
136162{
137163 std::vector<std::string> ret (array_.size ());
138164 for (uint_t i = 0 ; i < array_.size (); i++) {
@@ -141,7 +167,20 @@ std::vector<std::string> BitArray::get_bitstring(void)
141167 return ret;
142168}
143169
144- std::vector<std::string> BitArray::get_hexstring (void )
170+ std::vector<std::string> BitArray::get_bitstrings (reg_t & index)
171+ {
172+ uint_t size = std::min (array_.size (), index.size ());
173+ std::vector<std::string> ret (size);
174+
175+ for (uint_t i = 0 ; i < size; i++) {
176+ uint_t pos = index[i];
177+ if (pos < array_.size ())
178+ ret[i] = array_[pos].to_string ();
179+ }
180+ return ret;
181+ }
182+
183+ std::vector<std::string> BitArray::get_hexstrings (void )
145184{
146185 std::vector<std::string> ret (array_.size ());
147186 for (uint_t i = 0 ; i < array_.size (); i++) {
@@ -150,6 +189,20 @@ std::vector<std::string> BitArray::get_hexstring(void)
150189 return ret;
151190}
152191
192+ std::vector<std::string> BitArray::get_hexstrings (reg_t & index)
193+ {
194+ uint_t size = std::min (array_.size (), index.size ());
195+ std::vector<std::string> ret (size);
196+
197+ for (uint_t i = 0 ; i < size; i++) {
198+ uint_t pos = index[i];
199+ if (pos < array_.size ())
200+ ret[i] = array_[pos].to_hex_string ();
201+ }
202+ return ret;
203+ }
204+
205+
153206std::unordered_map<std::string, uint_t > BitArray::get_counts (void )
154207{
155208 std::unordered_map<std::string, uint_t > ret;
@@ -159,10 +212,23 @@ std::unordered_map<std::string, uint_t> BitArray::get_counts(void)
159212 return ret;
160213}
161214
215+ std::unordered_map<std::string, uint_t > BitArray::get_counts (reg_t & index)
216+ {
217+ uint_t size = std::min (array_.size (), index.size ());
218+ std::unordered_map<std::string, uint_t > ret;
219+
220+ for (uint_t i = 0 ; i < size; i++) {
221+ uint_t pos = index[i];
222+ if (pos < array_.size ())
223+ ret[array_[pos].to_string ()]++;
224+ }
225+ return ret;
226+ }
227+
162228void BitArray::from_json (nlohmann::ordered_json& input)
163229{
164- auto samples = input[" data " ][ " c " ][ " samples" ];
165- auto num_bits = input[" data " ][ " c " ][ " num_bits" ];
230+ auto samples = input[" samples" ];
231+ auto num_bits = input[" num_bits" ];
166232 auto num_shots = samples.size ();
167233 if (num_bits_ == 0 )
168234 num_bits_ = num_bits;
@@ -178,6 +244,27 @@ void BitArray::set_hexstring(uint_t index, std::string& input)
178244 array_[index].from_hex_string (input);
179245}
180246
247+ BitArray BitArray::get_subset (const uint_t start_bit, const uint_t num_bits)
248+ {
249+ BitArray ret;
250+ ret.allocate (array_.size (), num_bits);
251+
252+ for (uint_t i = 0 ; i < array_.size (); i++) {
253+ ret.array_ [i] = array_[i].get_subset (start_bit, num_bits);
254+ }
255+
256+ return ret;
257+ }
258+
259+ reg_t BitArray::bitcount (void )
260+ {
261+ reg_t count (array_.size ());
262+ for (uint_t i = 0 ; i < array_.size (); i++) {
263+ count[i] = array_[i].popcount ();
264+ }
265+ return count;
266+ }
267+
181268
182269
183270} // namespace primitives
0 commit comments