Skip to content

Commit

Permalink
Updated according to @nkanazawa1989 's suggestion qiskit-community#1268
Browse files Browse the repository at this point in the history
  • Loading branch information
Musa-Sina-Ertugrul committed Oct 17, 2023
1 parent d526f89 commit 09a3c99
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions qiskit_experiments/framework/experiment_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
from qiskit.result import Result
from qiskit.result import marginal_distribution
from qiskit.result.postprocess import format_counts_memory
from qiskit.result.utils import marginal_memory
from qiskit.providers.jobstatus import JobStatus, JOB_FINAL_STATES
from qiskit.exceptions import QiskitError
from qiskit.providers import Job, Backend, Provider
Expand Down Expand Up @@ -794,7 +795,10 @@ def add_data(
if isinstance(datum, dict):
self._result_data.append(datum)
elif isinstance(datum, Result):
self._add_result_data(datum)
if datum["metadata"]:
self._set_child_data(datum["metadata"]._metadata())
else:
self._add_result_data(datum)
else:
raise TypeError(f"Invalid data type {type(datum)}.")

Expand Down Expand Up @@ -853,7 +857,13 @@ def _marginalized_component_data(self, composite_data: List[Dict]) -> List[List[
composite_clbits = None

# Pre-process the memory if any to avoid redundant calls to format_counts_memory
f_memory = self._format_memory(datum, composite_clbits)
f_memory = None
if (
"memory" in datum
and composite_clbits is not None
and isinstance(datum["memory"][0], str)
):
f_memory = marginal_memory(datum["memory"], composite_clbits)

for i, index in enumerate(metadata["composite_index"]):
if index not in marginalized_data:
Expand Down Expand Up @@ -892,22 +902,7 @@ def _marginalized_component_data(self, composite_data: List[Dict]) -> List[List[

# Sort by index
return [marginalized_data[i] for i in sorted(marginalized_data.keys())]

@staticmethod
def _format_memory(datum: Dict, composite_clbits: List):
"""A helper method to convert level 2 memory (if it exists) to bit-string format."""
f_memory = None
if (
"memory" in datum
and composite_clbits is not None
and isinstance(datum["memory"][0], str)
):
num_cbits = 1 + max(cbit for cbit_list in composite_clbits for cbit in cbit_list)
header = {"memory_slots": num_cbits}
f_memory = list(format_counts_memory(shot, header) for shot in datum["memory"])

return f_memory


def add_jobs(
self,
jobs: Union[Job, List[Job]],
Expand Down

0 comments on commit 09a3c99

Please sign in to comment.