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

Set precision cmake #182

Merged
merged 44 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
a60c483
split include/src [WIP]
Oct 30, 2024
7aab74e
restore --gpus all
KowerKoint Nov 1, 2024
218f0bb
state_vector compile
KowerKoint Nov 1, 2024
86fad40
add installation
KowerKoint Nov 1, 2024
f3b56d8
[WIP] transfering
KowerKoint Nov 8, 2024
71f6d52
transfer most of functions
Nov 13, 2024
271c908
reflect commit 4d65f3d
Nov 13, 2024
d53e3dc
enable pip install
KowerKoint Nov 15, 2024
deb9c49
Github Actions other-project build test
KowerKoint Nov 15, 2024
ce75305
specify CMAKE_C**_COMPILER for example_project
KowerKoint Nov 15, 2024
965e9a3
fix typo
KowerKoint Nov 15, 2024
c9f8ba0
Python typename on double
KowerKoint Nov 15, 2024
752e069
remove float name from type
KowerKoint Nov 15, 2024
a9399ce
nvcc is not in sudo PATH
KowerKoint Nov 15, 2024
8938d40
setup python
KowerKoint Nov 15, 2024
2ec5e6e
eps_f = 1e-6 -> 1e-5
KowerKoint Nov 15, 2024
342ece5
remove mypy check from test
KowerKoint Nov 15, 2024
82df9d9
transfer Circuit
KowerKoint Nov 15, 2024
5fa9b86
float declare Circuit
KowerKoint Nov 15, 2024
7d81c58
modify example_project
KowerKoint Nov 15, 2024
2ec154c
eps_f = 1e-5 -> 1e-4
KowerKoint Nov 15, 2024
dd92103
find_package(CUDATookit)
KowerKoint Nov 15, 2024
a89d68b
find_package(CUDATookit)
KowerKoint Nov 15, 2024
1641b90
CMAKE_INSTALL_PREFIX, update README
KowerKoint Nov 15, 2024
eecb99d
merge_gate for new cmake
Nov 20, 2024
239438d
[WIP] tempretize python bindings
Nov 20, 2024
9aad61b
python precision
KowerKoint Nov 22, 2024
ae1c48b
fix CI python script
KowerKoint Nov 22, 2024
b049a04
cuda path for sudo
KowerKoint Nov 22, 2024
3143078
cuda 12.2.0 -> 12.6.2
KowerKoint Nov 22, 2024
b28bde7
READMEの要件を追加
KowerKoint Nov 22, 2024
114fcb6
12.6.2->12.6.1
KowerKoint Nov 22, 2024
ec83f31
upgrade cuda-toolkit job
KowerKoint Nov 22, 2024
2eabac4
Merge pull request #183 from qulacs/149-merge-2
KowerKoint Nov 22, 2024
214c972
Merge pull request #185 from qulacs/upgrade-cuda-version
KowerKoint Nov 27, 2024
81b37fe
sudo env
Nov 27, 2024
87f7453
remove extra quate
Nov 27, 2024
96ecce7
add description about precision on README
Dec 6, 2024
e23eb27
fix CNot, Toffoli, CCNot alias
Dec 6, 2024
ccd574e
revert and fix CX
Dec 6, 2024
b348a0d
std::float64_t -> double
Dec 6, 2024
2f4ea1e
Merge branch 'set-precision-cmake' into python-precision
KowerKoint Dec 6, 2024
3ea7568
Merge pull request #184 from qulacs/python-precision
KowerKoint Dec 13, 2024
c2a2461
Merge branch 'set-precision' into set-precision-cmake
Dec 13, 2024
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
2 changes: 1 addition & 1 deletion .devcontainer/gpu/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM nvidia/cuda:12.2.0-devel-ubuntu22.04
FROM nvidia/cuda:12.6.1-devel-ubuntu22.04

ENV DEBIAN_FRONTEND=noninteractive

