diff --git a/src/provider/accelerator.py b/src/provider/accelerator.py index 7e5202c..ded006d 100644 --- a/src/provider/accelerator.py +++ b/src/provider/accelerator.py @@ -10,6 +10,7 @@ from src.common import IBMQBackend, CombinedJob from src.resource_estimation import estimate_runtime, estimate_noise from src.tools import optimize_circuit_online, generate_subcircuit +from utils.helpers import time_conversion class Accelerator: @@ -26,41 +27,6 @@ def __init__( self._uuid = uuid4() self.queue: deque[CombinedJob] = deque([]) - @staticmethod - def time_conversion( - time: float, unit: str, target_unit: str = "us", dt: float | None = None - ) -> float: - """Converts a time from one unit to another. - - Args: - time (float): The time to convert. - unit (str): The unit of the time. - target_unit (str, optional): The target unit. Defaults to "us". - dt (float | None, optional): The duration in seconds of the device-dependent - time. Must be set if unit is in dt but target isn't. Defaults to None. - - Returns: - float: _description_ - """ - if unit == target_unit: - return time - - units = ["s", "ms", "us", "ns", "ps"] - - # target_unit must be a SI unit - assert target_unit in units - - # Convert dt (device-dependent time) to SI unit - if unit == "dt": - assert dt is not None - time *= dt - unit = "s" - - target_shift = units.index(target_unit) - current_shift = units.index(unit) - required_shift = 3 * (target_shift - current_shift) - return time * 10**required_shift - def compute_processing_time(self, circuit: QuantumCircuit) -> float: """Computes the processing time for the circuit for a single shot. @@ -75,7 +41,7 @@ def compute_processing_time(self, circuit: QuantumCircuit) -> float: time_in_ns = estimate_runtime(circuit) logging.debug("Done.") - return Accelerator.time_conversion(time_in_ns, "ns", target_unit="us") + return time_conversion(time_in_ns, "ns", target_unit="us") def compute_setup_time( self, circuit_from: QuantumCircuit | None, circuit_to: QuantumCircuit | None diff --git a/src/scheduling/common/convert.py b/src/scheduling/common/convert.py index b3dd7da..6736a4b 100644 --- a/src/scheduling/common/convert.py +++ b/src/scheduling/common/convert.py @@ -7,6 +7,7 @@ from src.common import UserCircuit from src.provider import Accelerator from src.resource_estimation import estimate_runtime, predict_device +from utils.helpers import time_conversion from .partition import cut_proxies from .types import CircuitProxy @@ -59,7 +60,7 @@ def convert_to_proxy( if isinstance(circuit, UserCircuit): tmp_circuit = circuit.circuit processing_time = estimate_runtime(tmp_circuit) - processing_time = Accelerator.time_conversion( + processing_time = time_conversion( processing_time, "ns", target_unit="us" ) noise = max( diff --git a/src/utils/helpers.py b/src/utils/helpers.py index 170b0aa..0e7080d 100644 --- a/src/utils/helpers.py +++ b/src/utils/helpers.py @@ -1,4 +1,5 @@ """Helper functions and classes.""" + from time import perf_counter @@ -17,3 +18,38 @@ def __enter__(self): def __exit__(self, typ, value, traceback): self._end = perf_counter() self.elapsed = self._end - self._start + + +def time_conversion( + time: float, unit: str, target_unit: str = "us", dt: float | None = None +) -> float: + """Converts a time from one unit to another. + + Args: + time (float): The time to convert. + unit (str): The unit of the time. + target_unit (str, optional): The target unit. Defaults to "us". + dt (float | None, optional): The duration in seconds of the device-dependent + time. Must be set if unit is in dt but target isn't. Defaults to None. + + Returns: + float: The time in the target unit. + """ + if unit == target_unit: + return time + + units = ["s", "ms", "us", "ns", "ps"] + + # target_unit must be a SI unit + assert target_unit in units + + # Convert dt (device-dependent time) to SI unit + if unit == "dt": + assert dt is not None + time *= dt + unit = "s" + + target_shift = units.index(target_unit) + current_shift = units.index(unit) + required_shift = 3 * (target_shift - current_shift) + return time * 10**required_shift