示例#1
0
文件: job_test.py 项目: tbcdebug/Cirq
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()
示例#2
0
文件: job_test.py 项目: m-szalay/Cirq
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)
    mock_sleep.assert_called_once()
示例#3
0
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')
示例#4
0
def test_job_fields_simulator_repetitions():
    job_dict = {
        'id': 'my_id',
        'target': 'simulator',
        'qubits': '5',
    }
    job = ionq.Job(None, job_dict)
    assert job.repetitions() is None
示例#5
0
文件: job_test.py 项目: m-szalay/Cirq
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(RuntimeError, match='ready'):
        _ = job.results(timeout_seconds=1, polling_seconds=0.1)
    assert mock_sleep.call_count == 11
示例#6
0
文件: job_test.py 项目: tbcdebug/Cirq
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'}},
    }
    job = ionq.Job(None, job_dict)
    results = job.results()
    assert results == ionq.SimulatorResult({0: 0.6, 2: 0.4}, 2)
示例#7
0
文件: job_test.py 项目: tbcdebug/Cirq
def test_job_results_qpu_endianness():
    job_dict = {
        'id': 'my_id',
        'status': 'completed',
        'qubits': '2',
        'target': 'qpu',
        '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)
示例#8
0
文件: job_test.py 项目: m-szalay/Cirq
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'
示例#9
0
文件: job_test.py 项目: m-szalay/Cirq
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'
示例#10
0
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'
示例#11
0
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
示例#12
0
文件: job_test.py 项目: tbcdebug/Cirq
def test_job_fields():
    job_dict = {
        'id': 'my_id',
        'target': 'qpu',
        'name': 'bacon',
        'qubits': '5',
        'status': 'completed',
        'metadata': {'shots': 1000},
    }
    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
示例#13
0
文件: job_test.py 项目: tbcdebug/Cirq
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
示例#14
0
文件: job_test.py 项目: tbcdebug/Cirq
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()
示例#15
0
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]}
示例#16
0
def test_job_results_simulator():
    job_dict = {
        'id': 'my_id',
        'status': 'completed',
        'qubits': '1',
        '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, 1: 0.4}, 1, {}, 100)
示例#17
0
文件: job_test.py 项目: tbcdebug/Cirq
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()
示例#18
0
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
示例#19
0
文件: job_test.py 项目: m-szalay/Cirq
def test_job_str():
    job = ionq.Job(None, {'id': 'my_id'})
    assert str(job) == 'cirq.ionq.Job(job_id=my_id)'
示例#20
0
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