def test_serialize_deserialize_fsim_gate_symbols(gate, theta, phi, phys_z): gate_set = cg.SerializableGateSet('test', cgc.LIMITED_FSIM_SERIALIZERS, [cgc.LIMITED_FSIM_DESERIALIZER]) q1 = cirq.GridQubit(5, 4) q2 = cirq.GridQubit(5, 5) op = gate(q1, q2) if phys_z: op = op.with_tags(cg.PhysicalZTag()) expected = op_proto({ 'gate': { 'id': 'fsim' }, 'args': { 'theta': theta, 'phi': phi, **_phys_z_args(phys_z) }, 'qubits': [{ 'id': '5_4' }, { 'id': '5_5' }], }) proto = gate_set.serialize_op(op, arg_function_language='linear') actual = gate_set.deserialize_op(proto, arg_function_language='linear') assert proto == expected assert actual == op assert_phys_z_tag(phys_z, actual)
def test_deserialize_z(half_turns): serialized_op = { 'gate': { 'id': 'z' }, 'args': { 'half_turns': { 'arg_value': { 'float_value': half_turns } }, 'type': { 'arg_value': { 'string_value': cirq.google.common_serializers.VIRTUAL_Z } } }, 'qubits': [{ 'id': '1_2' }] } q = cirq.GridQubit(1, 2) expected = cirq.ZPowGate(exponent=half_turns)(q) assert SINGLE_QUBIT_GATE_SET.deserialize_op_dict(serialized_op) == expected serialized_op['args']['type']['arg_value'][ 'string_value'] = cirq.google.common_serializers.PHYSICAL_Z expected = cirq.ZPowGate(exponent=half_turns)(q).with_tags( cg.PhysicalZTag()) assert SINGLE_QUBIT_GATE_SET.deserialize_op_dict(serialized_op) == expected
def test_serialize_deserialize_cz_gate(gate, exponent, phys_z): gate_set = cg.SerializableGateSet('test', [cgc.CZ_SERIALIZER], [cgc.CZ_POW_DESERIALIZER]) proto = op_proto({ 'gate': { 'id': 'cz' }, 'args': { 'half_turns': { 'arg_value': { 'float_value': exponent } }, **_phys_z_args(phys_z), }, 'qubits': [{ 'id': '5_4' }, { 'id': '5_5' }], }) q1 = cirq.GridQubit(5, 4) q2 = cirq.GridQubit(5, 5) op = gate(q1, q2) if phys_z: op = op.with_tags(cg.PhysicalZTag()) assert gate_set.serialize_op(op) == proto deserialized_op = gate_set.deserialize_op(proto) expected_gate = cirq.CZPowGate(exponent=exponent) cirq.testing.assert_allclose_up_to_global_phase( cirq.unitary(deserialized_op), cirq.unitary(expected_gate), atol=1e-7, ) assert_phys_z_tag(phys_z, deserialized_op)
def test_serialize_deserialize_iswap_symbols(phys_z): gate_set = cg.SerializableGateSet('test', cgc.LIMITED_FSIM_SERIALIZERS, [cgc.LIMITED_FSIM_DESERIALIZER]) q1 = cirq.GridQubit(5, 4) q2 = cirq.GridQubit(5, 5) op = cirq.ISWAP(q1, q2)**sympy.Symbol('t') if phys_z: op = op.with_tags(cg.PhysicalZTag()) proto = gate_set.serialize_op(op, arg_function_language='linear') actual = gate_set.deserialize_op(proto, arg_function_language='linear') assert isinstance(actual.untagged.gate, cirq.FSimGate) assert math.isclose(actual.untagged.gate.phi, 0) assert math.isclose(actual.untagged.gate.theta.subs('t', 2), -np.pi, abs_tol=1e-5) assert_phys_z_tag(phys_z, actual)
def test_serialize_z(gate, half_turns): q = cirq.GridQubit(1, 2) assert SINGLE_QUBIT_GATE_SET.serialize_op_dict(gate.on(q)) == { 'gate': { 'id': 'z' }, 'args': { 'half_turns': { 'arg_value': { 'float_value': half_turns } }, 'type': { 'arg_value': { 'string_value': 'virtual_propagates_forward' } } }, 'qubits': [{ 'id': '1_2' }] } physical_op = gate.on(q).with_tags(cg.PhysicalZTag()) assert SINGLE_QUBIT_GATE_SET.serialize_op_dict(physical_op) == { 'gate': { 'id': 'z' }, 'args': { 'half_turns': { 'arg_value': { 'float_value': half_turns } }, 'type': { 'arg_value': { 'string_value': cirq.google.common_serializers.PHYSICAL_Z } } }, 'qubits': [{ 'id': '1_2' }] }
def test_serialize_deserialize_fsim_gate(gate, theta, phi, phys_z): gate_set = cg.SerializableGateSet('test', cgc.LIMITED_FSIM_SERIALIZERS, [cgc.LIMITED_FSIM_DESERIALIZER]) proto = op_proto({ 'gate': { 'id': 'fsim' }, 'args': { 'theta': { 'arg_value': { 'float_value': theta } }, 'phi': { 'arg_value': { 'float_value': phi } }, **_phys_z_args(phys_z), }, 'qubits': [{ 'id': '5_4' }, { 'id': '5_5' }], }) q1 = cirq.GridQubit(5, 4) q2 = cirq.GridQubit(5, 5) op = gate(q1, q2) if phys_z: op = op.with_tags(cg.PhysicalZTag()) expected_gate = cirq.FSimGate(theta=theta, phi=phi) assert gate_set.serialize_op(op) == proto deserialized_op = gate_set.deserialize_op(proto) cirq.testing.assert_allclose_up_to_global_phase( cirq.unitary(deserialized_op), cirq.unitary(expected_gate), atol=1e-7, ) assert_phys_z_tag(phys_z, deserialized_op)
def assert_phys_z_tag(phys_z, op): has_tag = cg.PhysicalZTag() in op.tags assert has_tag == phys_z