Expand Down
6 changes: 3 additions & 3 deletions .devcontainer/gpu/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
"python.terminal.activateEnvironment": false,
// CAVEAT: you need to restart after building scaluq to take effect.
"C_Cpp.default.includePath": [
"scaluq",
"include",
"/usr/local/include/kokkos",
"build/_deps/eigen_fetch-src",
"build/_deps/googletest_fetch-src/googletest/include",
"build/_deps/eigen-src",
"build/_deps/googletest-src/googletest/include",
"~/.local/lib/python3.10/site-packages/nanobind/include",
"/usr/include/python3.10"
],
Expand Down
163 changes: 163 additions & 0 deletions .github/workflows/install.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
name: Install to System

on:
push:
paths-ignore:
- ".devcontainer/**"
- ".vscode/**"
- "doc/**"
- "*.md"
pull_request:
paths-ignore:
- ".devcontainer/**"
- ".vscode/**"
- "doc/**"
- "*.md"

jobs:
library:
name: Install Library
strategy:
matrix:
os: ["linux", "macos"]
architecture: ["x86_64", "arm64"]
device: ["cpu", "cuda"]
exclude:
- os: "macos"
device: "cuda"
# currently ARM runner is not supported
- os: "linux"
architecture: "arm64"
include:
- os: "linux"
architecture: "x86_64"
runs-on: "ubuntu-22.04"
- os: "macos"
architecture: "x86_64"
runs-on: "macos-13"
- os: "macos"
architecture: "arm64"
runs-on: "macos-14"
runs-on: ${{ matrix.runs-on }}
env:
CMAKE_C_COMPILER: ${{ matrix.runs-on == 'macos-13' && '/usr/local/opt/ccache/libexec/gcc-14'|| matrix.runs-on == 'macos-14' && '/opt/homebrew/opt/ccache/libexec/gcc-14' || '/usr/lib/ccache/gcc' }}
CMAKE_CXX_COMPILER: ${{ matrix.runs-on == 'macos-13' && '/usr/local/opt/ccache/libexec/g++-14'|| matrix.runs-on == 'macos-14' && '/opt/homebrew/opt/ccache/libexec/g++-14' || '/usr/lib/ccache/g++' }}
SCALUQ_USE_CUDA: ${{ matrix.device == 'cuda' && 'ON' || 'OFF' }}
SCALUQ_CUDA_ARCH: "PASCAL61"
steps:
- uses: actions/checkout@v4

- name: Install Ninja
if: ${{ matrix.os == 'linux' }}
run: sudo apt update && sudo apt install ninja-build

- name: Install Ninja
if: ${{ matrix.os == 'macos' }}
run: brew install ninja

- name: Setup ccache
uses: hendrikmuhs/[email protected]
with:
key: "${{ github.job }}-${{ matrix.os }}-${{ matrix.architecture }}-${{ matrix.device }}"
verbose: 2

- name: Install CUDA toolkit
if: ${{ matrix.device == 'cuda' }}
uses: Jimver/[email protected]
with:
cuda: "12.6.1"
method: "network"

- name: Show installed Compiler version
run: |
[ $SCALUQ_USE_CUDA = 'ON' ] && nvcc --version
ccache --version
$CMAKE_C_COMPILER --version
$CMAKE_CXX_COMPILER --version
cmake --version
ninja --version

- name: Configure
run: ./script/configure

- name: Install to system
run: sudo -E env "PATH=$PATH" ninja -C build install

- name: Build and Run other project
run: |
cd example_project/
cmake -B build/ -D "CMAKE_C_COMPILER=$CMAKE_C_COMPILER" -D "CMAKE_CXX_COMPILER=$CMAKE_CXX_COMPILER" -D "SCALUQ_USE_CUDA=$SCALUQ_USE_CUDA"
make -C build
if [ "$SCALUQ_USE_CUDA" != 'ON']; then
build/main
fi

