Exemplo n.º 1
0
def test_serialize_deserialize_circuit_with_tokens():
    q0 = cirq.GridQubit(1, 1)
    q1 = cirq.GridQubit(1, 2)
    tag1 = cg.CalibrationTag('abc123')
    tag2 = cg.CalibrationTag('def456')
    circuit = cirq.Circuit(
        cirq.X(q0).with_tags(tag1),
        cirq.X(q1).with_tags(tag2), cirq.X(q0))
    op1 = v2.program_pb2.Operation()
    op1.gate.id = 'x_pow'
    op1.args['half_turns'].arg_value.float_value = 1.0
    op1.qubits.add().id = '1_1'
    op1.token_constant_index = 0
    op2 = v2.program_pb2.Operation()
    op2.gate.id = 'x_pow'
    op2.args['half_turns'].arg_value.float_value = 1.0
    op2.qubits.add().id = '1_2'
    op2.token_constant_index = 1
    proto = v2.program_pb2.Program(
        language=v2.program_pb2.Language(arg_function_language='',
                                         gate_set='my_gate_set'),
        circuit=v2.program_pb2.Circuit(
            scheduling_strategy=v2.program_pb2.Circuit.MOMENT_BY_MOMENT,
            moments=[
                v2.program_pb2.Moment(operations=[op1, op2]),
                v2.program_pb2.Moment(
                    operations=[X_SERIALIZER.to_proto(cirq.X(q0))]),
            ],
        ),
        constants=[
            v2.program_pb2.Constant(string_value='abc123'),
            v2.program_pb2.Constant(string_value='def456')
        ])
    assert proto == MY_GATE_SET.serialize(circuit)
    assert MY_GATE_SET.deserialize(proto) == circuit
Exemplo n.º 2
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
Exemplo n.º 3
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))
Exemplo n.º 4
0
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_serialize_deserialize_op_with_token():
    q0 = cirq.GridQubit(1, 1)
    proto = op_proto(
        {
            'gate': {'id': 'x_pow'},
            'args': {
                'half_turns': {'arg_value': {'float_value': 0.125}},
            },
            'qubits': [{'id': '1_1'}],
            'token_value': 'abc123',
        }
    )
    op = cirq.XPowGate(exponent=0.125)(q0).with_tags(cg.CalibrationTag('abc123'))
    assert proto == MY_GATE_SET.serialize_op(op)
    assert MY_GATE_SET.deserialize_op(proto) == op
def test_serialize_deserialize_op_with_constants():
    q0 = cirq.GridQubit(1, 1)
    proto = op_proto(
        {
            'gate': {'id': 'x_pow'},
            'args': {
                'half_turns': {'arg_value': {'float_value': 0.125}},
            },
            'qubits': [{'id': '1_1'}],
            'token_constant_index': 0,
        }
    )
    op = cirq.XPowGate(exponent=0.125)(q0).with_tags(cg.CalibrationTag('abc123'))
    assert proto == MY_GATE_SET.serialize_op(op, constants=[])
    constant = v2.program_pb2.Constant()
    constant.string_value = 'abc123'
    assert MY_GATE_SET.deserialize_op(proto, constants=[constant]) == op
Exemplo n.º 7
0
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)