From e21e25d890fba7a8fe06a455efb9bc5c97d385f3 Mon Sep 17 00:00:00 2001 From: Sajid Kakroo Date: Mon, 9 Nov 2026 10:34:50 +0000 Subject: [PATCH 1/6] qnexus runtime traces tests --- integration/test_runtime_traces.py | 118 +++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 integration/test_runtime_traces.py diff --git a/integration/test_runtime_traces.py b/integration/test_runtime_traces.py new file mode 100644 index 0000000..64c7bd2 --- /dev/null +++ b/integration/test_runtime_traces.py @@ -0,0 +1,118 @@ +"""Tests for downloading runtime traces of jobs run on Helios emulators.""" + +import time +from typing import Callable, ContextManager +from uuid import uuid4 + +import pytest +from constants import JOB_TIMEOUT +from hugr.package import Package +from pytket.circuit import Circuit +from quantinuum_schemas.models.backend_config import HeliosEmulatorConfig +from selene_core.trace import Trace +from test_qir import make_qir_bitcode_from_file + +import qnexus as qnx +import qnexus.exceptions as qnx_exc +from qnexus.client import get_nexus_client +from qnexus.models.references import ( + ExecuteJobRef, + HUGRRef, + ProjectRef, + QIRRef, + RuntimeTracesRef, +) + +N_SHOTS = 1 +TRACE_TIMEOUT_SECONDS = 180 +TRACE_POLL_SECONDS = 10 + +pytestmark = pytest.mark.skip(reason="Runtime traces changes are not in QA env yet.") + +def wait_for_runtime_traces(job_ref: ExecuteJobRef) -> list[RuntimeTracesRef]: + """Traces are ingested after the job completes, so poll until the job has trace refs.""" + deadline = time.monotonic() + TRACE_TIMEOUT_SECONDS + while time.monotonic() < deadline: + runtime_traces = qnx.jobs.runtime_traces(job_ref) + if runtime_traces: + return runtime_traces + time.sleep(TRACE_POLL_SECONDS) + raise AssertionError( + f"No runtime traces for job {job_ref.id} after {TRACE_TIMEOUT_SECONDS}s" + ) + + +def helios_config() -> qnx.HeliosConfig: + return qnx.HeliosConfig( + system_name="Helios-1E", emulator_config=HeliosEmulatorConfig() + ) + + +def assert_trace_is_valid(trace: Trace) -> None: + """The trace has events, and every event has a source and a kind.""" + assert isinstance(trace, Trace) + assert len(trace.events) > 0 + kinds = {record.event.kind for record in trace.events} + # Every program allocates, measures and resets qubits + assert {"Gate", "Measurement", "Reset"} <= kinds, f"unexpected kinds: {kinds}" + assert all(record.source.kind for record in trace.events) + + +@pytest.mark.parametrize("program_type", ["qir", "hugr"]) +def test_download_runtime_trace( + program_type: str, + test_case_name: str, + create_qir_in_project: Callable[[str, str, bytes], ContextManager[QIRRef]], + create_hugr_in_project: Callable[[str, str, Package], ContextManager[HUGRRef]], + qa_hugr_package: Package, +) -> None: + """A completed job on a Helios emulator has a downloadable runtime trace.""" + project_name = f"project for {test_case_name}" + program_name = f"{program_type} for {test_case_name}" + + program_context: ContextManager[QIRRef] | ContextManager[HUGRRef] + if program_type == "qir": + program_context = create_qir_in_project( + project_name, program_name, make_qir_bitcode_from_file("base.ll") + ) + else: + program_context = create_hugr_in_project( + project_name, program_name, qa_hugr_package + ) + + with program_context as program_ref: + job_ref = qnx.start_execute_job( + programs=[program_ref], + n_shots=[N_SHOTS], + max_cost=[10.0], + n_qubits=[5], + backend_config=helios_config(), + project=qnx.projects.get(name=project_name), + name=f"{program_type} job for {test_case_name}", + ) + qnx.jobs.wait_for(job_ref, timeout=JOB_TIMEOUT) + + runtime_traces = wait_for_runtime_traces(job_ref) + assert len(runtime_traces) == 1 + assert isinstance(runtime_traces[0], RuntimeTracesRef) + + trace = runtime_traces[0].download_runtime_traces() + print(f"{program_type}: {len(trace.events)} events") + print(trace.model_dump_json(indent=2)) + assert_trace_is_valid(trace) + + +def test_download_runtime_trace_of_unknown_item( + test_case_name: str, + create_project: Callable[[str], ContextManager[ProjectRef]], +) -> None: + """Downloading the trace of a job item that does not exist gives a 404.""" + with create_project(f"project for {test_case_name}") as project_ref: + unknown_id = uuid4() + trace_ref = RuntimeTracesRef( + id=unknown_id, job_item_id=unknown_id, project=project_ref + ) + + with pytest.raises(qnx_exc.ResourceFetchFailed) as error: + trace_ref.download_runtime_traces() + assert error.value.status_code == 404 From e282bc18c0a8ad28743c871ae658f816f9df4f94 Mon Sep 17 00:00:00 2001 From: Sajid Kakroo Date: Mon, 9 Nov 2026 11:14:31 +0000 Subject: [PATCH 2/6] lint check --- integration/test_runtime_traces.py | 1 + 1 file changed, 1 insertion(+) diff --git a/integration/test_runtime_traces.py b/integration/test_runtime_traces.py index 64c7bd2..52ef01d 100644 --- a/integration/test_runtime_traces.py +++ b/integration/test_runtime_traces.py @@ -29,6 +29,7 @@ pytestmark = pytest.mark.skip(reason="Runtime traces changes are not in QA env yet.") + def wait_for_runtime_traces(job_ref: ExecuteJobRef) -> list[RuntimeTracesRef]: """Traces are ingested after the job completes, so poll until the job has trace refs.""" deadline = time.monotonic() + TRACE_TIMEOUT_SECONDS From 5bcfc38874283c9735b2f135171a32d577de3f17 Mon Sep 17 00:00:00 2001 From: Sajid Kakroo Date: Mon, 9 Nov 2026 18:10:44 +0000 Subject: [PATCH 3/6] Update: review comments --- integration/test_runtime_traces.py | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/integration/test_runtime_traces.py b/integration/test_runtime_traces.py index 52ef01d..85b809a 100644 --- a/integration/test_runtime_traces.py +++ b/integration/test_runtime_traces.py @@ -30,19 +30,6 @@ pytestmark = pytest.mark.skip(reason="Runtime traces changes are not in QA env yet.") -def wait_for_runtime_traces(job_ref: ExecuteJobRef) -> list[RuntimeTracesRef]: - """Traces are ingested after the job completes, so poll until the job has trace refs.""" - deadline = time.monotonic() + TRACE_TIMEOUT_SECONDS - while time.monotonic() < deadline: - runtime_traces = qnx.jobs.runtime_traces(job_ref) - if runtime_traces: - return runtime_traces - time.sleep(TRACE_POLL_SECONDS) - raise AssertionError( - f"No runtime traces for job {job_ref.id} after {TRACE_TIMEOUT_SECONDS}s" - ) - - def helios_config() -> qnx.HeliosConfig: return qnx.HeliosConfig( system_name="Helios-1E", emulator_config=HeliosEmulatorConfig() @@ -66,6 +53,7 @@ def test_download_runtime_trace( create_qir_in_project: Callable[[str, str, bytes], ContextManager[QIRRef]], create_hugr_in_project: Callable[[str, str, Package], ContextManager[HUGRRef]], qa_hugr_package: Package, + qa_helios_emulator_config: qnx.HeliosConfig ) -> None: """A completed job on a Helios emulator has a downloadable runtime trace.""" project_name = f"project for {test_case_name}" @@ -85,21 +73,19 @@ def test_download_runtime_trace( job_ref = qnx.start_execute_job( programs=[program_ref], n_shots=[N_SHOTS], - max_cost=[10.0], + max_cost=[10000.0], n_qubits=[5], - backend_config=helios_config(), + backend_config=qa_helios_emulator_config, project=qnx.projects.get(name=project_name), name=f"{program_type} job for {test_case_name}", ) qnx.jobs.wait_for(job_ref, timeout=JOB_TIMEOUT) - runtime_traces = wait_for_runtime_traces(job_ref) + runtime_traces = qnx.jobs.runtime_traces(job_ref) assert len(runtime_traces) == 1 assert isinstance(runtime_traces[0], RuntimeTracesRef) trace = runtime_traces[0].download_runtime_traces() - print(f"{program_type}: {len(trace.events)} events") - print(trace.model_dump_json(indent=2)) assert_trace_is_valid(trace) From 44a12f75e50db2b638a205080333cf0d4ff5aaad Mon Sep 17 00:00:00 2001 From: Sajid Kakroo Date: Mon, 9 Nov 2026 18:11:59 +0000 Subject: [PATCH 4/6] Update: review comments --- integration/test_runtime_traces.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/integration/test_runtime_traces.py b/integration/test_runtime_traces.py index 85b809a..bde6c64 100644 --- a/integration/test_runtime_traces.py +++ b/integration/test_runtime_traces.py @@ -7,8 +7,6 @@ import pytest from constants import JOB_TIMEOUT from hugr.package import Package -from pytket.circuit import Circuit -from quantinuum_schemas.models.backend_config import HeliosEmulatorConfig from selene_core.trace import Trace from test_qir import make_qir_bitcode_from_file @@ -16,7 +14,6 @@ import qnexus.exceptions as qnx_exc from qnexus.client import get_nexus_client from qnexus.models.references import ( - ExecuteJobRef, HUGRRef, ProjectRef, QIRRef, @@ -29,13 +26,6 @@ pytestmark = pytest.mark.skip(reason="Runtime traces changes are not in QA env yet.") - -def helios_config() -> qnx.HeliosConfig: - return qnx.HeliosConfig( - system_name="Helios-1E", emulator_config=HeliosEmulatorConfig() - ) - - def assert_trace_is_valid(trace: Trace) -> None: """The trace has events, and every event has a source and a kind.""" assert isinstance(trace, Trace) From c010858e77abefcefa515833fd3ea4e45a2034ab Mon Sep 17 00:00:00 2001 From: Sajid Kakroo Date: Mon, 9 Nov 2026 18:33:03 +0000 Subject: [PATCH 5/6] lint checks --- integration/test_runtime_traces.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/integration/test_runtime_traces.py b/integration/test_runtime_traces.py index bde6c64..54bbc0c 100644 --- a/integration/test_runtime_traces.py +++ b/integration/test_runtime_traces.py @@ -1,6 +1,5 @@ """Tests for downloading runtime traces of jobs run on Helios emulators.""" -import time from typing import Callable, ContextManager from uuid import uuid4 @@ -12,7 +11,6 @@ import qnexus as qnx import qnexus.exceptions as qnx_exc -from qnexus.client import get_nexus_client from qnexus.models.references import ( HUGRRef, ProjectRef, From 678859feea499220bc29c8ee95cce2072ec121dc Mon Sep 17 00:00:00 2001 From: Sajid Kakroo Date: Mon, 9 Nov 2026 18:35:18 +0000 Subject: [PATCH 6/6] format --- integration/conftest.py | 8 ++++++++ integration/test_runtime_traces.py | 3 ++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/integration/conftest.py b/integration/conftest.py index 3e7e4e1..cdb845b 100644 --- a/integration/conftest.py +++ b/integration/conftest.py @@ -18,6 +18,7 @@ AerStateConfig, AerUnitaryConfig, BackendConfig, + HeliosEmulatorConfig, QuantinuumConfig, QulacsConfig, SeleneConfig, @@ -537,6 +538,13 @@ def test_circuit() -> Circuit: return circuit +@pytest.fixture(name="qa_helios_emulator_config") +def helios_config() -> qnx.HeliosConfig: + return qnx.HeliosConfig( + system_name="Helios-1E", emulator_config=HeliosEmulatorConfig() + ) + + @pytest.fixture() def test_qv_circuit() -> Circuit: """A Quantum Volume pytket circuit. diff --git a/integration/test_runtime_traces.py b/integration/test_runtime_traces.py index 54bbc0c..847fb77 100644 --- a/integration/test_runtime_traces.py +++ b/integration/test_runtime_traces.py @@ -24,6 +24,7 @@ pytestmark = pytest.mark.skip(reason="Runtime traces changes are not in QA env yet.") + def assert_trace_is_valid(trace: Trace) -> None: """The trace has events, and every event has a source and a kind.""" assert isinstance(trace, Trace) @@ -41,7 +42,7 @@ def test_download_runtime_trace( create_qir_in_project: Callable[[str, str, bytes], ContextManager[QIRRef]], create_hugr_in_project: Callable[[str, str, Package], ContextManager[HUGRRef]], qa_hugr_package: Package, - qa_helios_emulator_config: qnx.HeliosConfig + qa_helios_emulator_config: qnx.HeliosConfig, ) -> None: """A completed job on a Helios emulator has a downloadable runtime trace.""" project_name = f"project for {test_case_name}"