# Copyright 2026 Arthur Strauss
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""QM sampler job: runs sampler PUBs as QUA programs and returns measurement counts.
Author: Arthur Strauss
Date: 2026-02-08
"""
from __future__ import annotations
from typing import Optional, Union, List, TYPE_CHECKING
import os
import inspect
import tempfile
import numpy as np
from qiskit.primitives import PrimitiveResult
from qiskit.primitives.containers import BitArray, SamplerPubResult, DataBin
from qiskit.primitives.containers.sampler_pub import SamplerPub
from qiskit.providers import JobStatus
from qiskit.result.models import MeasLevel
from qm.jobs.running_qm_job import RunningQmJob
from ..backend import QMBackend
from ..utils.measurement.classified import require_classified_meas_level
from ..utils.measurement.job_metadata import measurement_output_bit_sizes
from ..parameter_table import InputType, ParameterPool
from .primitive_plan import SamplerPlan
from .iqcc_job_mixin import IQCCJobMixin
from .qm_execution_options import (
await_running_jobs,
resolve_qiskit_job_id,
submit_qua_programs,
)
from .qua_programs import plan_primitive_programs, compute_locator
from .qm_primitive_job import QMPrimitiveJob
from .stream_assembly import bit_array_from_measurement_stream
if TYPE_CHECKING:
from iqcc_cloud_client.qmm_cloud import CloudJob
[docs]
class QMSamplerJob(QMPrimitiveJob):
"""Job handle for :class:`~qiskit_qm_provider.primitives.QMSamplerV2` execution.
Builds a QUA sampler program from pubs and returns classified counts via
:meth:`result`. See :attr:`program` for the compiled QUA source.
"""
def __init__(
self,
backend: QMBackend,
pubs: List[SamplerPub],
input_type: InputType,
**kwargs,
):
"""Create a sampler job.
Args:
backend: Backend that compiled the circuits.
pubs: Coerced sampler pubs to execute.
input_type: How circuit parameters are streamed to the OPX.
**kwargs: Additional options forwarded to QUA program generation
and backend execution (e.g. ``shots``, ``meas_level``).
"""
super().__init__(backend, pubs, input_type, **kwargs)
require_classified_meas_level(
self.metadata.get("meas_level", MeasLevel.CLASSIFIED),
context="QMSamplerJob",
)
ParameterPool.reset()
self._plans: List[SamplerPlan] = [
SamplerPlan.from_pub(pub, self._input_type, i) for i, pub in enumerate(self._pubs)
]
programs, self._chunk_layout = plan_primitive_programs(self._backend, self._plans, **self.metadata)
self._programs = programs
self._locator = compute_locator(self._chunk_layout)
def _result_function(self, qm_jobs: List[RunningQmJob]) -> PrimitiveResult[SamplerPubResult]:
results_handles = [job.result_handles for job in qm_jobs]
for handle in results_handles:
handle.wait_for_all_values()
all_data = []
for i, (pub, plan) in enumerate(zip(self._pubs, self._plans)):
qc_meas_data = {}
for output_key, bit_width in measurement_output_bit_sizes(pub.circuit).items():
if not plan.has_tasks: # zero bindings: never run, empty result of the PUB's shape
empty = np.zeros((*pub.shape, pub.shots, -(-bit_width // 8)), dtype=np.uint8)
qc_meas_data[output_key] = BitArray(empty, bit_width)
continue
chunk_idx, local_idx = self._locator[i]
raw = results_handles[chunk_idx].get(f"{output_key}_{local_idx}").fetch_all()
qc_meas_data[output_key] = bit_array_from_measurement_stream(pub, raw, bit_width)
sampler_data = SamplerPubResult(DataBin(**qc_meas_data, shape=pub.shape))
all_data.append(sampler_data)
return PrimitiveResult(all_data)
[docs]
def submit(self):
"""Submit the job to the backend.
Programs are submitted via :func:`~.submit_qua_programs` (``qm.execute``
for simulation; OPX1000 ``add_to_queue`` / OPX+ ``queue.add`` for real
hardware). Chunks are handled in order: each is waited on until running and its
parameters are streamed before the next chunk, queued behind it, can start. IQCC
sync-hook submission is handled by
:class:`IQCCSamplerJob`.
"""
if self._qm_jobs is not None:
raise RuntimeError("QM job has already been submitted")
if not self._programs: # every PUB has zero bindings: nothing to send
self._job_id, self._qm_jobs = resolve_qiskit_job_id(), []
return
pending_jobs = submit_qua_programs(
self._backend.qm,
self._backend.qmm,
self._programs,
self.metadata,
)
self._job_id = resolve_qiskit_job_id(pending_jobs)
self._qm_jobs = await_running_jobs(
pending_jobs, self._chunk_layout, entity="circuit indices", on_running=self._push_parameters
)
@property
def runtime_pubs(self) -> List[SamplerPlan]:
"""Execution plans for this sampler job: one
:class:`~qiskit_qm_provider.job.primitive_plan.SamplerPlan` per input PUB."""
return self._plans
def _push_parameters(self, qm_job, chunk: List[int]) -> None:
"""Stream circuit parameters to the OPX for the given chunk of pub indices."""
for global_idx in chunk:
self._plans[global_idx].push(qm_job, self._backend.qm)
[docs]
def result(self) -> PrimitiveResult[SamplerPubResult]:
"""Build and return classified measurement counts for all pubs.
Returns:
:class:`~qiskit.primitives.PrimitiveResult` with
:class:`~qiskit.primitives.SamplerPubResult` entries.
"""
if self._qm_jobs is None:
raise RuntimeError("QM job has not submitted yet")
return self._result_function(self._qm_jobs)
class IQCCSamplerJob(IQCCJobMixin, QMSamplerJob):
"""IQCC cloud variant of :class:`QMSamplerJob`.
Execution is **synchronous**: each :meth:`submit` call blocks until the
remote OPX program completes. ``CloudJob.status`` is therefore always
``"completed"``; real failure information lives in ``_run_data["stderr"]``
(see :class:`~.IQCCJobMixin` and :meth:`~.IQCCJobMixin.status`).
``result()`` raises :class:`~.IQCCCloudExecutionError` before attempting to
fetch streams when any chunk job's stderr contains a Python traceback.
"""
def submit(self):
"""Submit all QUA programs to the IQCC cloud backend.
For each chunk produced by :func:`~.plan_primitive_programs`, a separate
cloud job is executed synchronously. When the chunk's PUBs have
parameterised circuits, a sync-hook script is written to a temporary
file and passed to ``execute()``; the file is unlinked immediately
after the call regardless of outcome.
Results from all chunks are stitched back by :meth:`_result_function`
using the locator built at construction time.
"""
from .sync_hook import generate_sync_hook
if self._qm_jobs is not None:
raise RuntimeError("IQCC QM job has already been submitted")
programs = self._programs
timeout = self.metadata.get("timeout", None)
self._qm_jobs = []
for prog, chunk in zip(programs, self._chunk_layout):
chunk_plans = [self._plans[g] for g in chunk]
sync_hook_path = None
if any(p.streams_inputs for p in chunk_plans):
sync_hook_code = generate_sync_hook(chunk_plans)
with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f:
f.write(sync_hook_code)
sync_hook_path = f.name
options = {"sync_hook": sync_hook_path}
else:
options = {}
if timeout is not None:
options["timeout"] = timeout
try:
self._qm_jobs.append(self._backend.qm.execute(prog, options=options))
finally:
if sync_hook_path is not None:
os.unlink(sync_hook_path)
self._job_id = resolve_qiskit_job_id(self._qm_jobs)