コード例 #1
0
def test_from_proto_required_arg_not_assigned():
    deserializer = cg.GateOpDeserializer(
        serialized_gate_id='my_gate',
        gate_constructor=GateWithAttribute,
        args=[
            cg.DeserializingArg(serialized_name='my_val',
                                constructor_arg_name='val'),
            cg.DeserializingArg(serialized_name='not_req',
                                constructor_arg_name='not_req',
                                required=False),
        ],
    )
    serialized = op_proto({
        'gate': {
            'id': 'my_gate'
        },
        'args': {
            'my_val': {}
        },
        'qubits': [{
            'id': '1_2'
        }]
    })
    with pytest.raises(ValueError):
        deserializer.from_proto(serialized)
コード例 #2
0
def test_defaults():
    deserializer = cg.GateOpDeserializer(
        serialized_gate_id='my_gate',
        gate_constructor=GateWithAttribute,
        args=[
            cg.DeserializingArg(serialized_name='my_val',
                                constructor_arg_name='val',
                                default=1.0),
            cg.DeserializingArg(
                serialized_name='not_req',
                constructor_arg_name='not_req',
                default='hello',
                required=False,
            ),
        ],
    )
    serialized = op_proto({
        'gate': {
            'id': 'my_gate'
        },
        'args': {},
        'qubits': [{
            'id': '1_2'
        }]
    })
    g = GateWithAttribute(1.0)
    g.not_req = 'hello'
    assert deserializer.from_proto(serialized) == g(cirq.GridQubit(1, 2))
コード例 #3
0
def test_from_proto_not_required_ok():
    deserializer = cg.GateOpDeserializer(
        serialized_gate_id='my_gate',
        gate_constructor=GateWithAttribute,
        args=[
            cg.DeserializingArg(serialized_name='my_val',
                                constructor_arg_name='val'),
            cg.DeserializingArg(serialized_name='not_req',
                                constructor_arg_name='not_req',
                                required=False),
        ],
    )
    serialized = op_proto({
        'gate': {
            'id': 'my_gate'
        },
        'args': {
            'my_val': {
                'arg_value': {
                    'float_value': 0.125
                }
            }
        },
        'qubits': [{
            'id': '1_2'
        }],
    })
    q = cirq.GridQubit(1, 2)
    result = deserializer.from_proto(serialized)
    assert result == GateWithAttribute(0.125)(q)
コード例 #4
0
def test_from_proto_value_func():
    deserializer = cg.GateOpDeserializer(
        serialized_gate_id='my_gate',
        gate_constructor=GateWithAttribute,
        args=[
            cg.DeserializingArg(serialized_name='my_val',
                                constructor_arg_name='val',
                                value_func=lambda x: x + 1)
        ],
    )
    serialized = op_proto({
        'gate': {
            'id': 'my_gate'
        },
        'args': {
            'my_val': {
                'arg_value': {
                    'float_value': 0.125
                }
            }
        },
        'qubits': [{
            'id': '1_2'
        }],
    })
    q = cirq.GridQubit(1, 2)
    result = deserializer.from_proto(serialized)
    assert result == GateWithAttribute(1.125)(q)
コード例 #5
0
def test_token_with_references():
    deserializer = cg.GateOpDeserializer(
        serialized_gate_id='my_gate',
        gate_constructor=GateWithAttribute,
        args=[
            cg.DeserializingArg(serialized_name='my_val', constructor_arg_name='val'),
        ],
    )
    serialized = op_proto(
        {
            'gate': {'id': 'my_gate'},
            'args': {'my_val': {'arg_value': {'float_value': 1.25}}},
            'qubits': [{'id': '1_2'}],
            'token_constant_index': 1,
        }
    )
    op = GateWithAttribute(1.25)(cirq.GridQubit(1, 2))
    op = op.with_tags(cg.CalibrationTag('abc123'))
    constants = []
    constant = v2.program_pb2.Constant()
    constant.string_value = 'my_token'
    constants.append(constant)
    constant = v2.program_pb2.Constant()
    constant.string_value = 'abc123'
    constants.append(constant)
    assert deserializer.from_proto(serialized, constants=constants) == op

    with pytest.raises(ValueError, match='Proto has references to constants table'):
        deserializer.from_proto(serialized)
コード例 #6
0
def test_from_proto_function_argument_not_set():
    deserializer = cg.GateOpDeserializer(
        serialized_gate_id='my_gate',
        gate_constructor=GateWithAttribute,
        args=[
            cg.DeserializingArg(
                serialized_name='my_val',
                constructor_arg_name='val',
            )
        ],
    )
    serialized = op_proto(
        {
            'gate': {'id': 'my_gate'},
            'args': {
                'my_val': {
                    'func': {
                        'type': 'mul',
                        'args': [
                            {'symbol': 'x'},
                            {},
                        ],
                    }
                }
            },
            'qubits': [{'id': '1_2'}],
        }
    )
    with pytest.raises(ValueError, match='A multiplication argument is missing'):
        _ = deserializer.from_proto(serialized, arg_function_language='linear')
コード例 #7
0
def test_from_proto_unknown_function():
    deserializer = cg.GateOpDeserializer(
        serialized_gate_id='my_gate',
        gate_constructor=GateWithAttribute,
        args=[
            cg.DeserializingArg(
                serialized_name='my_val',
                constructor_arg_name='val',
            )
        ],
    )
    serialized = op_proto(
        {
            'gate': {'id': 'my_gate'},
            'args': {
                'my_val': {
                    'func': {
                        'type': 'UNKNOWN_OPERATION',
                        'args': [
                            {'symbol': 'x'},
                            {'arg_value': {'float_value': -1.0}},
                        ],
                    }
                }
            },
            'qubits': [{'id': '1_2'}],
        }
    )
    with pytest.raises(ValueError, match='Unrecognized function type'):
        _ = deserializer.from_proto(serialized)
