MCPcopy Create free account
hub / github.com/QuEST-Kit/QuEST / isPauliStrSumCoeffWithinQcompRange

Function isPauliStrSumCoeffWithinQcompRange

quest/src/core/parser.cpp:355–393  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

353
354
355bool isPauliStrSumCoeffWithinQcompRange(string line) {
356
357 // it is gauranteed that line is interpretable and contains a regex-matching
358 // coefficient, but we must additionally verify it is within range of qreal.
359 // So we duck type each of the 1 or 2 matches with the real regex (i.e. one or
360 // both of the real and imaginary components of a complex coeff).
361
362 // process only the coeff, since pauli codes may resemble coeffs (e.g. 1)
363 string coeff, _;
364 separateStringIntoCoeffAndPaulis(line, coeff, _); // discard pauli substr
365
366 // beautiful iterator boilerplate
367 sregex_iterator it(coeff.begin(), coeff.end(), regexes::real);
368 sregex_iterator end;
369
370 // valid coeffs contain 1 or 2 reals, never 0
371 if (it == end)
372 return false;
373
374 // enumerate all matches of 'real' regex in line
375 for (; it != end; it++) {
376
377 // remove whitespace (stold cannot handle space between sign and number)
378 string match = it->str(0);
379 removeWhiteSpace(match);
380
381 // return false if number cannot become a qreal
382 try {
383 precisionAgnosticStringToFloat(match);
384 } catch (const out_of_range&) {
385 return false;
386 } catch (const invalid_argument&) { // should be impossible (indicates bad regex)
387 return false;
388 }
389 }
390
391 // report that each double in coeff substr was parsable
392 return true;
393}
394
395
396void assertStringIsValidPauliStrSum(string lines, const char* caller) {

Callers 1

Calls 3

removeWhiteSpaceFunction · 0.85

Tested by

no test coverage detected