Skip to content
Open
Show file tree
Hide file tree
Changes from 102 commits
Commits
Show all changes
110 commits
Select commit Hold shift + click to select a range
129b60f
Update predictor(adding callbacks)
Mar 29, 2025
08889bd
Update
Apr 10, 2025
e2ff3fe
Restore helper.py and predictor.py to match upstream
Jul 29, 2025
1c32d15
Merge remote-tracking branch 'upstream/main'
Jul 29, 2025
78dc1aa
Implement new mapping actions
Jul 29, 2025
a3ba836
Fix: resolve pre-commit issues and add missing annotations
Jul 29, 2025
5935e6f
Fix: resolve pre-commit issues and add missing annotations
Jul 29, 2025
f71fb29
Fix: resolve pre-commit issues and add missing annotations
Jul 30, 2025
3c7592b
Fix: resolve pre-commit issues and add missing annotations
Jul 30, 2025
6db5c27
Fix mypy errors
Aug 1, 2025
47841c5
Fix mypy errors
Aug 1, 2025
b1ac8ce
Fix dependencies issues
Aug 3, 2025
5f8473c
Fix dependency issues
Aug 3, 2025
7491ec0
Add missing zip file
Aug 3, 2025
3346842
Fix issue with Python 3.13
Aug 3, 2025
6f7a73c
Merge branch 'main' into hybrid-mapping
Shaobo-Zhou Aug 3, 2025
6c67349
Remove Python 3.13 from noxfile.py due to compatibility issue
Aug 3, 2025
2692b96
Skip minimums session on Windows due to CI slowness
Aug 4, 2025
f4874e6
Fix bugs
Aug 5, 2025
54eec91
Fix bugs
Aug 5, 2025
845f7de
Use default Qiskit settings for VF2Layout and add assertion for nativ…
Shaobo-Zhou Aug 7, 2025
3418936
Debug
Shaobo-Zhou Aug 7, 2025
ae870cc
Fix missing argument
Shaobo-Zhou Aug 7, 2025
861bc62
Fix warning issues
Shaobo-Zhou Aug 7, 2025
fa989b6
Fix window runtime warning problem
Shaobo-Zhou Aug 7, 2025
405bd39
Fix window runtime warning problem
Shaobo-Zhou Aug 7, 2025
7b2f321
Add time limit for VF2PostLayout
Shaobo-Zhou Aug 7, 2025
b67d0a6
Fix windows runtime warning problem
Shaobo-Zhou Aug 7, 2025
bf7c9ee
Add new actions
Shaobo-Zhou Aug 29, 2025
6d2733f
Add new actions
Shaobo-Zhou Aug 29, 2025
878185a
Add evaluation code for baseline model
Shaobo-Zhou Sep 3, 2025
eae11a2
Set up code for testing new model
Shaobo-Zhou Sep 5, 2025
68306ec
Reset
Shaobo-Zhou Sep 5, 2025
fcba8fa
Add new actions
Shaobo-Zhou Sep 6, 2025
e5b7518
Fix dependencies
Shaobo-Zhou Sep 8, 2025
907ce2b
Fix dependencies
Shaobo-Zhou Sep 8, 2025
70dcd7d
Fix dependencies
Shaobo-Zhou Sep 8, 2025
e0364b1
Merge branch 'main' into new_structure
Shaobo-Zhou Sep 8, 2025
eb27098
🎨 pre-commit fixes
pre-commit-ci[bot] Sep 8, 2025
2688c0e
Fix dependencies
Shaobo-Zhou Sep 8, 2025
840d23d
Fix multiprocessing on Python 3.13
Shaobo-Zhou Sep 9, 2025
ac406ac
Fix timeout watcher
Shaobo-Zhou Sep 9, 2025
97c81bb
Update comments and restructure
Shaobo-Zhou Sep 15, 2025
470365d
Adjust test circuits from ALG to INDEP
Shaobo-Zhou Sep 15, 2025
caaf224
Update max synthesis size for bqskit
Shaobo-Zhou Sep 15, 2025
468da2e
Add tests for more coverage
Shaobo-Zhou Sep 17, 2025
737a9f2
Update tests
Shaobo-Zhou Sep 18, 2025
94c25ab
Update override
Shaobo-Zhou Sep 18, 2025
3ff922e
Update comments
Shaobo-Zhou Sep 18, 2025
782caf8
Update noxfile.py and CHANGELOG.md
Shaobo-Zhou Sep 22, 2025
44e0e40
Clean up venv after each session to free up space
Shaobo-Zhou Sep 23, 2025
350bae5
Clean up venv after each session to free up space
Shaobo-Zhou Sep 23, 2025
91208d1
Clean up venv after each session to free up space
Shaobo-Zhou Sep 23, 2025
622d409
Clean up venv after each session to free up space
Shaobo-Zhou Sep 23, 2025
fa008a6
Update noxfile
Shaobo-Zhou Sep 25, 2025
0c98e56
Update ibm runtime dependency
Shaobo-Zhou Sep 25, 2025
34a1c7c
Update noxfile
Shaobo-Zhou Sep 25, 2025
a8d069a
Update noxfile
Shaobo-Zhou Sep 25, 2025
114b79b
Update noxfile
Shaobo-Zhou Sep 25, 2025
aaf14b1
Fetch update from main
Shaobo-Zhou Sep 26, 2025
e39cd7e
Merge remote-tracking branch 'upstream/main' into new_structure
Shaobo-Zhou Nov 20, 2025
e7b4174
Fix ruff checks
Shaobo-Zhou Nov 20, 2025
fcab4fa
Update action space and add normalized gate counts as RL features
Shaobo-Zhou Nov 20, 2025
cb4d0fb
Fix FOM comparison logic
Shaobo-Zhou Nov 20, 2025
988a8f7
Remove 3-qubit gates from dict
Shaobo-Zhou Nov 21, 2025
9a40b3e
Add reward shaping
Shaobo-Zhou Nov 26, 2025
cee79cd
Add fallback for unsupported reward function
Shaobo-Zhou Nov 27, 2025
756d777
Minor Fixes
Shaobo-Zhou Nov 27, 2025
d60d17e
Minor Fixes
Shaobo-Zhou Nov 27, 2025
a05e37a
Merge branch 'main' into new_RL
Shaobo-Zhou Nov 27, 2025
85d4d57
Minor Fixes
Shaobo-Zhou Nov 27, 2025
a1370de
Fix predictorenv.py
Shaobo-Zhou Nov 27, 2025
e456f43
Update changelog and improve test coverage
Shaobo-Zhou Dec 3, 2025
de7c498
Update cost model
Shaobo-Zhou Dec 3, 2025
9a68c59
Merge branch 'main' into new_RL
Shaobo-Zhou Dec 3, 2025
d0a07a2
Update cost model
Shaobo-Zhou Dec 3, 2025
18cf31f
Fix CI issue
Shaobo-Zhou Dec 3, 2025
0c42b41
Improve coverage
Shaobo-Zhou Dec 3, 2025
a451710
Update src/mqt/predictor/reward.py
Shaobo-Zhou Dec 3, 2025
ac7dc28
Update src/mqt/predictor/rl/cost_model.py
Shaobo-Zhou Dec 3, 2025
67dc402
Update tests/hellinger_distance/test_estimated_hellinger_distance.py
Shaobo-Zhou Dec 3, 2025
8259b4d
Code improvements suggested by CodeRabbit
Shaobo-Zhou Dec 3, 2025
4b5bf6f
Fix warnings
Shaobo-Zhou Dec 3, 2025
14aa3ac
Improve coverage
Shaobo-Zhou Dec 4, 2025
04fde70
Improve coverage
Shaobo-Zhou Dec 4, 2025
1df8071
Update src/mqt/predictor/rl/predictorenv.py
Shaobo-Zhou Dec 4, 2025
6a104b8
Fixes
Shaobo-Zhou Dec 4, 2025
ae43600
Merge branch 'main' into new_RL
Shaobo-Zhou Dec 17, 2025
b91f4f4
Fixes
Shaobo-Zhou Dec 17, 2025
b2197b3
Fix format
Shaobo-Zhou Dec 17, 2025
331972a
Fixes
Shaobo-Zhou Dec 17, 2025
b8a80d2
Fixes
Shaobo-Zhou Dec 17, 2025
7810d4d
Update comments
Shaobo-Zhou Dec 18, 2025
8b3ecaf
Merge branch 'main' into new_RL
burgholzer Dec 25, 2025
bc49c63
✏️ curate changelog
burgholzer Dec 25, 2025
5cd1bf7
✏️ minimize unnecessary whitespace changes
burgholzer Dec 25, 2025
5a895d0
⏪ revert Windows workaround
burgholzer Dec 25, 2025
1ecb3e2
🏷️ Various typing fixes
burgholzer Dec 25, 2025
dde6f89
⏪ avoid CUDA out of memory error
burgholzer Dec 25, 2025
2e3d1c7
Update cost_model.py to QCEC style
Shaobo-Zhou Feb 4, 2026
c8b7201
Merge branch 'main' into new_RL
Shaobo-Zhou Feb 4, 2026
da3c95f
🎨 pre-commit fixes
pre-commit-ci[bot] Feb 4, 2026
56573a9
Adjusted implementation of reward approximation
Shaobo-Zhou Feb 8, 2026
e0cec3c
Coderabbit suggestions
Shaobo-Zhou Feb 8, 2026
a41b323
Resolve problem with estimated Hellinger distance
Shaobo-Zhou Feb 8, 2026
37c55a8
Merge branch 'main' into new_RL
Shaobo-Zhou Feb 8, 2026
734df4e
Add test coverage
Shaobo-Zhou Feb 8, 2026
d376268
Apply coderabbit suggestions
Shaobo-Zhou Feb 8, 2026
adaefa9
Apply coderabbit suggestions
Shaobo-Zhou Feb 8, 2026
7a0c40b
Apply coderabbit suggestions
Shaobo-Zhou Feb 8, 2026
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ This project adheres to [Semantic Versioning], with the exception that minor rel

