コード例 #1
0
ファイル: params_test.py プロジェクト: zhiyuzh/Cirq
def test_gen_param_sweep():
    s1 = {'parameter_key': 'foo', 'points': {'points': [1, 2, 3]}}
    s2 = {'parameter_key': 'bar', 'points': {'points': [4, 5]}}
    ps = {'sweep': {'factors': [{'sweeps': [s1]}, {'sweeps': [s2]}]}}
    out = cg.sweep_from_proto_dict(ps)
    assert out == cirq.Product(cirq.Zip(cirq.Points('foo', [1, 2, 3])),
                               cirq.Zip(cirq.Points('bar', [4, 5])))
コード例 #2
0
def test_gen_param_sweep():
    ps = params_pb2.ParameterSweep(
        sweep=params_pb2.ProductSweep(
            factors=[
                params_pb2.ZipSweep(
                    sweeps=[
                        params_pb2.SingleSweep(
                            parameter_key='foo', points=params_pb2.Points(points=[1, 2, 3])
                        )
                    ]
                ),
                params_pb2.ZipSweep(
                    sweeps=[
                        params_pb2.SingleSweep(
                            parameter_key='bar', points=params_pb2.Points(points=[4, 5])
                        )
                    ]
                ),
            ]
        )
    )
    out = params.sweep_from_proto(ps)
    assert out == cirq.Product(
        cirq.Zip(cirq.Points('foo', [1, 2, 3])), cirq.Zip(cirq.Points('bar', [4, 5]))
    )
コード例 #3
0
ファイル: sweeps_test.py プロジェクト: PWJ1900/Rlearncirq
def test_dict_to_zip_sweep():
    assert cirq.dict_to_zip_sweep({'t': [0, 2, 3]
                                   }) == (cirq.Zip(cirq.Points('t',
                                                               [0, 2, 3])))

    assert cirq.dict_to_zip_sweep({
        't': [0, 1],
        's': [2, 3],
        'r': 4
    }) == (cirq.Zip(cirq.Points('t', [0, 1]), cirq.Points('s', [2, 3]),
                    cirq.Points('r', [4])))
コード例 #4
0
ファイル: sweeps.py プロジェクト: towynlin/Cirq
def sweep_from_proto(msg: run_context_pb2.Sweep) -> cirq.Sweep:
    """Creates a Sweep from a v2 protobuf message."""
    which = msg.WhichOneof('sweep')
    if which is None:
        return cirq.UnitSweep
    if which == 'sweep_function':
        factors = [sweep_from_proto(m) for m in msg.sweep_function.sweeps]
        func_type = msg.sweep_function.function_type
        if func_type == run_context_pb2.SweepFunction.PRODUCT:
            return cirq.Product(*factors)
        if func_type == run_context_pb2.SweepFunction.ZIP:
            return cirq.Zip(*factors)

        raise ValueError(f'invalid sweep function type: {func_type}')
    if which == 'single_sweep':
        key = msg.single_sweep.parameter_key
        if msg.single_sweep.WhichOneof('sweep') == 'linspace':
            return cirq.Linspace(
                key=key,
                start=msg.single_sweep.linspace.first_point,
                stop=msg.single_sweep.linspace.last_point,
                length=msg.single_sweep.linspace.num_points,
            )
        if msg.single_sweep.WhichOneof('sweep') == 'points':
            return cirq.Points(key=key, points=msg.single_sweep.points.points)

        raise ValueError(f'single sweep type not set: {msg}')

    # coverage: ignore
    raise ValueError(f'sweep type not set: {msg}')
