Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 fix ZX-checker inadvertedly terminating early #382

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 31 additions & 31 deletions include/checker/zx/ZXChecker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <class CheckFun, class RuleFun>
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 <class CheckFun, class RuleFun>
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;
}
};

Expand Down
107 changes: 47 additions & 60 deletions src/checker/zx/ZXChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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