name: Tests
on:
  push:
    branches: [main, 'stable/*']
  pull_request:
    branches: [main, 'stable/*']
concurrency:
  group: ${{ github.repository }}-${{ github.ref }}-${{ github.head_ref }}-${{ github.workflow }}
  cancel-in-progress: true
jobs:
  lint:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ['3.10']
    steps:
      - uses: actions/checkout@v4
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
      - name: Pip cache
        uses: actions/cache@v4
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-${{ matrix.python-version}}-pip-lint-${{ hashFiles('setup.py','requirements-dev.txt','constraints.txt') }}
          restore-keys: |
            ${{ runner.os }}-${{ matrix.python-version}}-pip-lint-
            ${{ runner.os }}-${{ matrix.python-version}}-pip-
            ${{ runner.os }}-${{ matrix.python-version}}-
      - name: Install deps
        run: |
          set -e
          pip install -U pip wheel
          pip install -U -c constraints.txt -r requirements-dev.txt qiskit numpy scipy
        shell: bash
      - name: Run clang-format
        run: |
          set -e
          sh tools/clang-format.sh --Werror -n
        shell: bash          
      - name: Run Lint
        run: |
          set -e
          black --check qiskit_aer test tools setup.py
          pylint -j 2 -rn qiskit_aer
  sdist:
    runs-on: ${{ matrix.platform.os }}
    needs: ["lint"]
    strategy:
      matrix:
        python-version: [3.8, 3.9, "3.10", '3.11', "3.12.0"]
        platform: [
          { os: "ubuntu-latest", python-architecture: "x64" },
        ]
    steps:
      - uses: actions/checkout@v4
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
      - name: Pip cache
        uses: actions/cache@v4
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-${{ matrix.python-version}}-pip-sdist-${{ hashFiles('setup.py','requirements-dev.txt','constraints.txt') }}
          restore-keys: |
            ${{ runner.os }}-${{ matrix.python-version}}-pip-sdist-
            ${{ runner.os }}-${{ matrix.python-version}}-pip-
            ${{ runner.os }}-${{ matrix.python-version}}-
      - name: Install Deps
        run: python -m pip install -U setuptools wheel virtualenv build
      - name: Install openblas
        run: |
          set -e
          sudo apt-get update
          sudo apt-get install -y libopenblas-dev
        shell: bash
      - name: Build Sdist
        run: python -I -m build --sdist
      - name: Install from sdist and test
        run: |
          set -e
          mkdir out; cd out; virtualenv aer-test
          aer-test/bin/pip install ../dist/*tar.gz
          aer-test/bin/python ../tools/verify_wheels.py
          aer-test/bin/pip check
        shell: bash
  tests_linux:
    runs-on: ubuntu-latest
    name: Linux Python ${{ matrix.python-version }}${{ matrix.qiskit-extra && format(' ({0})', matrix.qiskit-extra) }}
    needs: [sdist, lint]
    timeout-minutes: 60
    strategy:
      matrix:
        python-version: [3.8, 3.9, "3.10", "3.11", "3.12.0"]
        qiskit-extra: [""]
        include:
          - python-version: "3.10"
            qiskit-extra: "'qiskit>=1.3.0b1'"

    env:
      AER_THRUST_BACKEND: OMP
      QISKIT_TEST_CAPTURE_STREAMS: 1
      # Needed to suppress a warning in jupyter-core 5.x by eagerly migrating to
      # a new internal interface that will be the default in jupyter-core 6.x.
      # This variable should become redundant on release of jupyter-core 6.
      JUPYTER_PLATFORM_DIRS: 1
    steps:
      - uses: actions/checkout@v4
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
      - name: Pip cache
        uses: actions/cache@v4
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-${{ matrix.python-version}}-pip-test-${{ hashFiles('setup.py','requirements-dev.txt','constraints.txt') }}
          restore-keys: |
            ${{ runner.os }}-${{ matrix.python-version}}-pip-test-
            ${{ runner.os }}-${{ matrix.python-version}}-pip-
            ${{ runner.os }}-${{ matrix.python-version}}-
      - name: Install openblas
        run: |
          set -e
          sudo apt-get update
          sudo apt-get install -y libopenblas-dev
        shell: bash
      - name: Install Aer and dependencies
        run: |
          python -m pip install -U \
            -c constraints.txt \
            -r requirements-dev.txt \
            ${{ matrix.qiskit-extra }} \
            .
      - name: Run Tests
        run: |
          set -e
          pip check
          rm -rf qiskit_aer
          stestr run --slowest
        shell: bash
  tests-json-input:
    runs-on: ${{ matrix.os }}
    needs: [sdist, lint]
    timeout-minutes: 60
    strategy:
      matrix:
        python-version: ['3.10']
        os: ["ubuntu-latest"]
    env:
      AER_THRUST_BACKEND: OMP
      QISKIT_TEST_CAPTURE_STREAMS: 1
      # Needed to suppress a warning in jupyter-core 5.x by eagerly migrating to
      # a new internal interface that will be the default in jupyter-core 6.x.
      # This variable should become redundant on release of jupyter-core 6.
      JUPYTER_PLATFORM_DIRS: 1
    steps:
      - uses: actions/checkout@v4
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
      - name: Pip cache
        uses: actions/cache@v4
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-${{ matrix.python-version}}-pip-test-${{ hashFiles('setup.py','requirements-dev.txt','constraints.txt') }}
          restore-keys: |
            ${{ runner.os }}-${{ matrix.python-version}}-pip-test-
            ${{ runner.os }}-${{ matrix.python-version}}-pip-
            ${{ runner.os }}-${{ matrix.python-version}}-
      - name: Install Deps
        run: python -m pip install -U -c constraints.txt -r requirements-dev.txt wheel build
      - name: Install openblas
        run: |
          set -e
          sudo apt-get update
          sudo apt-get install -y libopenblas-dev
        shell: bash
      - name: Install Aer
        run: |
          set -e
          python -I -m build --wheel --config-setting=--build-option=-DTEST_JSON=1
          pip install -U dist/*.whl
      - name: Run Tests
        run: |
          set -e
          pip check
          rm -rf qiskit_aer
          stestr run --slowest
        shell: bash
  tests_macos:
    runs-on: macos-13
    name: macOS Python ${{ matrix.python-version }}
    needs: [sdist, lint]
    timeout-minutes: 60
    strategy:
      matrix:
        python-version: [3.8, 3.9, "3.10", '3.11', "3.12.0"]
    env:
      AER_THRUST_BACKEND: OMP
      QISKIT_TEST_CAPTURE_STREAMS: 1
      # Needed to suppress a warning in jupyter-core 5.x by eagerly migrating to
      # a new internal interface that will be the default in jupyter-core 6.x.
      # This variable should become redundant on release of jupyter-core 6.
      JUPYTER_PLATFORM_DIRS: 1
    steps:
      - uses: actions/checkout@v4
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
      - name: Pip cache
        uses: actions/cache@v4
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-${{ matrix.python-version}}-pip-test-${{ hashFiles('setup.py','requirements-dev.txt','constraints.txt') }}
          restore-keys: |
            ${{ runner.os }}-${{ matrix.python-version}}-pip-test-
            ${{ runner.os }}-${{ matrix.python-version}}-pip-
            ${{ runner.os }}-${{ matrix.python-version}}-
      - name: Install Aer and dependencies
        run: |
          set -e
          pip install -U \
            -c constraints.txt \
            -r requirements-dev.txt \
            .
      - name: Run Tests
        run: |
          set -e
          pip install "pip<24.2"
          pip check
          rm -rf qiskit_aer
          stestr run --slowest
        shell: bash
  tests_windows:
    runs-on: windows-2019
    name: Windows Python ${{ matrix.python-version }}
    needs: ["lint", "sdist"]
    timeout-minutes: 60
    strategy:
      matrix:
        python-version: [3.8, 3.9, "3.10", "3.11", "3.12.0"]
    env:
      AER_THRUST_BACKEND: OMP
      QISKIT_TEST_CAPTURE_STREAMS: 1
      # Needed to suppress a warning in jupyter-core 5.x by eagerly migrating to
      # a new internal interface that will be the default in jupyter-core 6.x.
      # This variable should become redundant on release of jupyter-core 6.
      JUPYTER_PLATFORM_DIRS: 1
    steps:
      - uses: actions/checkout@v4
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
      - name: Pip cache
        uses: actions/cache@v4
        with:
          path: ~\AppData\Local\pip\Cache
          key: ${{ runner.os }}-${{ matrix.python-version}}-pip-test-${{ hashFiles('setup.py','requirements-dev.txt','constraints.txt') }}
          restore-keys: |
            ${{ runner.os }}-${{ matrix.python-version}}-pip-test-
            ${{ runner.os }}-${{ matrix.python-version}}-pip-
            ${{ runner.os }}-${{ matrix.python-version}}-
      - name: Add msbuild to PATH
        uses: microsoft/setup-msbuild@v2
      - name: Build Aer Windows
        env:
          CMAKE_GENERATOR: "Visual Studio 16 2019"
        run: |
          set -e
          pip install build
          python -I -m build --wheel
        shell: bash
      - name: Install Aer and dependencies
        run: |
          set -e
          shopt -s failglob
          pip install -U \
            -c constraints.txt \
            -r requirements-dev.txt \
            dist/*.whl
        shell: bash
      - name: Run Tests
        env:
          LANG: 'C.UTF-8'
          PYTHONIOENCODING: 'utf-8:backslashreplace'
        run: |
          set -e
          chcp.com 65001
          pip check
          rm -rf qiskit_aer
          stestr run --slowest
        shell: bash
