Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✅ Increase Testing Code Coverage #271

Merged
merged 5 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/codecov.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
ignore:
- "benchviewer/*"
- "mqt/bench/benchmark_generator.py"
- "tests/**/*"

coverage:
Expand Down
19 changes: 7 additions & 12 deletions src/mqt/bench/evaluation/evaluation.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
from __future__ import annotations

import pickle
import sys
from dataclasses import dataclass
from pathlib import Path
from typing import TYPE_CHECKING

from joblib import Parallel, delayed
from qiskit import QuantumCircuit

from mqt.bench import utils

if TYPE_CHECKING or sys.version_info >= (3, 10, 0): # pragma: no cover
from importlib import resources
else:
import importlib_resources as resources


def create_statistics() -> None:
source_circuits_list = [
file for file in Path(utils.get_default_qasm_output_path()).iterdir() if file.suffix == ".qasm"
]
def create_statistics(source_directory: Path | None = None, target_directory: Path | None = None) -> None:
if source_directory is None:
source_directory = Path(utils.get_default_qasm_output_path())

Check warning on line 15 in src/mqt/bench/evaluation/evaluation.py

View check run for this annotation

Codecov / codecov/patch

src/mqt/bench/evaluation/evaluation.py#L15

Added line #L15 was not covered by tests
if target_directory is None:
target_directory = Path(utils.get_default_evaluation_output_path())

Check warning on line 17 in src/mqt/bench/evaluation/evaluation.py

View check run for this annotation

Codecov / codecov/patch

src/mqt/bench/evaluation/evaluation.py#L17

Added line #L17 was not covered by tests
source_circuits_list = [file for file in source_directory.iterdir() if file.suffix == ".qasm"]
res_dicts = Parallel(n_jobs=-1, verbose=100)(
delayed(evaluate_qasm_file)(str(filename)) for filename in source_circuits_list
)
with (resources.files("mqt.bench") / "evaluation" / "evaluation_data.pkl").open("wb") as f:
with (target_directory / "evaluation_data.pkl").open("wb") as f:
pickle.dump(res_dicts, f)


Expand Down
5 changes: 5 additions & 0 deletions src/mqt/bench/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ def get_default_qasm_output_path() -> str:
return str(resources.files("mqt.benchviewer") / "static" / "files" / "qasm_output")


def get_default_evaluation_output_path() -> str:
"""Returns the path where all .qasm files are stored."""
return str(resources.files("mqt.bench") / "evaluation")


def get_zip_file_path() -> str:
"""Returns the path where the zip file is stored."""
return str(resources.files("mqt.benchviewer") / "static/files/MQTBench_all.zip")
Expand Down
92 changes: 90 additions & 2 deletions tests/test_bench.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import json
import pickle
from pathlib import Path
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -293,6 +294,11 @@ def test_quantumcircuit_native_and_mapped_levels(
assert res


def test_get_default_evaluation_output_path() -> None:
path = utils.get_default_evaluation_output_path()
assert Path(path).exists()


def test_openqasm_gates() -> None:
openqasm_gates = utils.get_openqasm_gates()
num_openqasm_gates = 42
Expand Down Expand Up @@ -640,6 +646,56 @@ def test_unidirectional_coupling_map() -> None:
"quantinuum",
"quantinuum_h2",
),
(
"grover-noancilla",
"alg",
5,
None,
"qiskit",
None,
"",
"",
),
(
"qwalk-noancilla",
"alg",
5,
None,
"qiskit",
None,
"",
"",
),
(
"grover-v-chain",
"alg",
5,
None,
"qiskit",
None,
"",
"",
),
(
"qwalk-v-chain",
"alg",
5,
None,
"qiskit",
None,
"",
"",
),
(
"shor",
"alg",
None,
"xsmall",
"qiskit",
None,
"",
"",
),
],
)
def test_get_benchmark(
Expand Down Expand Up @@ -766,7 +822,7 @@ def test_get_benchmark_faulty_parameters() -> None:

def test_create_benchmarks_from_config(output_path: str) -> None:
config = {
"timeout": 120,
"timeout": 1,
"benchmarks": [
{
"name": "ghz",
Expand All @@ -776,6 +832,25 @@ def test_create_benchmarks_from_config(output_path: str) -> None:
"stepsize": 1,
"precheck_possible": True,
},
{
"name": "grover",
"include": True,
"min_qubits": 2,
"max_qubits": 3,
"stepsize": 1,
"ancillary_mode": ["noancilla"],
"precheck_possible": False,
},
{"name": "shor", "include": True, "instances": ["small"], "precheck_possible": False},
{"name": "routing", "include": True, "min_nodes": 2, "max_nodes": 3, "precheck_possible": False},
{"name": "groundstate", "include": True, "instances": ["small"], "precheck_possible": False},
{
"name": "pricingput",
"include": True,
"min_uncertainty": 2,
"max_uncertainty": 3,
"precheck_possible": False,
},
],
}
file = Path("test_config.json")
Expand All @@ -786,6 +861,12 @@ def test_create_benchmarks_from_config(output_path: str) -> None:
generator.create_benchmarks_from_config(num_jobs=1)
file.unlink()

evaluation.create_statistics(source_directory=Path(output_path), target_directory=Path(output_path))

with (Path(output_path) / "evaluation_data.pkl").open("rb") as f:
res_dicts = pickle.load(f)
assert len(res_dicts) > 0


def test_configure_end(output_path: str) -> None:
# delete all files in the test directory and the directory itself
Expand Down Expand Up @@ -923,9 +1004,16 @@ def test_evaluate_qasm_file() -> None:
path = Path(filename)
res = evaluation.evaluate_qasm_file(filename)
assert type(res) == evaluation.EvaluationResult

path.unlink()

res = evaluation.evaluate_qasm_file("invalid_path.qasm")
assert type(res) == evaluation.EvaluationResult
assert res.num_qubits == -1
assert res.depth == -1
assert res.num_gates == -1
assert res.num_multiple_qubit_gates == -1
assert res.supermarq_features == utils.SupermarqFeatures(-1.0, -1.0, -1.0, -1.0, -1.0)


@pytest.mark.parametrize(
("search_str", "expected_val"),
Expand Down
Loading