Skip to content

Commit

Permalink
Move time_conversion to utils
Browse files Browse the repository at this point in the history
Since it's rather a independent method and it's used in two different places, it makes sense to move it out of Accelerator.
  • Loading branch information
Ectras committed Mar 25, 2024
1 parent 3f5cc74 commit 950e028
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 37 deletions.
38 changes: 2 additions & 36 deletions src/provider/accelerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/scheduling/common/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
36 changes: 36 additions & 0 deletions src/utils/helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Helper functions and classes."""

from time import perf_counter


Expand All @@ -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

0 comments on commit 950e028

Please sign in to comment.