Esempio n. 1
0
def _parameterized_value_to_proto(
        param: value.TParamVal) -> operations_pb2.ParameterizedFloat:
    if isinstance(param, sympy.Symbol):
        return operations_pb2.ParameterizedFloat(
            parameter_key=str(param.free_symbols.pop()))
    else:
        return operations_pb2.ParameterizedFloat(raw=float(param))
Esempio n. 2
0
def test_w_to_proto():
    gate = cirq.PhasedXPowGate(exponent=sympy.Symbol('k'), phase_exponent=1)
    proto = operations_pb2.Operation(exp_w=operations_pb2.ExpW(
        target=operations_pb2.Qubit(row=2, col=3),
        axis_half_turns=operations_pb2.ParameterizedFloat(raw=1),
        half_turns=operations_pb2.ParameterizedFloat(parameter_key='k')))
    assert_proto_dict_convert(gate, proto, cirq.GridQubit(2, 3))

    gate = cirq.PhasedXPowGate(exponent=0.5, phase_exponent=sympy.Symbol('j'))
    proto = operations_pb2.Operation(exp_w=operations_pb2.ExpW(
        target=operations_pb2.Qubit(row=2, col=3),
        axis_half_turns=operations_pb2.ParameterizedFloat(parameter_key='j'),
        half_turns=operations_pb2.ParameterizedFloat(raw=0.5)))
    assert_proto_dict_convert(gate, proto, cirq.GridQubit(2, 3))

    gate = cirq.X**0.25
    proto = operations_pb2.Operation(exp_w=operations_pb2.ExpW(
        target=operations_pb2.Qubit(row=2, col=3),
        axis_half_turns=operations_pb2.ParameterizedFloat(raw=0.0),
        half_turns=operations_pb2.ParameterizedFloat(raw=0.25)))
    assert_proto_dict_convert(gate, proto, cirq.GridQubit(2, 3))

    gate = cirq.Y**0.25
    proto = operations_pb2.Operation(exp_w=operations_pb2.ExpW(
        target=operations_pb2.Qubit(row=2, col=3),
        axis_half_turns=operations_pb2.ParameterizedFloat(raw=0.5),
        half_turns=operations_pb2.ParameterizedFloat(raw=0.25)))
    assert_proto_dict_convert(gate, proto, cirq.GridQubit(2, 3))

    gate = cirq.PhasedXPowGate(exponent=0.5, phase_exponent=sympy.Symbol('j'))
    proto = operations_pb2.Operation(exp_w=operations_pb2.ExpW(
        target=operations_pb2.Qubit(row=2, col=3),
        axis_half_turns=operations_pb2.ParameterizedFloat(parameter_key='j'),
        half_turns=operations_pb2.ParameterizedFloat(raw=0.5)))
    assert_proto_dict_convert(gate, proto, cirq.GridQubit(2, 3))
Esempio n. 3
0
def test_parameterized_value_from_proto():
    from_proto = programs._parameterized_value_from_proto

    m1 = operations_pb2.ParameterizedFloat(raw=5)
    assert from_proto(m1) == 5

    with pytest.raises(ValueError):
        from_proto(operations_pb2.ParameterizedFloat())

    m3 = operations_pb2.ParameterizedFloat(parameter_key='rr')
    assert from_proto(m3) == sympy.Symbol('rr')
Esempio n. 4
0
def test_z_proto_convert():
    gate = cirq.Z**sympy.Symbol('k')
    proto = operations_pb2.Operation(
        exp_z=operations_pb2.ExpZ(target=operations_pb2.Qubit(row=2, col=3),
                                  half_turns=operations_pb2.ParameterizedFloat(
                                      parameter_key='k')))

    assert_proto_dict_convert(gate, proto, cirq.GridQubit(2, 3))
    gate = cirq.Z**0.5
    proto = operations_pb2.Operation(
        exp_z=operations_pb2.ExpZ(target=operations_pb2.Qubit(row=2, col=3),
                                  half_turns=operations_pb2.ParameterizedFloat(
                                      raw=0.5)))
    assert_proto_dict_convert(gate, proto, cirq.GridQubit(2, 3))
def test_cz_proto_convert():
    gate = cirq.CZ**sympy.Symbol('k')
    proto = operations_pb2.Operation(exp_11=operations_pb2.Exp11(
        target1=operations_pb2.Qubit(row=2, col=3),
        target2=operations_pb2.Qubit(row=3, col=4),
        half_turns=operations_pb2.ParameterizedFloat(parameter_key='k'),
    ))
    assert_proto_dict_convert(gate, proto, cirq.GridQubit(2, 3),
                              cirq.GridQubit(3, 4))

    gate = cirq.CZ**0.5
    proto = operations_pb2.Operation(exp_11=operations_pb2.Exp11(
        target1=operations_pb2.Qubit(row=2, col=3),
        target2=operations_pb2.Qubit(row=3, col=4),
        half_turns=operations_pb2.ParameterizedFloat(raw=0.5),
    ))
    assert_proto_dict_convert(gate, proto, cirq.GridQubit(2, 3),
                              cirq.GridQubit(3, 4))