### Changed

- ✨ Improve RL reward design by adding intermediate rewards ([#526]) ([**@Shaobo-Zhou**])
- 🔧 Replace `mypy` with `ty` ([#572]) ([**@denialhaag**])
- 🐛 Fix instruction duration unit in estimated success probability calculation ([#445]) ([**@Shaobo-Zhou**])

### Removed

- ✨ Remove support for custom names of trained models ([#489]) ([**@bachase**])
- 🔥 Drop support for x86 macOS systems ([#421]) ([**@denialhaag**])

Expand Down Expand Up @@ -47,6 +51,7 @@ _📚 Refer to the [GitHub Release Notes](https://github.com/munich-quantum-tool
<!-- PR links -->

[#572]: https://github.com/munich-quantum-toolkit/predictor/pull/572
[#526]: https://github.com/munich-quantum-toolkit/predictor/pull/526
[#489]: https://github.com/munich-quantum-toolkit/predictor/pull/489
[#445]: https://github.com/munich-quantum-toolkit/predictor/pull/445
[#421]: https://github.com/munich-quantum-toolkit/predictor/pull/421
Expand Down
1 change: 0 additions & 1 deletion src/mqt/predictor/ml/predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,6 @@ def train_random_forest_model(
training_data = self._get_prepared_training_data()
num_cv = min(len(training_data.y_train), 5)
mdl = GridSearchCV(mdl, tree_param, cv=num_cv, n_jobs=8).fit(training_data.X_train, training_data.y_train)

joblib_dump(mdl, save_mdl_path)
logger.info("Random Forest model is trained and saved.")

Expand Down
2 changes: 1 addition & 1 deletion src/mqt/predictor/reward.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def estimated_success_probability(qc: QuantumCircuit, device: Target, precision:
if first_qubit_idx not in active_qubits:
continue

dt = device.dt # instruction durations are stored in unit dt
dt = device.dt or 1.0 # discrete time unit; fallback to 1.0 if unavailable
res *= np.exp(
-instruction.duration
* dt
Expand Down
3 changes: 2 additions & 1 deletion src/mqt/predictor/rl/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@

from bqskit import Circuit
from pytket._tket.passes import BasePass as tket_BasePass
from qiskit.passmanager import PropertySet
from qiskit.transpiler.basepasses import BasePass as qiskit_BasePass


Expand Down Expand Up @@ -143,7 +144,7 @@ class DeviceDependentAction(Action):
Callable[..., tuple[Any, ...] | Circuit],
]
)
do_while: Callable[[dict[str, Circuit]], bool] | None = None
do_while: Callable[[PropertySet], bool] | None = None


# Registry of actions
Expand Down
280 changes: 280 additions & 0 deletions src/mqt/predictor/rl/cost_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
# Copyright (c) 2023 - 2026 Chair for Design Automation, TUM
# Copyright (c) 2025 - 2026 Munich Quantum Software Company GmbH
# All rights reserved.
#
# SPDX-License-Identifier: MIT
#
# Licensed under the MIT License

"""Helper functions for approximating transformations to device-native gates.

This module provides a dynamic canonical gate cost model and approximate
fidelity/ESP estimates based on averaged 1q/2q error rates.

For each backend, a cost table of gate decompositions into the native gate set
is generated programmatically (and cached). This avoids rigid hard-coding of
costs. If a backend is unknown, a default basis (IBM Qiskit basis) is used as
a fallback with a warning, or users can extend the known device basis list.
"""

from __future__ import annotations

import logging
import warnings
from typing import cast

import numpy as np

# Attempt to import Qiskit for transpilation
from qiskit import QuantumCircuit, transpile

logger = logging.getLogger(__name__)

CanonicalCostTable = dict[str, tuple[int, int]]

# Cache for generated cost tables
DEVICE_COST_CACHE: dict[str, dict[str, tuple[int, int]]] = {}

# Pre-defined native gate sets for known devices (can be extended)
KNOWN_DEVICE_BASES: dict[str, list[str]] = {
"ibm_torino": [
"id",
"rz",
"rx",
"sx",
"x",
"cz",
"rzz",
], # IBM device example (native 1q: id/rz/rx/sx/x, 2q: cz, rzz)
"ankaa_3": ["id", "rz", "rx", "iswap"], # Rigetti Ankaa-3 (native 1q: rx, rz; native 2q: iSWAP)
"emerald": [
"id",
"rz",
"rx",
"cz",
"u",
], # IQM Emerald (native 1q: arbitrary single-qubit rotation 'u'; native 2q: cz)
# Additional devices can be added here...
}

# Heuristic set of known two-qubit basis gate names used to map averages to per-basis values.
# This is intentionally conservative; device-specific basis sets should be used when available.
TWO_Q_GATES: set[str] = {
"cx",
"cz",
"iswap",
"rzz",
"rxx",
"ryy",
"rzx",
"dcx",
"ecr",
"swap",
}


def build_error_rates_from_averages(device_id: str, p1_avg: float, p2_avg: float) -> dict[str, float]:
"""Construct a per-basis error rate mapping from averaged 1q/2q values.

This uses a simple heuristic to decide whether a basis gate is 1q or 2q.
"""
basis_gates = KNOWN_DEVICE_BASES.get(device_id, ["id", "rz", "sx", "x", "cx"])
error_rates: dict[str, float] = {}
for g in basis_gates:
error_rates[g] = p2_avg if g in TWO_Q_GATES else p1_avg
return error_rates


def build_gate_durations_from_averages(device_id: str, tau1_avg: float, tau2_avg: float) -> dict[str, float]:
"""Construct a per-basis gate duration mapping from averaged 1q/2q durations."""
basis_gates = KNOWN_DEVICE_BASES.get(device_id, ["id", "rz", "sx", "x", "cx"])
durations: dict[str, float] = {}
for g in basis_gates:
durations[g] = tau2_avg if g in TWO_Q_GATES else tau1_avg
return durations


def generate_cost_table(device_id: str) -> dict[str, tuple[int, int]]:
"""Generate a canonical gate cost table for the given device_id.

This function programmatically derives the (n_1q, n_2q) costs for common gates
by decomposing them into the device's native gate set via Qiskit transpilation.
If the device_id is not recognized in KNOWN_DEVICE_BASES, a generic basis
is assumed (using IBM's basis as a fallback) and a warning is emitted.
"""
if transpile is None or QuantumCircuit is None:
msg = "Qiskit is required to generate cost tables dynamically."
raise ImportError(msg)

# Determine the basis gates for this device
basis_gates = KNOWN_DEVICE_BASES.get(device_id)
if basis_gates is None:
warnings.warn(
f"No native gate-set defined for device '{device_id}'. "
"Generating cost table using a minimal universal basis (Qiskit default). "
"Results may be inaccurate. Consider specifying the gate set in KNOWN_DEVICE_BASES.",
UserWarning,
stacklevel=2,
)
logger.warning(f"No basis for device '{device_id}', using minimal universal basis for cost generation.")
# Default to minimal universal basis (Qiskit default)
basis_gates = ["id", "rz", "sx", "x", "cx"]

cost_table: dict[str, tuple[int, int]] = {}

# Structured gate definitions for dynamic profiling
gate_profiles = [
# Single-qubit gates (no params)
{
"gates": ["id", "x", "y", "z", "h", "s", "sdg", "t", "tdg", "sx", "sxdg", "u0"],
"qubits": 1,
"params": 0,
"controls": 0,
},
# Single-qubit gates (1 param)
{"gates": ["p", "rx", "ry", "rz", "u1", "r"], "qubits": 1, "params": 1, "controls": 0},
# Single-qubit gates (2 params)
{"gates": ["u2"], "qubits": 1, "params": 2, "controls": 0},
# Single-qubit gates (3 params)
{"gates": ["u", "u3"], "qubits": 1, "params": 3, "controls": 0},
# Two-qubit gates (no params)
{
"gates": ["cx", "cy", "cz", "ch", "csx", "swap", "iswap", "dcx", "ecr"],
"qubits": 2,
"params": 0,
"controls": 0,
},
# Two-qubit gates (1 param)
{"gates": ["rxx", "ryy", "rzz", "rzx", "cu1", "cp"], "qubits": 2, "params": 1, "controls": 0},
# Controlled single-qubit gates (1 param)
{"gates": ["crx", "cry", "crz"], "qubits": 1, "params": 1, "controls": 1},
# Controlled U3 (3 params)
{"gates": ["cu3", "cu"], "qubits": 1, "params": 3, "controls": 1},
# Multi-qubit gates (no params)
{"gates": ["ccx", "cswap", "rccx", "rc3x", "c3x", "c3sqrtx", "c4x"], "qubits": 1, "params": 0, "controls": 2},
]

def add_gate_to_cost_table(gate: str, qubits: int, params: int, controls: int) -> None:
total_qubits = qubits + controls
qc = QuantumCircuit(total_qubits)
try:
gate_name = "c" * controls + gate if controls > 0 else gate
if params == 0:
getattr(qc, gate_name)(*list(range(total_qubits)))
else:
param_values = list(range(1, params + 1))
getattr(qc, gate_name)(*param_values, *list(range(total_qubits)))
except Exception:
return # skip if not available
# For reference: store the transpiled circuit size (total basis gate count)
qc_trans = transpile(qc, basis_gates=basis_gates, optimization_level=1, seed_transpiler=42)
cost_table[gate if controls == 0 else ("c" * controls) + gate] = (qc_trans.size(), 0)

for profile in gate_profiles:
gates = cast("list[str]", profile["gates"])
qubits = int(cast("int", profile["qubits"]))
params = int(cast("int", profile.get("params", 0)))
controls = int(cast("int", profile.get("controls", 0)))
for gate in gates:
add_gate_to_cost_table(gate, qubits, params, controls)

# Ensure 'id' is treated as no-op (if not already in table due to optimization removal)
cost_table["id"] = (0, 0)

return cost_table


def get_cost_table(device_id: str) -> CanonicalCostTable:
"""Return the canonical cost table for `device_id`, generating it if necessary.

If the device is unknown (not predefined), the cost table is generated using a
default basis and a warning is emitted to indicate a potential inaccuracy.
The result is cached to avoid repeated computation.
"""
if device_id not in DEVICE_COST_CACHE:
# Generate and cache the cost table for this device
DEVICE_COST_CACHE[device_id] = generate_cost_table(device_id)
return DEVICE_COST_CACHE[device_id]


# --- Helper: Estimate basis gate counts in a transpiled circuit ---
def estimate_basis_gate_counts(qc: QuantumCircuit, *, basis_gates: list[str]) -> dict[str, int]:
"""Estimate the count of each basis gate in the transpiled circuit."""
qc_trans = transpile(qc, basis_gates=basis_gates, optimization_level=1, seed_transpiler=42)
gate_counts = dict.fromkeys(basis_gates, 0)
for instr, _, _ in qc_trans.data:
name = instr.name
if name in gate_counts:
gate_counts[name] += 1
return gate_counts


def approx_expected_fidelity(
qc: QuantumCircuit,
error_rates: dict[str, float],
*,
device_id: str = "ibm_torino",
) -> float:
"""Estimate expected fidelity using per-basis-gate error rates.

Args:
qc: QuantumCircuit to analyze
error_rates: dict mapping basis gate name to error rate (e.g., {"cx": 0.01, "rz": 0.001, ...})
device_id: device identifier for basis gates
Returns:
Estimated total fidelity as a float in [0, 1]
"""
basis_gates = KNOWN_DEVICE_BASES.get(device_id, ["id", "rz", "sx", "x", "cx"])
gate_counts = estimate_basis_gate_counts(qc, basis_gates=basis_gates)
fidelity = 1.0
for gate, count in gate_counts.items():
p = error_rates.get(gate, 0.0)
fidelity *= (1.0 - p) ** count
return float(max(min(fidelity, 1.0), 0.0))


def approx_estimated_success_probability(
qc: QuantumCircuit,
error_rates: dict[str, float],
gate_durations: dict[str, float],
tbar: float | None,
par_feature: float,
liv_feature: float,
n_qubits: int,
*,
device_id: str = "ibm_torino",
) -> float:
"""Estimate the Estimated Success Probability (ESP) using per-basis-gate error rates and durations.

Args:
qc: QuantumCircuit to analyze
error_rates: dict mapping basis gate name to error rate
gate_durations: dict mapping basis gate name to average duration (in same units as tbar)
tbar: average T1/T2 time (decoherence time)
par_feature: parallelism feature (0=serial, 1=fully parallel)
liv_feature: liveness feature (fraction of time qubits are active)
n_qubits: number of qubits in the circuit
device_id: device identifier for basis gates
Returns:
Estimated ESP as a float in [0, 1]
"""
basis_gates = KNOWN_DEVICE_BASES.get(device_id, ["id", "rz", "sx", "x", "cx"])
gate_counts = estimate_basis_gate_counts(qc, basis_gates=basis_gates)
# Fidelity from gate operations
f_gate = 1.0
for gate, count in gate_counts.items():
p = error_rates.get(gate, 0.0)
f_gate *= (1.0 - p) ** count

# Estimate effective circuit duration based on parallelism
n_q = max(n_qubits, 1)
k_eff = 1.0 + (n_q - 1.0) * float(par_feature)
# Total gate time: sum over all basis gates
total_gate_time = sum(gate_counts[g] * gate_durations.get(g, 0.0) for g in basis_gates) / k_eff

# Idle time penalty factor based on liveness
idle_fraction = max(0.0, 1.0 - float(liv_feature))
idle_factor = 1.0 if tbar is None or tbar <= 0.0 else float(np.exp(-(total_gate_time * idle_fraction) / tbar))

esp = f_gate * idle_factor
return float(max(min(esp, 1.0), 0.0))
Loading
Loading