コード例 #5
0
def _create_rep_rate_circuit(
        parameterized: bool, gate: cirq.Gate, qubits: List[cirq.Qid],
        depth: int,
        num_sweeps: Optional[int]) -> Tuple[cirq.Circuit, cirq.Sweep]:
    """Creates a testing circuit based on parameters.

        The circuit will be of alternating single and two qubit layers
        using the qubits specified and a number of moments specified by depth.

        This function will also create a sweep if parameterized is true,
        by parameterized all single qubit layers with random values for
        angles.
        """

    c = cirq.Circuit()
    symbol_count = 0
    for layer in range(depth):
        if layer % 2:
            moment, symbol_count = _sq_layer(qubits, parameterized,
                                             symbol_count)
            c.append(moment)
        else:
            c.append(_entangling_layer(gate, qubits))
    c.append(cirq.Moment(cirq.measure(*qubits)))

    if not parameterized:
        return (c, None)
    sweeps = cirq.Zip(*[
        cirq.Linspace('s_%d' % i, start=0, stop=1, length=num_sweeps)
        for i in range(symbol_count)
    ])
    return (c, sweeps)
コード例 #6
0
def test_cpmg_sweep():
    sweep = t2._cpmg_sweep([1, 3, 5])
    expected = cirq.Zip(cirq.Points('pulse_0', [1, 1, 1]),
                        cirq.Points('pulse_1', [0, 1, 1]),
                        cirq.Points('pulse_2', [0, 1, 1]),
                        cirq.Points('pulse_3', [0, 0, 1]),
                        cirq.Points('pulse_4', [0, 0, 1]))
    assert sweep == expected
コード例 #7
0
def _sweep_from_param_sweep_zip_proto(
        param_sweep_zip: params_pb2.ZipSweep) -> cirq.Sweep:
    if len(param_sweep_zip.sweeps) > 0:
        return cirq.Zip(*[
            _sweep_from_single_param_sweep_proto(sweep)
            for sweep in param_sweep_zip.sweeps
        ])
    return cirq.UnitSweep
コード例 #8
0
def _to_zip_product(sweep: cirq.Sweep) -> cirq.Product:
    """Converts sweep to a product of zips of single sweeps, if possible."""
    if not isinstance(sweep, cirq.Product):
        sweep = cirq.Product(sweep)
    if not all(isinstance(f, cirq.Zip) for f in sweep.factors):
        factors = [
            f if isinstance(f, cirq.Zip) else cirq.Zip(f)
            for f in sweep.factors
        ]
        sweep = cirq.Product(*factors)
    for factor in sweep.factors:
        for term in cast(cirq.Zip, factor).sweeps:
            if not isinstance(term, sweeps.SingleSweep):
                raise ValueError(
                    f'cannot convert to zip-product form: {sweep}')
    return sweep
コード例 #9
0
ファイル: params_test.py プロジェクト: zhiyuzh/Cirq
    return [empty_sweep, empty_product, empty_zip, full_sweep]


@pytest.mark.parametrize('param_sweep', example_sweeps())
def test_param_sweep_size_versus_gen(param_sweep):
    sweep = cg.sweep_from_proto_dict(param_sweep)
    print(sweep)
    predicted_size = len(sweep)
    out = list(sweep)
    assert len(out) == predicted_size


@pytest.mark.parametrize('sweep,expected', [
    (cirq.UnitSweep, cirq.UnitSweep),
    (cirq.Linspace('a', 0, 10,
                   25), cirq.Product(cirq.Zip(cirq.Linspace('a', 0, 10, 25)))),
    (cirq.Points(
        'a', [1, 2, 3]), cirq.Product(cirq.Zip(cirq.Points('a', [1, 2, 3])))),
    (
        cirq.Zip(cirq.Linspace('a', 0, 1, 5), cirq.Points('b', [1, 2, 3])),
        cirq.Product(
            cirq.Zip(cirq.Linspace('a', 0, 1, 5), cirq.Points('b',
                                                              [1, 2, 3]))),
    ),
    (
        cirq.Product(cirq.Linspace('a', 0, 1, 5), cirq.Points('b', [1, 2, 3])),
        cirq.Product(cirq.Zip(cirq.Linspace('a', 0, 1, 5)),
                     cirq.Zip(cirq.Points('b', [1, 2, 3]))),
    ),
    (
        cirq.Product(