Esempio n. 1
0
def test_multiple_serializers():
    serializer1 = cg.GateOpSerializer(
        gate_type=cirq.XPowGate,
        serialized_gate_id='x_pow',
        args=[
            cg.SerializingArg(serialized_name='half_turns',
                              serialized_type=float,
                              op_getter='exponent')
        ],
        can_serialize_predicate=lambda x: x.gate.exponent != 1)
    serializer2 = cg.GateOpSerializer(
        gate_type=cirq.XPowGate,
        serialized_gate_id='x',
        args=[
            cg.SerializingArg(serialized_name='half_turns',
                              serialized_type=float,
                              op_getter='exponent')
        ],
        can_serialize_predicate=lambda x: x.gate.exponent == 1)
    gate_set = cg.SerializableGateSet(gate_set_name='my_gate_set',
                                      serializers=[serializer1, serializer2],
                                      deserializers=[])
    q0 = cirq.GridQubit(1, 1)
    assert gate_set.serialize_op(cirq.X(q0)).gate.id == 'x'
    assert gate_set.serialize_op(cirq.X(q0)**0.5).gate.id == 'x_pow'
Esempio n. 2
0
def test_to_proto_not_required_ok():
    serializer = cg.GateOpSerializer(
        gate_type=GateWithProperty,
        serialized_gate_id='my_gate',
        args=[
            cg.SerializingArg(serialized_name='my_val',
                              serialized_type=float,
                              op_getter='val'),
            cg.SerializingArg(serialized_name='not_req',
                              serialized_type=float,
                              op_getter='not_req',
                              required=False)
        ])
    expected = op_proto({
        'gate': {
            'id': 'my_gate'
        },
        'args': {
            'my_val': {
                'arg_value': {
                    'float_value': 0.125
                }
            }
        },
        'qubits': [{
            'id': '1_2'
        }]
    })

    q = cirq.GridQubit(1, 2)
    assert serializer.to_proto(GateWithProperty(0.125)(q)) == expected
Esempio n. 3
0
def test_token_serialization_with_constant_reference(constants, expected_index,
                                                     expected_constants):
    serializer = cg.GateOpSerializer(gate_type=GateWithAttribute,
                                     serialized_gate_id='my_gate',
                                     args=[
                                         cg.SerializingArg(
                                             serialized_name='my_val',
                                             serialized_type=float,
                                             op_getter='val')
                                     ])
    # Make a local copy since we are modifying the array in-place.
    constants = copy.copy(constants)
    q = cirq.GridQubit(1, 2)
    tag = cg.CalibrationTag('my_token')
    expected = op_proto({
        'gate': {
            'id': 'my_gate'
        },
        'args': {
            'my_val': {
                'arg_value': {
                    'float_value': 0.125
                }
            }
        },
        'qubits': [{
            'id': '1_2'
        }],
        'token_constant_index': expected_index
    })
    assert expected == serializer.to_proto(
        GateWithAttribute(0.125)(q).with_tags(tag), constants=constants)
    assert constants == expected_constants
Esempio n. 4
0
def test_token_serialization():
    serializer = cg.GateOpSerializer(gate_type=GateWithAttribute,
                                     serialized_gate_id='my_gate',
                                     args=[
                                         cg.SerializingArg(
                                             serialized_name='my_val',
                                             serialized_type=float,
                                             op_getter='val')
                                     ])
    q = cirq.GridQubit(1, 2)
    tag = cg.CalibrationTag('my_token')
    expected = op_proto({
        'gate': {
            'id': 'my_gate'
        },
        'args': {
            'my_val': {
                'arg_value': {
                    'float_value': 0.125
                }
            }
        },
        'qubits': [{
            'id': '1_2'
        }],
        'token_value': 'my_token'
    })
    assert expected == serializer.to_proto(
        GateWithAttribute(0.125)(q).with_tags(tag))
Esempio n. 5
0
def test_to_proto_line_qubit_supported():
    serializer = cg.GateOpSerializer(gate_type=GateWithProperty,
                                     serialized_gate_id='my_gate',
                                     args=[
                                         cg.SerializingArg(
                                             serialized_name='my_val',
                                             serialized_type=float,
                                             op_getter='val')
                                     ])
    q = cirq.LineQubit('10')
    arg_value = 1.0
    result = serializer.to_proto(GateWithProperty(arg_value)(q))

    expected = op_proto({
        'gate': {
            'id': 'my_gate'
        },
        'args': {
            'my_val': {
                'arg_value': {
                    'float_value': arg_value
                }
            }
        },
        'qubits': [{
            'id': '10'
        }]
    })
    assert result == expected
