def test_fsim_gate_not_allowed(gate):
    q1 = cirq.GridQubit(5, 4)
    q2 = cirq.GridQubit(5, 5)
    gate_set = cg.SerializableGateSet('test', cgc.LIMITED_FSIM_SERIALIZERS,
                                      [cgc.LIMITED_FSIM_DESERIALIZER])
    with pytest.raises(ValueError):
        gate_set.serialize_op(gate(q1, q2))
def test_cz_pow_non_integer_does_not_serialize():
    gate_set = cg.SerializableGateSet('test', [cgc.CZ_SERIALIZER],
                                      [cgc.CZ_POW_DESERIALIZER])
    q1 = cirq.GridQubit(5, 4)
    q2 = cirq.GridQubit(5, 5)
    with pytest.raises(ValueError):
        gate_set.serialize_op(cirq.CZ(q1, q2)**0.5)
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_gateset_with_added_gates_again():
    """Verify that adding a serializer twice doesn't mess anything up."""
    q = cirq.GridQubit(2, 2)
    x_gateset = cg.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],
    )

    assert xx_gateset.gate_set_name == 'xx'
    assert xx_gateset.is_supported_operation(cirq.X(q))
    assert not 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))
    assert xx_gateset.serialize_op(expected_gate) == proto
    assert xx_gateset.deserialize_op(proto) == expected_gate
Beispiel #5
0
def test_multiple_serializers():
    serializer1 = cg.GateOpSerializer(
        gate_type=cirq.XPowGate,
        serialized_gate_id='x_pow',
        args=[
            cg.SerializingArg(serialized_name='half_turns',
                              serialized_type=float,
                              op_getter='exponent')
        ],
        can_serialize_predicate=lambda x: x.gate.exponent != 1)
    serializer2 = cg.GateOpSerializer(
        gate_type=cirq.XPowGate,
        serialized_gate_id='x',
        args=[
            cg.SerializingArg(serialized_name='half_turns',
                              serialized_type=float,
                              op_getter='exponent')
        ],
        can_serialize_predicate=lambda x: x.gate.exponent == 1)
    gate_set = cg.SerializableGateSet(gate_set_name='my_gate_set',
                                      serializers=[serializer1, serializer2],
                                      deserializers=[])
    q0 = cirq.GridQubit(1, 1)
    assert gate_set.serialize_op(cirq.X(q0)).gate.id == 'x'
    assert gate_set.serialize_op(cirq.X(q0)**0.5).gate.id == 'x_pow'
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_fsim_gate(gate, theta, phi):
    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
                }
            }
        },
        'qubits': [{
            'id': '5_4'
        }, {
            'id': '5_5'
        }]
    })
    q1 = cirq.GridQubit(5, 4)
    q2 = cirq.GridQubit(5, 5)
    op = cirq.FSimGate(theta=theta, phi=phi)
    assert gate_set.serialize_op(gate(q1, q2)) == proto
    cirq.testing.assert_allclose_up_to_global_phase(
        cirq.unitary(gate_set.deserialize_op(proto)),
        cirq.unitary(op),
        atol=1e-7,
    )
def test_serialize_deserialize_cz_gate(gate, exponent):
    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
                }
            }
        },
        'qubits': [{
            'id': '5_4'
        }, {
            'id': '5_5'
        }]
    })
    q1 = cirq.GridQubit(5, 4)
    q2 = cirq.GridQubit(5, 5)
    op = cirq.CZPowGate(exponent=exponent)
    assert gate_set.serialize_op(gate(q1, q2)) == proto
    cirq.testing.assert_allclose_up_to_global_phase(
        cirq.unitary(gate_set.deserialize_op(proto)),
        cirq.unitary(op),
        atol=1e-7,
    )
def test_gateset_with_added_gates():
    q = cirq.GridQubit(1, 1)
    x_gateset = cg.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],
    )
    assert x_gateset.gate_set_name == 'x'
    assert x_gateset.is_supported_operation(cirq.X(q))
    assert not x_gateset.is_supported_operation(cirq.Y(q))
    assert xy_gateset.gate_set_name == 'xy'
    assert xy_gateset.is_supported_operation(cirq.X(q))
    assert 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))
    assert xy_gateset.serialize_op(expected_gate) == proto
    assert xy_gateset.deserialize_op(proto) == expected_gate
def test_serialize_deserialize_iswap_symbols():
    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')
    proto = gate_set.serialize_op(op, arg_function_language='linear')
    actual = gate_set.deserialize_op(proto, arg_function_language='linear')
    assert isinstance(actual.gate, cirq.FSimGate)
    assert math.isclose(actual.gate.phi, 0)
    assert math.isclose(actual.gate.theta.subs('t', 2), -np.pi, abs_tol=1e-5)
