diff --git a/quantum/gate/compilers/scaffold/ScaffoldCompiler.cpp b/quantum/gate/compilers/scaffold/ScaffoldCompiler.cpp index 5fb06a80fd2caf5abfcd09c15e123d3d6f9e3065..60eb374313326b169f648f9edd742daffebba457 100644 --- a/quantum/gate/compilers/scaffold/ScaffoldCompiler.cpp +++ b/quantum/gate/compilers/scaffold/ScaffoldCompiler.cpp @@ -49,7 +49,8 @@ void ScaffoldCompiler::modifySource() { kernelSource.erase(kernelSource.find("__qpu__"), 7); kernelSource = std::string("module ") + kernelSource; - std::string qubitAllocationLine, cbitAllocationLine;// = " qbit qreg[3];\n"; + std::string qubitAllocationLine, cbitAllocationLine, cbitVarName; + std::map<int, int> cbitToQubit; std::regex qbitName("qbit\\s.*"); qubitAllocationLine = (*std::sregex_iterator(kernelSource.begin(), kernelSource.end(), @@ -58,38 +59,44 @@ void ScaffoldCompiler::modifySource() { boost::split(splitQbit, qubitAllocationLine, boost::is_any_of(" ")); auto qbitVarName = splitQbit[1].substr(0, splitQbit[1].find_first_of("[")); - std::regex cbitName("cbit\\s.*"); - cbitAllocationLine = (*std::sregex_iterator(kernelSource.begin(), kernelSource.end(), - cbitName)).str() + "\n"; - std::vector<std::string> splitCbit; - boost::split(splitCbit, cbitAllocationLine, boost::is_any_of(" ")); - auto cbitVarName = splitCbit[1].substr(0, splitCbit[1].find_first_of("[")); - - std::regex measurements(".*Meas.*"); - std::map<int, int> cbitToQubit; - for (auto i = std::sregex_iterator(kernelSource.begin(), kernelSource.end(), - measurements); i != std::sregex_iterator(); ++i) { - auto measurement = (*i).str(); - boost::trim(measurement); - - boost::erase_all(measurement, "MeasZ"); - boost::erase_all(measurement, "("); - boost::erase_all(measurement, ")"); - boost::erase_all(measurement, cbitVarName); - boost::erase_all(measurement, qbitVarName); - // Should now have [#] = [#] - boost::erase_all(measurement, "["); - boost::erase_all(measurement, "]"); - - std::vector<std::string> splitVec; - boost::split(splitVec, measurement, boost::is_any_of("=")); - auto cbit = splitVec[0]; - auto qbit = splitVec[1]; - boost::trim(cbit); - boost::trim(qbit); - - cbitToQubit.insert(std::make_pair(std::stoi(cbit), std::stoi(qbit))); - } + std::regex cbitName("cbit\\s.*"); + auto it = std::sregex_iterator(kernelSource.begin(), kernelSource.end(), + cbitName); + if (it != std::sregex_iterator()) { + cbitAllocationLine = (*std::sregex_iterator(kernelSource.begin(), + kernelSource.end(), cbitName)).str() + "\n"; + std::vector<std::string> splitCbit; + boost::split(splitCbit, cbitAllocationLine, boost::is_any_of(" ")); + cbitVarName = splitCbit[1].substr(0, + splitCbit[1].find_first_of("[")); + + std::regex measurements(".*Meas.*"); + for (auto i = std::sregex_iterator(kernelSource.begin(), + kernelSource.end(), measurements); i != std::sregex_iterator(); + ++i) { + auto measurement = (*i).str(); + boost::trim(measurement); + + boost::erase_all(measurement, "MeasZ"); + boost::erase_all(measurement, "("); + boost::erase_all(measurement, ")"); + boost::erase_all(measurement, cbitVarName); + boost::erase_all(measurement, qbitVarName); + // Should now have [#] = [#] + boost::erase_all(measurement, "["); + boost::erase_all(measurement, "]"); + + std::vector<std::string> splitVec; + boost::split(splitVec, measurement, boost::is_any_of("=")); + auto cbit = splitVec[0]; + auto qbit = splitVec[1]; + boost::trim(cbit); + boost::trim(qbit); + + cbitToQubit.insert( + std::make_pair(std::stoi(cbit), std::stoi(qbit))); + } + } // conditional on measurements // FIXME FOR NOW WE ONLY ACCEPT format diff --git a/quantum/gate/compilers/scaffold/tests/ScaffoldCompilerTester.cpp b/quantum/gate/compilers/scaffold/tests/ScaffoldCompilerTester.cpp index 2cc2022be27b4969db0a4460dc25a11c80cb04ff..74f630fc4afa5ffd533d72734973feaf80370e82 100644 --- a/quantum/gate/compilers/scaffold/tests/ScaffoldCompilerTester.cpp +++ b/quantum/gate/compilers/scaffold/tests/ScaffoldCompilerTester.cpp @@ -86,7 +86,7 @@ BOOST_AUTO_TEST_CASE(checkCodeWithMeasurementIf) { " H(qreg[0]);\n" " creg[0] = MeasZ(qreg[0]);\n" " creg[1] = MeasZ(qreg[1]);\n" - " if(creg[0] == 1) Z(qreg[2]);\n" + " if (creg[0] == 1) Z(qreg[2]);\n" " if (creg[1] == 1) X(qreg[2]);\n" "}\n");