Esempio n. 6
0
def test_to_proto_unsupported_type():
    serializer = cg.GateOpSerializer(gate_type=GateWithProperty,
                                     serialized_gate_id='my_gate',
                                     args=[
                                         cg.SerializingArg(
                                             serialized_name='my_val',
                                             serialized_type=bytes,
                                             op_getter='val')
                                     ])
    q = cirq.GridQubit(1, 2)
    with pytest.raises(ValueError, match='bytes'):
        serializer.to_proto(GateWithProperty(b's')(q))
Esempio n. 7
0
def test_to_proto_unsupported_qubit_type():
    serializer = cg.GateOpSerializer(gate_type=GateWithProperty,
                                     serialized_gate_id='my_gate',
                                     args=[
                                         cg.SerializingArg(
                                             serialized_name='my_val',
                                             serialized_type=float,
                                             op_getter='val')
                                     ])
    q = cirq.NamedQubit('a')
    with pytest.raises(ValueError, match='GridQubit'):
        serializer.to_proto(GateWithProperty(1.0)(q))
Esempio n. 8
0
def test_to_proto_required_but_not_present():
    serializer = cg.GateOpSerializer(gate_type=GateWithProperty,
                                     serialized_gate_id='my_gate',
                                     args=[
                                         cg.SerializingArg(
                                             serialized_name='my_val',
                                             serialized_type=float,
                                             op_getter=lambda x: None)
                                     ])
    q = cirq.GridQubit(1, 2)
    with pytest.raises(ValueError, match='required'):
        serializer.to_proto(GateWithProperty(1.0)(q))
Esempio n. 9
0
def test_can_serialize_gate_subclass():
    serializer = cg.GateOpSerializer(
        gate_type=GateWithAttribute,
        serialized_gate_id='my_gate',
        args=[
            cg.SerializingArg(serialized_name='my_val',
                              serialized_type=float,
                              gate_getter='val')
        ],
        can_serialize_predicate=lambda x: x.val == 1)
    assert serializer.can_serialize_gate(SubclassGate(1))
    assert not serializer.can_serialize_gate(SubclassGate(0))
Esempio n. 10
0
def test_to_proto_no_getattr():
    serializer = cg.GateOpSerializer(gate_type=GateWithProperty,
                                     serialized_gate_id='my_gate',
                                     args=[
                                         cg.SerializingArg(
                                             serialized_name='my_val',
                                             serialized_type=float,
                                             op_getter='nope')
                                     ])
    q = cirq.GridQubit(1, 2)
    with pytest.raises(ValueError, match='does not have'):
        serializer.to_proto(GateWithProperty(1.0)(q))
Esempio n. 11
0
def test_to_proto_type_mismatch(val_type, val):
    serializer = cg.GateOpSerializer(gate_type=GateWithProperty,
                                     serialized_gate_id='my_gate',
                                     args=[
                                         cg.SerializingArg(
                                             serialized_name='my_val',
                                             serialized_type=val_type,
                                             op_getter='val')
                                     ])
    q = cirq.GridQubit(1, 2)
    with pytest.raises(ValueError, match=str(type(val))):
        serializer.to_proto(GateWithProperty(val)(q))
Esempio n. 12
0
def test_to_proto_gate_mismatch():
    serializer = cg.GateOpSerializer(gate_type=GateWithProperty,
                                     serialized_gate_id='my_gate',
                                     args=[
                                         cg.SerializingArg(
                                             serialized_name='my_val',
                                             serialized_type=float,
                                             op_getter='val')
                                     ])
    q = cirq.GridQubit(1, 2)
    with pytest.raises(ValueError, match='GateWithAttribute.*GateWithProperty'):
        serializer.to_proto(GateWithAttribute(1.0)(q))
Esempio n. 13
0
def test_can_serialize_operation_subclass():
    serializer = cg.GateOpSerializer(
        gate_type=GateWithAttribute,
        serialized_gate_id='my_gate',
        args=[
            cg.SerializingArg(serialized_name='my_val',
                              serialized_type=float,
                              op_getter='val')
        ],
        can_serialize_predicate=lambda x: x.gate.val == 1)
    q = cirq.GridQubit(1, 1)
    assert serializer.can_serialize_operation(SubclassGate(1)(q))
    assert not serializer.can_serialize_operation(SubclassGate(0)(q))
Esempio n. 14
0
def test_to_proto_gate_predicate():
    serializer = cg.GateOpSerializer(
        gate_type=GateWithAttribute,
        serialized_gate_id='my_gate',
        args=[
            cg.SerializingArg(serialized_name='my_val',
                              serialized_type=float,
                              gate_getter='val')
        ],
        can_serialize_predicate=lambda x: x.val == 1)
    q = cirq.GridQubit(1, 2)
    assert serializer.to_proto_dict(GateWithAttribute(0)(q)) is None
    assert serializer.to_proto_dict(GateWithAttribute(1)(q)) is not None
