From 8d2f9cf80650046d5ee59b8d8b4671d1b01bc3d4 Mon Sep 17 00:00:00 2001 From: Yasuhito Takamiya Date: Thu, 5 Dec 2024 09:36:10 +0900 Subject: [PATCH 1/7] ci: split CI workflow and add ruff lint - Split ci.yaml into mypy.yaml, ruff.yaml, and test.yaml - Add ruff lint command to ruff.yaml --- .../actions/setup-python-with-uv/action.yml | 32 +++++++++++++ .github/workflows/ci.yaml | 44 ------------------ .github/workflows/mypy.yaml | 27 +++++++++++ .github/workflows/ruff.yaml | 46 +++++++++++++++++++ .github/workflows/test.yaml | 38 +++++++++++++++ 5 files changed, 143 insertions(+), 44 deletions(-) create mode 100644 .github/actions/setup-python-with-uv/action.yml delete mode 100644 .github/workflows/ci.yaml create mode 100644 .github/workflows/mypy.yaml create mode 100644 .github/workflows/ruff.yaml create mode 100644 .github/workflows/test.yaml diff --git a/.github/actions/setup-python-with-uv/action.yml b/.github/actions/setup-python-with-uv/action.yml new file mode 100644 index 0000000..c572d26 --- /dev/null +++ b/.github/actions/setup-python-with-uv/action.yml @@ -0,0 +1,32 @@ +name: Install Python with uv +description: | + This GitHub Action installs Python using the uv tool. + It pins the specified Python version, caches uv files, and installs dependencies. + +inputs: + python-version: + description: Python version + required: true + +runs: + using: composite + steps: + - name: Install uv + run: curl -LsSf https://astral.sh/uv/install.sh | sh + shell: bash + + - name: Pin Python Version + run: | + export PYTHONUNBUFFERED=True + uv python pin ${{ inputs.python-version }} + shell: bash + + - uses: actions/cache@v4 + id: cache-uv + with: + path: ~/.cache/uv + key: ${{ runner.os }}-python-${{ inputs.python-version }}-uv + + - name: Install Dependencies + run: uv sync + shell: bash diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml deleted file mode 100644 index 76c47f3..0000000 --- a/.github/workflows/ci.yaml +++ /dev/null @@ -1,44 +0,0 @@ -name: CI - -on: - push: - branches: [main] - pull_request: - branches: [main] - -jobs: - test: - timeout-minutes: 60 - runs-on: ubuntu-latest - strategy: - matrix: - python-version: ['3.10', '3.12'] - - steps: - - name: Checkout repository - uses: actions/checkout@v4 - - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v4 - with: - python-version: ${{ matrix.python-version }} - - - name: Install uv - uses: astral-sh/setup-uv@v3 - - - name: Check code formatting - run: uv run ruff check - - - name: Check Python types - run: uv run mypy - - - name: Run tests - run: uv run pytest - - - name: Upload coverage to Codecov - if: matrix.python-version == '3.12' - uses: codecov/codecov-action@v5 - with: - files: ./coverage.xml - token: ${{ secrets.CODECOV_TOKEN }} - slug: oqtopus-team/tranqu diff --git a/.github/workflows/mypy.yaml b/.github/workflows/mypy.yaml new file mode 100644 index 0000000..ce6afb2 --- /dev/null +++ b/.github/workflows/mypy.yaml @@ -0,0 +1,27 @@ +name: Mypy + +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + lint: + runs-on: ubuntu-latest + + strategy: + matrix: + python-version: ["3.10", "3.11", "3.12"] + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup Python ${{ matrix.python-version }} with uv + uses: ./.github/actions/setup-python-with-uv + with: + python-version: ${{ matrix.python-version }} + + - name: Check Python types + run: uv run mypy diff --git a/.github/workflows/ruff.yaml b/.github/workflows/ruff.yaml new file mode 100644 index 0000000..2d88e4d --- /dev/null +++ b/.github/workflows/ruff.yaml @@ -0,0 +1,46 @@ +name: Ruff + +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + lint: + runs-on: ubuntu-latest + + strategy: + matrix: + python-version: ["3.10", "3.11", "3.12"] + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup Python ${{ matrix.python-version }} with uv + uses: ./.github/actions/setup-python-with-uv + with: + python-version: ${{ matrix.python-version }} + + - name: Lint + run: uv run ruff check --output-format=github . + + format: + runs-on: ubuntu-latest + + strategy: + matrix: + python-version: ["3.10", "3.11", "3.12"] + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup Python ${{ matrix.python-version }} with uv + uses: ./.github/actions/setup-python-with-uv + with: + python-version: ${{ matrix.python-version }} + + - name: Format + run: uv run ruff format . --check --diff diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..446e021 --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,38 @@ +name: Test + +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + pytest: + runs-on: ubuntu-latest + + strategy: + matrix: + python-version: ["3.10", "3.11", "3.12"] + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup Python ${{ matrix.python-version }} with uv + uses: ./.github/actions/setup-python-with-uv + with: + python-version: ${{ matrix.python-version }} + + - name: Run Pytest if directory exists + run: | + if [ -d "./tests/" ]; then + uv run pytest -s + fi + + - name: Upload coverage to Codecov + if: matrix.python-version == '3.12' + uses: codecov/codecov-action@v5 + with: + files: ./coverage.xml + token: ${{ secrets.CODECOV_TOKEN }} + slug: oqtopus-team/tranqu From 788c76bd769bb645504182a0cc8bf1bf510fe553 Mon Sep 17 00:00:00 2001 From: Yasuhito Takamiya Date: Thu, 5 Dec 2024 09:40:38 +0900 Subject: [PATCH 2/7] fix ruff format error --- src/tranqu/transpiler_dispatcher.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/tranqu/transpiler_dispatcher.py b/src/tranqu/transpiler_dispatcher.py index fcd3025..f953ef6 100644 --- a/src/tranqu/transpiler_dispatcher.py +++ b/src/tranqu/transpiler_dispatcher.py @@ -147,7 +147,9 @@ def _convert_program(self, program: Any, from_lib: str, to_lib: Any) -> Any: # to_lib, ) if not (can_convert_to_qiskit and can_convert_to_target): - msg = f"No ProgramConverter path found to convert from {from_lib} to {to_lib}" # noqa: E501 + msg = ( + f"No ProgramConverter path found to convert from {from_lib} to {to_lib}" # noqa: E501 + ) raise ProgramConversionPathNotFoundError(msg) return self._program_converter_manager.fetch_converter( @@ -183,7 +185,9 @@ def _convert_device( to_lib, ) if not (can_convert_to_qiskit and can_convert_to_target): - msg = f"No DeviceConverter path found to convert from {from_lib} to {to_lib}" # noqa: E501 + msg = ( + f"No DeviceConverter path found to convert from {from_lib} to {to_lib}" # noqa: E501 + ) raise DeviceConversionPathNotFoundError(msg) return self._device_converter_manager.fetch_converter("qiskit", to_lib).convert( From 8fafcd5cc546bc37978d853fbdfe768b3baf7491 Mon Sep 17 00:00:00 2001 From: Yasuhito Takamiya Date: Thu, 5 Dec 2024 09:42:31 +0900 Subject: [PATCH 3/7] fix ruff errors --- src/tranqu/transpiler_dispatcher.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tranqu/transpiler_dispatcher.py b/src/tranqu/transpiler_dispatcher.py index f953ef6..8266390 100644 --- a/src/tranqu/transpiler_dispatcher.py +++ b/src/tranqu/transpiler_dispatcher.py @@ -148,7 +148,7 @@ def _convert_program(self, program: Any, from_lib: str, to_lib: Any) -> Any: # ) if not (can_convert_to_qiskit and can_convert_to_target): msg = ( - f"No ProgramConverter path found to convert from {from_lib} to {to_lib}" # noqa: E501 + f"No ProgramConverter path found to convert from {from_lib} to {to_lib}" ) raise ProgramConversionPathNotFoundError(msg) @@ -186,7 +186,7 @@ def _convert_device( ) if not (can_convert_to_qiskit and can_convert_to_target): msg = ( - f"No DeviceConverter path found to convert from {from_lib} to {to_lib}" # noqa: E501 + f"No DeviceConverter path found to convert from {from_lib} to {to_lib}" ) raise DeviceConversionPathNotFoundError(msg) From a1d5f0389ef54d78a45c67ab5878099298ca2d74 Mon Sep 17 00:00:00 2001 From: Yasuhito Takamiya Date: Thu, 5 Dec 2024 09:54:57 +0900 Subject: [PATCH 4/7] add tests --- tests/tranqu/test_tranqu.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/tranqu/test_tranqu.py b/tests/tranqu/test_tranqu.py index eae8fcc..c2ad39f 100644 --- a/tests/tranqu/test_tranqu.py +++ b/tests/tranqu/test_tranqu.py @@ -2,10 +2,12 @@ import re +import pytest from qiskit import QuantumCircuit as QiskitCircuit from tranqu import Tranqu, __version__ from tranqu.program_converter import ProgramConverter +from tranqu.transpiler_dispatcher import ProgramConversionPathNotFoundError class EnigmaCircuit: @@ -134,3 +136,13 @@ def test_transpile_openqasm3_program_for_oqtopus_device_with_qiskit_transpiler( c[1] = measure $2; """ assert result.transpiled_program == expected_program + + def test_program_conversion_path_not_found(self): + tranqu = Tranqu() + circuit = EnigmaCircuit() + + with pytest.raises( + ProgramConversionPathNotFoundError, + match="No ProgramConverter path found to convert from enigma to qiskit", + ): + tranqu.transpile(circuit, "enigma", "qiskit") From 60b187b95619cae1607a7f9b4c98d77a3263f4a2 Mon Sep 17 00:00:00 2001 From: Yasuhito Takamiya Date: Thu, 5 Dec 2024 10:00:27 +0900 Subject: [PATCH 5/7] add more tests --- tests/tranqu/test_tranqu.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/tests/tranqu/test_tranqu.py b/tests/tranqu/test_tranqu.py index c2ad39f..fa6f024 100644 --- a/tests/tranqu/test_tranqu.py +++ b/tests/tranqu/test_tranqu.py @@ -7,7 +7,10 @@ from tranqu import Tranqu, __version__ from tranqu.program_converter import ProgramConverter -from tranqu.transpiler_dispatcher import ProgramConversionPathNotFoundError +from tranqu.transpiler_dispatcher import ( + DeviceConversionPathNotFoundError, + ProgramConversionPathNotFoundError, +) class EnigmaCircuit: @@ -146,3 +149,20 @@ def test_program_conversion_path_not_found(self): match="No ProgramConverter path found to convert from enigma to qiskit", ): tranqu.transpile(circuit, "enigma", "qiskit") + + def test_device_conversion_path_not_found(self): + tranqu = Tranqu() + circuit = QiskitCircuit(2) + device = {"name": "custom_device", "qubits": [], "couplings": []} + + with pytest.raises( + DeviceConversionPathNotFoundError, + match="No DeviceConverter path found to convert from custom to qiskit", + ): + tranqu.transpile( + circuit, + program_lib="qiskit", + transpiler_lib="qiskit", + device=device, + device_lib="custom", + ) From 4d7953735edd98b40f81f17a787a0d0ba35f3cbc Mon Sep 17 00:00:00 2001 From: Yasuhito Takamiya Date: Thu, 5 Dec 2024 11:22:15 +0900 Subject: [PATCH 6/7] consolidate workflows into a single file --- .github/workflows/{test.yaml => ci.yaml} | 9 +++++ .github/workflows/mypy.yaml | 27 -------------- .github/workflows/ruff.yaml | 46 ------------------------ 3 files changed, 9 insertions(+), 73 deletions(-) rename .github/workflows/{test.yaml => ci.yaml} (80%) delete mode 100644 .github/workflows/mypy.yaml delete mode 100644 .github/workflows/ruff.yaml diff --git a/.github/workflows/test.yaml b/.github/workflows/ci.yaml similarity index 80% rename from .github/workflows/test.yaml rename to .github/workflows/ci.yaml index 446e021..f41b965 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/ci.yaml @@ -23,6 +23,15 @@ jobs: with: python-version: ${{ matrix.python-version }} + - name: Lint + run: uv run ruff check --output-format=github . + + - name: Format + run: uv run ruff format . --check --diff + + - name: Check Python types + run: uv run mypy + - name: Run Pytest if directory exists run: | if [ -d "./tests/" ]; then diff --git a/.github/workflows/mypy.yaml b/.github/workflows/mypy.yaml deleted file mode 100644 index ce6afb2..0000000 --- a/.github/workflows/mypy.yaml +++ /dev/null @@ -1,27 +0,0 @@ -name: Mypy - -on: - push: - branches: [main] - pull_request: - branches: [main] - -jobs: - lint: - runs-on: ubuntu-latest - - strategy: - matrix: - python-version: ["3.10", "3.11", "3.12"] - - steps: - - name: Checkout - uses: actions/checkout@v4 - - - name: Setup Python ${{ matrix.python-version }} with uv - uses: ./.github/actions/setup-python-with-uv - with: - python-version: ${{ matrix.python-version }} - - - name: Check Python types - run: uv run mypy diff --git a/.github/workflows/ruff.yaml b/.github/workflows/ruff.yaml deleted file mode 100644 index 2d88e4d..0000000 --- a/.github/workflows/ruff.yaml +++ /dev/null @@ -1,46 +0,0 @@ -name: Ruff - -on: - push: - branches: [main] - pull_request: - branches: [main] - -jobs: - lint: - runs-on: ubuntu-latest - - strategy: - matrix: - python-version: ["3.10", "3.11", "3.12"] - - steps: - - name: Checkout - uses: actions/checkout@v4 - - - name: Setup Python ${{ matrix.python-version }} with uv - uses: ./.github/actions/setup-python-with-uv - with: - python-version: ${{ matrix.python-version }} - - - name: Lint - run: uv run ruff check --output-format=github . - - format: - runs-on: ubuntu-latest - - strategy: - matrix: - python-version: ["3.10", "3.11", "3.12"] - - steps: - - name: Checkout - uses: actions/checkout@v4 - - - name: Setup Python ${{ matrix.python-version }} with uv - uses: ./.github/actions/setup-python-with-uv - with: - python-version: ${{ matrix.python-version }} - - - name: Format - run: uv run ruff format . --check --diff From 1a617b7b29e3467491d43375034acdba753d14a9 Mon Sep 17 00:00:00 2001 From: Yasuhito Takamiya Date: Thu, 5 Dec 2024 11:22:49 +0900 Subject: [PATCH 7/7] fix --- .github/workflows/ci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index f41b965..9cf4869 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -1,4 +1,4 @@ -name: Test +name: CI on: push: