Skip to content

Commit

Permalink
Add eval and cell events (#2004)
Browse files Browse the repository at this point in the history
  • Loading branch information
idavis authored Nov 5, 2024
1 parent d1c01ab commit b85a22a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
12 changes: 11 additions & 1 deletion pip/qsharp/_ipython.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
execution within Jupyter notebooks.
"""

from time import monotonic
from IPython.display import display, Javascript, clear_output
from IPython.core.magic import register_cell_magic
from ._native import QSharpError
from ._qsharp import get_interpreter
from . import telemetry_events
import pathlib


Expand All @@ -30,8 +32,16 @@ def callback(output):
# is finished executing.
display(display_id=True)

telemetry_events.on_run_cell()
start_time = monotonic()

try:
return get_interpreter().interpret(cell, callback)
results = get_interpreter().interpret(cell, callback)

durationMs = (monotonic() - start_time) * 1000
telemetry_events.on_run_cell_end(durationMs)

return results
except QSharpError as e:
# pylint: disable=raise-missing-from
raise QSharpCellError(str(e))
Expand Down
13 changes: 11 additions & 2 deletions pip/qsharp/_qsharp.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def init(
_config = Config(target_profile, language_features, manifest_contents, project_root)
# Return the configuration information to provide a hint to the
# language service through the cell output.
return _config
return _config


def get_interpreter() -> Interpreter:
Expand Down Expand Up @@ -243,7 +243,15 @@ def callback(output: Output) -> None:
pass
print(output, flush=True)

return get_interpreter().interpret(source, callback)
telemetry_events.on_eval()
start_time = monotonic()

results = get_interpreter().interpret(source, callback)

durationMs = (monotonic() - start_time) * 1000
telemetry_events.on_eval_end(durationMs)

return results


class ShotResult(TypedDict):
Expand Down Expand Up @@ -387,6 +395,7 @@ def compile(entry_expr: str) -> QirInputData:
telemetry_events.on_compile_end(durationMs, target_profile)
return res


def circuit(
entry_expr: Optional[str] = None, *, operation: Optional[str] = None
) -> Circuit:
Expand Down
30 changes: 30 additions & 0 deletions pip/qsharp/telemetry_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,36 @@ def on_run_end(durationMs: float, shots: int) -> None:
)


def on_eval() -> None:
log_telemetry(
"qsharp.eval",
1,
)


def on_eval_end(durationMs: float) -> None:
log_telemetry(
"qsharp.eval.durationMs",
durationMs,
type="histogram",
)


def on_run_cell() -> None:
log_telemetry(
"qsharp.run.cell",
1,
)


def on_run_cell_end(durationMs: float) -> None:
log_telemetry(
"qsharp.run.cell.durationMs",
durationMs,
type="histogram",
)


def on_compile(profile: str) -> None:
log_telemetry("qsharp.compile", 1, properties={"profile": profile})

Expand Down

0 comments on commit b85a22a

Please sign in to comment.