Exemplo n.º 1
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 = {
        '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_dict(serialized)
    assert result == GateWithAttribute(1.125)(q)
Exemplo n.º 2
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 = {
        '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_dict(serialized)
    assert result == GateWithAttribute(0.125)(q)
Exemplo n.º 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 = {
        '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_dict(serialized,
                                         arg_function_language='linear')
Exemplo n.º 4
0
def test_from_proto_unknown_arg_type():
    deserializer = cg.GateOpDeserializer(serialized_gate_id='my_gate',
                                         gate_constructor=GateWithAttribute,
                                         args=[
                                             cg.DeserializingArg(
                                                 serialized_name='my_val',
                                                 constructor_arg_name='val',
                                             )
                                         ])
    serialized = {
        'gate': {
            'id': 'my_gate'
        },
        'args': {
            'my_val': {
                'arg_value': {
                    'what_value': 0.125
                }
            }
        },
        'qubits': [{
            'id': '1_2'
        }]
    }
    with pytest.raises(Exception, match='what_value'):
        deserializer.from_proto_dict(serialized)
Exemplo n.º 5
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
Exemplo n.º 6
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)
Exemplo n.º 7
0
def test_from_proto_missing_required_arg():
    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': {
            'not_req': {
                'arg_value': {
                    'float_value': 0.125
                }
            }
        },
        'qubits': [{
            'id': '1_2'
        }]
    })
    with pytest.raises(ValueError):
        deserializer.from_proto(serialized)
Exemplo n.º 8
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))
Exemplo n.º 9
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)
Exemplo n.º 10
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 = {
        'gate': {
            'id': 'my_gate'
        },
        'args': {
            'my_val': {}
        },
        'qubits': [{
            'id': '1_2'
        }]
    }
    with pytest.raises(ValueError):
        deserializer.from_proto_dict(serialized)
Exemplo n.º 11
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 = {
        'gate': {
            'id': 'my_gate'
        },
        'args': {
            'not_my_val': {
                'arg_value': {
                    'float_value': 0.1
                }
            }
        },
        'qubits': [{
            'id': '1_2'
        }]
    }
    with pytest.raises(ValueError, match='my_val'):
        deserializer.from_proto_dict(serialized)
Exemplo n.º 12
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)))
Exemplo n.º 13
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)
Exemplo n.º 14
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)
Exemplo n.º 15
0
import cirq
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.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=[])
    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=[])
    ],
)


@pytest.fixture(scope='session', autouse=True)
def mock_grpc_client():
    with mock.patch(
        'cirq.google.engine.engine_client.quantum.QuantumEngineServiceClient'
    ) as _fixture:
        yield _fixture


def test_engine():
    processor = cg.EngineProcessor('a', 'p', EngineContext())
    assert processor.engine().project_id == 'a'
Exemplo n.º 17
0
    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',
        )
    ],