Skip to content

Commit

Permalink
[SDS-423] Coupling map qiskit (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
QFer committed Aug 31, 2021
1 parent 88ad921 commit de217f6
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
16 changes: 15 additions & 1 deletion src/quantuminspire/qiskit/qi_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,12 @@ def _result(self, result_function: Callable[[BaseJob], List[ExperimentResult]],
time.sleep(wait)

experiment_results = result_function(self)
total_time_taken = sum(getattr(experiment_result, "time_taken", 0.0) for
experiment_result in experiment_results)

return QIResult(backend_name=self._backend.backend_name, backend_version=quantum_inspire_version,
job_id=self.job_id(), qobj_id=self.job_id(), success=True, results=experiment_results)
job_id=self.job_id(), qobj_id=self.job_id(), success=True, results=experiment_results,
status=self.status(), time_taken=total_time_taken)

def result(self, timeout: Optional[float] = None, wait: float = 0.5) -> QIResult:
""" Return the result for the experiments in the latest run for this project.
Expand Down Expand Up @@ -185,6 +189,16 @@ def add_job(self, job: QuantumInspireJob) -> None:
"""
self.jobs.append(job)

def queue_position(self, refresh: bool = False) -> Optional[int]:
"""
Return the position for this Job in the Quantum Inspire queue (when in status QUEUED).
Currently we don't have this info available.
:return:
The queue position of the job. Currently None (not available).
"""
return None

def status(self) -> JobStatus:
"""
Check the status of the jobs submitted in the latest run.
Expand Down
2 changes: 1 addition & 1 deletion src/quantuminspire/qiskit/qi_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class QIResult(Result): # type: ignore
"""
def __init__(self, backend_name: str, backend_version: str, qobj_id: str, job_id: str, success: bool,
results: List[ExperimentResult], date: Any = None, status: Any = None, header: Any = None,
**kwargs: str) -> None:
**kwargs: Any) -> None:
"""
Construct a new QIResult object. Not normally called directly, use a QIJob to get the QIResult.
Expand Down
2 changes: 1 addition & 1 deletion src/quantuminspire/qiskit/quantum_inspire_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def _adjust_backend_configuration(config: QasmBackendConfiguration, backend: Dic
coupling_map = []
for i in range(len(backend['topology']['edges'])):
for j in backend['topology']['edges'][i]:
coupling_map.append((i, j))
coupling_map.append([i, j])
config.coupling_map = None if len(coupling_map) == 0 else coupling_map

def set_authentication_details(self, email: str, password: str, qi_url: str = QI_URL) -> None:
Expand Down
19 changes: 19 additions & 0 deletions src/tests/quantuminspire/qiskit/test_qi_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def test_result(self):
results = job.result()

self.assertTrue(results.success)
self.assertEqual(results.time_taken, 0.54)
self.assertDictEqual({'counts': {'0x0': 42}}, results.data(0))
self.assertDictEqual({'counts': {'0x1': 42}}, results.data(1))
self.assertDictEqual({'0': 42}, results.get_counts(0))
Expand Down Expand Up @@ -165,6 +166,24 @@ def test_cancel(self):
job.cancel()
api.delete_project.assert_called_with(42)

def test_queue_position(self):
api = Mock()
api.get_job.side_effect = [{'name': 'test_job', 'status': 'NEW'},
{'name': 'other_job', 'status': 'NEW'}]
job_id = '42'
backend = Mock()
quantuminspire_job = Mock()
quantuminspire_job.get_job_identifier.side_effect = [1, 2]
qijob = QIJob(backend, job_id, api)
qijob.add_job(quantuminspire_job)
qijob.add_job(quantuminspire_job)
status = qijob.status()
self.assertEqual(JobStatus.QUEUED, status)
queue_position = qijob.queue_position(True)
self.assertIsNone(queue_position)
queue_position = qijob.queue_position()
self.assertIsNone(queue_position)

def test_status(self):
api = Mock()
api.get_job.side_effect = [{'name': 'test_job', 'status': 'COMPLETE'},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ def test_hardware_backend(self):
quantum_inpire_provider._api.get_backend_types.return_value = [self.hardware_backend_type]
backend = quantum_inpire_provider.get_backend(name='qi_hardware')
self.assertEqual('qi_hardware', backend.name())
self.assertEqual(backend.configuration().coupling_map, [(0, 2), (1, 2), (2, 0), (2, 1),
(2, 3), (2, 4), (3, 2), (4, 2)])
self.assertEqual(backend.configuration().coupling_map, [[0, 2], [1, 2], [2, 0], [2, 1],
[2, 3], [2, 4], [3, 2], [4, 2]])
self.assertFalse(backend.configuration().simulator)
self.assertEqual(5, backend.configuration().n_qubits)
self.assertTrue(backend.configuration().memory)
Expand All @@ -141,7 +141,7 @@ def test_hardware_backend2(self):
quantum_inpire_provider._api.get_backend_types.return_value = [self.hardware_backend_type2]
backend = quantum_inpire_provider.get_backend(name='qi_hardware')
self.assertEqual('qi_hardware', backend.name())
self.assertEqual(backend.configuration().coupling_map, [(0, 1), (1, 0)])
self.assertEqual(backend.configuration().coupling_map, [[0, 1], [1, 0]])
self.assertFalse(backend.configuration().simulator)
self.assertEqual(2, backend.configuration().n_qubits)
self.assertTrue(backend.configuration().memory)
Expand Down

0 comments on commit de217f6

Please sign in to comment.