| 498 | |
| 499 | |
| 500 | PauliStrSum parser_validateAndParsePauliStrSum(string lines, bool rightIsLeastSignificant, const char* caller) { |
| 501 | assertStringIsValidPauliStrSum(lines, caller); |
| 502 | |
| 503 | // allocate space for as many strings as there are lines, though we might collect fewer (we skip empty lines) |
| 504 | qindex numLines = getNumLines(lines); |
| 505 | vector<qcomp> coeffs; coeffs.reserve(numLines); |
| 506 | vector<PauliStr> strings; strings.reserve(numLines); |
| 507 | |
| 508 | // parse each line in-turn |
| 509 | stringstream stream(lines); |
| 510 | for (string line; getline(stream, line); ) { |
| 511 | |
| 512 | // skip empty lines |
| 513 | if (isOnlyWhiteSpace(line)) |
| 514 | continue; |
| 515 | |
| 516 | qcomp coeff; |
| 517 | PauliStr string; |
| 518 | parseWeightedPaulis(line, coeff, string, rightIsLeastSignificant); // validates |
| 519 | |
| 520 | coeffs.push_back(coeff); |
| 521 | strings.push_back(string); |
| 522 | } |
| 523 | |
| 524 | // invoke other API function, which will run additional validation (e.g. checking |
| 525 | // memory allocations succeeded) and may fail, reporting that 'createPauliStrSum()' |
| 526 | // failed, rather than caller. That's a minor, acceptable evil. Furthermore, this |
| 527 | // call creates new memory alongside our existing vectors, meaning total memory |
| 528 | // is temporarily double than that which is strictly necessary; also acceptable. |
| 529 | PauliStrSum out = createPauliStrSum(strings, coeffs); |
| 530 | return out; |
| 531 | } |
| 532 | |
| 533 | |
| 534 |
no test coverage detected