NVIDIA / cuda-quantum

C++ and Python support for the CUDA Quantum programming model for heterogeneous quantum-classical workflows
https://nvidia.github.io/cuda-quantum/
Other
496 stars 181 forks source link

Building on the Mac #595

Open amccaskey opened 1 year ago

amccaskey commented 1 year ago

Here's my attempt to build things natively on the mac. I used the conda environment defined in #449

name: cuda-quantum 
channels:
  - conda-forge
  - defaults
dependencies:
# build tools
  - cmake=3.26
  - ninja
  - python=3.9.*
#  - gxx_linux-64=11.3
# llvm deps
  - clangdev=16.0.6
  - llvmlite=0.40.1
  - mlir=16.0.6
  - lit=16.0.6
# python deps
  - numpy
  - ipython
  - pytest
# doc tools
  - sphinx
  - sphinx_rtd_theme
  - breathe
  - enum_tools
  - sphinx-toolbox
  - filelock
  - myst-parser
  - fastapi
  - uvicorn
mamba env create -f cudaq.yml
conda activate cuda-quantum

There were a few fixes I had to make

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 10405af..cec13c4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -130,7 +130,11 @@ SET(CMAKE_SKIP_BUILD_RPATH FALSE)
 SET(CMAKE_SKIP_INSTALL_RPATH FALSE)
 SET(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
 SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
+if (APPLE)
+SET(CMAKE_INSTALL_RPATH "@loader_path;@loader_path/lib;@loader_path/lib/plugins;@loader_path/../lib;@loader_path/../lib/plugins")
+else()
 SET(CMAKE_INSTALL_RPATH "$ORIGIN:$ORIGIN/lib:$ORIGIN/lib/plugins:$ORIGIN/../lib:$ORIGIN/../lib/plugins")
+endif()

 SET(BLA_STATIC ON)
 if(NOT DEFINED OPENSSL_USE_STATIC_LIBS)
diff --git a/lib/Frontend/nvqpp/ConvertExpr.cpp b/lib/Frontend/nvqpp/ConvertExpr.cpp
index 51a8c8d..06e0c6f 100644
--- a/lib/Frontend/nvqpp/ConvertExpr.cpp
+++ b/lib/Frontend/nvqpp/ConvertExpr.cpp
@@ -585,8 +585,8 @@ bool QuakeBridgeVisitor::VisitImplicitCastExpr(clang::ImplicitCastExpr *x) {
     return pushValue(subValue);
   }
   case clang::CastKind::CK_FloatingCast: {
-    auto dstType = x->getType();
-    auto val = x->getSubExpr();
+    [[maybe_unused]] auto dstType = x->getType();
+    [[maybe_unused]] auto val = x->getSubExpr();
     assert(val->getType()->isFloatingType() && dstType->isFloatingType());
     auto value = popValue();
     auto toType = cast<FloatType>(castToTy);
diff --git a/lib/Optimizer/Transforms/ApplyOpSpecialization.cpp b/lib/Optimizer/Transforms/ApplyOpSpecialization.cpp
index 0975d90..ff383ad 100644
--- a/lib/Optimizer/Transforms/ApplyOpSpecialization.cpp
+++ b/lib/Optimizer/Transforms/ApplyOpSpecialization.cpp
@@ -598,7 +598,7 @@ public:
           op->setAttr("is_adj", builder.getUnitAttr());
       }

-      auto *newOp = builder.clone(*op, mapper);
+      [[maybe_unused]] auto *newOp = builder.clone(*op, mapper);
       assert(newOp->getNumResults() == 0);
       op->erase();
     }
