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

Attempt to Add ROCm Support to Qiskit Aer for AMD GPUs (issue #2113) #2310

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
72 changes: 72 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,75 @@ jobs:
with:
path: ./wheelhouse/*.whl
name: artifact-${{ github.job }}
wheel-gpu-rocm:
runs-on: "ubuntu-latest"
steps:
- name: Maximize build space
uses: easimon/maximize-build-space@master
with:
root-reserve-mb: 30000
swap-size-mb: 1024
remove-dotnet: 'true'
remove-android: 'true'
remove-haskell: 'true'
remove-codeql: 'true'

- uses: actions/checkout@v4

- uses: actions/setup-python@v5
name: Install Python
with:
python-version: '3.13'

- name: Install ROCm Dependencies
run: |
sudo apt update
sudo apt install -y rocm-dev rocm-libs rocblas rocm-llvm
echo "/opt/rocm/bin" >> $GITHUB_PATH

- name: Install cibuildwheel
run: |
python -m pip install cibuildwheel==2.22.0

- name: Build ROCm wheels
env:
CIBW_BEFORE_ALL: "yum install -y openblas-devel"
CIBW_SKIP: "*-manylinux_i686 pp* cp36* cp37* cp38* cp39* cp310* cp311* *musllinux*"
CIBW_ENVIRONMENT: QISKIT_AER_PACKAGE_NAME=qiskit-aer-gpu-rocm AER_THRUST_BACKEND=ROCM AER_ROCM_ARCH="gfx900 gfx906 gfx908 gfx90a gfx1030"
CIBW_REPAIR_WHEEL_COMMAND: 'auditwheel repair --exclude librocblas.so.0 --exclude librocm-runtime.so.1 -w {dest_dir} {wheel}'
run: |
python -m cibuildwheel --output-dir wheelhouse

- uses: actions/upload-artifact@v4
with:
path: ./wheelhouse/*.whl
name: artifact-${{ github.job }}
test-rocm:
runs-on: ubuntu-latest
needs: wheel-gpu-rocm # Ensures the ROCm wheels are built before testing
steps:
- uses: actions/checkout@v4

- uses: actions/setup-python@v5
with:
python-version: '3.13'

- name: Install ROCm Dependencies
run: |
sudo apt update
sudo apt install -y rocm-dev rocm-libs rocblas rocm-llvm
echo "/opt/rocm/bin" >> $GITHUB_PATH

- name: Install Test Dependencies
run: |
pip install -r requirements-dev.txt pytest

- name: Install ROCm Build
run: |
pip install ./wheelhouse/qiskit_aer_gpu_rocm*.whl

- name: Run Unit Tests for ROCm
env:
AER_THRUST_BACKEND: ROCM
run: |
pytest -v tests --disable-warnings
56 changes: 56 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,62 @@ jobs:
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: wheelhouse
gpu-build-rocm:
name: Build qiskit-aer-gpu-rocm wheels
strategy:
matrix:
os: ["ubuntu-latest"]
runs-on: ${{ matrix.os }}
environment: release
permissions:
id-token: write
steps:
- name: Maximize build space
uses: easimon/maximize-build-space@master
with:
root-reserve-mb: 32000
swap-size-mb: 1024
remove-dotnet: 'true'
remove-android: 'true'
remove-haskell: 'true'
remove-codeql: 'true'

- uses: actions/checkout@v4

- uses: actions/setup-python@v5
name: Install Python
with:
python-version: '3.13'

- name: Install ROCm Dependencies
run: |
sudo apt update
sudo apt install -y rocm-dev rocm-libs rocblas rocm-llvm
echo "/opt/rocm/bin" >> $GITHUB_PATH

- name: Install cibuildwheel
run: |
python -m pip install cibuildwheel==2.22.0

- name: Build ROCm wheels
env:
CIBW_BEFORE_ALL: "yum install -y openblas-devel"
CIBW_SKIP: "*-manylinux_i686 pp* cp36* cp37* cp38* *musllinux*"
CIBW_TEST_SKIP: "*"
CIBW_ENVIRONMENT: QISKIT_AER_PACKAGE_NAME=qiskit-aer-gpu-rocm AER_THRUST_BACKEND=ROCM AER_ROCM_ARCH="gfx900 gfx906 gfx908 gfx90a gfx1030"
CIBW_REPAIR_WHEEL_COMMAND: 'auditwheel repair --exclude librocblas.so.0 --exclude librocm-runtime.so.1 -w {dest_dir} {wheel}'
run: |
python -m cibuildwheel --output-dir wheelhouse

- uses: actions/upload-artifact@v4
with:
path: ./wheelhouse/*.whl
name: aer-deploy-separate-gpu-build-rocm-${{ matrix.os }}

- name: Publish ROCm wheels to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: wheelhouse
build_wheels_s390x:
name: Build wheels on s390x
runs-on: ${{ matrix.os }}
Expand Down
5 changes: 4 additions & 1 deletion qiskit_aer/primitives/estimator.py
Copy link
Author

Choose a reason for hiding this comment

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

Accidentally added this commit from a different issue

Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,11 @@ def _combine_circs(circuit: QuantumCircuit, meas_circuits: list[QuantumCircuit])
circs = []
for meas_circuit in meas_circuits:
new_circ = circuit.copy()
# Track existing classical register names in new_circ
existing_creg_names = {creg.name for creg in new_circ.cregs}
for creg in meas_circuit.cregs:
new_circ.add_register(creg)
if creg.name not in existing_creg_names: # Prevent duplicate registers
new_circ.add_register(creg)
new_circ.compose(meas_circuit, inplace=True)
_update_metadata(new_circ, meas_circuit.metadata)
circs.append(new_circ)
Expand Down
69 changes: 69 additions & 0 deletions test/terra/backends/aer_simulator/test_rocm_backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2018, 2024.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
"""
AerSimulator ROCm Integration Tests
"""
import os
import pytest
from qiskit import QuantumCircuit
from qiskit_aer import AerSimulator
from test.terra.backends.simulator_test_case import SimulatorTestCase, supported_methods


class TestROCmBackend(SimulatorTestCase):
"""AerSimulator tests for ROCm backend"""

@pytest.mark.skipif(
"AER_THRUST_BACKEND" not in os.environ or os.environ["AER_THRUST_BACKEND"] != "ROCM",
reason="Skipping ROCm-specific tests",
)
@supported_methods(["statevector"])
def test_rocm_backend_initialization(self, method, device):
"""Test if Qiskit Aer initializes with ROCm backend."""
backend = self.backend(method=method, device=device)
self.assertEqual(backend.backend_name, "aer_simulator")

@pytest.mark.skipif(
"AER_THRUST_BACKEND" not in os.environ or os.environ["AER_THRUST_BACKEND"] != "ROCM",
reason="Skipping ROCm-specific tests",
)
@supported_methods(["statevector"])
def test_rocm_statevector(self, method, device):
"""Test a simple circuit on the ROCm backend."""
qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)

backend = self.backend(method=method, device=device)
result = backend.run(qc).result()
statevector = result.get_statevector()

self.assertEqual(len(statevector), 2**2)

@pytest.mark.skipif(
"AER_THRUST_BACKEND" not in os.environ or os.environ["AER_THRUST_BACKEND"] != "ROCM",
reason="Skipping ROCm-specific tests",
)
@supported_methods(["statevector"])
def test_rocm_expectation_value(self, method, device):
"""Test expectation value calculation on ROCm."""
qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)

backend = self.backend(method=method, device=device)
result = backend.run(qc).result()
statevector = result.get_statevector()

# Check probability of |00⟩ state (around 50% for H + CNOT)
prob_00 = abs(statevector[0]) ** 2
self.assertTrue(0.49 < prob_00 < 0.51)