def test_multiple_serializers(self): """Compound serialization.""" serializer1 = op_serializer.GateOpSerializer( gate_type=cirq.XPowGate, serialized_gate_id='x_pow', args=[ op_serializer.SerializingArg(serialized_name='half_turns', serialized_type=float, op_getter='exponent') ], can_serialize_predicate=lambda x: x.gate.exponent != 1) serializer2 = op_serializer.GateOpSerializer( gate_type=cirq.XPowGate, serialized_gate_id='x', args=[ op_serializer.SerializingArg(serialized_name='half_turns', serialized_type=float, op_getter='exponent') ], can_serialize_predicate=lambda x: x.gate.exponent == 1) gate_set = serializable_gate_set.SerializableGateSet( gate_set_name='my_gate_set', serializers=[serializer1, serializer2], deserializers=[]) q0 = cirq.GridQubit(1, 1) self.assertEqual(gate_set.serialize_op(cirq.X(q0)).gate.id, 'x') self.assertEqual( gate_set.serialize_op(cirq.X(q0)**0.5).gate.id, 'x_pow')
def test_gateset_with_added_gates(self): """Test adding new gates to gateset.""" q = cirq.GridQubit(1, 1) x_gateset = serializable_gate_set.SerializableGateSet( gate_set_name='x', serializers=[X_SERIALIZER], deserializers=[X_DESERIALIZER], ) xy_gateset = x_gateset.with_added_gates( gate_set_name='xy', serializers=[Y_SERIALIZER], deserializers=[Y_DESERIALIZER], ) self.assertEqual(x_gateset.gate_set_name, 'x') self.assertTrue(x_gateset.is_supported_operation(cirq.X(q))) self.assertFalse(x_gateset.is_supported_operation(cirq.Y(q))) self.assertEqual(xy_gateset.gate_set_name, 'xy') self.assertTrue(xy_gateset.is_supported_operation(cirq.X(q))) self.assertTrue(xy_gateset.is_supported_operation(cirq.Y(q))) # test serialization and deserialization proto = op_proto({ 'gate': { 'id': 'y_pow' }, 'args': { 'half_turns': { 'arg_value': { 'float_value': 0.125 } }, }, 'qubits': [{ 'id': '1_1' }] }) expected_gate = cirq.YPowGate(exponent=0.125)(cirq.GridQubit(1, 1)) self.assertEqual(xy_gateset.serialize_op(expected_gate), proto) self.assertEqual(xy_gateset.deserialize_op(proto), expected_gate)
def test_is_supported_operation_can_serialize_predicate(self): """Test can_serialize predicate for operations.""" q = cirq.GridQubit(1, 2) serializer = op_serializer.GateOpSerializer( gate_type=cirq.XPowGate, serialized_gate_id='x_pow', args=[ op_serializer.SerializingArg( serialized_name='half_turns', serialized_type=float, op_getter='exponent', ) ], can_serialize_predicate=lambda x: x.gate.exponent == 1.0) gate_set = serializable_gate_set.SerializableGateSet( gate_set_name='my_gate_set', serializers=[serializer], deserializers=[X_DESERIALIZER]) self.assertTrue(gate_set.is_supported_operation(cirq.XPowGate()(q))) self.assertFalse( gate_set.is_supported_operation(cirq.XPowGate()(q)**0.5)) self.assertTrue(gate_set.is_supported_operation(cirq.X(q)))
def test_gateset_with_added_gates_again(self): """Verify that adding a serializer twice doesn't mess anything up.""" q = cirq.GridQubit(2, 2) x_gateset = serializable_gate_set.SerializableGateSet( gate_set_name='x', serializers=[X_SERIALIZER], deserializers=[X_DESERIALIZER], ) xx_gateset = x_gateset.with_added_gates( gate_set_name='xx', serializers=[X_SERIALIZER], deserializers=[X_DESERIALIZER], ) self.assertEqual(xx_gateset.gate_set_name, 'xx') self.assertTrue(xx_gateset.is_supported_operation(cirq.X(q))) self.assertFalse(xx_gateset.is_supported_operation(cirq.Y(q))) # test serialization and deserialization proto = op_proto({ 'gate': { 'id': 'x_pow' }, 'args': { 'half_turns': { 'arg_value': { 'float_value': 0.125 } }, }, 'qubits': [{ 'id': '1_1' }] }) expected_gate = cirq.XPowGate(exponent=0.125)(cirq.GridQubit(1, 1)) self.assertEqual(xx_gateset.serialize_op(expected_gate), proto) self.assertEqual(xx_gateset.deserialize_op(proto), expected_gate)
) Y_DESERIALIZER = op_deserializer.GateOpDeserializer( serialized_gate_id='y_pow', gate_constructor=cirq.YPowGate, args=[ op_deserializer.DeserializingArg( serialized_name='half_turns', constructor_arg_name='exponent', ) ], ) MY_GATE_SET = serializable_gate_set.SerializableGateSet( gate_set_name='my_gate_set', serializers=[X_SERIALIZER], deserializers=[X_DESERIALIZER], ) def op_proto(json): """Json to proto.""" op = program_pb2.Operation() json_format.ParseDict(json, op) return op class SerializableGateSetTest(tf.test.TestCase): """Test SerializableGateSet and associated deserialize functionality.""" def test_supported_gate_types(self):
for g, g_name in PHASED_EIGEN_GATES_DICT.items() ] + [ _amplitude_damp_channel_deserializer(), _asymmetric_depolarize_deserializer(), _bit_flip_channel_deserializer(), _depolarize_channel_deserializer(), _fsim_gate_deserializer(), _gad_channel_deserializer(), _identity_gate_deserializer(), _phase_damp_channel_deserializer(), _reset_channel_deserializer(), _phase_flip_channel_deserializer() ] SERIALIZER = serializable_gate_set.SerializableGateSet( gate_set_name="tfq_gate_set", serializers=SERIALIZERS, deserializers=DESERIALIZERS) def serialize_circuit(circuit_inp): """Returns a `cirq.Program` proto representing the `cirq.Circuit`. Note that the circuit must use gates valid in the tfq_gate_set. Currently we only support scalar multiplication of symbols and no other more complex arithmetic expressions. This means we can support things like X**(3*alpha), and Rx(alpha). Because we use the `cirq.Program` proto, we only support `cirq.GridQubit` instances during serialization of circuits. Note: once serialized terminal measurements are removed.