diff --git a/bootstrap.ps1 b/bootstrap.ps1 index 44db7e6c17a..a426accfd37 100644 --- a/bootstrap.ps1 +++ b/bootstrap.ps1 @@ -21,8 +21,13 @@ Pop-Location if (-not (Test-Path Env:/AGENT_OS)) { # If not CI build, i.e. local build (if AGENT_OS envvar is not defined) if ($Env:ENABLE_NATIVE -ne "false") { - Write-Host "Build release flavor of the native simulator" $Env:BUILD_CONFIGURATION = "Release" + Write-Host "Build release flavor of the Sparse Simulator" + Push-Location (Join-Path $PSScriptRoot "src/Simulation/Simulators/SparseSimulator") + .\build.ps1 + Pop-Location + + Write-Host "Build release flavor of the full state simulator" Push-Location (Join-Path $PSScriptRoot "src/Simulation/Native") .\build-native-simulator.ps1 Pop-Location diff --git a/build/build.ps1 b/build/build.ps1 index 10a19ee503c..0b882d4a226 100644 --- a/build/build.ps1 +++ b/build/build.ps1 @@ -17,6 +17,8 @@ if ($Env:ENABLE_QIRRUNTIME -ne "false") { } if ($Env:ENABLE_NATIVE -ne "false") { + ( & (Join-Path $PSScriptRoot .. src Simulation Simulators SparseSimulator build.ps1) ) || ( $script:all_ok = $False ) + $nativeSimulator = (Join-Path $PSScriptRoot "../src/Simulation/Native") & "$nativeSimulator/build-native-simulator.ps1" if ($LastExitCode -ne 0) { diff --git a/build/test.ps1 b/build/test.ps1 index 8beec26e232..521d974ee8d 100644 --- a/build/test.ps1 +++ b/build/test.ps1 @@ -5,6 +5,8 @@ $all_ok = $True if ($Env:ENABLE_NATIVE -ne "false") { + ( & (Join-Path $PSScriptRoot .. src Simulation Simulators SparseSimulator test.ps1) ) || ( $script:all_ok = $False ) + $nativeSimulator = (Join-Path $PSScriptRoot "../src/Simulation/Native") & "$nativeSimulator/test-native-simulator.ps1" if ($LastExitCode -ne 0) { diff --git a/src/Qir/Runtime/prerequisites.ps1 b/src/Qir/Runtime/prerequisites.ps1 index 74b9607269d..0452c936612 100644 --- a/src/Qir/Runtime/prerequisites.ps1 +++ b/src/Qir/Runtime/prerequisites.ps1 @@ -6,9 +6,10 @@ if ($Env:ENABLE_QIRRUNTIME -ne "false") { if (($IsWindows) -or ((Test-Path Env:/AGENT_OS) -and ($Env:AGENT_OS.StartsWith("Win")))) { if (!(Get-Command clang -ErrorAction SilentlyContinue) -or ` - !(Get-Command clang-format -ErrorAction SilentlyContinue)) { - choco install llvm --version=11.1.0 - Write-Host "##vso[task.setvariable variable=PATH;]$Env:Path;C:\Program Files\LLVM\bin" + !(Get-Command clang-format -ErrorAction SilentlyContinue) -or ` + (Test-Path Env:/AGENT_OS)) { + choco install llvm --version=11.1.0 --allow-downgrade + Write-Host "##vso[task.setvariable variable=PATH;]$($env:SystemDrive)\Program Files\LLVM\bin;$Env:PATH" } if (!(Get-Command ninja -ErrorAction SilentlyContinue)) { choco install ninja diff --git a/src/Qir/qir-utils.ps1 b/src/Qir/qir-utils.ps1 index 2d507d87e3e..11aac34ecfa 100644 --- a/src/Qir/qir-utils.ps1 +++ b/src/Qir/qir-utils.ps1 @@ -192,9 +192,10 @@ function Build-CMakeProject { $env:CXX = "clang++.exe" $env:RC = "clang++.exe" - if (!(Get-Command clang -ErrorAction SilentlyContinue) -and (choco find --idonly -l llvm) -contains "llvm") { + if ((!(Get-Command clang -ErrorAction SilentlyContinue) -and (choco find --idonly -l llvm) -contains "llvm") -or ` + (Test-Path Env:/AGENT_OS)) { # LLVM was installed by Chocolatey, so add the install location to the path. - $env:PATH += ";$($env:SystemDrive)\Program Files\LLVM\bin" + $env:PATH = "$($env:SystemDrive)\Program Files\LLVM\bin;$env:Path" } if (Get-Command clang-tidy -ErrorAction SilentlyContinue) { diff --git a/src/Simulation/Simulators/SparseSimulator/Native/CMakeLists.txt b/src/Simulation/Simulators/SparseSimulator/Native/CMakeLists.txt index 12ce6e42ce3..2d790552d71 100644 --- a/src/Simulation/Simulators/SparseSimulator/Native/CMakeLists.txt +++ b/src/Simulation/Simulators/SparseSimulator/Native/CMakeLists.txt @@ -9,16 +9,6 @@ set(CMAKE_MACOSX_RPATH 1) # Main build files add_library(SparseQuantumSimulator SHARED factory.cpp capi.cpp) -# Set OpenMP if it is available -find_package(OpenMP REQUIRED) -if(OpenMP_CXX_FOUND) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}") - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}") - if (OpenMP_CXX_VERSION_MAJOR GREATER_EQUAL 3) - target_compile_definitions(SparseQuantumSimulator PRIVATE DOMP_GE_V3=1) - endif() -endif() - # Windows adds a special dllexport command which must be defined if (WIN32) target_compile_options(SparseQuantumSimulator PUBLIC -fdeclspec) @@ -26,12 +16,7 @@ if (WIN32) endif() # Try to optimize with gcc if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - target_compile_options(SparseQuantumSimulator PUBLIC -O3 -ftree-vectorize -mavx2 -mfma -fopenmp) -endif() - -# Sets NDEBUG to true for all non-debug settings -if (NOT (CMAKE_BUILD_TYPE:STRING STREQUAL "Debug")) - target_compile_definitions(SparseQuantumSimulator PRIVATE NDEBUG=1) + target_compile_options(SparseQuantumSimulator PUBLIC -O3 -ftree-vectorize -mavx2 -mfma) endif() message("Compiler flags: ${CMAKE_CXX_FLAGS_RELEASE}") diff --git a/src/Simulation/Simulators/SparseSimulator/README.md b/src/Simulation/Simulators/SparseSimulator/README.md index 7e384109821..c5f1cdf3ea4 100644 --- a/src/Simulation/Simulators/SparseSimulator/README.md +++ b/src/Simulation/Simulators/SparseSimulator/README.md @@ -17,7 +17,12 @@ This is a an alternative quantum simulator, compatible with Q\# and Microsoft's - Dotnet cake v1.1.0 # Setup -To build the Sparse Simulator, call `dotnet restore` from the main `SparseSimulator` folder, then `dotnet cake`. This builds the C++ backend and the C\# interface. +Bild: +``` +cd SparseSimulator +pwsh ./build.ps1 +``` +This builds the C++ backend and the C\# interface. To use the SparseSimulator in a Q\# project, ensure that it includes ```xml diff --git a/src/Simulation/Simulators/SparseSimulator/SparseSimulatorCS/SparseSimulator.csproj b/src/Simulation/Simulators/SparseSimulator/SparseSimulatorCS/SparseSimulator.csproj index ce7ad730387..4555bc3d262 100644 --- a/src/Simulation/Simulators/SparseSimulator/SparseSimulatorCS/SparseSimulator.csproj +++ b/src/Simulation/Simulators/SparseSimulator/SparseSimulatorCS/SparseSimulator.csproj @@ -8,8 +8,8 @@ - ..\Native\build\libSparseQuantumSimulator.dylib - ..\Native\build\libSparseQuantumSimulator.so + ..\Native\build\$(Configuration)\libSparseQuantumSimulator.dylib + ..\Native\build\$(Configuration)\libSparseQuantumSimulator.so ..\Native\build\$(Configuration)\SparseQuantumSimulator.dll $(QsimDllMac) $(QsimDllLinux) diff --git a/src/Simulation/Simulators/SparseSimulator/build.cake b/src/Simulation/Simulators/SparseSimulator/build.cake deleted file mode 100644 index 9b923a0f297..00000000000 --- a/src/Simulation/Simulators/SparseSimulator/build.cake +++ /dev/null @@ -1,72 +0,0 @@ -#addin nuget:?package=Cake.CMake&version=1.2.0 - -var release_configuration = Argument("configuration", "Release"); -var debug_configuration = Argument("configuration", "Debug"); -var target = Argument("target", "Build"); - -////////////////////////////////////////////////////////////////////// -// TASKS -////////////////////////////////////////////////////////////////////// - -Task("Clean") - .WithCriteria(c => HasArgument("rebuild")) - .Does(() => -{ - CleanDirectory($"./{release_configuration}"); - CleanDirectory($"./{debug_configuration}"); -}); - -Task("CMake" -) .IsDependentOn("Clean") - .Does(() => -{ - CMake(new CMakeSettings { - OutputPath = Directory("Native/build"), - SourcePath = Directory("Native/"), - }); - - CMakeBuild(new CMakeBuildSettings { - BinaryPath = Directory("Native/build"), - Configuration = release_configuration - }); -}); - -Task("Build") - .IsDependentOn("CMake") - .Does(() => -{ - DotNetCoreBuild(".", new DotNetCoreBuildSettings - { - Configuration = release_configuration, - }); -}); - -Task("CMake_debug" -) .IsDependentOn("Clean") - .Does(() => -{ - CMake(new CMakeSettings { - OutputPath = Directory("Native/build"), - SourcePath = Directory("Native/"), - }); - - CMakeBuild(new CMakeBuildSettings { - BinaryPath = Directory("Native/build"), - Configuration = debug_configuration - }); -}); - -Task("Build_debug") - .IsDependentOn("CMake_debug") - .Does(() => -{ - DotNetCoreBuild(".", new DotNetCoreBuildSettings - { - Configuration = debug_configuration, - }); -}); - - -RunTarget(target); -RunTarget(Argument("target", "Build_debug")); - diff --git a/src/Simulation/Simulators/SparseSimulator/build.ps1 b/src/Simulation/Simulators/SparseSimulator/build.ps1 new file mode 100644 index 00000000000..d3452dad70c --- /dev/null +++ b/src/Simulation/Simulators/SparseSimulator/build.ps1 @@ -0,0 +1,70 @@ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. + +#Requires -Version 7.1 + +& (Join-Path $PSScriptRoot .. .. .. .. build set-env.ps1) +$FailureCommands = 'Write-Host "##vso[task.logissue type=error;] Failed to build SparseSimulator. See errors below or above." ; Pop-Location ; Exit 1' + +# BULD NATIVE PART + +# mkdir Native\build\(Debug|Release) +$BuildDir = (Join-Path $PSScriptRoot "Native" "build" $Env:BUILD_CONFIGURATION) +if (-not (Test-Path $BuildDir)) { + New-Item -Path $BuildDir -ItemType "directory" | Out-Null +} + +# pushd Native\build\(Debug|Release) +Push-Location $BuildDir + + $CmakeConfigCommand = "cmake -G Ninja -D CMAKE_VERBOSE_MAKEFILE:BOOL=ON -D CMAKE_BUILD_TYPE=$Env:BUILD_CONFIGURATION -S ..\.. " # Without `-G Ninja` the compiler chosen is always `cl.exe`. + + if (($IsMacOS) -or ((Test-Path Env:/AGENT_OS) -and ($Env:AGENT_OS.StartsWith("Darwin")))) + { + Write-Host "On MacOS build using the default C/C++ compiler (should be AppleClang)" + } + else { + if (($IsLinux) -or ((Test-Path Env:AGENT_OS) -and ($Env:AGENT_OS.StartsWith("Lin")))) + { + Write-Host "On Linux build using Clang" + $CC = "clang-11" + $CXX = "clang++-11" + #$clangTidy = "-DCMAKE_CXX_CLANG_TIDY=clang-tidy-11" + } + elseif (($IsWindows) -or ((Test-Path Env:AGENT_OS) -and ($Env:AGENT_OS.StartsWith("Win")))) + { + Write-Host "On Windows build using Clang" + $CC = "clang.exe" + $CXX = "clang++.exe" + + if (!(Get-Command clang -ErrorAction SilentlyContinue) -and (choco find --idonly -l llvm) -contains "llvm") { + # LLVM was installed by Chocolatey, so add the install location to the path. + $env:PATH += ";$($env:SystemDrive)\Program Files\LLVM\bin" + } + + #if (Get-Command clang-tidy -ErrorAction SilentlyContinue) { + # # Only run clang-tidy if it's installed. This is because the package used by chocolatey on + # # the build pipeline doesn't include clang-tidy, so we allow skipping that there and let + # # the Linux build catch tidy issues. + # $clangTidy = "-DCMAKE_CXX_CLANG_TIDY=clang-tidy" + #} + } + + $CmakeConfigCommand += " -D CMAKE_C_COMPILER=$CC -D CMAKE_CXX_COMPILER=$CXX " + } + + # Generate the build scripts: + ( Invoke-Expression $CmakeConfigCommand ) || ( Invoke-Expression $FailureCommands ) + + # Invoke the build scripts: + ( cmake --build . ) || ( Invoke-Expression $FailureCommands ) + +# popd +Pop-Location + + +# BUILD C# PART AND TESTS + +Push-Location $PSScriptRoot + ( dotnet build . --configuration $Env:BUILD_CONFIGURATION ) || ( Invoke-Expression $FailureCommands ) +Pop-Location diff --git a/src/Simulation/Simulators/SparseSimulator/test.ps1 b/src/Simulation/Simulators/SparseSimulator/test.ps1 new file mode 100644 index 00000000000..45193d4461c --- /dev/null +++ b/src/Simulation/Simulators/SparseSimulator/test.ps1 @@ -0,0 +1,11 @@ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. + +& (Join-Path $PSScriptRoot .. .. .. .. build set-env.ps1) + +$FailureCommands = 'Write-Host "##vso[task.logissue type=error;] Failed to test SparseSimulator. See errors below or above." ; Pop-Location ; Exit 1' + +Push-Location $PSScriptRoot + ( dotnet test . --configuration $Env:BUILD_CONFIGURATION ) || ` + ( Invoke-Expression $FailureCommands ) +Pop-Location