Skip to content

Commit 918c875

Browse files
committed
chore: extract_result update
1 parent 9db4d9c commit 918c875

File tree

1 file changed

+40
-16
lines changed
  • mpqp/execution/providers

1 file changed

+40
-16
lines changed

mpqp/execution/providers/ibm.py

+40-16
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ def submit_remote_ibm(job: Job) -> tuple[str, "RuntimeJobV2"]:
604604

605605

606606
@typechecked
607-
def run_remote_ibm(job: Job) -> Result:
607+
def run_remote_ibm(job: Job) -> BatchResult | Result:
608608
"""Submits the job on the right IBM remote device, precised in the job in
609609
parameter, and waits until the job is completed.
610610
@@ -632,7 +632,7 @@ def extract_result(
632632
result: "QiskitResult | EstimatorResult | PrimitiveResult[PubResult | SamplerPubResult]",
633633
job: Optional[Job],
634634
device: "IBMDevice | IBMSimulatedDevice | AZUREDevice",
635-
) -> Result: # TODO: [multi-obs] return BatchResult for multi observable
635+
) -> BatchResult | Result: # TODO: [multi-obs] return BatchResult for multi observable
636636
"""Parses a result from ``IBM`` execution (remote or local) in a ``MPQP``
637637
:class:`~mpqp.execution.result.Result`.
638638
@@ -654,20 +654,44 @@ def extract_result(
654654
res_data = result[0].data
655655
# res_data is a DataBin, which means all typechecking is out of the
656656
# windows for this specific object
657-
658-
# If we are in observable mode
659-
if hasattr(res_data, "evs"):
660-
if job is None:
661-
job = Job(JobType.OBSERVABLE, QCircuit(0), device)
662-
663-
mean = float(res_data.evs) # pyright: ignore[reportAttributeAccessIssue]
664-
error = float(res_data.stds) # pyright: ignore[reportAttributeAccessIssue]
665-
shots = (
666-
job.measure.shots
667-
if job.device.is_simulator() and job.measure is not None
668-
else result[0].metadata["shots"]
669-
)
670-
return Result(job, mean, error, shots)
657+
all_results = []
658+
res_data = result
659+
660+
results = res_data.evs if isinstance(res_data.evs, list) else [res_data.evs]
661+
for single_result in results:
662+
if hasattr(res_data, "evs"): #
663+
if job is None:
664+
job = Job(JobType.OBSERVABLE, QCircuit(0), device)
665+
666+
mean = float(
667+
single_result
668+
) # pyright: ignore[reportAttributeAccessIssue]
669+
error = float(
670+
res_data.stds
671+
) # pyright: ignore[reportAttributeAccessIssue]
672+
shots = (
673+
job.measure.shots
674+
if job.device.is_simulator() and job.measure is not None
675+
else result[0].metadata["shots"]
676+
)
677+
all_results.append(Result(job, mean, error, shots))
678+
679+
if len(all_results) > 1:
680+
return BatchResult(all_results)
681+
682+
return all_results[0]
683+
# if hasattr(res_data, "evs"):
684+
# if job is None:
685+
# job = Job(JobType.OBSERVABLE, QCircuit(0), device)
686+
687+
# mean = float(res_data.evs) # pyright: ignore[reportAttributeAccessIssue]
688+
# error = float(res_data.stds) # pyright: ignore[reportAttributeAccessIssue]
689+
# shots = (
690+
# job.measure.shots
691+
# if job.device.is_simulator() and job.measure is not None
692+
# else result[0].metadata["shots"]
693+
# )
694+
# return Result(job, mean, error, shots)
671695
# If we are in sample mode
672696
else:
673697
if job is None:

0 commit comments

Comments
 (0)