Skip to content

Instantly share code, notes, and snippets.

@gyu-don
Last active March 6, 2025 15:45
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"]
@LukeTheWalker
Copy link

LukeTheWalker commented Mar 6, 2025

I am trying to compile it using your instruction (but without podman because I keep getting some errors), I was able to create the wheel but then I get:

Traceback (most recent call last):
  File "/home/lgreco/Development/TTC/circuit_contraction.py", line 8, in <module>
    from qiskit_aer import AerSimulator
  File "/home/lgreco/.conda/envs/TensorNets/lib/python3.11/site-packages/qiskit_aer/__init__.py", line 69, in <module>
    from qiskit_aer.aerprovider import AerProvider
  File "/home/lgreco/.conda/envs/TensorNets/lib/python3.11/site-packages/qiskit_aer/aerprovider.py", line 20, in <module>
    from .backends.aer_simulator import AerSimulator
  File "/home/lgreco/.conda/envs/TensorNets/lib/python3.11/site-packages/qiskit_aer/backends/__init__.py", line 17, in <module>
    from .aer_simulator import AerSimulator
  File "/home/lgreco/.conda/envs/TensorNets/lib/python3.11/site-packages/qiskit_aer/backends/aer_simulator.py", line 22, in <module>
    from .aerbackend import AerBackend, AerError
  File "/home/lgreco/.conda/envs/TensorNets/lib/python3.11/site-packages/qiskit_aer/backends/aerbackend.py", line 31, in <module>
    from ..noise.noise_model import NoiseModel, QuantumErrorLocation
  File "/home/lgreco/.conda/envs/TensorNets/lib/python3.11/site-packages/qiskit_aer/noise/__init__.py", line 241, in <module>
    from .noise_model import NoiseModel
  File "/home/lgreco/.conda/envs/TensorNets/lib/python3.11/site-packages/qiskit_aer/noise/noise_model.py", line 36, in <module>
    from ..backends.backend_utils import BASIS_GATES
  File "/home/lgreco/.conda/envs/TensorNets/lib/python3.11/site-packages/qiskit_aer/backends/backend_utils.py", line 30, in <module>
    from .controller_wrappers import aer_initialize_libraries
ImportError: libomp.so: cannot open shared object file: No such file or directory

Any idea on what it may be caused by?
I am using a condo env on python 3.11.11

@gyu-don
Copy link
Author

gyu-don commented Mar 6, 2025

Your issue seems to be related to the absence of OpenMP libraries (libomp.so), which is causing the ImportError.
If you happen to be using Ubuntu or a similar distribution, you can install the OpenMP library system-wide using:

sudo apt-get install libomp-dev

@LukeTheWalker
Copy link

I have it installed, that's the strange thing, also with the pypi version of qiskit-aer I do not encounter this error. Also I have verified that it correctly works since when running the simulator in CPU mode (with the normal qiskit-aer package) all the thread show significant usage in btop.

@gyu-don
Copy link
Author

gyu-don commented Mar 6, 2025

That’s a strange issue, and I’m not sure about the exact cause. You might want to check where libomp.so is located and try adding it to LD_LIBRARY_PATH. Beyond that, I don’t have any further insights.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment