| 1174 | return { alpha,beta,phi }; |
| 1175 | } |
| 1176 | void Encode::sparse_isometry(const QVec &q, const std::map<std::string, double>& data) |
| 1177 | { |
| 1178 | |
| 1179 | if (data.empty()) |
| 1180 | { |
| 1181 | QCERR_AND_THROW_ERRSTR(run_fail, "Error: The input map data must not null."); |
| 1182 | } |
| 1183 | |
| 1184 | int size = (*data.begin()).first.size(); |
| 1185 | |
| 1186 | for (auto i : data) |
| 1187 | { |
| 1188 | if (i.first.size() != size) |
| 1189 | { |
| 1190 | QCERR_AND_THROW_ERRSTR(run_fail, "Error: The input map data.key must have same dimension."); |
| 1191 | } |
| 1192 | for (char c : i.first) |
| 1193 | { |
| 1194 | if (c != '0'&&c != '1') |
| 1195 | { |
| 1196 | QCERR_AND_THROW_ERRSTR(run_fail, "Error: The input map data.key must be binary string."); |
| 1197 | } |
| 1198 | } |
| 1199 | |
| 1200 | } |
| 1201 | |
| 1202 | const double max_precision = 1e-13; |
| 1203 | double tmp_sum = 0.0; |
| 1204 | |
| 1205 | for (const auto i : data) |
| 1206 | { |
| 1207 | |
| 1208 | tmp_sum += i.second*i.second; |
| 1209 | } |
| 1210 | |
| 1211 | if (std::abs(1.0 - tmp_sum) > max_precision) |
| 1212 | { |
| 1213 | if (std::abs(tmp_sum) < max_precision) |
| 1214 | { |
| 1215 | QCERR("Error: The input vector b is zero."); |
| 1216 | return; |
| 1217 | } |
| 1218 | |
| 1219 | QCERR_AND_THROW_ERRSTR(run_fail, "Error: The input vector b must satisfy the normalization condition."); |
| 1220 | } |
| 1221 | |
| 1222 | if (data.size() == 1) |
| 1223 | { |
| 1224 | basic_encode(q, (*data.begin()).first); |
| 1225 | return; |
| 1226 | } |
| 1227 | |
| 1228 | std::string key; |
| 1229 | QVec qubits; |
| 1230 | key = (*data.begin()).first; |
| 1231 | int n_qubits = key.size(); |
| 1232 | |
| 1233 | if (n_qubits > q.size()) { |