diff --git a/lib/Optimizer/Transforms/LowerUnwind.cpp b/lib/Optimizer/Transforms/LowerUnwind.cpp
index b631cd6..6f49c99 100644
--- a/lib/Optimizer/Transforms/LowerUnwind.cpp
+++ b/lib/Optimizer/Transforms/LowerUnwind.cpp
@@ -321,7 +321,7 @@ struct ScopeOpPattern : public OpRewritePattern<cudaq::cc::ScopeOp> {

   LogicalResult matchAndRewrite(cudaq::cc::ScopeOp scope,
                                 PatternRewriter &rewriter) const override {
-    auto iter = infoMap.opParentMap.find(scope.getOperation());
+    [[maybe_unused]] auto iter = infoMap.opParentMap.find(scope.getOperation());
     assert(iter != infoMap.opParentMap.end() && iter->second.asPrimitive);
     LLVM_DEBUG(llvm::dbgs() << "replacing scope @" << scope.getLoc() << '\n');
     auto loc = scope.getLoc();
@@ -711,7 +711,7 @@ struct UnwindReturnOpPattern

   LogicalResult matchAndRewrite(cudaq::cc::UnwindReturnOp retOp,
                                 PatternRewriter &rewriter) const override {
-    auto iter = infoMap.opParentMap.find(retOp.getOperation());
+    [[maybe_unused]] auto iter = infoMap.opParentMap.find(retOp.getOperation());
     assert(iter != infoMap.opParentMap.end());
     auto *blk = rewriter.getInsertionBlock();
     auto pos = rewriter.getInsertionPoint();
diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt
index cc4507c..ccc3c63 100644
--- a/python/CMakeLists.txt
+++ b/python/CMakeLists.txt
@@ -11,7 +11,11 @@ set(LIBRARY_NAME _pycudaq)
 if (NOT SKBUILD)
   SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_RPATH}:$ORIGIN/../../lib:$ORIGIN/../../lib/plugins")
 else()
+  if (APPLE)
+  SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_RPATH};@loader_path/../cuda_quantum.libs")
+  else()
   SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_RPATH}:$ORIGIN/../cuda_quantum.libs")
+  endif()
 endif()

 find_package(Python COMPONENTS Interpreter)
