def test_sampler_multiple_jobs(): mock_service = mock.MagicMock() job_dict0 = { 'id': '1', 'status': 'completed', 'qubits': '1', 'target': 'qpu', 'metadata': {'shots': 4, 'measurement0': f'a{chr(31)}0'}, 'data': {'histogram': {'0': '0.25', '1': '0.75'}}, } job_dict1 = { 'id': '1', 'status': 'completed', 'qubits': '1', 'target': 'qpu', 'metadata': {'shots': 4, 'measurement0': f'a{chr(31)}0'}, 'data': {'histogram': {'0': '0.5', '1': '0.5'}}, } job0 = ionq.Job(client=mock_service, job_dict=job_dict0) job1 = ionq.Job(client=mock_service, job_dict=job_dict1) mock_service.create_job.side_effect = [job0, job1] sampler = ionq.Sampler(service=mock_service, target='qpu') q0 = cirq.LineQubit(0) x = sp.Symbol('x') circuit = cirq.Circuit(cirq.X(q0) ** x, cirq.measure(q0, key='a')) results = sampler.sample( program=circuit, repetitions=4, params=[cirq.ParamResolver({x: '0.5'}), cirq.ParamResolver({x: '0.6'})], ) pd.testing.assert_frame_equal( results, pd.DataFrame( columns=['x', 'a'], index=[0, 1, 2, 3] * 2, data=[[0.5, 0], [0.5, 1], [0.5, 1], [0.5, 1], [0.6, 0], [0.6, 0], [0.6, 1], [0.6, 1]], ), ) circuit0 = cirq.Circuit(cirq.X(q0) ** 0.5, cirq.measure(q0, key='a')) circuit1 = cirq.Circuit(cirq.X(q0) ** 0.6, cirq.measure(q0, key='a')) mock_service.create_job.assert_has_calls( [ mock.call(circuit=circuit0, repetitions=4, target='qpu'), mock.call(circuit=circuit1, repetitions=4, target='qpu'), ] ) assert mock_service.create_job.call_count == 2
def test_job_status_refresh(): for status in ionq.Job.NON_TERMINAL_STATES: mock_client = mock.MagicMock() mock_client.get_job.return_value = { 'id': 'my_id', 'status': 'completed' } job = ionq.Job(mock_client, {'id': 'my_id', 'status': status}) assert job.status() == 'completed' mock_client.get_job.assert_called_with('my_id') for status in ionq.Job.TERMINAL_STATES: mock_client = mock.MagicMock() job = ionq.Job(mock_client, {'id': 'my_id', 'status': status}) assert job.status() == status mock_client.get_job.assert_not_called()
def test_sampler_simulator(): mock_service = mock.MagicMock() job_dict = { 'id': '1', 'status': 'completed', 'qubits': '1', 'target': 'simulator', 'metadata': { 'shots': 4, 'measurement0': f'a{chr(31)}0' }, 'data': { 'histogram': { '0': '0.25', '1': '0.75' } }, } job = ionq.Job(client=mock_service, job_dict=job_dict) mock_service.create_job.return_value = job sampler = ionq.Sampler(service=mock_service, target='simulator', seed=10) q0 = cirq.LineQubit(0) circuit = cirq.Circuit(cirq.X(q0), cirq.measure(q0, key='a')) results = sampler.sample(program=circuit, repetitions=4) pd.testing.assert_frame_equal( results, pd.DataFrame(columns=['a'], index=[0, 1, 2, 3], data=[[1], [0], [1], [1]])) mock_service.create_job.assert_called_once_with(circuit=circuit, repetitions=4, target='simulator')
def test_job_results_poll(mock_sleep): ready_job = { 'id': 'my_id', 'status': 'ready', } completed_job = { 'id': 'my_id', 'status': 'completed', 'qubits': '1', 'target': 'qpu', 'metadata': { 'shots': 1000 }, 'data': { 'histogram': { '0': '0.6', '1': '0.4' } }, } mock_client = mock.MagicMock() mock_client.get_job.side_effect = [ready_job, completed_job] job = ionq.Job(mock_client, ready_job) results = job.results(polling_seconds=0) assert results == ionq.QPUResult({0: 600, 1: 400}, 1, measurement_dict={}) mock_sleep.assert_called_once()
def test_job_results_qpu(): job_dict = { 'id': 'my_id', 'status': 'completed', 'qubits': '2', 'target': 'qpu', 'metadata': { 'shots': 1000, 'measurement0': f'a{chr(31)}0,1' }, 'data': { 'histogram': { '0': '0.6', '2': '0.4' } }, 'warning': { 'messages': ['foo', 'bar'] }, } job = ionq.Job(None, job_dict) with warnings.catch_warnings(record=True) as w: results = job.results() assert len(w) == 2 assert "foo" in str(w[0].message) assert "bar" in str(w[1].message) expected = ionq.QPUResult({0: 600, 1: 400}, 2, {'a': [0, 1]}) assert results == expected
def test_job_results_poll_timeout(mock_sleep): ready_job = {'id': 'my_id', 'status': 'ready'} mock_client = mock.MagicMock() mock_client.get_job.return_value = ready_job job = ionq.Job(mock_client, ready_job) with pytest.raises(TimeoutError, match='seconds'): _ = job.results(timeout_seconds=1, polling_seconds=0.1) assert mock_sleep.call_count == 11
def test_job_delete(): ready_job = {'id': 'my_id', 'status': 'ready'} deleted_job = {'id': 'my_id', 'status': 'deleted'} mock_client = mock.MagicMock() mock_client.delete_job.return_value = deleted_job job = ionq.Job(mock_client, ready_job) job.delete() mock_client.delete_job.assert_called_with(job_id='my_id') assert job.status() == 'deleted'
def test_job_cancel(): ready_job = {'id': 'my_id', 'status': 'ready'} canceled_job = {'id': 'my_id', 'status': 'canceled'} mock_client = mock.MagicMock() mock_client.cancel_job.return_value = canceled_job job = ionq.Job(mock_client, ready_job) job.cancel() mock_client.cancel_job.assert_called_with(job_id='my_id') assert job.status() == 'canceled'
def test_job_results_failed(): job_dict = { 'id': 'my_id', 'status': 'failed', 'failure': { 'error': 'too many qubits' } } job = ionq.Job(None, job_dict) with pytest.raises(RuntimeError, match='too many qubits'): _ = job.results() assert job.status() == 'failed'
def test_job_results_poll_timeout_with_error_message(mock_sleep): ready_job = { 'id': 'my_id', 'status': 'failure', 'failure': { 'error': 'too many qubits' } } mock_client = mock.MagicMock() mock_client.get_job.return_value = ready_job job = ionq.Job(mock_client, ready_job) with pytest.raises(RuntimeError, match='too many qubits'): _ = job.results(timeout_seconds=1, polling_seconds=0.1) assert mock_sleep.call_count == 11
def test_job_results_qpu_target_endianness(): job_dict = { 'id': 'my_id', 'status': 'completed', 'qubits': '2', 'target': 'qpu.target', 'metadata': { 'shots': 1000 }, 'data': { 'histogram': { '0': '0.6', '1': '0.4' } }, } job = ionq.Job(None, job_dict) results = job.results() assert results == ionq.QPUResult({0: 600, 2: 400}, 2, measurement_dict={})
def test_job_fields_update_status(): job_dict = { 'id': 'my_id', 'target': 'qpu', 'name': 'bacon', 'qubits': '5', 'status': 'running', 'metadata': { 'shots': 1000 }, } mock_client = mock.MagicMock() mock_client.get_job.return_value = job_dict job = ionq.Job(mock_client, job_dict) assert job.job_id() == 'my_id' assert job.target() == 'qpu' assert job.name() == 'bacon' assert job.num_qubits() == 5 assert job.repetitions() == 1000
def test_job_fields(): job_dict = { 'id': 'my_id', 'target': 'qpu', 'name': 'bacon', 'qubits': '5', 'status': 'completed', 'metadata': { 'shots': 1000, 'measurement0': f'a{chr(31)}0,1' }, } job = ionq.Job(None, job_dict) assert job.job_id() == 'my_id' assert job.target() == 'qpu' assert job.name() == 'bacon' assert job.num_qubits() == 5 assert job.repetitions() == 1000 assert job.measurement_dict() == {'a': [0, 1]}
def test_job_results_simulator_endianness(): job_dict = { 'id': 'my_id', 'status': 'completed', 'qubits': '2', 'target': 'simulator', 'data': { 'histogram': { '0': '0.6', '1': '0.4' } }, 'metadata': { 'shots': '100' }, } job = ionq.Job(None, job_dict) results = job.results() assert results == ionq.SimulatorResult({0: 0.6, 2: 0.4}, 2, {}, 100)
def test_job_fields_unsuccessful(): job_dict = { 'id': 'my_id', 'target': 'qpu', 'name': 'bacon', 'qubits': '5', 'status': 'deleted', 'metadata': { 'shots': 1000 }, } job = ionq.Job(None, job_dict) with pytest.raises(ionq.IonQUnsuccessfulJobException, match='deleted'): _ = job.target() with pytest.raises(ionq.IonQUnsuccessfulJobException, match='deleted'): _ = job.name() with pytest.raises(ionq.IonQUnsuccessfulJobException, match='deleted'): _ = job.num_qubits() with pytest.raises(ionq.IonQUnsuccessfulJobException, match='deleted'): _ = job.repetitions()
def test_job_results_qpu(): job_dict = { 'id': 'my_id', 'status': 'completed', 'qubits': '2', 'target': 'qpu', 'metadata': { 'shots': 1000, 'measurement0': f'a{chr(31)}0,1' }, 'data': { 'histogram': { '0': '0.6', '2': '0.4' } }, } job = ionq.Job(None, job_dict) results = job.results() expected = ionq.QPUResult({0: 600, 1: 400}, 2, {'a': [0, 1]}) assert results == expected
def test_job_results_rounding_qpu(): job_dict = { 'id': 'my_id', 'status': 'completed', 'qubits': '2', 'target': 'qpu', 'metadata': { 'shots': 5000, 'measurement0': f'a{chr(31)}0,1' }, 'data': { 'histogram': { '0': '0.0006', '2': '0.9994' } }, } # 5000*0.0006 ~ 2.9999 but should be interpreted as 3 job = ionq.Job(None, job_dict) expected = ionq.QPUResult({0: 3, 1: 4997}, 2, {'a': [0, 1]}) results = job.results() assert results == expected
def test_job_fields_cannot_get_status(): job_dict = { 'id': 'my_id', 'target': 'qpu', 'name': 'bacon', 'qubits': '5', 'status': 'running', 'metadata': { 'shots': 1000 }, } mock_client = mock.MagicMock() mock_client.get_job.side_effect = ionq.IonQException('bad') job = ionq.Job(mock_client, job_dict) with pytest.raises(ionq.IonQException, match='bad'): _ = job.target() with pytest.raises(ionq.IonQException, match='bad'): _ = job.name() with pytest.raises(ionq.IonQException, match='bad'): _ = job.num_qubits() with pytest.raises(ionq.IonQException, match='bad'): _ = job.repetitions()
def test_job_str(): job = ionq.Job(None, {'id': 'my_id'}) assert str(job) == 'cirq_ionq.Job(job_id=my_id)'
def test_job_results_failed_no_error_message(): job_dict = {'id': 'my_id', 'status': 'failed', 'failure': {}} job = ionq.Job(None, job_dict) with pytest.raises(RuntimeError, match='failed'): _ = job.results() assert job.status() == 'failed'