Skip to content

Commit

Permalink
[SDS-242] Optimized-flag not valid for hardware (#96)
Browse files Browse the repository at this point in the history
* [SDS-242] Optimized-flag not valid for hardware
  • Loading branch information
QFer committed May 18, 2021
1 parent e5d3ac8 commit dcd1b26
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/quantuminspire/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ def _create_job(self, name: str, asset: Dict[str, Any], number_of_shots: int,
The properties describing the new job.
See :meth:`~.get_job` for a description of the job properties.
"""
full_state_projection = full_state_projection and not backend_type.get("is_hardware_backend", True)
payload = {
'status': 'NEW',
'name': name,
Expand Down
3 changes: 2 additions & 1 deletion src/quantuminspire/qiskit/backend_qx.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ def run(self, qobj: QasmQobj) -> QIJob:
job = QIJob(self, str(project['id']), self.__api)
for experiment in experiments:
self.__validate_number_of_clbits(experiment)
full_state_projection = self.__validate_full_state_projection(experiment)
full_state_projection = BaseBackend.configuration(self).simulator and \
self.__validate_full_state_projection(experiment)
if not full_state_projection:
QuantumInspireBackend.__validate_unsupported_measurements(experiment)
job_for_experiment = self._submit_experiment(experiment, number_of_shots, project=project,
Expand Down
22 changes: 22 additions & 0 deletions src/tests/quantuminspire/qiskit/test_backend_qx.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,28 @@ def test_for_fsp_no_measurements(self):
simulator.run(qobj)
result_experiment.assert_called_once_with(experiment, 25, project=project, full_state_projection=True)

def test_for_non_fsp_hardware_backend(self):
with patch.object(QuantumInspireBackend, "_submit_experiment", return_value=Mock()) as result_experiment:
api = Mock()
project = {'id': 42}
api.create_project.return_value = project
api.execute_qasm_async.return_value = 42
api.get_backend_type_by_name.return_value = {'max_number_of_shots': 4096}
config = QuantumInspireBackend.DEFAULT_CONFIGURATION
config.backend_name = 'qi_hardware'
config.simulator = False
simulator = QuantumInspireBackend(api, config)
instructions = [{'name': 'cx', 'qubits': [0, 1]},
{'name': 'x', 'qubits': [0]}]
experiment = self._basic_experiment_dictionary
experiment['instructions'] = instructions
qjob_dict = self._basic_qobj_dictionary
qjob_dict['experiments'][0] = experiment
qobj = QasmQobj.from_dict(qjob_dict)
experiment = qobj.experiments[0]
simulator.run(qobj)
result_experiment.assert_called_once_with(experiment, 25, project=project, full_state_projection=False)

def test_measurement_2_qubits_to_1_classical_bit(self):
with patch.object(QuantumInspireBackend, "_submit_experiment", return_value=Mock()):
api = Mock()
Expand Down
25 changes: 24 additions & 1 deletion src/tests/quantuminspire/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ def test_create_job_has_correct_input_and_output_without_fsp(self):
self.assertTrue(len(print_string) == 0)

api.show_fsp_warning(enable=True) # Enable warning about non fsp
actual = api._create_job(name, asset, number_of_shots, backend_type_hw, full_state_projection=False)
actual = api._create_job(name, asset, number_of_shots, backend_type_hw, full_state_projection=True)
self.assertDictEqual(expected, actual)
# Verify warning. None on hw backend
print_string = mock_stdout.getvalue()
Expand Down Expand Up @@ -562,6 +562,29 @@ def test_create_job_has_correct_input_and_output_with_fsp(self):
actual = api._create_job(name, asset, number_of_shots, backend_type_sim, full_state_projection=True)
self.assertDictEqual(expected, actual)

def test_create_job_has_correct_input_and_output_without_fsp_for_hardware_backend(self):
name = 'TestJob'
asset = {'url': 'https://api.quantum-inspire.com/assets/1/'}
project = {'backend_type': 'https://api.quantum-inspire.com/backendtypes/1/'}
backend_type_hw = {'url': 'https://api.quantum-inspire.com/backendtypes/1/',
'name': 'QI Hardware',
'is_hardware_backend': True}
number_of_shots = 1
expected_payload = {
'status': 'NEW',
'name': name,
'input': asset['url'],
'backend_type': project['backend_type'],
'number_of_shots': number_of_shots,
'full_state_projection': False,
'user_data': ''
}
expected = self.__mock_job_handler(expected_payload, 'create', None, None, ['test', 'create'], expected_payload)
self.coreapi_client.handlers['jobs'] = partial(self.__mock_job_handler, expected_payload, 'create')
api = QuantumInspireAPI('FakeURL', self.authentication, coreapi_client_class=self.coreapi_client)
actual = api._create_job(name, asset, number_of_shots, backend_type_hw, full_state_projection=True)
self.assertDictEqual(expected, actual)

def test_create_job_raises_api_error(self):
name = 'CreateJobFail'
asset = {'url': 'https://api.quantum-inspire.com/assets/1/'}
Expand Down

0 comments on commit dcd1b26

Please sign in to comment.