diff --git a/Makefile b/Makefile index da6102f..22bc04a 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,7 @@ all: ci extra format: black --quiet --check . + isort --quiet --check . test: pytest --doctest-modules -q lint: diff --git a/pyproject.toml b/pyproject.toml index 532487e..447b8da 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,3 +6,6 @@ ignore = ["E501"] # line length enforced by black [tool.mypy] plugins = ["numpy.typing.mypy_plugin"] + +[tool.isort] +profile = "black" diff --git a/requirements.txt b/requirements.txt index 43a4a55..880f861 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,8 @@ -qubrabench@git+https://github.com/qubrabench/qubrabench.git#egg=3844bc51f3f142c7175aae5c5e0a1ce7d5e86a01 +qubrabench@git+https://github.com/qubrabench/qubrabench.git#egg=v0.2.0 numpy~=1.24 matplotlib~=3.7 black[jupyter] +isort pytest~=7.3 ruff~=0.0 mypy~=1.2 diff --git a/usecase/use_max.py b/usecase/use_max.py index 945e5ac..c2c4161 100644 --- a/usecase/use_max.py +++ b/usecase/use_max.py @@ -1,10 +1,30 @@ +from typing import Callable, Sequence + +import numpy as np +import pytest from qubrabench.algorithms.max import max -from qubrabench.benchmark import track_queries, named_oracle +from qubrabench.benchmark import QueryStats, oracle, track_queries + + +def max_value_of_function(it: Sequence[float], f: Callable[[float], float]): + return max(it, key=f, error=1e-5) + + +def test_use_max_quadratic(): + @oracle + def f(x: float): + return 4 * x - x**2 + N = 10000 -def max_value_of_function(it, f): with track_queries() as tracker: - result = max(it, key=named_oracle("max_key")(f), error=1e-5) - stats = tracker.get_stats("max_key") + domain = np.linspace(-10, 10, num=N) + result = max_value_of_function(domain, f) + assert result == pytest.approx(2, rel=1e-3) - return result, stats + assert tracker.get_stats(f) == QueryStats( + classical_actual_queries=N, + classical_expected_queries=N, + quantum_expected_classical_queries=0, + quantum_expected_quantum_queries=pytest.approx(15610.0172), + ) diff --git a/usecase/use_max_test.py b/usecase/use_max_test.py deleted file mode 100644 index 8077934..0000000 --- a/usecase/use_max_test.py +++ /dev/null @@ -1,12 +0,0 @@ -from use_max import max_value_of_function -import numpy as np -import pytest - - -def test_use_max_quadratic(): - def f(x: float): - return 4 * x - x**2 - - domain = np.linspace(-10, 10, num=10000) - result, _ = max_value_of_function(domain, f) - assert result == pytest.approx(2, rel=1e-3)