From 7ef95f0332006c1f42299a923a3e04b4185ce7e5 Mon Sep 17 00:00:00 2001 From: WrathfulSpatula Date: Wed, 26 Jun 2024 11:22:42 -0400 Subject: [PATCH] Fix Windows/Mac builds --- include/qbdt.hpp | 4 ++++ src/qbdt/node.cpp | 30 +++++++++++++++++++++++++++++- src/qbdt/node_interface.cpp | 4 ++++ src/qbdt/tree.cpp | 12 ++++++++++++ 4 files changed, 49 insertions(+), 1 deletion(-) diff --git a/include/qbdt.hpp b/include/qbdt.hpp index 3cfb10c11..2c6dfef09 100644 --- a/include/qbdt.hpp +++ b/include/qbdt.hpp @@ -105,7 +105,11 @@ class QBdt : public QParity, public QInterface { scale *= leaf->scale; } +#if defined(__APPLE__) || (defined(_WIN32) && !defined(__CYGWIN__)) + getLambda((bitCapIntOcl)i, scale); +#else getLambda((bitCapIntOcl)i, complex((real1)(real(scale).to_double()), (real1)(imag(scale).to_double()))); +#endif }); } template void SetTraversal(Fn setLambda) diff --git a/src/qbdt/node.cpp b/src/qbdt/node.cpp index fb54bc1ec..d460fb4e7 100644 --- a/src/qbdt/node.cpp +++ b/src/qbdt/node.cpp @@ -128,10 +128,18 @@ void QBdtNode::Prune(bitLenInt depth, bitLenInt parDepth) if (IS_NODE_0(b0->scale)) { b0->SetZero(); +#if defined(__APPLE__) || (defined(_WIN32) && !defined(__CYGWIN__)) + b1->scale /= abs(b1->scale); +#else b1->scale /= (complex_x)sqrt(norm(b1->scale).to_double()); +#endif } else if (IS_NODE_0(b1->scale)) { b1->SetZero(); +#if defined(__APPLE__) || (defined(_WIN32) && !defined(__CYGWIN__)) + b0->scale /= abs(b0->scale); +#else b0->scale /= (complex_x)sqrt(norm(b0->scale).to_double()); +#endif } const complex_x phaseFac = (complex_x)std::polar(ONE_R1, @@ -311,7 +319,11 @@ void QBdtNode::Normalize(bitLenInt depth) std::lock_guard lock(b0->mtx); #endif +#if defined(__APPLE__) || (defined(_WIN32) && !defined(__CYGWIN__)) + const real1_x nrm = (real1_x)sqrt(2 * norm(b0->scale)); +#else const real1_x nrm = (real1_x)sqrt(2 * (norm(b0->scale).to_double())); +#endif b0->Normalize(depth); b0->scale *= ONE_R1_X / nrm; @@ -322,7 +334,11 @@ void QBdtNode::Normalize(bitLenInt depth) std::lock_guard lock1(b1->mtx, std::adopt_lock); #endif +#if defined(__APPLE__) || (defined(_WIN32) && !defined(__CYGWIN__)) + const real1_x nrm = (real1_x)sqrt(norm(b0->scale) + norm(b1->scale)); +#else const real1_x nrm = (real1_x)sqrt((norm(b0->scale) + norm(b1->scale)).to_double()); +#endif b0->Normalize(depth); b1->Normalize(depth); @@ -358,7 +374,11 @@ void QBdtNode::PopStateVector(bitLenInt depth, bitLenInt parDepth) #endif b0->PopStateVector(depth); - const real1_x nrm = (real1_x)(2 * (norm(b0->scale).to_double())); +#if defined(__APPLE__) || (defined(_WIN32) && !defined(__CYGWIN__)) + const real1_x nrm = (real1_x)sqrt(2 * norm(b0->scale)); +#else + const real1_x nrm = (real1_x)sqrt(2 * (norm(b0->scale).to_double())); +#endif if (nrm <= _qrack_qbdt_sep_thresh) { scale = ZERO_CMPLX; @@ -368,7 +388,11 @@ void QBdtNode::PopStateVector(bitLenInt depth, bitLenInt parDepth) return; } +#if defined(__APPLE__) || (defined(_WIN32) && !defined(__CYGWIN__)) + scale = std::polar(sqrt(nrm), std::arg(b0->scale)); +#else scale = (complex_x)std::polar((real1)sqrt(nrm.to_double()), std::arg(complexFixedToFloating(b0->scale))); +#endif b0->scale /= scale; return; @@ -410,7 +434,11 @@ void QBdtNode::PopStateVector(bitLenInt depth, bitLenInt parDepth) return; } +#if defined(__APPLE__) || (defined(_WIN32) && !defined(__CYGWIN__)) + scale = std::polar(sqrt(nrm0 + nrm1), std::arg(b0->scale)); +#else scale = (complex_x)std::polar((real1)sqrt((nrm0 + nrm1).to_double()), std::arg(complexFixedToFloating(b0->scale))); +#endif b0->scale /= scale; b1->scale /= scale; } diff --git a/src/qbdt/node_interface.cpp b/src/qbdt/node_interface.cpp index 267020c97..4334e4790 100644 --- a/src/qbdt/node_interface.cpp +++ b/src/qbdt/node_interface.cpp @@ -176,7 +176,11 @@ QBdtNodeInterfacePtr QBdtNodeInterface::RemoveSeparableAtDepth( } QBdtNodeInterfacePtr toRet = ShallowClone(); +#if defined(__APPLE__) || (defined(_WIN32) && !defined(__CYGWIN__)) + toRet->scale /= abs(toRet->scale); +#else toRet->scale /= (complex_x)sqrt((real1)(norm(toRet->scale).to_double())); +#endif if (!size) { branches[0U] = NULL; diff --git a/src/qbdt/tree.cpp b/src/qbdt/tree.cpp index 7f673eb0b..f372571c3 100644 --- a/src/qbdt/tree.cpp +++ b/src/qbdt/tree.cpp @@ -541,13 +541,21 @@ bool QBdt::ForceM(bitLenInt qubit, bool result, bool doForce, bool doApply) return; } b0->SetZero(); +#if defined(__APPLE__) || (defined(_WIN32) && !defined(__CYGWIN__)) + b1->scale /= abs(b1->scale); +#else b1->scale /= (complex_x)sqrt(norm(b1->scale).to_double()); +#endif } else { if (IS_NODE_0(b0->scale)) { b0->SetZero(); return; } +#if defined(__APPLE__) || (defined(_WIN32) && !defined(__CYGWIN__)) + b0->scale /= abs(b0->scale); +#else b0->scale /= (complex_x)sqrt(norm(b0->scale).to_double()); +#endif b1->SetZero(); } }); @@ -571,7 +579,11 @@ bitCapInt QBdt::MAllOptionalCollapse(bool isCollapsing) } for (bitLenInt i = 0U; i < qubitCount; ++i) { +#if defined(__APPLE__) || (defined(_WIN32) && !defined(__CYGWIN__)) + real1_f oneChance = clampProb((real1_f)norm(leaf->branches[1U]->scale)); +#else real1_f oneChance = clampProb((real1_f)(norm(leaf->branches[1U]->scale).to_double())); +#endif bool bitResult; if (oneChance >= ONE_R1) { bitResult = true;