From 9477162bce3da91bc2d603988e5138e09bf331d0 Mon Sep 17 00:00:00 2001 From: Melvin Strobl Date: Mon, 23 Sep 2024 11:25:09 +0200 Subject: [PATCH 01/12] splitted tests Signed-off-by: Melvin Strobl --- .../workflows/{python-app.yml => quality.yml} | 3 -- .github/workflows/testing.yml | 33 +++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) rename .github/workflows/{python-app.yml => quality.yml} (95%) create mode 100644 .github/workflows/testing.yml diff --git a/.github/workflows/python-app.yml b/.github/workflows/quality.yml similarity index 95% rename from .github/workflows/python-app.yml rename to .github/workflows/quality.yml index 694f557..75c3ae9 100644 --- a/.github/workflows/python-app.yml +++ b/.github/workflows/quality.yml @@ -34,6 +34,3 @@ jobs: flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - - name: Test with pytest - run: | - pytest diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml new file mode 100644 index 0000000..a6e0641 --- /dev/null +++ b/.github/workflows/testing.yml @@ -0,0 +1,33 @@ +# This workflow will install Python dependencies, run tests and lint with a single version of Python +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python + +name: Pytest + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +permissions: + contents: read + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Set up Python 3.11 + uses: actions/setup-python@v3 + with: + python-version: "3.11" + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install pytest pennylane + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + - name: Test with pytest + run: | + pytest From 40537568dcb0d8642e5632e8b846f056aed2c51b Mon Sep 17 00:00:00 2001 From: Melvin Strobl Date: Mon, 23 Sep 2024 11:48:54 +0200 Subject: [PATCH 02/12] fixed cache test Signed-off-by: Melvin Strobl --- tests/test_model.py | 78 ++++++++++++++++++++++++--------------------- 1 file changed, 42 insertions(+), 36 deletions(-) diff --git a/tests/test_model.py b/tests/test_model.py index 7642724..f1fdd02 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -5,6 +5,9 @@ import logging import inspect import shutil +import os +import hashlib + logger = logging.getLogger(__name__) @@ -116,44 +119,47 @@ def test_cache() -> None: except Exception as e: logger.warning(e) - test_cases = [ - { - "shots": 1024, - "execution_type": "expval", - "shape": (), - }, - { - "shots": -1, - "execution_type": "density", - "shape": (4, 4), - }, - { - "shots": 1024, - "execution_type": "probs", - "shape": (2,), - }, - ] - - for test_case in test_cases: - model = Model( - n_qubits=2, - n_layers=1, - circuit_type="Circuit_19", - data_reupload=True, - initialization="random", - output_qubit=0, - shots=test_case["shots"], - ) + model = Model( + n_qubits=2, + n_layers=1, + circuit_type="Circuit_19", + ) - result = model( - model.params, - inputs=None, - noise_params=None, - cache=True, - execution_type=test_case["execution_type"], - ) + result = model( + model.params, + inputs=None, + cache=True, + ) - assert result.shape == test_case["shape"], f"Test case: {test_case} failed" + hs = hashlib.md5( + repr( + { + "n_qubits": model.n_qubits, + "n_layers": model.n_layers, + "pqc": model.pqc.__class__.__name__, + "dru": model.data_reupload, + "params": model.params, + "noise_params": model.noise_params, + "execution_type": model.execution_type, + "inputs": None, + "output_qubit": model.output_qubit, + } + ).encode("utf-8") + ).hexdigest() + + cache_folder: str = ".cache" + if not os.path.exists(cache_folder): + raise Exception("Cache folder does not exist.") + + name: str = f"pqc_{hs}.npy" + file_path: str = os.path.join(cache_folder, name) + + if os.path.isfile(file_path): + cached_result = np.load(file_path) + + assert np.array_equal( + result, cached_result + ), "Cached result and calcualted result is not equal." def test_initialization() -> None: From 8ada2b2e76d1e4d10696868d623cc9d1fbaa0759 Mon Sep 17 00:00:00 2001 From: Melvin Strobl Date: Mon, 23 Sep 2024 12:21:42 +0200 Subject: [PATCH 03/12] naming Signed-off-by: Melvin Strobl --- .github/workflows/quality.yml | 2 +- .github/workflows/testing.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/quality.yml b/.github/workflows/quality.yml index 75c3ae9..142e396 100644 --- a/.github/workflows/quality.yml +++ b/.github/workflows/quality.yml @@ -1,7 +1,7 @@ # This workflow will install Python dependencies, run tests and lint with a single version of Python # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python -name: Lint and Pytest +name: Code Quality on: push: diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index a6e0641..a145e42 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -1,7 +1,7 @@ # This workflow will install Python dependencies, run tests and lint with a single version of Python # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python -name: Pytest +name: Testing on: push: From e47e015e448c851aa4ac46410d343b3103a42e51 Mon Sep 17 00:00:00 2001 From: Melvin Strobl Date: Mon, 23 Sep 2024 12:21:57 +0200 Subject: [PATCH 04/12] added batch dim merge Signed-off-by: Melvin Strobl --- qml_essentials/model.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/qml_essentials/model.py b/qml_essentials/model.py index cc751fa..3e78203 100644 --- a/qml_essentials/model.py +++ b/qml_essentials/model.py @@ -540,6 +540,9 @@ def _forward( else: result = result.mean(axis=0) + if len(result.shape) == 3 and result.shape[0] == 1: + result = result[0] + if cache: np.save(file_path, result) From b65e9e21694c667b307896fee1fb926707369f8d Mon Sep 17 00:00:00 2001 From: Melvin Strobl Date: Mon, 23 Sep 2024 12:22:09 +0200 Subject: [PATCH 05/12] fixed param Signed-off-by: Melvin Strobl --- tests/test_entanglement.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_entanglement.py b/tests/test_entanglement.py index c3209bc..e53e0b5 100644 --- a/tests/test_entanglement.py +++ b/tests/test_entanglement.py @@ -34,7 +34,6 @@ def test_entanglement() -> None: circuit_type=test_case["circuit_type"], data_reupload=True, initialization="random", - output_qubit=0, ) ent_cap = Entanglement.meyer_wallach( From b4148f51f46f0a1c9cdc0e64adf1f58af8989ef5 Mon Sep 17 00:00:00 2001 From: Melvin Strobl Date: Mon, 23 Sep 2024 12:22:19 +0200 Subject: [PATCH 06/12] fixed parity test Signed-off-by: Melvin Strobl --- tests/test_model.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/test_model.py b/tests/test_model.py index f1fdd02..fd4a31c 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -492,17 +492,19 @@ def test_parity() -> None: n_qubits=2, n_layers=1, circuit_type="Circuit_1", - output_qubit=[0, 1], + output_qubit=[0, 1], # parity ) model_b = Model( n_qubits=2, n_layers=1, circuit_type="Circuit_1", - output_qubit=-1, + output_qubit=-1, # individual ) - result_a = model_a(model_a.params, inputs=None, cache=False, force_mean=True) - result_b = model_b(model_a.params, inputs=None, cache=False) # use same params! + result_a = model_a(params=model_a.params, inputs=None, force_mean=True) + result_b = model_b( + params=model_a.params, inputs=None, force_mean=True + ) # use same params! assert not np.allclose( result_a, result_b From 1c0ff924b0e61420a39196adf3694fc34bcc0afb Mon Sep 17 00:00:00 2001 From: Melvin Strobl Date: Mon, 23 Sep 2024 12:25:32 +0200 Subject: [PATCH 07/12] added failing test from before to the proper test fct Signed-off-by: Melvin Strobl --- tests/test_model.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/tests/test_model.py b/tests/test_model.py index fd4a31c..3414885 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -396,6 +396,15 @@ def test_local_and_global_meas() -> None: inputs = np.array([0.1, 0.2, 0.3]) test_cases = [ { + "inputs": None, + "execution_type": "expval", + "output_qubit": -1, + "shots": -1, + "out_shape": (2, 1), + "warning": False, + }, + { + "inputs": np.array([0.1, 0.2, 0.3]), "execution_type": "expval", "output_qubit": -1, "shots": -1, @@ -403,6 +412,7 @@ def test_local_and_global_meas() -> None: "warning": False, }, { + "inputs": np.array([0.1, 0.2, 0.3]), "execution_type": "expval", "output_qubit": 0, "shots": -1, @@ -410,6 +420,7 @@ def test_local_and_global_meas() -> None: "warning": False, }, { + "inputs": np.array([0.1, 0.2, 0.3]), "execution_type": "expval", "output_qubit": [0, 1], "shots": -1, @@ -417,6 +428,15 @@ def test_local_and_global_meas() -> None: "warning": False, }, { + "inputs": None, + "execution_type": "density", + "output_qubit": -1, + "shots": -1, + "out_shape": (4, 4), + "warning": False, + }, + { + "inputs": np.array([0.1, 0.2, 0.3]), "execution_type": "density", "output_qubit": -1, "shots": -1, @@ -424,6 +444,7 @@ def test_local_and_global_meas() -> None: "warning": False, }, { + "inputs": np.array([0.1, 0.2, 0.3]), "execution_type": "density", "output_qubit": 0, "shots": -1, @@ -431,6 +452,7 @@ def test_local_and_global_meas() -> None: "warning": True, }, { + "inputs": np.array([0.1, 0.2, 0.3]), "execution_type": "probs", "output_qubit": -1, "shots": 1024, @@ -438,6 +460,7 @@ def test_local_and_global_meas() -> None: "warning": False, }, { + "inputs": np.array([0.1, 0.2, 0.3]), "execution_type": "probs", "output_qubit": 0, "shots": 1024, @@ -445,6 +468,7 @@ def test_local_and_global_meas() -> None: "warning": False, }, { + "inputs": np.array([0.1, 0.2, 0.3]), "execution_type": "probs", "output_qubit": [0, 1], "shots": 1024, @@ -467,7 +491,7 @@ def test_local_and_global_meas() -> None: with pytest.warns(UserWarning): out = model( model.params, - inputs=inputs, + inputs=test_case["inputs"], noise_params=None, cache=False, execution_type=test_case["execution_type"], @@ -475,7 +499,7 @@ def test_local_and_global_meas() -> None: else: out = model( model.params, - inputs=inputs, + inputs=test_case["inputs"], noise_params=None, cache=False, execution_type=test_case["execution_type"], From 4576a8995179ad1f7783dcc70f7144fa846fca74 Mon Sep 17 00:00:00 2001 From: Melvin Strobl Date: Mon, 23 Sep 2024 12:39:55 +0200 Subject: [PATCH 08/12] adapted badges Signed-off-by: Melvin Strobl --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 29e5d9c..2d1736c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # QML Essentials -[![version](https://img.shields.io/badge/version-0.1.12-green.svg)](https://ea3a0fbb-599f-4d83-86f1-0e71abe27513.ka.bw-cloud-instance.org/lc3267/quantum/) [![Pipx Status](https://servers.stroblme.de/api/badge/3/uptime/72?color=%2331c754&labelColor=%233f4850)](https://servers.stroblme.de/status/open) [![Lint and Pytest](https://github.com/cirKITers/qml-essentials/actions/workflows/python-app.yml/badge.svg)](https://github.com/cirKITers/qml-essentials/actions/workflows/python-app.yml) [![Page Build](https://github.com/cirKITers/qml-essentials/actions/workflows/pages/pages-build-deployment/badge.svg)](https://github.com/cirKITers/qml-essentials/actions/workflows/pages/pages-build-deployment) +[![version](https://img.shields.io/badge/version-0.1.12-green.svg)](https://ea3a0fbb-599f-4d83-86f1-0e71abe27513.ka.bw-cloud-instance.org/lc3267/quantum/) [![Pipx Status](https://servers.stroblme.de/api/badge/3/uptime/72?color=%2331c754&labelColor=%233f4850)](https://servers.stroblme.de/status/open) [![Code Quality](https://github.com/cirKITers/qml-essentials/actions/workflows/quality.yml/badge.svg)](https://github.com/cirKITers/qml-essentials/actions/workflows/quality.yml) [![Testing](https://github.com/cirKITers/qml-essentials/actions/workflows/testing.yml/badge.svg)](https://github.com/cirKITers/qml-essentials/actions/workflows/testing.yml) [![Page Build](https://github.com/cirKITers/qml-essentials/actions/workflows/pages/pages-build-deployment/badge.svg)](https://github.com/cirKITers/qml-essentials/actions/workflows/pages/pages-build-deployment) ## :scroll: About From 4c20f8c28932cb9269ff645642bc079daee1c94f Mon Sep 17 00:00:00 2001 From: Melvin Strobl Date: Mon, 23 Sep 2024 12:45:34 +0200 Subject: [PATCH 09/12] added poetry tests Signed-off-by: Melvin Strobl --- .github/workflows/quality.yml | 9 +++++---- .github/workflows/testing.yml | 11 ++++++----- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/.github/workflows/quality.yml b/.github/workflows/quality.yml index 142e396..da0732b 100644 --- a/.github/workflows/quality.yml +++ b/.github/workflows/quality.yml @@ -19,15 +19,16 @@ jobs: steps: - uses: actions/checkout@v4 + - name: Install poetry + run: pipx install poetry - name: Set up Python 3.11 - uses: actions/setup-python@v3 + uses: actions/setup-python@v5 with: python-version: "3.11" + cache: 'poetry' # caching pip dependencies - name: Install dependencies run: | - python -m pip install --upgrade pip - pip install flake8 pytest pennylane - if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + run: poetry install - name: Lint with flake8 run: | # stop the build if there are Python syntax errors or undefined names diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index a145e42..2e21816 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -19,15 +19,16 @@ jobs: steps: - uses: actions/checkout@v4 + - name: Install poetry + run: pipx install poetry - name: Set up Python 3.11 - uses: actions/setup-python@v3 + uses: actions/setup-python@v5 with: python-version: "3.11" + cache: 'poetry' # caching pip dependencies - name: Install dependencies run: | - python -m pip install --upgrade pip - pip install pytest pennylane - if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + run: poetry install - name: Test with pytest run: | - pytest + run: poetry run pytest From 4199ce3ddd4b7ac548a45eb41f0a2ecd93bd7200 Mon Sep 17 00:00:00 2001 From: Melvin Strobl Date: Mon, 23 Sep 2024 12:45:40 +0200 Subject: [PATCH 10/12] fixed unused imports Signed-off-by: Melvin Strobl --- tests/test_entanglement.py | 2 -- tests/test_expressiblity.py | 2 -- 2 files changed, 4 deletions(-) diff --git a/tests/test_entanglement.py b/tests/test_entanglement.py index e53e0b5..f4242e6 100644 --- a/tests/test_entanglement.py +++ b/tests/test_entanglement.py @@ -1,8 +1,6 @@ from qml_essentials.model import Model from qml_essentials.entanglement import Entanglement -import pytest -import numpy as np import logging import math diff --git a/tests/test_expressiblity.py b/tests/test_expressiblity.py index e5512e1..b20d345 100644 --- a/tests/test_expressiblity.py +++ b/tests/test_expressiblity.py @@ -1,8 +1,6 @@ from qml_essentials.model import Model from qml_essentials.expressibility import Expressibility -import pytest -import numpy as np import logging import math From 97e134de765528f39345c150eb37f73bdd8ddae2 Mon Sep 17 00:00:00 2001 From: Melvin Strobl Date: Mon, 23 Sep 2024 12:46:19 +0200 Subject: [PATCH 11/12] fixed flake 8 poetry Signed-off-by: Melvin Strobl --- .github/workflows/quality.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/quality.yml b/.github/workflows/quality.yml index da0732b..d74e8d3 100644 --- a/.github/workflows/quality.yml +++ b/.github/workflows/quality.yml @@ -32,6 +32,6 @@ jobs: - name: Lint with flake8 run: | # stop the build if there are Python syntax errors or undefined names - flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics + poetry run flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide - flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics + poetry run flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics From 81854c49207025d81487b494f5893c03e092734e Mon Sep 17 00:00:00 2001 From: Melvin Strobl Date: Mon, 23 Sep 2024 12:47:53 +0200 Subject: [PATCH 12/12] fixed actions Signed-off-by: Melvin Strobl --- .github/workflows/quality.yml | 5 ++--- .github/workflows/testing.yml | 6 ++---- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/.github/workflows/quality.yml b/.github/workflows/quality.yml index d74e8d3..19ed938 100644 --- a/.github/workflows/quality.yml +++ b/.github/workflows/quality.yml @@ -27,10 +27,9 @@ jobs: python-version: "3.11" cache: 'poetry' # caching pip dependencies - name: Install dependencies - run: | - run: poetry install + run: poetry install - name: Lint with flake8 - run: | + run: # stop the build if there are Python syntax errors or undefined names poetry run flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index 2e21816..786898d 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -27,8 +27,6 @@ jobs: python-version: "3.11" cache: 'poetry' # caching pip dependencies - name: Install dependencies - run: | - run: poetry install + run: poetry install - name: Test with pytest - run: | - run: poetry run pytest + run: poetry run pytest