This is an automated email from the ASF dual-hosted git repository.
rawkintrevo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/mahout.git
The following commit(s) were added to refs/heads/main by this push:
new 05353f22f Improve pytest configuration and testing structure (#566)
05353f22f is described below
commit 05353f22f992aca08c33e11a3a6620e13b4cf1db
Author: Guan Ming(Wesley) Chiu <[email protected]>
AuthorDate: Tue Oct 7 19:43:48 2025 +0800
Improve pytest configuration and testing structure (#566)
---
pyproject.toml | 6 +++++
testing/README.md | 30 ++++++++++++++--------
testing/__init__.py | 16 ++++++++++++
testing/conftest.py | 30 ++++++++++++++++++++++
testing/test_final_quantum_states.py | 50 ++++++++++++++++++++++++++++--------
5 files changed, 110 insertions(+), 22 deletions(-)
diff --git a/pyproject.toml b/pyproject.toml
index 68431d907..dbcb445e1 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -17,6 +17,12 @@ dependencies = [
[project.optional-dependencies]
dev = ["pytest>=8.1.1", "ruff>=0.13.1"]
+[tool.pytest.ini_options]
+testpaths = ["testing"]
+python_files = "test_*.py"
+python_functions = "test_*"
+addopts = ["-v", "--tb=short"]
+
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
diff --git a/testing/README.md b/testing/README.md
index 1071e3bb4..458df69b5 100644
--- a/testing/README.md
+++ b/testing/README.md
@@ -15,19 +15,27 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
-Apache Mahout Testing Suite
-===========
+
+# Apache Mahout Testing Suite
+
For each backend supported in Apache Mahout, the testing suite executes an
example circuit using the qumat implementation of the backend, and then
executes the same example circuit using the backend's native implementation.
The test then checks that the resulting final state vectors are the same.
-The testing suite is run using pytest, which is installed by default using
poetry. To run the tests, simply run
-```
-pytest
+The testing suite is run using `pytest`, which is installed by default using
poetry. To run the tests, simply run
+
+## Running Tests
+
+```bash
+# Run all tests
+poetry run pytest
+
+# Run with verbose output
+poetry run pytest -v
```
-### How to add a test for a new backend
-In order to add *my-new-backend* to the testing suite:
-1. Create a file `testing/my-new-backend_helpers.py`
-2. In `testing/my-new-backend_helpers.py`, create a function
`get_qumat_backend_config` which returns the qumat backend config needed for
the qumat implementation of my-new-backend
-3. In `testing/my-new-backend_helpers.py`, create a function
`get_native_example_final_state_vector` which builds and executes the example
circuit using the native implementation of my-new-backend
-4. In `testing/test_final_quantum_states.py`, add `"my-new-backend"` to
`backends_to_test`
+## Adding New Backend Tests
+
+1. Create `testing/my-new-backend_helpers.py` with:
+ - `get_qumat_backend_config()` function which returns the qumat backend
config needed for the qumat implementation of my-new-backend
+ - `get_native_example_final_state_vector()` function which builds and
executes the example circuit using the native implementation of my-new-backend
+2. Add `"my-new-backend"` to `testing_backends` fixture in `conftest.py`
diff --git a/testing/__init__.py b/testing/__init__.py
new file mode 100644
index 000000000..cce3acad3
--- /dev/null
+++ b/testing/__init__.py
@@ -0,0 +1,16 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
diff --git a/testing/conftest.py b/testing/conftest.py
new file mode 100644
index 000000000..39ce482d0
--- /dev/null
+++ b/testing/conftest.py
@@ -0,0 +1,30 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+import pytest
+import sys
+from pathlib import Path
+
+# Add project root to Python path
+project_root = Path(__file__).parent.parent
+sys.path.insert(0, str(project_root))
+
+
[email protected](scope="session")
+def testing_backends():
+ """Fixture to provide the list of backends to test."""
+ return ["qiskit"] # Can be expanded to include "cirq", "braket" when ready
diff --git a/testing/test_final_quantum_states.py
b/testing/test_final_quantum_states.py
index aeda2cd0c..0742e3f7a 100644
--- a/testing/test_final_quantum_states.py
+++ b/testing/test_final_quantum_states.py
@@ -14,24 +14,32 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
-from qumat_helpers import get_qumat_example_final_state_vector
+
+import pytest
import numpy as np
from importlib import import_module
-def test_final_state_vector():
- # Specify initial computational basis state vector
- initial_ket_str = "001"
+from .qumat_helpers import get_qumat_example_final_state_vector
+
+
+class TestFinalQuantumStates:
+ """Test class for final quantum state comparisons between QuMat and native
implementations."""
+
+ @pytest.mark.parametrize("initial_ket_str", ["000", "001", "010", "011"])
+ def test_qiskit_final_state_vector(self, initial_ket_str):
+ """Test that QuMat produces same final state as native Qiskit
implementation."""
+ backend_name = "qiskit"
- backends_to_test = ["qiskit"]
- for backend_name in backends_to_test:
- backend_module = import_module(f"{backend_name}_helpers",
package="qumat")
- # use native implementation
+ # Import backend-specific helpers
+ backend_module = import_module(f".{backend_name}_helpers",
package="testing")
+
+ # Get native implementation result
native_example_vector =
backend_module.get_native_example_final_state_vector(
initial_ket_str
)
- # use qumat implementation
+ # Get QuMat implementation result
qumat_backend_config = backend_module.get_qumat_backend_config(
"get_final_state_vector"
)
@@ -39,5 +47,25 @@ def test_final_state_vector():
qumat_backend_config, initial_ket_str
)
- # Compare final state vectors from qumat vs. native implementation
- np.testing.assert_array_equal(qumat_example_vector,
native_example_vector)
+ # Compare final state vectors from QuMat vs. native implementation
+ np.testing.assert_array_equal(
+ qumat_example_vector,
+ native_example_vector,
+ err_msg=f"State vectors don't match for initial state
{initial_ket_str} using {backend_name}",
+ )
+
+ def test_all_backends_consistency(self, testing_backends):
+ """Test that all available backends produce consistent results."""
+ initial_ket_str = "001"
+ results = {}
+
+ for backend_name in testing_backends:
+ backend_module = import_module(
+ f".{backend_name}_helpers", package="testing"
+ )
+ qumat_backend_config = backend_module.get_qumat_backend_config(
+ "get_final_state_vector"
+ )
+ results[backend_name] = get_qumat_example_final_state_vector(
+ qumat_backend_config, initial_ket_str
+ )