def test_is_supported_gate_can_serialize_predicate():
    serializer = cg.GateOpSerializer(
        gate_type=cirq.XPowGate,
        serialized_gate_id='x_pow',
        args=[
            cg.SerializingArg(
                serialized_name='half_turns',
                serialized_type=float,
                gate_getter='exponent',
            )
        ],
        can_serialize_predicate=lambda x: x.exponent == 1.0)
    gate_set = cg.SerializableGateSet(gate_set_name='my_gate_set',
                                      serializers=[serializer],
                                      deserializers=[X_DESERIALIZER])
    assert gate_set.is_supported_gate(cirq.XPowGate())
    assert not gate_set.is_supported_gate(cirq.XPowGate()**0.5)
    assert gate_set.is_supported_gate(cirq.X)
Esempio n. 16
0
def test_is_supported_operation_can_serialize_predicate():
    q = cirq.GridQubit(1, 2)
    serializer = cg.GateOpSerializer(
        gate_type=cirq.XPowGate,
        serialized_gate_id='x_pow',
        args=[
            cg.SerializingArg(
                serialized_name='half_turns',
                serialized_type=float,
                op_getter='exponent',
            )
        ],
        can_serialize_predicate=lambda x: x.gate.exponent == 1.0,
    )
    gate_set = cg.SerializableGateSet(gate_set_name='my_gate_set',
                                      serializers=[serializer],
                                      deserializers=[X_DESERIALIZER])
    assert gate_set.is_supported_operation(cirq.XPowGate()(q))
    assert not gate_set.is_supported_operation(cirq.XPowGate()(q)**0.5)
    assert gate_set.is_supported_operation(cirq.X(q))
Esempio n. 17
0
def test_to_proto_callable(val_type, val, arg_value):
    serializer = cg.GateOpSerializer(gate_type=GateWithMethod,
                                     serialized_gate_id='my_gate',
                                     args=[
                                         cg.SerializingArg(
                                             serialized_name='my_val',
                                             serialized_type=val_type,
                                             gate_getter=get_val)
                                     ])
    q = cirq.GridQubit(1, 2)
    result = serializer.to_proto_dict(GateWithMethod(val)(q))
    expected = {
        'gate': {
            'id': 'my_gate'
        },
        'args': {
            'my_val': arg_value
        },
        'qubits': [{
            'id': '1_2'
        }]
    }
    assert result == expected
Esempio n. 18
0
def test_defaults_not_serialized():
    serializer = cg.GateOpSerializer(gate_type=GateWithAttribute,
                                     serialized_gate_id='my_gate',
                                     args=[
                                         cg.SerializingArg(
                                             serialized_name='my_val',
                                             serialized_type=float,
                                             default=1.0,
                                             op_getter='val')
                                     ])
    q = cirq.GridQubit(1, 2)
    no_default = op_proto({
        'gate': {
            'id': 'my_gate'
        },
        'args': {
            'my_val': {
                'arg_value': {
                    'float_value': 0.125
                }
            }
        },
        'qubits': [{
            'id': '1_2'
        }]
    })
    assert no_default == serializer.to_proto(GateWithAttribute(0.125)(q))
    with_default = op_proto({
        'gate': {
            'id': 'my_gate'
        },
        'qubits': [{
            'id': '1_2'
        }]
    })
    assert with_default == serializer.to_proto(GateWithAttribute(1.0)(q))
Esempio n. 19
0
def test_to_proto_property(val_type, val, arg_value):
    serializer = cg.GateOpSerializer(gate_type=GateWithProperty,
                                     serialized_gate_id='my_gate',
                                     args=[
                                         cg.SerializingArg(
                                             serialized_name='my_val',
                                             serialized_type=val_type,
                                             op_getter='val')
                                     ])
    q = cirq.GridQubit(1, 2)
    result = serializer.to_proto(GateWithProperty(val)(q),
                                 arg_function_language='linear')
    expected = op_proto({
        'gate': {
            'id': 'my_gate'
        },
        'args': {
            'my_val': arg_value
        },
        'qubits': [{
            'id': '1_2'
        }]
    })
    assert result == expected
Esempio n. 20
0
from typing import Dict
import pytest
from google.protobuf import json_format

import cirq
import cirq.google as cg
from cirq.google.api import v2

X_SERIALIZER = cg.GateOpSerializer(
    gate_type=cirq.XPowGate,
    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',
        )
    ],
)