python:
name: Install Python package
strategy:
matrix:
os: ["linux", "macos"]
architecture: ["x86_64", "arm64"]
device: ["cpu", "cuda"]
python-version: ["3.12"]
exclude:
- os: "macos"
device: "cuda"
# currently ARM runner is not supported
- os: "linux"
architecture: "arm64"
include:
- os: "linux"
architecture: "x86_64"
runs-on: "ubuntu-22.04"
- os: "macos"
architecture: "x86_64"
runs-on: "macos-13"
- os: "macos"
architecture: "arm64"
runs-on: "macos-14"
runs-on: ${{ matrix.runs-on }}
env:
CMAKE_C_COMPILER: ${{ matrix.runs-on == 'macos-13' && '/usr/local/opt/ccache/libexec/gcc-14'|| matrix.runs-on == 'macos-14' && '/opt/homebrew/opt/ccache/libexec/gcc-14' || '/usr/lib/ccache/gcc' }}
CMAKE_CXX_COMPILER: ${{ matrix.runs-on == 'macos-13' && '/usr/local/opt/ccache/libexec/g++-14'|| matrix.runs-on == 'macos-14' && '/opt/homebrew/opt/ccache/libexec/g++-14' || '/usr/lib/ccache/g++' }}
SCALUQ_USE_CUDA: ${{ matrix.device == 'cuda' && 'ON' || 'OFF' }}
SCALUQ_CUDA_ARCH: "PASCAL61"
steps:
- uses: actions/checkout@v4

- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Setup ccache
uses: hendrikmuhs/[email protected]
with:
key: "${{ github.job }}-${{ matrix.os }}-${{ matrix.architecture }}-${{ matrix.device }}"
verbose: 2

- name: Install CUDA toolkit
if: ${{ matrix.device == 'cuda' }}
uses: Jimver/[email protected]
with:
cuda: "12.6.1"
method: "network"

- name: Show installed Compiler version
run: |
[ $SCALUQ_USE_CUDA = 'ON' ] && nvcc --version
ccache --version
$CMAKE_C_COMPILER --version
$CMAKE_CXX_COMPILER --version

- name: Install mypy
run: python -m pip install mypy

- name: Install to system
run: python -m pip install .

- name: Run / Test stub
if: ${{ matrix.device == 'cpu' }} # currently GPU runner is not supported
run: |
echo -e "from scaluq.f64 import StateVector, gate\nstate = StateVector(2)\nx = gate.X(0)\nx.update_quantum_state(state)\nprint(state.get_amplitudes())" > /tmp/sample.py
python /tmp/sample.py
python -m mypy /tmp/sample.py
14 changes: 2 additions & 12 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ jobs:
os: ["linux", "macos"]
architecture: ["x86_64", "arm64"]
device: ["cpu", "cuda"]
python-version: ["3.10"]
exclude:
- os: "macos"
device: "cuda"
Expand Down Expand Up @@ -66,9 +65,9 @@ jobs:

- name: Install CUDA toolkit
if: ${{ matrix.device == 'cuda' }}
uses: Jimver/[email protected].11
uses: Jimver/[email protected].19
with:
cuda: "12.2.0"
cuda: "12.6.1"
method: "network"

- name: Show installed Compiler version
Expand All @@ -83,9 +82,6 @@ jobs:
- name: Install scaluq for Ubuntu
run: ./script/build_gcc.sh

- name: Install scaluq Python module
run: pip install .[ci]

- name: Test in Ubuntu
if: ${{ matrix.device == 'cpu' }} # currently GPU runner is not supported
run: |
Expand All @@ -95,9 +91,3 @@ jobs:
NPROC=$(nproc)
fi
OMP_PROC_BIND=false ninja test -C build -j ${NPROC}

