| 166 | // use DDT test Complement |
| 167 | |
| 168 | void checkQComplement(int bit_len, int value) |
| 169 | { |
| 170 | auto qvm = initQuantumMachine(QMachineType::CPU); |
| 171 | |
| 172 | QVec a = qvm->qAllocMany(bit_len); |
| 173 | int ancil_capacity = int(2 * bit_len + 1 - std::floor(std::log2(bit_len))); |
| 174 | QVec ancil = qvm->qAllocMany(ancil_capacity); |
| 175 | |
| 176 | // qcla complement |
| 177 | QProg qcla_qprog = createEmptyQProg(); |
| 178 | if (0 != value) |
| 179 | { |
| 180 | qcla_qprog << bind_data(value, a); |
| 181 | } |
| 182 | |
| 183 | qcla_qprog << QComplement_V2(a, ancil); |
| 184 | |
| 185 | auto result_qcla = dynamic_cast<CPUQVM *>(qvm)->probRunDict(qcla_qprog, a); |
| 186 | auto result_qcla_anc = dynamic_cast<CPUQVM *>(qvm)->probRunDict(qcla_qprog, ancil); |
| 187 | |
| 188 | // qft complement |
| 189 | QProg qft_qprog = createEmptyQProg(); |
| 190 | if (0 != value) |
| 191 | { |
| 192 | qft_qprog << bind_data(value, a); |
| 193 | } |
| 194 | |
| 195 | qft_qprog << QComplement_V2(a, ancil[0]); |
| 196 | |
| 197 | auto result_qft = dynamic_cast<CPUQVM *>(qvm)->probRunDict(qft_qprog, a); |
| 198 | auto result_qft_anc = dynamic_cast<CPUQVM *>(qvm)->probRunDict(qft_qprog, ancil); |
| 199 | |
| 200 | // original complement |
| 201 | QProg qprog = createEmptyQProg(); |
| 202 | if (0 != value) |
| 203 | { |
| 204 | qprog << bind_data(value, a); |
| 205 | } |
| 206 | qprog << QComplement(a, ancil); |
| 207 | |
| 208 | auto result = dynamic_cast<CPUQVM *>(qvm)->probRunDict(qprog, a); |
| 209 | auto result_anc = dynamic_cast<CPUQVM *>(qvm)->probRunDict(qprog, ancil); |
| 210 | |
| 211 | // ASSERT_EQ(result.size(), result_qcla.size()); |
| 212 | |
| 213 | for (auto it : result) |
| 214 | { |
| 215 | double total = it.second + result_qcla.at(it.first) + result_qft.at(it.first); |
| 216 | if (total <= 0.01) |
| 217 | continue; |
| 218 | ASSERT_TRUE(euqal_in_tolerance(it.second, result_qcla.at(it.first))); |
| 219 | ASSERT_TRUE(euqal_in_tolerance(it.second, result_qft.at(it.first))); |
| 220 | } |
| 221 | |
| 222 | for (auto it : result_anc) |
| 223 | { |
| 224 | double total = it.second + result_qcla_anc.at(it.first) + result_qft_anc.at(it.first); |
| 225 | if (total <= 0.01) |
no test coverage detected