| 106 | } |
| 107 | |
| 108 | std::vector<double > QSVM::solve(std::vector<std::vector<double > > x, std::vector<double >y) |
| 109 | { |
| 110 | if (x.empty() || x[0].empty()) |
| 111 | { |
| 112 | QCERR("x param error!"); |
| 113 | throw run_fail("x param error!"); |
| 114 | } |
| 115 | |
| 116 | std::vector<double> temp; |
| 117 | for (int i = 0; i < x.size(); i++) |
| 118 | { |
| 119 | double row_squ_sum = 0; |
| 120 | for (int j = 0; j < x[i].size(); j++) |
| 121 | { |
| 122 | row_squ_sum += x[i][j] * x[i][j]; |
| 123 | } |
| 124 | if (row_squ_sum < 1e-9) |
| 125 | row_squ_sum = 0.0000001; |
| 126 | temp.push_back(row_squ_sum); |
| 127 | } |
| 128 | |
| 129 | std::vector<std::vector<double > > norm_x; |
| 130 | for (int i = 0; i < x.size(); i++) |
| 131 | { |
| 132 | std::vector<double> norm_row_x; |
| 133 | for (int j = 0; j < x[i].size(); j++) |
| 134 | { |
| 135 | double norm_val = x[i][j] / sqrt(temp[i]); |
| 136 | norm_row_x.push_back(norm_val); |
| 137 | } |
| 138 | norm_x.push_back(norm_row_x); |
| 139 | } |
| 140 | |
| 141 | //get the F matrix |
| 142 | std::vector<std::vector<double > > F_matrix; |
| 143 | std::vector<double> tmp(norm_x.size() + 1, 1); |
| 144 | F_matrix.push_back(tmp); |
| 145 | for (auto _a : norm_x) |
| 146 | { |
| 147 | temp.clear(); |
| 148 | temp.push_back(1); |
| 149 | for (auto _b : norm_x) |
| 150 | { |
| 151 | double p0 = construct_qcircuit(m_qv, m_cv, _a, _b); |
| 152 | if (p0 < 0.5) |
| 153 | { |
| 154 | p0 = 0.5 + 0.00000000001; |
| 155 | } |
| 156 | p0 = round(sqrt(p0 * 2 - 1) * 100000) / 100000; |
| 157 | temp.push_back(p0); |
| 158 | } |
| 159 | F_matrix.push_back(temp); |
| 160 | } |
| 161 | F_matrix[0][0] = 0; |
| 162 | |
| 163 | |
| 164 | if (F_matrix.size() != F_matrix[0].size()) |
| 165 | { |