Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions qiskit/result/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@ def __init__(
status (str): The status of the experiment
seed (int): The seed used for simulation (if run on a simulator)
meas_return (str): The type of measurement returned
header (dict): A free form dictionary
header for the experiment
header (dict): A free form dictionary header for the experiment
kwargs: Arbitrary extra fields

Raises:
Expand Down
18 changes: 10 additions & 8 deletions qiskit/result/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class Result:
each experiment success)
results (list[ExperimentResult]): corresponding results for array of
experiments of the input
date (str): optional date field
status (str): optional status field
header (dict): an optional free form dictionary header
"""

_metadata = {}
Expand Down Expand Up @@ -88,7 +91,7 @@ def to_dict(self):
"backend_name": self.backend_name,
"backend_version": self.backend_version,
"date": self.date,
"header": None if self.header is None else self.header.to_dict(),
"header": self.header,
"job_id": self.job_id,
"status": self.status,
"success": self.success,
Expand Down Expand Up @@ -128,11 +131,10 @@ def data(self, experiment=None):
the get_xxx method, and the data will be post-processed for the data type.

Args:
experiment (str or QuantumCircuit or Schedule or int or None): the index of the
experiment (str or QuantumCircuit or int or None): the index of the
experiment. Several types are accepted for convenience::
* str: the name of the experiment.
* QuantumCircuit: the name of the circuit instance will be used.
* Schedule: the name of the schedule instance will be used.
* int: the position of the experiment.
* None: if there is only one experiment, returns it.

Expand Down Expand Up @@ -179,7 +181,7 @@ def get_memory(self, experiment=None):
['00000', '01000', '10100', '10100', '11101', '11100', '00101', ..., '01010']

Args:
experiment (str or QuantumCircuit or Schedule or int or None): the index of the
experiment (str or QuantumCircuit or int or None): the index of the
experiment, as specified by ``data()``.

Returns:
Expand Down Expand Up @@ -231,7 +233,7 @@ def get_counts(self, experiment=None):
"""Get the histogram data of an experiment.

Args:
experiment (str or QuantumCircuit or Schedule or int or None): the index of the
experiment (str or QuantumCircuit or int or None): the index of the
experiment, as specified by ``data([experiment])``.

Returns:
Expand Down Expand Up @@ -283,7 +285,7 @@ def get_statevector(self, experiment=None, decimals=None):
"""Get the final statevector of an experiment.

Args:
experiment (str or QuantumCircuit or Schedule or int or None): the index of the
experiment (str or QuantumCircuit or int or None): the index of the
experiment, as specified by ``data()``.
decimals (int): the number of decimals in the statevector.
If None, does not round.
Expand All @@ -305,7 +307,7 @@ def get_unitary(self, experiment=None, decimals=None):
"""Get the final unitary of an experiment.

Args:
experiment (str or QuantumCircuit or Schedule or int or None): the index of the
experiment (str or QuantumCircuit or int or None): the index of the
experiment, as specified by ``data()``.
decimals (int): the number of decimals in the unitary.
If None, does not round.
Expand All @@ -326,7 +328,7 @@ def _get_experiment(self, key=None):
"""Return a single experiment result from a given key.

Args:
key (str or QuantumCircuit or Schedule or int or None): the index of the
key (str or QuantumCircuit or int or None): the index of the
experiment, as specified by ``data()``.

Returns:
Expand Down
32 changes: 32 additions & 0 deletions test/python/result/test_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,38 @@ def test_result_repr(self):
)
self.assertEqual(expected, repr(result))

def test_result_to_dict(self):
"""Test that dictionary is constructed correctly for a results object."""
raw_counts = {"0x0": 4, "0x2": 10}
data = models.ExperimentResultData(counts=raw_counts)
exp_result_header = {"creg_sizes": [["c0", 2], ["c0", 1], ["c1", 1]], "memory_slots": 4}
exp_result = models.ExperimentResult(
shots=14, success=True, meas_level=2, data=data, header=exp_result_header
)
header = {"header-property": 1}
result = Result(results=[exp_result], header=header, **self.base_result_args)

expected = {
"backend_name": "test_backend",
"backend_version": "1.0.0",
"header": {"header-property": 1},
"date": None,
"job_id": "job-123",
"status": None,
"success": True,
"results": [
{
"shots": 14,
"success": True,
"data": {"counts": {"0x0": 4, "0x2": 10}},
"meas_level": 2,
"header": {"creg_sizes": [["c0", 2], ["c0", 1], ["c1", 1]], "memory_slots": 4},
}
],
}

self.assertEqual(expected, result.to_dict())

def test_multiple_circuits_counts(self):
"""Test that counts are returned either as a list or a single item.

Expand Down