| 201 | |
| 202 | |
| 203 | auto getQubitsSwappedToMaxSuffix(Qureg qureg, vector<int> qubits) { |
| 204 | |
| 205 | // this function is called by any-targ partial trace, and is used to find |
| 206 | // targets in the prefix substate and where they can be swapped into the suffix |
| 207 | // to enable subsequent embarrassingly parallel simulation. Note we seek the MAX |
| 208 | // available indices in the suffix, since this heuristically reduces the |
| 209 | // disordering of the surviving qubits after the trace, reducing the number of |
| 210 | // subsequent order-restoring SWAPs |
| 211 | |
| 212 | // nothing to do if all qubits are already in suffix |
| 213 | if (!doesGateRequireComm(qureg, qubits)) |
| 214 | return qubits; |
| 215 | |
| 216 | // prepare mask to avoid quadratic nested looping |
| 217 | qindex qubitMask = getBitMask(qubits.data(), qubits.size()); |
| 218 | int maxFreeSuffixQubit = getIndOfNextLeftmostZeroBit(qubitMask, qureg.logNumAmpsPerNode); |
| 219 | |
| 220 | // enumerate qubits backward, modifying our copy of qubits as we go |
| 221 | for (size_t i=qubits.size(); i-- != 0; ) { |
| 222 | int qubit = qubits[i]; |
| 223 | |
| 224 | // consider only qubits in the prefix substate |
| 225 | if (util_isQubitInSuffix(qubit, qureg)) |
| 226 | continue; |
| 227 | |
| 228 | // swap the prefix qubit into the largest available suffix position |
| 229 | qubits[i] = maxFreeSuffixQubit; |
| 230 | |
| 231 | // update trackers |
| 232 | qubitMask = flipTwoBits(qubitMask, qubit, maxFreeSuffixQubit); |
| 233 | maxFreeSuffixQubit = getIndOfNextLeftmostZeroBit(qubitMask, maxFreeSuffixQubit); |
| 234 | } |
| 235 | |
| 236 | // return our modified copy |
| 237 | return qubits; |
| 238 | } |
| 239 | |
| 240 | |
| 241 | auto getNonSwappedCtrlsAndStates(vector<int> oldCtrls, vector<int> oldStates, vector<int> newCtrls) { |
no test coverage detected