Skip to content

Commit

Permalink
🎨 improved RL Predictor qcompile method signature (#176)
Browse files Browse the repository at this point in the history
This PR improves the method signature of
`mqt.predictor.rl.helper.qcompile`.
  • Loading branch information
nquetschlich authored Dec 14, 2023
1 parent 3f1a4b4 commit b1d52a3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/mqt/predictor/rl/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import importlib_metadata as metadata
import importlib_resources as resources


logger = logging.getLogger("mqt-predictor")


Expand All @@ -83,8 +84,8 @@

def qcompile(
qc: QuantumCircuit | str,
figure_of_merit: reward.figure_of_merit = "expected_fidelity",
device_name: str = "ibm_washington",
figure_of_merit: reward.figure_of_merit | None = "expected_fidelity",
device_name: str | None = "ibm_washington",
predictor_singleton: rl.Predictor | None = None,
) -> tuple[QuantumCircuit, list[str]]:
"""Compiles a given quantum circuit to a device optimizing for the given figure of merit.
Expand All @@ -93,13 +94,19 @@ def qcompile(
qc (QuantumCircuit | str): The quantum circuit to be compiled. If a string is given, it is assumed to be a path to a qasm file.
figure_of_merit (reward.reward_functions, optional): The figure of merit to be used for compilation. Defaults to "expected_fidelity".
device_name (str, optional): The name of the device to compile to. Defaults to "ibm_washington".
predictor_singleton (rl.Predictor, optional): A predictor object that is used for compilation. If None, a new predictor object is created. Defaults to None.
predictor_singleton (rl.Predictor, optional): A predictor object that is used for compilation to reduce compilation time when compiling multiple quantum circuits. If None, a new predictor object is created. Defaults to None.
Returns:
tuple[QuantumCircuit, list[str]] | bool: Returns a tuple containing the compiled quantum circuit and the compilation information. If compilation fails, False is returned.
"""

if predictor_singleton is None:
if figure_of_merit is None:
msg = "figure_of_merit must not be None if predictor_singleton is None."
raise ValueError(msg)
if device_name is None:
msg = "device_name must not be None if predictor_singleton is None."
raise ValueError(msg)
predictor = rl.Predictor(figure_of_merit=figure_of_merit, device_name=device_name)
else:
predictor = predictor_singleton
Expand Down
8 changes: 8 additions & 0 deletions tests/compilation/test_predictor_rl.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,11 @@ def test_qcompile_with_newly_trained_models(figure_of_merit: reward.figure_of_me

assert isinstance(qc_compiled, QuantumCircuit)
assert compilation_information is not None


def test_qcompile_with_false_input() -> None:
qc = get_benchmark("dj", 1, 5)
with pytest.raises(ValueError, match="figure_of_merit must not be None if predictor_singleton is None."):
rl.helper.qcompile(qc, None, "ibm_washington")
with pytest.raises(ValueError, match="device_name must not be None if predictor_singleton is None."):
rl.helper.qcompile(qc, "expected_fidelity", None)

0 comments on commit b1d52a3

Please sign in to comment.