Skip to content

Commit adaefa9

Browse files
committed
Apply coderabbit suggestions
1 parent d376268 commit adaefa9

File tree

2 files changed

+14
-25
lines changed

2 files changed

+14
-25
lines changed

src/mqt/predictor/rl/predictorenv.py

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,6 @@ def __init__(
193193
self.state: QuantumCircuit = QuantumCircuit()
194194

195195
self.error_occurred = False
196-
self._gates_in_basis_check = GatesInBasis(basis_gates=self.device.operation_names)
197-
self._check_map = CheckMap(coupling_map=self.device.build_coupling_map())
198-
self._last_qc_id: int | None = None
199-
self._last_native_mapped: tuple[bool, bool] | None = None
200196

201197
def _apply_and_update(self, action: int) -> QuantumCircuit | None:
202198
"""Apply an action, normalize the circuit, and update internal state."""
@@ -319,7 +315,15 @@ def calculate_reward(self, qc: QuantumCircuit | None = None, mode: str = "auto")
319315
elif mode == "approx":
320316
kind = "approx"
321317
else: # "auto"
322-
kind = "exact" if self._is_native_and_mapped(qc) else "approx"
318+
check_nat_gates = GatesInBasis(basis_gates=self.device.operation_names)
319+
check_nat_gates(qc)
320+
only_native = bool(check_nat_gates.property_set["all_gates_in_basis"])
321+
322+
check_mapping = CheckMap(coupling_map=self.device.build_coupling_map())
323+
check_mapping(qc)
324+
mapped = bool(check_mapping.property_set["is_swap_mapped"])
325+
326+
kind = "exact" if (only_native and mapped) else "approx"
323327

324328
if kind == "exact":
325329
if self.reward_function == "expected_fidelity":
@@ -701,21 +705,3 @@ def _get_props(name: str, qargs: tuple[int, ...]) -> InstructionProperties | Non
701705

702706
self._tbar = float(np.median(tmins)) if tmins else None
703707
self._dev_avgs_cached = True
704-
705-
def _native_and_mapped(self, qc: QuantumCircuit) -> tuple[bool, bool]:
706-
qc_id = id(qc)
707-
if qc_id == self._last_qc_id and self._last_native_mapped is not None:
708-
return self._last_native_mapped
709-
710-
self._gates_in_basis_check(qc)
711-
only_native = bool(self._gates_in_basis_check.property_set["all_gates_in_basis"])
712-
self._check_map(qc)
713-
mapped = bool(self._check_map.property_set["is_swap_mapped"])
714-
715-
self._last_qc_id = qc_id
716-
self._last_native_mapped = (only_native, mapped)
717-
return only_native, mapped
718-
719-
def _is_native_and_mapped(self, qc: QuantumCircuit) -> bool:
720-
only_native, mapped = self._native_and_mapped(qc)
721-
return only_native and mapped

tests/compilation/test_predictor_rl.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import re
1414
from pathlib import Path
15+
from typing import TYPE_CHECKING
1516

1617
import pytest
1718
from mqt.bench import BenchmarkLevel, get_benchmark
@@ -21,7 +22,6 @@
2122
from qiskit.transpiler import InstructionProperties, Target
2223
from qiskit.transpiler.passes import CheckMap, GatesInBasis
2324

24-
from mqt.predictor.reward import figure_of_merit
2525
from mqt.predictor.rl import Predictor, rl_compile
2626
from mqt.predictor.rl.actions import (
2727
CompilationOrigin,
@@ -33,6 +33,9 @@
3333
)
3434
from mqt.predictor.rl.helper import create_feature_dict, get_path_trained_model
3535

36+
if TYPE_CHECKING:
37+
from mqt.predictor.reward import figure_of_merit
38+
3639

3740
def test_predictor_env_reset_from_string() -> None:
3841
"""Test the reset function of the predictor environment with a quantum circuit given as a string as input."""
@@ -172,7 +175,7 @@ def test_approx_reward_paths_use_cached_per_gate_maps(monkeypatch: pytest.Monkey
172175
assert isinstance(predictor.env._dur_by_gate, dict) # noqa: SLF001
173176
assert len(predictor.env._err_by_gate) > 0 # noqa: SLF001
174177

175-
if figure_of_merit == "estimated_success_probability":
178+
if fom == "estimated_success_probability":
176179
assert len(predictor.env._dur_by_gate) > 0 # noqa: SLF001
177180
# tbar is optional depending on backend calibration; just sanity-check type
178181
assert predictor.env._tbar is None or predictor.env._tbar > 0.0 # noqa: SLF001

0 commit comments

Comments
 (0)