Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revising the completion_times property #1208

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
19 changes: 9 additions & 10 deletions qiskit_experiments/framework/experiment_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,8 @@ def __init__(
# data storage
self._result_data = ThreadSafeList()
self._figures = ThreadSafeOrderedDict(self._db_data.figure_names)
self._analysis_results = AnalysisResultTable()

self._analysis_results = ThreadSafeOrderedDict()
self._completion_times = ThreadSafeOrderedDict()
self._deleted_figures = deque()
self._deleted_analysis_results = deque()

Expand All @@ -377,12 +377,7 @@ def experiment(self):
@property
def completion_times(self) -> Dict[str, datetime]:
"""Returns the completion times of the jobs."""
job_times = {}
for job_id, job in self._jobs.items():
if job is not None and "COMPLETED" in job.time_per_step():
job_times[job_id] = job.time_per_step().get("COMPLETED")

return job_times
return dict(self._completion_times)

@property
def tags(self) -> List[str]:
Expand Down Expand Up @@ -916,8 +911,11 @@ def _add_job_data(
pass
self._add_result_data(job_result, jid)
LOG.debug("Job data added [Job ID: %s]", jid)
# sets the endtime to be the time the last successful job was added
self.end_datetime = datetime.now()
completed_at = datetime.now()
with self._completion_times.lock:
self._completion_times[jid] = completed_at
# sets the end time to be the time the last successful job was added
self.end_datetime = completed_at
return jid, True
except Exception as ex: # pylint: disable=broad-except
# Handle cancelled jobs
Expand Down Expand Up @@ -2463,6 +2461,7 @@ def __json_encode__(self):
"_deleted_figures": self._deleted_figures,
"_deleted_analysis_results": self._deleted_analysis_results,
"_result_data": self._result_data,
"_completion_times": self._completion_times,
"_extra_data": self._extra_data,
"_created_in_db": self._created_in_db,
"_figures": self._safe_serialize_figures(), # Convert figures to SVG
Expand Down
13 changes: 13 additions & 0 deletions test/database_service/test_db_experiment_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,19 @@ def _analysis(*args): # pylint: disable = unused-argument
self.assertEqual(exp_data.analysis_status(), AnalysisStatus.CANCELLED)
self.assertEqual(exp_data.status(), ExperimentStatus.CANCELLED)

def test_completion_times(self):
"""Test the completion_times property"""
jid = "1234"
job = mock.create_autospec(Job, instance=True)
job.job_id.return_value = jid
job.status = JobStatus.DONE

exp_data = ExperimentData(experiment_type="qiskit_test")
exp_data.add_jobs(job)
completion_times = exp_data.completion_times
self.assertTrue(jid in completion_times)
self.assertTrue(isinstance(completion_times[jid], datetime))

def test_partial_cancel_analysis(self):
"""Test canceling experiment analysis."""

Expand Down