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)
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_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)
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)
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_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')
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)
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') ], )
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') ], )
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') ], )
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, )
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_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_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)
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)
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', ) ], )