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 = { '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_dict(serialized) assert result == GateWithAttribute(0.125)(q)
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 = { 'gate': { 'id': 'my_gate' }, 'args': { 'my_val': {} }, 'qubits': [{ 'id': '1_2' }] } with pytest.raises(ValueError): deserializer.from_proto_dict(serialized)
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))
def test_from_proto_missing_required_arg(): 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': { 'not_req': { 'arg_value': { 'float_value': 0.125 } } }, 'qubits': [{ 'id': '1_2' }] }) with pytest.raises(ValueError): deserializer.from_proto(serialized)
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 = { '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_dict(serialized) assert result == GateWithAttribute(1.125)(q)
def test_from_proto_unknown_arg_type(): deserializer = cg.GateOpDeserializer(serialized_gate_id='my_gate', gate_constructor=GateWithAttribute, args=[ cg.DeserializingArg( serialized_name='my_val', constructor_arg_name='val', ) ]) serialized = { 'gate': { 'id': 'my_gate' }, 'args': { 'my_val': { 'arg_value': { 'what_value': 0.125 } } }, 'qubits': [{ 'id': '1_2' }] } with pytest.raises(Exception, match='what_value'): deserializer.from_proto_dict(serialized)
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 = { '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_dict(serialized, arg_function_language='linear')
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 = { 'gate': { 'id': 'my_gate' }, 'args': { 'not_my_val': { 'arg_value': { 'float_value': 0.1 } } }, 'qubits': [{ 'id': '1_2' }] } with pytest.raises(ValueError, match='my_val'): deserializer.from_proto_dict(serialized)
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
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)
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)
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)
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)
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', ) ], )