diff --git a/runtime/cudaq/builder/kernels.h b/runtime/cudaq/builder/kernels.h
index 9db9c22..eab14a9 100644
--- a/runtime/cudaq/builder/kernels.h
+++ b/runtime/cudaq/builder/kernels.h
@@ -74,7 +74,7 @@ void from_state(Kernel &&kernel, QuakeValue &qubits,
   std::vector<double> omega, stateAbs;
   for (auto &d : data) {
     omega.push_back(std::arg(d));
-    stateAbs.push_back(std::fabs(d));
+    stateAbs.push_back(std::abs(d));
     if (std::fabs(omega.back()) > 1e-6)
       omegaNonZero = true;
   }

With all this the "library-mode" unittests all pass. The issue with cudaq-quake

bin/cudaq-quake ../test/AST-Quake/ctor-1.cpp -v
clang version 16.0.6
Target: arm64-apple-darwin22.6.0
Thread model: posix
InstalledDir: 
clang Invocation:
 "cudaq-quake" "-cc1" "-triple" "arm64-apple-macosx13.0.0" "-Wundef-prefix=TARGET_OS_" "-Werror=undef-prefix" "-Wdeprecated-objc-isa-usage" "-Werror=deprecated-objc-isa-usage" "-fsyntax-only" "-disable-free" "-clear-ast-before-backend" "-disable-llvm-verifier" "-discard-value-names" "-main-file-name" "ctor-1.cpp" "-mrelocation-model" "pic" "-pic-level" "2" "-mframe-pointer=non-leaf" "-ffp-contract=on" "-fno-rounding-math" "-funwind-tables=1" "-fcompatibility-qualified-id-block-type-checking" "-fvisibility-inlines-hidden-static-local-var" "-target-cpu" "apple-m1" "-target-feature" "+v8.5a" "-target-feature" "+crc" "-target-feature" "+crypto" "-target-feature" "+dotprod" "-target-feature" "+fp-armv8" "-target-feature" "+fp16fml" "-target-feature" "+lse" "-target-feature" "+ras" "-target-feature" "+rcpc" "-target-feature" "+rdm" "-target-feature" "+neon" "-target-feature" "+zcm" "-target-feature" "+zcz" "-target-feature" "+fullfp16" "-target-feature" "+sm4" "-target-feature" "+sha3" "-target-feature" "+sha2" "-target-feature" "+aes" "-target-abi" "darwinpcs" "-mllvm" "-treat-scalable-fixed-error-as-warning" "-debugger-tuning=lldb" "-target-linker-version" "609" "-v" "-fcoverage-compilation-dir=/Users/amccaskey/research/cuda-quantum/build" "-resource-dir" "/Users/amccaskey/mambaforge/envs/cuda-quantum/lib/clang/16" "-I" "../test/AST-Quake" "-I" "/Users/amccaskey/research/cuda-quantum/runtime" "-internal-isystem" "/Users/amccaskey/mambaforge/envs/cuda-quantum/include/c++/v1" "-internal-isystem" "/usr/local/include" "-internal-isystem" "/Users/amccaskey/mambaforge/envs/cuda-quantum/lib/clang/16/include" "-internal-externc-isystem" "/usr/include" "-std=c++20" "-fdeprecated-macro" "-fdebug-compilation-dir=/Users/amccaskey/research/cuda-quantum/build" "-ferror-limit" "19" "-stack-protector" "1" "-fblocks" "-fencode-extended-block-signature" "-fregister-global-dtors-with-atexit" "-fgnuc-version=4.2.1" "-fno-implicit-modules" "-fcxx-exceptions" "-fexceptions" "-fmax-type-align=16" "-fcolor-diagnostics" "-D__GCC_HAVE_DWARF2_CFI_ASM=1" "-x" "c++" "ctor-1.cpp"

clang -cc1 version 16.0.6 based upon LLVM 16.0.6 default target arm64-apple-darwin22.6.0
ignoring nonexistent directory "/usr/local/include"
ignoring nonexistent directory "/usr/include"
#include "..." search starts here:
#include <...> search starts here:
 ../test/AST-Quake
 /Users/amccaskey/research/cuda-quantum/runtime
 /Users/amccaskey/mambaforge/envs/cuda-quantum/include/c++/v1
 /Users/amccaskey/mambaforge/envs/cuda-quantum/lib/clang/16/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
ctor-1.cpp:11:10: fatal error: cannot open file '../test/AST-Quake/cudaq.h': No such file or directory
#include <cudaq.h>
         ^

If I go into the cudaq-quake tool and remove that relative path include, then I get

clang -cc1 version 16.0.6 based upon LLVM 16.0.6 default target arm64-apple-darwin22.6.0
ignoring nonexistent directory "/usr/local/include"
ignoring nonexistent directory "/usr/include"
#include "..." search starts here:
#include <...> search starts here:
 /Users/amccaskey/research/cuda-quantum/runtime
 /Users/amccaskey/mambaforge/envs/cuda-quantum/include/c++/v1
 /Users/amccaskey/mambaforge/envs/cuda-quantum/lib/clang/16/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
In file included from ctor-1.cpp:11:
In file included from /Users/amccaskey/research/cuda-quantum/runtime/cudaq.h:11:
/Users/amccaskey/research/cuda-quantum/runtime/common/NoiseModel.h:11:10: fatal error: cannot open file '/Users/amccaskey/research/cuda-quantum/runtime/array': No such file or directory
#include <array>

If I include the system header path

clang -cc1 version 16.0.6 based upon LLVM 16.0.6 default target arm64-apple-darwin22.6.0
ignoring nonexistent directory "/usr/local/include"
ignoring nonexistent directory "/usr/include"
#include "..." search starts here:
#include <...> search starts here:
 /Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1
 /Users/amccaskey/research/cuda-quantum/runtime
 /Users/amccaskey/mambaforge/envs/cuda-quantum/include/c++/v1
 /Users/amccaskey/mambaforge/envs/cuda-quantum/lib/clang/16/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
ctor-1.cpp:11:10: fatal error: cannot open file '/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/cudaq.h': No such file or directory
#include <cudaq.h>
kukushechkin commented 1 year ago

I encountered missing headers issues and fixed it in this branch. It is pretty outdated, but errors look identical, so can be used as an inspiration for the fix.