示例#1
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)
示例#2
0
def test_deserialize_circuit_op_errors():
    constants = [default_circuit_proto()]
    deserialized_constants = [default_circuit()]

    proto = v2.program_pb2.CircuitOperation()
    proto.circuit_constant_index = 0
    proto.repetition_specification.repetition_count = 1

    NO_CIRCUIT_OP_GATE_SET = cg.SerializableGateSet(
        gate_set_name='no_circuit_op_gateset',
        serializers=[X_SERIALIZER],
        deserializers=[X_DESERIALIZER],
    )
    with pytest.raises(ValueError, match='Unsupported deserialized of a CircuitOperation'):
        NO_CIRCUIT_OP_GATE_SET.deserialize_op(
            proto, constants=constants, deserialized_constants=deserialized_constants
        )

    BAD_CIRCUIT_DESERIALIZER = cg.GateOpDeserializer(
        serialized_gate_id='circuit', gate_constructor=cirq.ZPowGate, args=[]
    )
    BAD_CIRCUIT_DESERIALIZER_GATE_SET = cg.SerializableGateSet(
        gate_set_name='bad_circuit_gateset',
        serializers=[CIRCUIT_OP_SERIALIZER],
        deserializers=[BAD_CIRCUIT_DESERIALIZER],
    )
    with pytest.raises(ValueError, match='Expected CircuitOpDeserializer for id "circuit"'):
        BAD_CIRCUIT_DESERIALIZER_GATE_SET.deserialize_op(
            proto, constants=constants, deserialized_constants=deserialized_constants
        )
示例#3
0
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')
示例#4
0
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)
示例#5
0
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)
示例#6
0
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)
示例#7
0
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))
示例#8
0
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)
示例#9
0
def _create_gate_op_deserializer(*, serialized_gate_id, gate_constructor,
                                 args):
    with cirq.testing.assert_deprecated('CircuitSerializer',
                                        deadline='v0.16',
                                        count=1):
        return cg.GateOpDeserializer(serialized_gate_id=serialized_gate_id,
                                     gate_constructor=gate_constructor,
                                     args=args)
示例#10
0
def test_deprecated_fields():
    deserializer = cg.GateOpDeserializer(
        serialized_gate_id='my_gate',
        gate_constructor=GateWithAttribute,
        args=[],
    )
    with assert_deprecated('Use serialized_id', deadline='v0.13'):
        assert deserializer.serialized_gate_id == deserializer.serialized_id
示例#11
0
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')
        ],
    )
示例#12
0
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')
            ],
        )
示例#13
0
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')
            ],
        )
示例#14
0
def test_get_engine_device(get_processor):
    device_spec = _to_any(
        Merge(
            """
valid_gate_sets: [{
    name: 'test_set',
    valid_gates: [{
        id: 'x',
        number_of_qubits: 1,
        gate_duration_picos: 1000,
        valid_targets: ['1q_targets']
    }]
}],
valid_qubits: ['0_0', '1_1'],
valid_targets: [{
    name: '1q_targets',
    target_ordering: SYMMETRIC,
    targets: [{
        ids: ['0_0']
    }]
}]
""",
            v2.device_pb2.DeviceSpecification(),
        ))

    gate_set = cg.SerializableGateSet(
        gate_set_name='x_gate_set',
        serializers=[
            cg.GateOpSerializer(gate_type=cirq.XPowGate,
                                serialized_gate_id='x',
                                args=[])
        ],
        deserializers=[
            cg.GateOpDeserializer(serialized_gate_id='x',
                                  gate_constructor=cirq.XPowGate,
                                  args=[])
        ],
    )

    get_processor.return_value = qtypes.QuantumProcessor(
        device_spec=device_spec)
    device = cirq_google.get_engine_device('rainbow',
                                           'project',
                                           gatesets=[gate_set])
    assert set(device.qubits) == {cirq.GridQubit(0, 0), cirq.GridQubit(1, 1)}
    device.validate_operation(cirq.X(cirq.GridQubit(0, 0)))
    with pytest.raises(ValueError):
        device.validate_operation(cirq.X(cirq.GridQubit(1, 2)))
    with pytest.raises(ValueError):
        device.validate_operation(cirq.Y(cirq.GridQubit(0, 0)))
示例#15
0
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)
示例#16
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 _just_meas():
    # Deprecations: cirq_google.SerializableGateSet, cirq_google.GateOpSerializer, and
    # cirq_google.GateOpDeserializer
    with cirq.testing.assert_deprecated('SerializableGateSet',
                                        'CircuitSerializer',
                                        deadline='v0.16',
                                        count=None):
        return cg.SerializableGateSet(
            gate_set_name='meas_gate_set',
            serializers=[
                cg.GateOpSerializer(gate_type=cirq.MeasurementGate,
                                    serialized_gate_id='meas',
                                    args=[])
            ],
            deserializers=[
                cg.GateOpDeserializer(serialized_gate_id='meas',
                                      gate_constructor=cirq.MeasurementGate,
                                      args=[])
            ],
        )
示例#18
0
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)
示例#19
0
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)
示例#20
0
    }]
}]
""",
        v2.device_pb2.DeviceSpecification(),
    ))

_GATE_SET = cg.SerializableGateSet(
    gate_set_name='x_gate_set',
    serializers=[
        cg.GateOpSerializer(gate_type=cirq.XPowGate,
                            serialized_gate_id='x',
                            args=[])
    ],
    deserializers=[
        cg.GateOpDeserializer(serialized_gate_id='x',
                              gate_constructor=cirq.XPowGate,
                              args=[])
    ],
)

_CIRCUIT = cirq.Circuit(
    cirq.X(cirq.GridQubit(5, 2))**0.5,
    cirq.measure(cirq.GridQubit(5, 2), key='result'))

_RESULTS_V2 = v2.result_pb2.Result(sweep_results=[
    v2.result_pb2.SweepResult(
        repetitions=1,
        parameterized_results=[
            v2.result_pb2.ParameterizedResult(
                params=v2.result_pb2.ParameterDict(assignments={'a': 1}),
                measurement_results=[
示例#21
0
import cirq_google as cg
import cirq_google.api.v2 as v2
import cirq_google.api.v2.device_pb2 as device_pb2
import cirq_google.devices.known_devices as cgdk
import cirq_google.serialization.common_serializers as cgc

_JUST_CZ = cg.SerializableGateSet(
    gate_set_name='cz_gate_set',
    serializers=[
        cg.GateOpSerializer(gate_type=cirq.CZPowGate,
                            serialized_gate_id='cz',
                            args=[])
    ],
    deserializers=[
        cg.GateOpDeserializer(serialized_gate_id='cz',
                              gate_constructor=cirq.CZPowGate,
                              args=[])
    ],
)

_JUST_MEAS = cg.SerializableGateSet(
    gate_set_name='meas_gate_set',
    serializers=[
        cg.GateOpSerializer(gate_type=cirq.MeasurementGate,
                            serialized_gate_id='meas',
                            args=[])
    ],
    deserializers=[
        cg.GateOpDeserializer(serialized_gate_id='meas',
                              gate_constructor=cirq.MeasurementGate,
                              args=[])
    gate_type=cirq.XPowGate,
    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',
        )
    ],