Skip to content

Commit

Permalink
Address timing dependence in ExperimentData.add_jobs test (#1267)
Browse files Browse the repository at this point in the history
Previously, the test `test_add_data_job` called `add_jobs` with two jobs
and then tested that the results from `ExperimentData.data()` matched
the results of the first job concatenated with the second.
`ExperimentData` uses threads to handle tracking jobs. Even though these
jobs are dummy jobs that take no time, `ExperimentData` would
occasionally add the results from the second job before the first and
then fail the results comparison. With this change, a label is attached
to each result and then the results are sorted based on the label before
testing against the expected results.

Closes #1044
  • Loading branch information
wshanks authored Sep 7, 2023
1 parent 60655bb commit 4f08d6f
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions test/database_service/test_db_experiment_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ def test_add_data_result(self):
def test_add_data_result_metadata(self):
"""Test add result metadata."""
exp_data = ExperimentData(backend=self.backend, experiment_type="qiskit_test")
result1 = self._get_job_result(1, has_metadata=False)
result2 = self._get_job_result(1, has_metadata=True)
result1 = self._get_job_result(1, no_metadata=True)
result2 = self._get_job_result(1)

exp_data.add_data(result1)
exp_data.add_data(result2)
Expand All @@ -119,12 +119,14 @@ def test_add_data_job(self):
"""Test add job data."""
a_job = mock.create_autospec(Job, instance=True)
a_job.result.return_value = self._get_job_result(3)
num_circs = 3
jobs = []
for _ in range(2):
job = mock.create_autospec(Job, instance=True)
job.result.return_value = self._get_job_result(2)
job.result.return_value = self._get_job_result(2, label_from=num_circs)
job.status.return_value = JobStatus.DONE
jobs.append(job)
num_circs = num_circs + 2

expected = a_job.result().get_counts()
for job in jobs:
Expand All @@ -135,7 +137,13 @@ def test_add_data_job(self):
self.assertExperimentDone(exp_data)
exp_data.add_jobs(jobs)
self.assertExperimentDone(exp_data)
self.assertEqual(expected, [sdata["counts"] for sdata in exp_data.data()])
self.assertEqual(
expected,
[
sdata["counts"]
for sdata in sorted(exp_data.data(), key=lambda x: x["metadata"]["label"])
],
)
self.assertIn(a_job.job_id(), exp_data.job_ids)

def test_add_data_job_callback(self):
Expand Down Expand Up @@ -1073,7 +1081,7 @@ def _job2_result():
exp_data.data(0)["counts"], [copied.data(0)["counts"], copied.data(1)["counts"]]
)

def _get_job_result(self, circ_count, has_metadata=False):
def _get_job_result(self, circ_count, label_from=0, no_metadata=False):
"""Return a job result with random counts."""
job_result = {
"backend_name": BackendData(self.backend).name,
Expand All @@ -1085,12 +1093,12 @@ def _get_job_result(self, circ_count, has_metadata=False):
}
circ_result_template = {"shots": 1024, "success": True, "data": {}}

for _ in range(circ_count):
for i_circ in range(circ_count):
counts = randrange(1024)
circ_result = copy.copy(circ_result_template)
circ_result["data"] = {"counts": {"0x0": counts, "0x3": 1024 - counts}}
if has_metadata:
circ_result["header"] = {"metadata": {"meas_basis": "pauli"}}
if not no_metadata:
circ_result["header"] = {"metadata": {"label": label_from + i_circ}}
job_result["results"].append(circ_result)

return Result.from_dict(job_result)
Expand Down

0 comments on commit 4f08d6f

Please sign in to comment.