diff --git a/include/checker/zx/ZXChecker.hpp b/include/checker/zx/ZXChecker.hpp index 12209d4a..0b96f685 100644 --- a/include/checker/zx/ZXChecker.hpp +++ b/include/checker/zx/ZXChecker.hpp @@ -36,72 +36,72 @@ class ZXEquivalenceChecker : public EquivalenceChecker { // the following methods are adaptations of the core ZX simplification // routines that additionally check a criterion for early termination of the // simplification. - std::size_t fullReduceApproximate(); - std::size_t fullReduce(); + bool fullReduceApproximate(); + bool fullReduce(); - std::size_t gadgetSimp(); - std::size_t interiorCliffordSimp(); - std::size_t cliffordSimp(); + bool gadgetSimp(); + bool interiorCliffordSimp(); + bool cliffordSimp(); - std::size_t idSimp() { - return simplifyVertices(zx::checkIdSimp, zx::removeId); - } + bool idSimp() { return simplifyVertices(zx::checkIdSimp, zx::removeId); } - std::size_t spiderSimp() { + bool spiderSimp() { return simplifyEdges(zx::checkSpiderFusion, zx::fuseSpiders); } - std::size_t localCompSimp() { + bool localCompSimp() { return simplifyVertices(zx::checkLocalComp, zx::localComp); } - std::size_t pivotPauliSimp() { + bool pivotPauliSimp() { return simplifyEdges(zx::checkPivotPauli, zx::pivotPauli); } - std::size_t pivotSimp() { return simplifyEdges(zx::checkPivot, zx::pivot); } + bool pivotSimp() { return simplifyEdges(zx::checkPivot, zx::pivot); } - std::size_t pivotGadgetSimp() { + bool pivotGadgetSimp() { return simplifyEdges(zx::checkPivotGadget, zx::pivotGadget); } template - std::size_t simplifyVertices(CheckFun check, RuleFun rule) { - std::size_t nSimplifications = 0U; - bool newMatches = true; - - while (!isDone() && newMatches) { - newMatches = false; + bool simplifyVertices(CheckFun check, RuleFun rule) { + bool simplified = false; + while (!isDone()) { + auto moreSimplified = false; for (const auto& [v, _] : miter.getVertices()) { if (isDone() || !check(miter, v)) { continue; } rule(miter, v); - newMatches = true; - ++nSimplifications; + moreSimplified = true; + } + if (!moreSimplified) { + break; } + simplified |= true; } - return nSimplifications; + return simplified; } template - std::size_t simplifyEdges(CheckFun check, RuleFun rule) { - std::size_t nSimplifications = 0U; - bool newMatches = true; - - while (!isDone() && newMatches) { - newMatches = false; + bool simplifyEdges(CheckFun check, RuleFun rule) { + bool simplified = false; + while (!isDone()) { + auto moreSimplified = false; for (const auto& [v0, v1] : miter.getEdges()) { if (isDone() || miter.isDeleted(v0) || miter.isDeleted(v1) || !check(miter, v0, v1)) { continue; } rule(miter, v0, v1); - newMatches = true; - ++nSimplifications; + moreSimplified = true; + } + if (!moreSimplified) { + break; } + simplified |= true; } - return nSimplifications; + return simplified; } }; diff --git a/src/checker/zx/ZXChecker.cpp b/src/checker/zx/ZXChecker.cpp index b0b2f6c3..07395832 100644 --- a/src/checker/zx/ZXChecker.cpp +++ b/src/checker/zx/ZXChecker.cpp @@ -154,98 +154,85 @@ qc::Permutation invertPermutations(const qc::QuantumComputation& qc) { complete(qc.initialLayout, qc.getNqubits())); } -std::size_t ZXEquivalenceChecker::fullReduceApproximate() { - auto nSimplifications = fullReduce(); +bool ZXEquivalenceChecker::fullReduceApproximate() { + auto simplified = fullReduce(); while (!isDone()) { miter.approximateCliffords(tolerance); - const auto newSimps = fullReduce(); - if (newSimps == 0U) { + if (!fullReduce()) { break; } - nSimplifications += newSimps; + simplified = true; } - return nSimplifications; + return simplified; } -std::size_t ZXEquivalenceChecker::fullReduce() { +bool ZXEquivalenceChecker::fullReduce() { if (!isDone()) { miter.toGraphlike(); } - interiorCliffordSimp(); - - bool newMatches = true; - std::size_t nSimplifications = 0U; - while (!isDone() && newMatches) { - newMatches = false; - cliffordSimp(); - const auto nGadget = gadgetSimp(); - interiorCliffordSimp(); - const auto nPivot = pivotGadgetSimp(); - if ((nGadget + nPivot) != 0U) { - newMatches = true; - ++nSimplifications; + auto simplified = interiorCliffordSimp(); + while (!isDone()) { + auto moreSimplified = cliffordSimp(); + moreSimplified |= gadgetSimp(); + moreSimplified |= interiorCliffordSimp(); + moreSimplified |= pivotGadgetSimp(); + if (!moreSimplified) { + break; } + simplified = true; } if (!isDone()) { miter.removeDisconnectedSpiders(); } - - return nSimplifications; + return simplified; } -std::size_t ZXEquivalenceChecker::gadgetSimp() { - std::size_t nSimplifications = 0U; - bool newMatches = true; - - while (!isDone() && newMatches) { - newMatches = false; +bool ZXEquivalenceChecker::gadgetSimp() { + auto simplified = false; + while (!isDone()) { + auto moreSimplified = false; for (const auto& [v, _] : miter.getVertices()) { if (miter.isDeleted(v)) { continue; } - - if (!isDone() && checkAndFuseGadget(miter, v)) { - newMatches = true; - ++nSimplifications; + if (checkAndFuseGadget(miter, v)) { + moreSimplified = true; } } + if (!moreSimplified) { + break; + } + simplified = true; } - return nSimplifications; + return simplified; } -std::size_t ZXEquivalenceChecker::interiorCliffordSimp() { - spiderSimp(); - - bool newMatches = true; - std::size_t nSimplifications = 0U; - while (!isDone() && newMatches) { - newMatches = false; - const auto nId = idSimp(); - const auto nSpider = spiderSimp(); - const auto nPivot = pivotPauliSimp(); - const auto nLocalComp = localCompSimp(); - - if ((nId + nSpider + nPivot + nLocalComp) != 0U) { - newMatches = true; - ++nSimplifications; +bool ZXEquivalenceChecker::interiorCliffordSimp() { + auto simplified = spiderSimp(); + while (!isDone()) { + auto moreSimplified = idSimp(); + moreSimplified |= spiderSimp(); + moreSimplified |= pivotPauliSimp(); + moreSimplified |= localCompSimp(); + if (!moreSimplified) { + break; } + simplified = true; } - return nSimplifications; + return simplified; } -std::size_t ZXEquivalenceChecker::cliffordSimp() { - bool newMatches = true; - std::size_t nSimplifications = 0U; - while (!isDone() && newMatches) { - newMatches = false; - const auto nClifford = interiorCliffordSimp(); - const auto nPivot = pivotSimp(); - if ((nClifford + nPivot) != 0U) { - newMatches = true; - ++nSimplifications; +bool ZXEquivalenceChecker::cliffordSimp() { + auto simplified = false; + while (!isDone()) { + auto moreSimplified = interiorCliffordSimp(); + moreSimplified |= pivotSimp(); + if (!moreSimplified) { + break; } + simplified = true; } - return nSimplifications; + return simplified; } } // namespace ec