Skip to content

Instantly share code, notes, and snippets.

@gyu-don
Last active June 29, 2024 12:44
Show Gist options
  • Save gyu-don/6ddc6359b468e8fa34dec709ccd746a6 to your computer and use it in GitHub Desktop.
Save gyu-don/6ddc6359b468e8fa34dec709ccd746a6 to your computer and use it in GitHub Desktop.
Build `qiskit-aer` for ROCm (AMD GPU)

How to build Qiskit-aer for ROCm (AMD GPU)

Qiskit-aer is high-performance quantum computing simulators and ROCm is the computing platform for AMD GPUs like Radeon and Instinct.

As you know, the most powerful and prevalent GPU framework for numerical computation purposes today is NVIDIA CUDA, and AMD has been slow to develop software stacks for that application.

Qiskit-aer's GPU support is available through the cuQuantum library and its own implementation. The cuQuantum library can only run on NVIDIA GPUs. However, Qiskit-aer's own implementation can run on AMD GPUs.

Build

First, we prepare a clone of Qiskit-aer.

git clone https://github.com/Qiskit/qiskit-aer.git

We use Podman, which has a different architecture than Docker, but is used in much the same way. If you are not familiar with it, you may use Docker. Rye is a Python package manager.

FROM rocm/rocm-terminal:latest

ARG UID=1000
ARG GID=1000

RUN sudo apt update && \
    sudo apt install -y cmake libspdlog-dev libopenblas-dev python3-dev mpich ninja-build rocblas rocthrust && \
    sudo mkdir /work && \
    sudo chown $UID:$GID /work
USER $UID
WORKDIR /work
RUN curl -sSf https://rye.astral.sh/get | RYE_INSTALL_OPTION="--yes" bash 
COPY build.sh /work
CMD ["bash", "/work/build.sh"]

And, we prepare build script.

#!/bin/bash
PY_VERSION=3.11

# Change to workdir
cd /work

# Make and activate Python environment
source $HOME/.rye/env
rye init --virtual .
rye pin $PY_VERSION
rye add pybind11 scikit-build setuptools wheel
rye add "conan<2.0.0"
rye sync
. .venv/bin/activate

# Build qiskit-aer
cd /work/qiskit-aer
# You can add ROCm architecture to edit `-DAER_ROCM_ARCH=...`
# You can disable MPI to remove `-DAER_MPI=ON`
QISKIT_AER_PACKAGE_NAME='qiskit-aer-gpu-rocm' python3 setup.py bdist_wheel -- \
    -DCPPFLAGS+=-I.venv/lib/python${PY_VERSION}/site-packages/pybind11/include/ \
    -DAER_THRUST_BACKEND=ROCM -DAER_MPI=ON -DAER_ROCM_ARCH=gfx1101

Then, we run podman.

podman build . -t qiskit-aer-rocm
podman run -it --rm -v $PWD/qiskit-aer:/work/qiskit-aer --userns=keep-id qiskit-aer-rocm

Compiled binary file is created as wheel file in qiskit-aer/dist directory.

Run

Install a wheel.

pip install qiskit-aer/dist/qiskit_aer_gpu_rocm-0.15.0-cp311-cp311-linux_x86_64.whl

Run GPU simulator.

gpusim = AerSimulator(method='statevector', device='GPU')

circ = transpile(QuantumVolume(25, 25, seed=0), basis_gates=["rz", "sx", "ecr"])
circ.measure_all()
result = gpusim.run(circ, shots=10000).result()

The results are posted on benchmark.ipynb. In this result, GPU is 8x faster than CPU.

Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
#!/bin/bash
PY_VERSION=3.11
# Change to workdir
cd /work
# Make and activate Python environment
source $HOME/.rye/env
rye init --virtual .
rye pin $PY_VERSION
rye add pybind11 scikit-build setuptools wheel
rye add "conan<2.0.0"
rye sync
. .venv/bin/activate
# Build qiskit-aer
cd /work/qiskit-aer
QISKIT_AER_PACKAGE_NAME='qiskit-aer-gpu-rocm' python3 setup.py bdist_wheel -- \
-DCPPFLAGS+=-I.venv/lib/python${PY_VERSION}/site-packages/pybind11/include/ \
-DAER_THRUST_BACKEND=ROCM -DAER_MPI=ON -DAER_ROCM_ARCH=gfx1101
cp dist/*.whl ../dist
podman build . -t qiskit-aer-rocm
podman run -it --rm -v $PWD/qiskit-aer:/work/qiskit-aer --userns=keep-id qiskit-aer-rocm
FROM rocm/rocm-terminal:latest
ARG UID=1000
ARG GID=1000
RUN sudo apt update && \
sudo apt install -y cmake libspdlog-dev libopenblas-dev python3-dev mpich ninja-build rocblas rocthrust && \
sudo mkdir /work && \
sudo chown $UID:$GID /work
USER $UID
WORKDIR /work
RUN curl -sSf https://rye.astral.sh/get | RYE_INSTALL_OPTION="--yes" bash
COPY build.sh /work
CMD ["bash", "/work/build.sh"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment