Example #1
0
def test_get_batch_size(get_program):
    # Has to fetch from engine if not _program specified.
    program = cg.EngineProgram('a',
                               'b',
                               EngineContext(),
                               result_type=ResultType.Batch)
    get_program.return_value = qtypes.QuantumProgram(code=_BATCH_PROGRAM_V2)
    assert program.batch_size() == 1

    # If _program specified, uses that value.
    program = cg.EngineProgram(
        'a',
        'b',
        EngineContext(),
        _program=qtypes.QuantumProgram(code=_BATCH_PROGRAM_V2),
        result_type=ResultType.Batch,
    )
    assert program.batch_size() == 1

    with pytest.raises(ValueError, match='ResultType.Program'):
        program = cg.EngineProgram('a',
                                   'b',
                                   EngineContext(),
                                   result_type=ResultType.Program)
        _ = program.batch_size()

    with pytest.raises(ValueError, match='cirq.google.api.v2.Program'):
        get_program.return_value = qtypes.QuantumProgram(code=_PROGRAM_V2)
        program = cg.EngineProgram('a',
                                   'b',
                                   EngineContext(),
                                   result_type=ResultType.Batch)
        _ = program.batch_size()
Example #2
0
def test_description(get_program):
    program = cg.EngineProgram(
        'a', 'b', EngineContext(), _program=quantum.QuantumProgram(description='hello')
    )
    assert program.description() == 'hello'

    get_program.return_value = quantum.QuantumProgram(description='hello')
    assert cg.EngineProgram('a', 'b', EngineContext()).description() == 'hello'
    get_program.assert_called_once_with('a', 'b', False)
Example #3
0
def test_get_circuit_unsupported_program_type(get_program):
    program = cg.EngineProgram('a', 'b', EngineContext())
    get_program.return_value = qtypes.QuantumProgram(code=qtypes.any_pb2.Any(
        type_url='type.googleapis.com/unknown.proto'))

    with pytest.raises(ValueError, match='unknown.proto'):
        program.get_circuit()
Example #4
0
def test_delete(delete_program):
    program = cg.EngineProgram('a', 'b', EngineContext())
    program.delete()
    delete_program.assert_called_with('a', 'b', delete_jobs=False)

    program.delete(delete_jobs=True)
    delete_program.assert_called_with('a', 'b', delete_jobs=True)
Example #5
0
def test_labels():
    program = cg.EngineProgram(
        'a',
        'b',
        EngineContext(),
        _program=qtypes.QuantumProgram(labels={'t': '1'}))
    assert program.labels() == {'t': '1'}
Example #6
0
def test_add_labels(add_program_labels):
    program = cg.EngineProgram('a',
                               'b',
                               EngineContext(),
                               _program=qtypes.QuantumProgram(labels={}))
    assert program.labels() == {}

    add_program_labels.return_value = qtypes.QuantumProgram(labels={
        'a': '1',
    })
    assert program.add_labels({'a': '1'}).labels() == {'a': '1'}
    add_program_labels.assert_called_with('a', 'b', {'a': '1'})

    add_program_labels.return_value = qtypes.QuantumProgram(labels={
        'a': '2',
        'b': '1'
    })
    assert program.add_labels({
        'a': '2',
        'b': '1'
    }).labels() == {
        'a': '2',
        'b': '1'
    }
    add_program_labels.assert_called_with('a', 'b', {'a': '2', 'b': '1'})
Example #7
0
def test_get_circuit_v1(get_program):
    program = cg.EngineProgram('a', 'b', EngineContext())
    get_program.return_value = qtypes.QuantumProgram(
        code=_to_any(v1.program_pb2.Program()))

    with pytest.raises(ValueError, match='v1 Program is not supported'):
        program.get_circuit()
Example #8
0
def test_run_sweeps_delegation(create_job):
    create_job.return_value = ('steve', quantum.QuantumJob())
    program = cg.EngineProgram('my-proj', 'my-prog', EngineContext())
    param_resolver = cirq.ParamResolver({})
    job = program.run_sweep(
        job_id='steve', repetitions=10, params=param_resolver, processor_ids=['mine']
    )
    assert job._job == quantum.QuantumJob()
Example #9
0
def test_run_batch_delegation(create_job):
    create_job.return_value = ('kittens', quantum.QuantumJob())
    program = cg.EngineProgram('my-meow', 'my-meow', EngineContext(), result_type=ResultType.Batch)
    resolver_list = [cirq.Points('cats', [1.0, 2.0, 3.0]), cirq.Points('cats', [4.0, 5.0, 6.0])]
    job = program.run_batch(
        job_id='steve', repetitions=10, params_list=resolver_list, processor_ids=['lazykitty']
    )
    assert job._job == quantum.QuantumJob()
Example #10
0
def test_run_calibration_no_processors(create_job):
    create_job.return_value = ('dogs', qtypes.QuantumJob())
    program = cg.EngineProgram('woof',
                               'woof',
                               EngineContext(),
                               result_type=ResultType.Calibration)
    with pytest.raises(ValueError, match='No processors specified'):
        _ = program.run_calibration(job_id='spot')
Example #11
0
def test_run_calibration_delegation(create_job):
    create_job.return_value = ('dogs', qtypes.QuantumJob())
    program = cg.EngineProgram('woof',
                               'woof',
                               EngineContext(),
                               result_type=ResultType.Calibration)
    job = program.run_calibration(processor_ids=['lazydog'])
    assert job._job == qtypes.QuantumJob()
Example #12
0
def test_get_circuit_v2_unknown_gateset(get_program):
    program = cg.EngineProgram('a', 'b', EngineContext())
    get_program.return_value = qtypes.QuantumProgram(code=_to_any(
        v2.program_pb2.Program(language=v2.program_pb2.Language(
            gate_set="BAD_GATESET"))))

    with pytest.raises(ValueError, match='BAD_GATESET'):
        program.get_circuit()
Example #13
0
def test_run_in_batch_mode():
    program = cg.EngineProgram('no-meow',
                               'no-meow',
                               EngineContext(),
                               result_type=ResultType.Batch)
    with pytest.raises(ValueError, match='Please use run_batch'):
        _ = program.run_sweep(repetitions=1,
                              processor_ids=['lazykitty'],
                              params=cirq.Points('cats', [1.0, 2.0, 3.0]))
Example #14
0
def test_get_circuit_v2(get_program):
    circuit = cirq.Circuit(
        cirq.X(cirq.GridQubit(5, 2))**0.5,
        cirq.measure(cirq.GridQubit(5, 2), key='result'))

    program = cg.EngineProgram('a', 'b', EngineContext())
    get_program.return_value = qtypes.QuantumProgram(code=_PROGRAM_V2)
    assert program.get_circuit() == circuit
    get_program.assert_called_once_with('a', 'b', True)
Example #15
0
def test_create_time():
    program = cg.EngineProgram(
        'a',
        'b',
        EngineContext(),
        _program=qtypes.QuantumProgram(
            create_time=qtypes.timestamp_pb2.Timestamp(seconds=1581515101)),
    )
    assert program.create_time() == datetime.datetime(2020, 2, 12, 13, 45, 1)
Example #16
0
def test_set_labels(set_program_labels):
    program = cg.EngineProgram('a', 'b', EngineContext())
    set_program_labels.return_value = quantum.QuantumProgram(labels={'a': '1', 'b': '1'})
    assert program.set_labels({'a': '1', 'b': '1'}).labels() == {'a': '1', 'b': '1'}
    set_program_labels.assert_called_with('a', 'b', {'a': '1', 'b': '1'})

    set_program_labels.return_value = quantum.QuantumProgram()
    assert program.set_labels({}).labels() == {}
    set_program_labels.assert_called_with('a', 'b', {})
Example #17
0
def test_set_description(set_program_description):
    program = cg.EngineProgram('a', 'b', EngineContext())
    set_program_description.return_value = quantum.QuantumProgram(description='world')
    assert program.set_description('world').description() == 'world'
    set_program_description.assert_called_with('a', 'b', 'world')

    set_program_description.return_value = quantum.QuantumProgram(description='')
    assert program.set_description('').description() == ''
    set_program_description.assert_called_with('a', 'b', '')
Example #18
0
def test_update_time(get_program):
    program = cg.EngineProgram('a', 'b', EngineContext())
    get_program.return_value = quantum.QuantumProgram(
        update_time=timestamp_pb2.Timestamp(seconds=1581515101)
    )
    assert program.update_time() == datetime.datetime(
        2020, 2, 12, 13, 45, 1, tzinfo=datetime.timezone.utc
    )
    get_program.assert_called_once_with('a', 'b', False)
Example #19
0
def test_run_batch_not_in_batch_mode():
    program = cg.EngineProgram('no-meow', 'no-meow', EngineContext())
    resolver_list = [
        cirq.Points('cats', [1.0, 2.0, 3.0]),
        cirq.Points('cats', [4.0, 5.0, 6.0])
    ]
    with pytest.raises(ValueError, match='Can only use run_batch'):
        _ = program.run_batch(repetitions=1,
                              processor_ids=['lazykitty'],
                              params_list=resolver_list)
Example #20
0
def test_run_batch_no_processors():
    program = cg.EngineProgram('no-meow',
                               'no-meow',
                               EngineContext(),
                               result_type=ResultType.Batch)
    resolver_list = [
        cirq.Points('cats', [1.0, 2.0]),
        cirq.Points('cats', [3.0, 4.0])
    ]
    with pytest.raises(ValueError, match='No processors specified'):
        _ = program.run_batch(repetitions=1, params_list=resolver_list)
Example #21
0
def test_remove_labels(remove_program_labels):
    program = cg.EngineProgram(
        'a', 'b', EngineContext(), _program=quantum.QuantumProgram(labels={'a': '1', 'b': '1'})
    )
    assert program.labels() == {'a': '1', 'b': '1'}

    remove_program_labels.return_value = quantum.QuantumProgram(labels={'b': '1'})
    assert program.remove_labels(['a']).labels() == {'b': '1'}
    remove_program_labels.assert_called_with('a', 'b', ['a'])

    remove_program_labels.return_value = quantum.QuantumProgram(labels={})
    assert program.remove_labels(['a', 'b', 'c']).labels() == {}
    remove_program_labels.assert_called_with('a', 'b', ['a', 'b', 'c'])
Example #22
0
def test_get_circuit_batch(get_program):
    circuit = cirq.Circuit(
        cirq.X(cirq.GridQubit(5, 2)) ** 0.5, cirq.measure(cirq.GridQubit(5, 2), key='result')
    )

    program = cg.EngineProgram('a', 'b', EngineContext())
    get_program.return_value = quantum.QuantumProgram(code=_BATCH_PROGRAM_V2)
    with pytest.raises(ValueError, match='A program number must be specified'):
        program.get_circuit()
    with pytest.raises(ValueError, match='Only 1 in the batch but index 1 was specified'):
        program.get_circuit(1)
    assert program.get_circuit(0) == circuit
    get_program.assert_called_once_with('a', 'b', True)
Example #23
0
def test_run_delegation(create_job, get_results):
    dt = datetime.datetime.now(tz=datetime.timezone.utc)
    create_job.return_value = (
        'steve',
        quantum.QuantumJob(
            name='projects/a/programs/b/jobs/steve',
            execution_status=quantum.ExecutionStatus(state=quantum.ExecutionStatus.State.SUCCESS),
            update_time=dt,
        ),
    )
    get_results.return_value = quantum.QuantumResult(
        result=util.pack_any(
            Merge(
                """sweep_results: [{
        repetitions: 4,
        parameterized_results: [{
            params: {
                assignments: {
                    key: 'a'
                    value: 1
                }
            },
            measurement_results: {
                key: 'q'
                qubit_measurement_results: [{
                  qubit: {
                    id: '1_1'
                  }
                  results: '\006'
                }]
            }
        }]
    }]
""",
                v2.result_pb2.Result(),
            )
        )
    )

    program = cg.EngineProgram('a', 'b', EngineContext())
    param_resolver = cirq.ParamResolver({})
    results = program.run(
        job_id='steve', repetitions=10, param_resolver=param_resolver, processor_ids=['mine']
    )

    assert results == cg.EngineResult(
        params=cirq.ParamResolver({'a': 1.0}),
        measurements={'q': np.array([[False], [True], [True], [False]], dtype=bool)},
        job_id='steve',
        job_finished_time=dt,
    )
Example #24
0
def test_run_batch_no_sweeps(create_job):
    # Running with no sweeps is fine. Uses program's batch size to create
    # proper empty sweeps.
    create_job.return_value = ('kittens', quantum.QuantumJob())
    program = cg.EngineProgram(
        'my-meow',
        'my-meow',
        _program=quantum.QuantumProgram(code=_BATCH_PROGRAM_V2),
        context=EngineContext(),
        result_type=ResultType.Batch,
    )
    job = program.run_batch(job_id='steve', repetitions=10, processor_ids=['lazykitty'])
    assert job._job == quantum.QuantumJob()
    batch_run_context = v2.batch_pb2.BatchRunContext()
    create_job.call_args[1]['run_context'].Unpack(batch_run_context)
    assert len(batch_run_context.run_contexts) == 1
Example #25
0
def test_list_jobs(list_jobs):
    job1 = quantum.QuantumJob(name='projects/proj/programs/prog1/jobs/job1')
    job2 = quantum.QuantumJob(name='projects/otherproj/programs/prog1/jobs/job2')
    list_jobs.return_value = [job1, job2]

    ctx = EngineContext()
    result = cg.EngineProgram(project_id='proj', program_id='prog1', context=ctx).list_jobs()
    list_jobs.assert_called_once_with(
        'proj',
        'prog1',
        created_after=None,
        created_before=None,
        has_labels=None,
        execution_states=None,
    )
    assert [(j.program_id, j.project_id, j.job_id, j.context, j._job) for j in result] == [
        ('prog1', 'proj', 'job1', ctx, job1),
        ('prog1', 'otherproj', 'job2', ctx, job2),
    ]
Example #26
0
def test_str():
    program = cg.EngineProgram('my-proj', 'my-prog', EngineContext())
    assert str(
        program
    ) == 'EngineProgram(project_id=\'my-proj\', program_id=\'my-prog\')'
Example #27
0
def test_delete_jobs(delete_job):
    program = cg.EngineProgram('a', 'b', EngineContext())
    program.delete_job('c')
    delete_job.assert_called_with('a', 'b', 'c')
Example #28
0
def test_bad_sweep_proto():
    engine = cg.Engine(project_id='project-id',
                       proto_version=cg.ProtoVersion.UNDEFINED)
    program = cg.EngineProgram('proj', 'prog', engine.context)
    with pytest.raises(ValueError, match='invalid run context proto version'):
        program.run_sweep()
Example #29
0
def test_engine():
    program = cg.EngineProgram('a', 'b', EngineContext())
    assert program.engine().project_id == 'a'
Example #30
0
def test_get_job():
    program = cg.EngineProgram('a', 'b', EngineContext())
    assert program.get_job('c').job_id == 'c'