- name: Test if stub exists
if: ${{ matrix.device == 'cpu' }} # currently GPU runner is not supported
run: |
echo -e "from scaluq import StateVector\nfrom scaluq.gate import I" > /tmp/stub_sample.py
mypy /tmp/stub_sample.py
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@
/doc/build/
/python/scaluq/_version.py
/kokkos-tools
/example_project/build/
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ if(NOT DEFINED SCALUQ_USE_CUDA)
set(SCALUQ_USE_CUDA OFF)
endif(NOT DEFINED SCALUQ_USE_CUDA)
if(NOT DEFINED SCALUQ_USE_TEST)
set(SCALUQ_USE_TEST ON)
set(SCALUQ_USE_TEST OFF)
endif(NOT DEFINED SCALUQ_USE_TEST)
if(NOT DEFINED SCALUQ_USE_EXE)
set(SCALUQ_USE_EXE ON)
set(SCALUQ_USE_EXE OFF)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

システムにインストールしたいだけの人を優先してデフォルト値を変更

endif(NOT DEFINED SCALUQ_USE_EXE)

message(STATUS "SKBUILD = ${SKBUILD}")
Expand Down Expand Up @@ -135,7 +135,7 @@ if ((${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU") OR (${CMAKE_CXX_COMPILER_ID} STREQ
endif()

### Add subdirectories ###
add_subdirectory(scaluq)
add_subdirectory(src)
if(SKBUILD)
add_subdirectory(python)
endif(SKBUILD)
Expand Down
54 changes: 36 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,42 @@ scaluq は、量子回路シミュレータ [Qulacs](https://github.com/qulacs/q
- [nanobind](https://github.com/wjakob/nanobind) の導入により、よりコンパクトかつ高速な Python へのバインディングを実現します。
- 複数の量子状態に対して同じ回路を適用させるようなケースに対して、より高速なインターフェースを提供します(未実装)。

## 依存ライブラリ
## ビルド時要件

- Ninja 1.10 以上
- GCC 11 以上
- CMake 3.21 以上
- CUDA 12.2 以上(GPU利用時のみ)
- CUDA 12.6 以上(GPU利用時のみ)
※これより低いバージョンでも動作する可能性はありますが確認していません

## 実行時要件
- CUDA 12.6 以上(GPU利用時のみ)
※これより低いバージョンでも動作する可能性はありますが確認していません

## C++ ライブラリとしてインストール

Qulacs2023 を静的ライブラリとしてインストールするには、以下の一連のコマンドを実行します。
scaluq を静的ライブラリとしてインストールするには、以下の一連のコマンドを実行します。

```txt
git clone https://github.com/qulacs/scaluq
cd scaluq
./script/build_gcc.sh
script/configure
sudo -E env "PATH=$PATH" ninja -C build install
```

NVIDIA GPU と CUDA が利用可能ならば、以下のコマンドで GPU バージョンをインストールできます。ビルドスクリプトの実行の際に `SCALUQ_USE_CUDA` オプションを付けます。
- 依存ライブラリのEigenとKokkosも同時にインストールされます
- `CMAKE_INSTALL_PREFIX`を設定することで `/usr/local`以外にインストールすることもできます。ユーザーローカルにインストールしたい場合や、別の設定でビルドしたKokkosと衝突させたくない場合は明示的に指定してください。例: `CMAKE_INSTALL_PREFIX=~/.local script/configure; ninja -C build install`
- ビルドしたものを`/usr/local/bin`に配置するため`sudo`コマンドを用いていますが、ビルド時の環境変数をユーザーのものにするため例では`-E`と`env "PATH=$PATH"`を指定しています。
- NVIDIA GPU と CUDA が利用可能ならば、`SCALUQ_USE_CUDA=Yes`を設定してconfigureすることでCUDAを利用するライブラリとしてインストールできます。例: `SCALUQ_USE_CUDA=Yes script/configure; sudo env -E "PATH=$PATH" ninja -C build install'`

```txt
SCALUQ_USE_CUDA=ON ./script/build_gcc.sh
```

ただし、オプションを変更して再ビルドする際には、CMake にセットされたキャッシュ変数をクリアするため、必ず以下のコマンドを実行してください。
オプションを変更して再ビルドする際には、CMake にセットされたキャッシュ変数をクリアするため、必ず以下のコマンドを実行してください。

```txt
rm build/CMakeCache.txt
```

インストール済みのscaluqを利用したプロジェクトでのCMake設定例を[example_project/](example_project/CMakeLists.txt)に提示しています。

## Python ライブラリとしてインストール
Python のライブラリとしても使用することができます。
```txt
Expand All @@ -65,26 +72,26 @@ https://scaluq.readthedocs.io/en/latest/index.html
#include <iostream>
#include <cstdint>

#include <circuit/circuit.hpp>
#include <gate/gate_factory.hpp>
#include <operator/operator.hpp>
#include <state/state_vector.hpp>
#include <scaluq/circuit/circuit.hpp>
#include <scaluq/gate/gate_factory.hpp>
#include <scaluq/operator/operator.hpp>
#include <scaluq/state/state_vector.hpp>

int main() {
scaluq::initialize(); // must be called before using any scaluq methods
{
const std::uint64_t n_qubits = 3;
scaluq::StateVector state = scaluq::StateVector::Haar_random_state(n_qubits, 0);
scaluq::StateVector<double> state = scaluq::StateVector::Haar_random_state(n_qubits, 0);
std::cout << state << std::endl;

scaluq::Circuit circuit(n_qubits);
scaluq::Circuit<double> circuit(n_qubits);
circuit.add_gate(scaluq::gate::X(0));
circuit.add_gate(scaluq::gate::CNot(0, 1));
circuit.add_gate(scaluq::gate::Y(1));
circuit.add_gate(scaluq::gate::RX(1, std::numbers::pi / 2));
circuit.update_quantum_state(state);

scaluq::Operator observable(n_qubits);
scaluq::Operator<double> observable(n_qubits);
observable.add_random_operator(1, 0);
auto value = observable.get_expectation_value(state);
std::cout << value << std::endl;
Expand All @@ -96,7 +103,7 @@ int main() {
## サンプルコード(Python)

```Python
from scaluq import *
from scaluq.f64 import *
import math

n_qubits = 3
Expand All @@ -115,3 +122,14 @@ value = observable.get_expectation_value(state)
print(value)

```

# 精度指定について
scaluqでは、計算に使用する浮動小数点数のサイズとして32bitと64bitが選択できます。
通常は64bitの使用が推奨されますが、量子機械学習での利用などあまり精度が必要でない場合は32bitを使用すると最大2倍程度の高速化が見込めます。

同じ精度のオブジェクト同士でしか演算を行うことができません。
例えば32bit用に作成したゲートでは64bitの`StateVector`を更新できません。

C++の場合、状態、ゲート、演算子、回路のクラスやゲートを生成する関数が、テンプレート引数を取るようになっており、そこに`float`または`double`を指定することで選択します。

Pythonの場合、精度に合わせて`scaluq.f32`と`scaluq.f64`のどちらかのサブモジュールからオブジェクトを`import`します。
19 changes: 19 additions & 0 deletions example_project/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
cmake_minimum_required(VERSION 3.21)

project(example)

find_package(OpenMP)
if(SCALUQ_USE_CUDA)
find_package(CUDAToolkit)
endif()
find_package(Kokkos)
find_package(scaluq)
add_executable(main main.cpp)
target_include_directories(main PUBLIC /usr/local/include/scaluq)
target_include_directories(main PUBLIC /usr/local/include/kokkos)
target_include_directories(main PUBLIC /usr/local/include/eigen3)
target_compile_features(main PUBLIC cxx_std_20)
target_compile_options(main PUBLIC -fopenmp)
target_compile_definitions(main PUBLIC OPENMP)
target_link_libraries(main PUBLIC scaluq::scaluq)

10 changes: 10 additions & 0 deletions example_project/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <scaluq/state/state_vector.hpp>

int main() {
scaluq::initialize();
{
scaluq::StateVector<double> state(2);
std::cout << state << std::endl;
}
scaluq::finalize();
}
Loading