Beispiel #11
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)))
def test_is_supported_gate_can_serialize_predicate():
    serializer = cg.GateOpSerializer(
        gate_type=cirq.XPowGate,
        serialized_gate_id='x_pow',
        args=[
            cg.SerializingArg(
                serialized_name='half_turns',
                serialized_type=float,
                gate_getter='exponent',
            )
        ],
        can_serialize_predicate=lambda x: x.exponent == 1.0)
    gate_set = cg.SerializableGateSet(gate_set_name='my_gate_set',
                                      serializers=[serializer],
                                      deserializers=[X_DESERIALIZER])
    assert gate_set.is_supported_gate(cirq.XPowGate())
    assert not gate_set.is_supported_gate(cirq.XPowGate()**0.5)
    assert gate_set.is_supported_gate(cirq.X)
Beispiel #13
0
def test_is_supported_operation_can_serialize_predicate():
    q = cirq.GridQubit(1, 2)
    serializer = cg.GateOpSerializer(
        gate_type=cirq.XPowGate,
        serialized_gate_id='x_pow',
        args=[
            cg.SerializingArg(
                serialized_name='half_turns',
                serialized_type=float,
                op_getter='exponent',
            )
        ],
        can_serialize_predicate=lambda x: x.gate.exponent == 1.0,
    )
    gate_set = cg.SerializableGateSet(gate_set_name='my_gate_set',
                                      serializers=[serializer],
                                      deserializers=[X_DESERIALIZER])
    assert gate_set.is_supported_operation(cirq.XPowGate()(q))
    assert not gate_set.is_supported_operation(cirq.XPowGate()(q)**0.5)
    assert gate_set.is_supported_operation(cirq.X(q))
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 test_wait_gate():
    gate_set = cg.SerializableGateSet('test', [cgc.WAIT_GATE_SERIALIZER],
                                      [cgc.WAIT_GATE_DESERIALIZER])
    proto = op_proto({
        'gate': {
            'id': 'wait'
        },
        'args': {
            'nanos': {
                'arg_value': {
                    'float_value': 20.0
                }
            }
        },
        'qubits': [{
            'id': '1_2'
        }],
    })
    q = cirq.GridQubit(1, 2)
    op = cirq.wait(q, nanos=20)
    assert gate_set.serialize_op(op) == proto
    assert gate_set.deserialize_op(proto) == op
Beispiel #16
0
def test_wait_gate():
    gate_set = cg.SerializableGateSet('test', [cgc.WAIT_GATE_SERIALIZER],
                                      [cgc.WAIT_GATE_DESERIALIZER])
    proto_dict = {
        'gate': {
            'id': 'wait'
        },
        'args': {
            'nanos': {
                'arg_value': {
                    'float_value': 20.0
                }
            }
        },
        'qubits': [{
            'id': '1_2'
        }]
    }
    q = cirq.GridQubit(1, 2)
    op = cirq.WaitGate(cirq.Duration(nanos=20)).on(q)
    assert gate_set.serialize_op_dict(op) == proto_dict
    assert gate_set.deserialize_op_dict(proto_dict) == op
def test_serialize_deserialize_fsim_gate_symbols(gate, theta, phi):
    gate_set = cg.SerializableGateSet('test', cgc.LIMITED_FSIM_SERIALIZERS,
                                      [cgc.LIMITED_FSIM_DESERIALIZER])
    q1 = cirq.GridQubit(5, 4)
    q2 = cirq.GridQubit(5, 5)
    expected = op_proto({
        'gate': {
            'id': 'fsim'
        },
        'args': {
            'theta': theta,
            'phi': phi
        },
        'qubits': [{
            'id': '5_4'
        }, {
            'id': '5_5'
        }]
    })
    proto = gate_set.serialize_op(gate(q1, q2), arg_function_language='linear')
    actual = gate_set.deserialize_op(proto, arg_function_language='linear')
    assert proto == expected
    assert actual == gate(q1, q2)
    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=[])
    ],
)


@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())
Beispiel #19
0
import copy
import pytest

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=[
Beispiel #20
0
)

Y_DESERIALIZER = cg.GateOpDeserializer(
    serialized_gate_id='y_pow',
    gate_constructor=cirq.YPowGate,
    args=[
        cg.DeserializingArg(
            serialized_name='half_turns',
            constructor_arg_name='exponent',
        )
    ],
)

MY_GATE_SET = cg.SerializableGateSet(
    gate_set_name='my_gate_set',
    serializers=[X_SERIALIZER],
    deserializers=[X_DESERIALIZER],
)


def op_proto(json: Dict) -> v2.program_pb2.Operation:
    op = v2.program_pb2.Operation()
    json_format.ParseDict(json, op)
    return op


def test_supported_gate_types():
    assert MY_GATE_SET.supported_gate_types() == (cirq.XPowGate, )


def test_is_supported_operation():