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

✅ Improved Test Coverage #175

Merged
merged 10 commits into from
Dec 13, 2023
6 changes: 2 additions & 4 deletions src/mqt/predictor/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
logger = logging.getLogger("mqt-predictor")


def create_qiskit_result(qc: QuantumCircuit, device: dict[str, Any] | None = None) -> Result:
def create_qiskit_result(qc: QuantumCircuit, device: dict[str, Any]) -> Result:
"""Creates a Result object for a given benchmark and device using qiskit for compilation.

Args:
Expand All @@ -35,7 +35,6 @@ def create_qiskit_result(qc: QuantumCircuit, device: dict[str, Any] | None = Non
Returns:
Result: Returns a Result object containing the compiled quantum circuit.
"""
assert device is not None
if qc.num_qubits > device["max_qubits"]:
return Result("qiskit_", -1, None, device["name"])
start_time = time.time()
Expand All @@ -56,7 +55,7 @@ def create_qiskit_result(qc: QuantumCircuit, device: dict[str, Any] | None = Non

def create_tket_result(
qc: QuantumCircuit,
device: dict[str, Any] | None = None,
device: dict[str, Any],
) -> Result:
"""Creates a Result object for a given benchmark and device using tket for compilation.

Expand All @@ -67,7 +66,6 @@ def create_tket_result(
Returns:
Result: Returns a Result object containing the compiled quantum circuit.
"""
assert device is not None
if qc.num_qubits > device["max_qubits"]:
return Result("tket_" + device["name"], -1, None, device["name"])
tket_qc = qiskit_to_tk(qc)
Expand Down
2 changes: 1 addition & 1 deletion src/mqt/predictor/ml/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def save_training_data(

def load_training_data(
figure_of_merit: reward.figure_of_merit = "expected_fidelity",
) -> tuple[NDArray[np.float_], list[str], list[NDArray[np.float_]]]:
) -> tuple[list[NDArray[np.float_]], list[str], list[NDArray[np.float_]]]:
"""Loads and returns the training data from the training data folder.

Args:
Expand Down
8 changes: 8 additions & 0 deletions tests/compilation/test_helper_rl.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ def test_get_action_terminate() -> None:
def test_get_actions_devices() -> None:
assert len(rl.helper.get_devices()) == rl.helper.NUM_ACTIONS_DEVICES

with pytest.raises(RuntimeError):
rl.helper.get_device("false_input")


def test_get_device_index_of_device_false_input() -> None:
with pytest.raises(RuntimeError):
rl.helper.get_device_index_of_device("false_input")


def test_get_actions_mapping() -> None:
assert len(rl.helper.get_actions_mapping()) == rl.helper.NUM_ACTIONS_MAPPING
Expand Down
19 changes: 19 additions & 0 deletions tests/device_selection/test_helper_ml.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import pytest

from mqt.bench import benchmark_generator
from mqt.predictor import ml, qcompile

Expand All @@ -20,12 +22,23 @@ def test_get_index_to_device_LUT() -> None:
def test_load_training_data() -> None:
assert ml.helper.load_training_data() is not None

with pytest.raises(FileNotFoundError, match="Training data not found. Please run the training script first."):
ml.helper.load_training_data("false_input") # type: ignore[arg-type]


def test_save_training_data() -> None:
training_data, names_list, scores_list = ml.helper.load_training_data()
ml.helper.save_training_data(training_data, names_list, scores_list, "expected_fidelity")


def test_create_feature_dict() -> None:
qc = benchmark_generator.get_benchmark("dj", 1, 3)
feature_vector = ml.helper.create_feature_dict(qc)
assert feature_vector is not None

with pytest.raises(ValueError, match="Invalid input for 'qc' parameter."):
ml.helper.create_feature_dict("false_input")


def test_get_openqasm_gates() -> None:
assert ml.helper.get_openqasm_gates() is not None
Expand Down Expand Up @@ -66,3 +79,9 @@ def test_qcompile() -> None:
assert quantum_device in ml.helper.get_index_to_device_LUT().values()
assert qc_compiled.layout is not None
assert len(qc_compiled) > 0


def test_get_path_results() -> None:
for get_ghz_path_results in (True, False):
path = ml.helper.get_path_results(ghz_results=get_ghz_path_results)
assert path.exists()
20 changes: 18 additions & 2 deletions tests/test_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

from pathlib import Path

from qiskit import QuantumCircuit

from mqt.bench import get_benchmark
from mqt.predictor import rl
from mqt.predictor.evaluation import evaluate_sample_circuit
from mqt.predictor import Result, rl
from mqt.predictor.evaluation import create_qiskit_result, create_tket_result, evaluate_sample_circuit


def test_evaluate_sample_circuit() -> None:
Expand All @@ -25,3 +27,17 @@ def test_evaluate_sample_circuit() -> None:
assert all(key in res for key in expected_keys)
if Path(filename).exists():
Path(filename).unlink()


def test_false_input() -> None:
res = create_tket_result(QuantumCircuit(1000), rl.helper.get_devices()[0])
assert isinstance(res, Result)
assert res.compilation_time == -1.0
assert res.fidelity == -1.0
assert res.critical_depth == -1.0

res = create_qiskit_result(QuantumCircuit(1000), rl.helper.get_devices()[0])
assert isinstance(res, Result)
assert res.compilation_time == -1.0
assert res.fidelity == -1.0
assert res.critical_depth == -1.0
10 changes: 10 additions & 0 deletions tests/test_result.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from __future__ import annotations

from mqt.predictor import Result


def test_result_none_input() -> None:
res = Result("test", 1.0, None, "test_device")
assert res.compilation_time == 1.0
assert res.fidelity == -1.0
assert res.critical_depth == -1.0
17 changes: 17 additions & 0 deletions tests/test_reward.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from __future__ import annotations

import pytest
from qiskit import QuantumCircuit

from mqt.predictor import reward


def test__false_inputs() -> None:
with pytest.raises(ValueError, match="Device not supported"):
reward.expected_fidelity(QuantumCircuit(), "test_device")

with pytest.raises(ValueError, match="Device not supported"):
reward.calc_expected_fidelity_ibm(QuantumCircuit(), "test_device")

with pytest.raises(ValueError, match="Device not supported"):
reward.calc_expected_fidelity_ionq(QuantumCircuit(), "test_device")
Loading