Skip to content

Commit

Permalink
🎨 pre-commit fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pre-commit-ci[bot] committed Apr 20, 2024
1 parent 7d0935f commit d423dbe
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/mqt/bench/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def parse_benchmark_name_and_instance(algorithm: str) -> tuple[str, str | None]:
as expected by :func:`get_benchmark`.
"""

if algorithm.startswith("shor_") or algorithm.startswith("groundstate_"):
if algorithm.startswith(("shor_", "groundstate_")):
as_list = algorithm.split("_", 2)
assert len(as_list) == 2
return cast(tuple[str, str], tuple(as_list))
Expand Down
158 changes: 107 additions & 51 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,94 @@
from mqt.bench import get_benchmark, CompilerSettings, QiskitSettings
from pytest_console_scripts import ScriptRunner
from __future__ import annotations

from typing import TYPE_CHECKING

import pytest
from qiskit.qasm2 import dumps

from mqt.bench import CompilerSettings, QiskitSettings, get_benchmark

if TYPE_CHECKING:
from pytest_console_scripts import ScriptRunner


@pytest.mark.parametrize(
("args", "expected_output"),
[
([
"--level", "alg",
"--algorithm", "ghz",
"--num-qubits", "10",
], dumps(get_benchmark(level="alg", benchmark_name="ghz", circuit_size=10))),
([
"--level", "alg",
"--algorithm", "shor_xsmall",
"--num-qubits", "10",
], "OPENQASM 2.0;"), # Note: shor is non-deterministic, so just a basic sanity check
([
"--level", "alg",
"--algorithm", "ghz",
"--num-qubits", "20",
], dumps(get_benchmark(level="alg", benchmark_name="ghz", circuit_size=20))),
([
"--level", "indep",
"--algorithm", "ghz",
"--num-qubits", "20",
"--compiler", "qiskit",
], dumps(get_benchmark(level="indep", benchmark_name="ghz", circuit_size=20, compiler="qiskit"))),
([
"--level", "mapped",
"--algorithm", "ghz",
"--num-qubits", "20",
"--compiler", "qiskit",
"--qiskit-optimization-level", "2",
"--native-gate-set", "ibm",
"--device", "ibm_montreal",
], dumps(get_benchmark(
level="mapped",
benchmark_name="ghz",
circuit_size=20,
compiler="qiskit",
compiler_settings=CompilerSettings(QiskitSettings(optimization_level=2)),
provider_name="ibm",
device_name="ibm_montreal",
))),
(
[
"--level",
"alg",
"--algorithm",
"ghz",
"--num-qubits",
"10",
],
dumps(get_benchmark(level="alg", benchmark_name="ghz", circuit_size=10)),
),
(
[
"--level",
"alg",
"--algorithm",
"shor_xsmall",
"--num-qubits",
"10",
],
"OPENQASM 2.0;",
), # Note: shor is non-deterministic, so just a basic sanity check
(
[
"--level",
"alg",
"--algorithm",
"ghz",
"--num-qubits",
"20",
],
dumps(get_benchmark(level="alg", benchmark_name="ghz", circuit_size=20)),
),
(
[
"--level",
"indep",
"--algorithm",
"ghz",
"--num-qubits",
"20",
"--compiler",
"qiskit",
],
dumps(get_benchmark(level="indep", benchmark_name="ghz", circuit_size=20, compiler="qiskit")),
),
(
[
"--level",
"mapped",
"--algorithm",
"ghz",
"--num-qubits",
"20",
"--compiler",
"qiskit",
"--qiskit-optimization-level",
"2",
"--native-gate-set",
"ibm",
"--device",
"ibm_montreal",
],
dumps(
get_benchmark(
level="mapped",
benchmark_name="ghz",
circuit_size=20,
compiler="qiskit",
compiler_settings=CompilerSettings(QiskitSettings(optimization_level=2)),
provider_name="ibm",
device_name="ibm_montreal",
)
),
),
],
)
def test_cli(args: list[str], expected_output: str, script_runner: ScriptRunner) -> None:
Expand All @@ -60,17 +104,29 @@ def test_cli(args: list[str], expected_output: str, script_runner: ScriptRunner)
(["asd"], "usage: mqt.bench.cli"),
(["--benchmark", "ae"], "usage: mqt.bench.cli"),
# Note: We don't care about the actual error messages in most cases
([
"--level", "indep",
"--algorithm", "ghz",
"--num-qubits", "20",
# Missing compiler option
], ""),
([
"--level", "alg",
"--algorithm", "not-a-valid-benchmark",
"--num-qubits", "20",
], ""),
(
[
"--level",
"indep",
"--algorithm",
"ghz",
"--num-qubits",
"20",
# Missing compiler option
],
"",
),
(
[
"--level",
"alg",
"--algorithm",
"not-a-valid-benchmark",
"--num-qubits",
"20",
],
"",
),
],
)
def test_cli_errors(args: list[str], expected_output: str, script_runner: ScriptRunner) -> None:
Expand Down

0 comments on commit d423dbe

Please sign in to comment.