| 887 | } |
| 888 | |
| 889 | void Encode::ds_quantum_state_preparation(const QVec &q, const std::map<std::string, double>& data) |
| 890 | { |
| 891 | |
| 892 | if (data.empty()) { |
| 893 | |
| 894 | QCERR_AND_THROW_ERRSTR(run_fail, "Error: The input map data must not null."); |
| 895 | } |
| 896 | |
| 897 | int size = (*data.begin()).first.size(); |
| 898 | |
| 899 | if (size * 2 > q.size()) { |
| 900 | |
| 901 | QCERR_AND_THROW_ERRSTR(run_fail, "Error: The input qubits size error."); |
| 902 | } |
| 903 | |
| 904 | for (auto i : data) |
| 905 | { |
| 906 | if (i.first.size() != size) |
| 907 | { |
| 908 | QCERR_AND_THROW_ERRSTR(run_fail, "Error: The input map data.key must have same dimension."); |
| 909 | } |
| 910 | for (char c : i.first) |
| 911 | { |
| 912 | if (c != '0'&&c != '1') |
| 913 | { |
| 914 | QCERR_AND_THROW_ERRSTR(run_fail, "Error: The input map data.key must be binary string."); |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | } |
| 919 | const double max_precision = 1e-13; |
| 920 | double tmp_sum = 0.0; |
| 921 | |
| 922 | for (const auto i : data) |
| 923 | { |
| 924 | tmp_sum += (i.second*i.second); |
| 925 | } |
| 926 | if (std::abs(1.0 - tmp_sum) > max_precision) |
| 927 | { |
| 928 | if (std::abs(tmp_sum) < max_precision) |
| 929 | { |
| 930 | QCERR("Error: The input vector b is zero."); |
| 931 | return; |
| 932 | } |
| 933 | |
| 934 | QCERR_AND_THROW_ERRSTR(run_fail, "Error: The input vector b must satisfy the normalization condition."); |
| 935 | } |
| 936 | |
| 937 | double norm = 1.0; |
| 938 | m_qcircuit << X(q[0]); |
| 939 | int k = 0; |
| 940 | auto iter = data.begin(); |
| 941 | int numqubits = (*iter).first.size(); |
| 942 | QVec u = q[0]; |
| 943 | QVec a; |
| 944 | QVec m; |
| 945 | for (int i = 1; i < numqubits; ++i) { |
| 946 | a.push_back(q[i]); |