コード例 #8
0
def base_deserializer():
    return cg.GateOpDeserializer(
        serialized_gate_id='my_gate',
        gate_constructor=GateWithAttribute,
        args=[
            cg.DeserializingArg(serialized_name='my_val',
                                constructor_arg_name='val')
        ],
    )
コード例 #9
0
def _y_deserializer():
    with cirq.testing.assert_deprecated('CircuitSerializer',
                                        deadline='v0.16',
                                        count=None):
        return cg.GateOpDeserializer(
            serialized_gate_id='y_pow',
            gate_constructor=cirq.YPowGate,
            args=[
                cg.DeserializingArg(serialized_name='half_turns',
                                    constructor_arg_name='exponent')
            ],
        )
コード例 #10
0
def base_deserializer():
    # Deprecated: cirq_google.GateOpDeserializer and cirq_google.DeserializingArg.
    with cirq.testing.assert_deprecated('CircuitSerializer',
                                        deadline='v0.16',
                                        count=2):
        return cg.GateOpDeserializer(
            serialized_gate_id='my_gate',
            gate_constructor=GateWithAttribute,
            args=[
                cg.DeserializingArg(serialized_name='my_val',
                                    constructor_arg_name='val')
            ],
        )
コード例 #11
0
def _create_deserializing_arg(*,
                              serialized_name,
                              constructor_arg_name,
                              value_func=None,
                              required=True,
                              default=None):
    with cirq.testing.assert_deprecated('CircuitSerializer',
                                        deadline='v0.16',
                                        count=1):
        return cg.DeserializingArg(
            serialized_name=serialized_name,
            constructor_arg_name=constructor_arg_name,
            value_func=value_func,
            required=required,
            default=default,
        )
コード例 #12
0
def test_from_proto(val_type, val, arg_value):
    deserializer = cg.GateOpDeserializer(
        serialized_gate_id='my_gate',
        gate_constructor=GateWithAttribute,
        args=[
            cg.DeserializingArg(
                serialized_name='my_val',
                constructor_arg_name='val',
            )
        ],
    )
    serialized = op_proto(
        {'gate': {'id': 'my_gate'}, 'args': {'my_val': arg_value}, 'qubits': [{'id': '1_2'}]}
    )
    q = cirq.GridQubit(1, 2)
    result = deserializer.from_proto(serialized, arg_function_language='linear')
    assert result == GateWithAttribute(val)(q)
コード例 #13
0
def test_token():
    deserializer = cg.GateOpDeserializer(
        serialized_gate_id='my_gate',
        gate_constructor=GateWithAttribute,
        args=[
            cg.DeserializingArg(serialized_name='my_val', constructor_arg_name='val'),
        ],
    )
    serialized = op_proto(
        {
            'gate': {'id': 'my_gate'},
            'args': {'my_val': {'arg_value': {'float_value': 1.25}}},
            'qubits': [{'id': '1_2'}],
            'token_value': 'abc123',
        }
    )
    op = GateWithAttribute(1.25)(cirq.GridQubit(1, 2))
    op = op.with_tags(cg.CalibrationTag('abc123'))
    assert deserializer.from_proto(serialized) == op
コード例 #14
0
def test_from_proto_required_missing():
    deserializer = cg.GateOpDeserializer(
        serialized_gate_id='my_gate',
        gate_constructor=GateWithAttribute,
        args=[
            cg.DeserializingArg(
                serialized_name='my_val',
                constructor_arg_name='val',
            )
        ],
    )
    serialized = op_proto(
        {
            'gate': {'id': 'my_gate'},
            'args': {'not_my_val': {'arg_value': {'float_value': 0.125}}},
            'qubits': [{'id': '1_2'}],
        }
    )
    with pytest.raises(Exception, match='my_val'):
        deserializer.from_proto(serialized)
コード例 #15
0
def test_from_proto_value_type_not_recognized():
    deserializer = cg.GateOpDeserializer(
        serialized_gate_id='my_gate',
        gate_constructor=GateWithAttribute,
        args=[
            cg.DeserializingArg(
                serialized_name='my_val',
                constructor_arg_name='val',
            )
        ],
    )
    serialized = op_proto(
        {
            'gate': {'id': 'my_gate'},
            'args': {
                'my_val': {
                    'arg_value': {},
                }
            },
            'qubits': [{'id': '1_2'}],
        }
    )
    with pytest.raises(ValueError, match='Unrecognized value type'):
        _ = deserializer.from_proto(serialized)
コード例 #16
0
    serialized_gate_id='x_pow',
    args=[
        cg.SerializingArg(
            serialized_name='half_turns',
            serialized_type=float,
            op_getter='exponent',
        )
    ],
)

X_DESERIALIZER = cg.GateOpDeserializer(
    serialized_gate_id='x_pow',
    gate_constructor=cirq.XPowGate,
    args=[
        cg.DeserializingArg(
            serialized_name='half_turns',
            constructor_arg_name='exponent',
        )
    ],
)

Y_SERIALIZER = cg.GateOpSerializer(
    gate_type=cirq.YPowGate,
    serialized_gate_id='y_pow',
    args=[
        cg.SerializingArg(
            serialized_name='half_turns',
            serialized_type=float,
            op_getter='exponent',
        )
    ],
)