예제 #1
0
def test_grid_device_from_proto():
    grid_qubits, spec = _create_device_spec_with_horizontal_couplings()

    device = cirq_google.GridDevice.from_proto(spec)

    assert len(device.metadata.qubit_set) == len(grid_qubits)
    assert device.metadata.qubit_set == frozenset(grid_qubits)
    assert all(
        frozenset((cirq.GridQubit(row, 0), cirq.GridQubit(row, 1))) in device.metadata.qubit_pairs
        for row in range(GRID_HEIGHT)
    )
    assert device.metadata.gateset == cirq.Gateset(
        cirq_google.FSimGateFamily(gates_to_accept=[cirq_google.SYC]),
        cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP]),
        cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP_INV]),
        cirq_google.FSimGateFamily(gates_to_accept=[cirq.CZ]),
        cirq.ops.phased_x_z_gate.PhasedXZGate,
        cirq.ops.common_gates.XPowGate,
        cirq.ops.common_gates.YPowGate,
        cirq.ops.phased_x_gate.PhasedXPowGate,
        cirq.GateFamily(
            cirq.ops.common_gates.ZPowGate, tags_to_ignore=[cirq_google.PhysicalZTag()]
        ),
        cirq.GateFamily(
            cirq.ops.common_gates.ZPowGate, tags_to_accept=[cirq_google.PhysicalZTag()]
        ),
        cirq_google.experimental.ops.coupler_pulse.CouplerPulse,
        cirq.ops.measurement_gate.MeasurementGate,
        cirq.ops.wait_gate.WaitGate,
    )
    assert tuple(device.metadata.compilation_target_gatesets) == (
        cirq.CZTargetGateset(),
        cirq_google.SycamoreTargetGateset(),
        cirq.SqrtIswapTargetGateset(use_sqrt_iswap_inv=True),
    )

    base_duration = cirq.Duration(picos=1_000)
    assert device.metadata.gate_durations == {
        cirq_google.FSimGateFamily(gates_to_accept=[cirq_google.SYC]): base_duration * 0,
        cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP]): base_duration * 1,
        cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP_INV]): base_duration * 2,
        cirq_google.FSimGateFamily(gates_to_accept=[cirq.CZ]): base_duration * 3,
        cirq.GateFamily(cirq.ops.phased_x_z_gate.PhasedXZGate): base_duration * 4,
        cirq.GateFamily(cirq.ops.common_gates.XPowGate): base_duration * 4,
        cirq.GateFamily(cirq.ops.common_gates.YPowGate): base_duration * 4,
        cirq.GateFamily(cirq.ops.phased_x_gate.PhasedXPowGate): base_duration * 4,
        cirq.GateFamily(
            cirq.ops.common_gates.ZPowGate, tags_to_ignore=[cirq_google.PhysicalZTag()]
        ): base_duration
        * 5,
        cirq.GateFamily(
            cirq.ops.common_gates.ZPowGate, tags_to_accept=[cirq_google.PhysicalZTag()]
        ): base_duration
        * 6,
        cirq.GateFamily(cirq_google.experimental.ops.coupler_pulse.CouplerPulse): base_duration * 7,
        cirq.GateFamily(cirq.ops.measurement_gate.MeasurementGate): base_duration * 8,
        cirq.GateFamily(cirq.ops.wait_gate.WaitGate): base_duration * 9,
    }
예제 #2
0
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)
예제 #3
0
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)
예제 #4
0
def test_deserialize_z(half_turns):
    serialized_op = op_proto({
        'gate': {
            'id': 'z'
        },
        'args': {
            'half_turns': {
                'arg_value': {
                    'float_value': half_turns
                }
            },
            'type': {
                'arg_value': {
                    'string_value': cgc.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(serialized_op) == expected

    serialized_op.args['type'].arg_value.string_value = cgc.PHYSICAL_Z
    expected = cirq.ZPowGate(exponent=half_turns)(q).with_tags(
        cg.PhysicalZTag())
    assert SINGLE_QUBIT_GATE_SET.deserialize_op(serialized_op) == expected
예제 #5
0
def test_to_proto():
    device_info, expected_spec = _create_device_spec_with_horizontal_couplings(
    )

    # The set of gates in gate_durations are consistent with what's generated in
    # _create_device_spec_with_horizontal_couplings()
    base_duration = cirq.Duration(picos=1_000)
    gate_durations = {
        cirq.GateFamily(cirq_google.SYC):
        base_duration * 0,
        cirq.GateFamily(cirq.SQRT_ISWAP):
        base_duration * 1,
        cirq.GateFamily(cirq.SQRT_ISWAP_INV):
        base_duration * 2,
        cirq.GateFamily(cirq.CZ):
        base_duration * 3,
        cirq.GateFamily(cirq.ops.phased_x_z_gate.PhasedXZGate):
        base_duration * 4,
        cirq.GateFamily(cirq.ops.common_gates.ZPowGate,
                        tags_to_ignore=[cirq_google.PhysicalZTag()]):
        base_duration * 5,
        cirq.GateFamily(cirq.ops.common_gates.ZPowGate,
                        tags_to_accept=[cirq_google.PhysicalZTag()]):
        base_duration * 6,
        cirq.GateFamily(cirq_google.experimental.ops.coupler_pulse.CouplerPulse):
        base_duration * 7,
        cirq.GateFamily(cirq.ops.measurement_gate.MeasurementGate):
        base_duration * 8,
        cirq.GateFamily(cirq.ops.wait_gate.WaitGate):
        base_duration * 9,
    }

    spec = grid_device.create_device_specification_proto(
        qubits=device_info.grid_qubits,
        pairs=device_info.qubit_pairs,
        gateset=cirq.Gateset(*gate_durations.keys()),
        gate_durations=gate_durations,
    )

    assert text_format.MessageToString(spec) == text_format.MessageToString(
        expected_spec)
예제 #6
0
def test_sycamore_devices(device, qubit_size, layout_str):
    q0 = cirq.GridQubit(5, 3)
    q1 = cirq.GridQubit(5, 4)
    valid_sycamore_gates_and_ops = [
        cirq_google.SYC,
        cirq.SQRT_ISWAP,
        cirq.SQRT_ISWAP_INV,
        cirq.X,
        cirq.Y,
        cirq.Z,
        cirq.Z(q0).with_tags(cirq_google.PhysicalZTag()),
        coupler_pulse.CouplerPulse(hold_time=cirq.Duration(nanos=10),
                                   coupling_mhz=25.0),
        cirq.measure(q0),
        cirq.WaitGate(cirq.Duration(millis=5)),
        # TODO(#5050) Uncomment after GlobalPhaseGate support is added.
        # cirq.GlobalPhaseGate(-1.0),
    ]
    syc = cirq.FSimGate(theta=np.pi / 2, phi=np.pi / 6)(q0, q1)
    sqrt_iswap = cirq.FSimGate(theta=np.pi / 4, phi=0)(q0, q1)

    assert str(device) == layout_str
    assert len(device.metadata.qubit_pairs) == qubit_size
    assert all(gate_or_op in device.metadata.gateset
               for gate_or_op in valid_sycamore_gates_and_ops)
    assert len(device.metadata.gate_durations) == len(
        device.metadata.gateset.gates)
    assert any(
        isinstance(cgs, cirq_google.SycamoreTargetGateset)
        for cgs in device.metadata.compilation_target_gatesets)
    assert any(
        isinstance(cgs, cirq.SqrtIswapTargetGateset)
        for cgs in device.metadata.compilation_target_gatesets)

    device.validate_operation(syc)
    device.validate_operation(sqrt_iswap)

    assert next(
        (duration
         for gate_family, duration in device.metadata.gate_durations.items()
         if syc in gate_family),
        None,
    ) == cirq.Duration(nanos=12)
    assert next(
        (duration
         for gate_family, duration in device.metadata.gate_durations.items()
         if sqrt_iswap in gate_family),
        None,
    ) == cirq.Duration(nanos=32)
예제 #7
0
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)
예제 #8
0
def test_serialize_deserialize_fsim_gate(gate, theta, phi, phys_z):
    # Deprecations: cirq_google.SerializableGateSet and common serializers.
    with cirq.testing.assert_deprecated('SerializableGateSet',
                                        'CircuitSerializer',
                                        deadline='v0.16',
                                        count=3):
        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)
예제 #9
0
def test_serialize_z(gate, half_turns):
    q = cirq.GridQubit(1, 2)
    assert SINGLE_QUBIT_GATE_SET.serialize_op(gate.on(q)) == op_proto({
        '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(physical_op) == op_proto({
        'gate': {
            'id': 'z'
        },
        'args': {
            'half_turns': {
                'arg_value': {
                    'float_value': half_turns
                }
            },
            'type': {
                'arg_value': {
                    'string_value': cgc.PHYSICAL_Z
                }
            },
        },
        'qubits': [{
            'id': '1_2'
        }],
    })
예제 #10
0
def assert_phys_z_tag(phys_z, op):
    has_tag = cg.PhysicalZTag() in op.tags
    assert has_tag == phys_z
예제 #11
0
         'qubit_constant_index': [0],
     }),
 ),
 (
     cirq.ZPowGate(exponent=0.5)(Q0),
     op_proto({
         'zpowgate': {
             'exponent': {
                 'float_value': 0.5
             }
         },
         'qubit_constant_index': [0],
     }),
 ),
 (
     cirq.ZPowGate(exponent=0.5)(Q0).with_tags(cg.PhysicalZTag()),
     op_proto({
         'zpowgate': {
             'exponent': {
                 'float_value': 0.5
             },
             'is_physical_z': True
         },
         'qubit_constant_index': [0],
     }),
 ),
 (
     cirq.PhasedXPowGate(phase_exponent=0.125, exponent=0.5)(Q0),
     op_proto({
         'phasedxpowgate': {
             'phase_exponent': {
예제 #12
0
def test_to_proto_backward_compatibility():
    # Deprecations: cirq_google.SerializableGateSet and
    # cirq_google.device.known_devices.create_device_proto_for_qubits()
    with cirq.testing.assert_deprecated(
            'SerializableGateSet',
            'create_device_specification_proto()` can be used',
            deadline='v0.16',
            count=None,
    ):
        device_info, _ = _create_device_spec_with_horizontal_couplings()

        # The set of gates in gate_durations are consistent with what's generated in
        # _create_device_spec_with_horizontal_couplings()
        base_duration = cirq.Duration(picos=1_000)
        gate_durations = {
            cirq.GateFamily(cirq_google.SYC):
            base_duration * 0,
            cirq.GateFamily(cirq.SQRT_ISWAP):
            base_duration * 1,
            cirq.GateFamily(cirq.SQRT_ISWAP_INV):
            base_duration * 2,
            cirq.GateFamily(cirq.CZ):
            base_duration * 3,
            cirq.GateFamily(cirq.ops.phased_x_z_gate.PhasedXZGate):
            base_duration * 4,
            cirq.GateFamily(cirq.ops.common_gates.ZPowGate,
                            tags_to_ignore=[cirq_google.PhysicalZTag()]):
            base_duration * 5,
            cirq.GateFamily(cirq.ops.common_gates.ZPowGate,
                            tags_to_accept=[cirq_google.PhysicalZTag()]):
            base_duration * 6,
            cirq.GateFamily(cirq_google.experimental.ops.coupler_pulse.CouplerPulse):
            base_duration * 7,
            cirq.GateFamily(cirq.ops.measurement_gate.MeasurementGate):
            base_duration * 8,
            cirq.GateFamily(cirq.ops.wait_gate.WaitGate):
            base_duration * 9,
        }

        # Serialize the old way
        spec = known_devices.create_device_proto_for_qubits(
            device_info.grid_qubits,
            device_info.qubit_pairs,
            [cirq_google.FSIM_GATESET],
            known_devices._SYCAMORE_DURATIONS_PICOS,
        )

        # Serialize the new way
        grid_device.create_device_specification_proto(
            qubits=device_info.grid_qubits,
            pairs=device_info.qubit_pairs,
            gateset=cirq.Gateset(*gate_durations.keys()),
            gate_durations=gate_durations,
            out=spec,
        )

        with cirq.testing.assert_deprecated('Use cirq_google.GridDevice',
                                            deadline='v0.16',
                                            count=None):
            # Deserialize both ways
            serializable_dev = cirq_google.SerializableDevice.from_proto(
                spec, [cirq_google.FSIM_GATESET])
            grid_dev = cirq_google.GridDevice.from_proto(spec)

            assert serializable_dev.metadata.qubit_set == grid_dev.metadata.qubit_set
            assert serializable_dev.metadata.qubit_pairs == grid_dev.metadata.qubit_pairs

            assert serializable_dev.metadata.gateset == cirq.Gateset(
                cirq.FSimGate,
                cirq.ISwapPowGate,
                cirq.CZPowGate,
                cirq.PhasedXPowGate,
                cirq.XPowGate,
                cirq.YPowGate,
                cirq.ZPowGate,
                cirq.PhasedXZGate,
                cirq.MeasurementGate,
                cirq.WaitGate,
                cirq.GlobalPhaseGate,
            )

            assert grid_dev.metadata.gateset == device_info.expected_gateset
            assert (tuple(grid_dev.metadata.compilation_target_gatesets) ==
                    device_info.expected_target_gatesets)

            assert grid_dev.metadata.gate_durations == device_info.expected_gate_durations
예제 #13
0
def _create_device_spec_with_horizontal_couplings():
    # Qubit layout:
    #   x -- x
    #   x -- x
    #   x -- x
    #   x -- x
    #   x -- x

    grid_qubits = [
        cirq.GridQubit(i, j) for i in range(GRID_HEIGHT) for j in range(2)
    ]

    spec = v2.device_pb2.DeviceSpecification()

    spec.valid_qubits.extend([v2.qubit_to_proto_id(q) for q in grid_qubits])

    qubit_pairs = []
    grid_targets = spec.valid_targets.add()
    grid_targets.name = '2_qubit_targets'
    grid_targets.target_ordering = v2.device_pb2.TargetSet.SYMMETRIC
    for row in range(int(GRID_HEIGHT / 2)):
        qubit_pairs.append((cirq.GridQubit(row, 0), cirq.GridQubit(row, 1)))
    for row in range(int(GRID_HEIGHT / 2), GRID_HEIGHT):
        # Flip the qubit pair order for the second half of qubits
        # to verify GridDevice properly handles pair symmetry.
        qubit_pairs.append((cirq.GridQubit(row, 1), cirq.GridQubit(row, 0)))
    for pair in qubit_pairs:
        new_target = grid_targets.targets.add()
        new_target.ids.extend([v2.qubit_to_proto_id(q) for q in pair])

    gate_names = [
        'syc',
        'sqrt_iswap',
        'sqrt_iswap_inv',
        'cz',
        'phased_xz',
        'virtual_zpow',
        'physical_zpow',
        'coupler_pulse',
        'meas',
        'wait',
    ]
    gate_durations = [(n, i * 1000) for i, n in enumerate(gate_names)]
    for gate_name, duration in sorted(gate_durations):
        gate = spec.valid_gates.add()
        getattr(gate, gate_name).SetInParent()
        gate.gate_duration_picos = duration

    expected_gateset = cirq.Gateset(
        cirq_google.FSimGateFamily(gates_to_accept=[cirq_google.SYC]),
        cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP]),
        cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP_INV]),
        cirq_google.FSimGateFamily(gates_to_accept=[cirq.CZ]),
        cirq.ops.phased_x_z_gate.PhasedXZGate,
        cirq.ops.common_gates.XPowGate,
        cirq.ops.common_gates.YPowGate,
        cirq.ops.phased_x_gate.PhasedXPowGate,
        cirq.GateFamily(cirq.ops.common_gates.ZPowGate,
                        tags_to_ignore=[cirq_google.PhysicalZTag()]),
        cirq.GateFamily(cirq.ops.common_gates.ZPowGate,
                        tags_to_accept=[cirq_google.PhysicalZTag()]),
        cirq_google.experimental.ops.coupler_pulse.CouplerPulse,
        cirq.ops.measurement_gate.MeasurementGate,
        cirq.ops.wait_gate.WaitGate,
    )

    base_duration = cirq.Duration(picos=1_000)
    expected_gate_durations = {
        cirq_google.FSimGateFamily(gates_to_accept=[cirq_google.SYC]):
        base_duration * 0,
        cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP]):
        base_duration * 1,
        cirq_google.FSimGateFamily(gates_to_accept=[cirq.SQRT_ISWAP_INV]):
        base_duration * 2,
        cirq_google.FSimGateFamily(gates_to_accept=[cirq.CZ]):
        base_duration * 3,
        cirq.GateFamily(cirq.ops.phased_x_z_gate.PhasedXZGate):
        base_duration * 4,
        cirq.GateFamily(cirq.ops.common_gates.XPowGate):
        base_duration * 4,
        cirq.GateFamily(cirq.ops.common_gates.YPowGate):
        base_duration * 4,
        cirq.GateFamily(cirq.ops.phased_x_gate.PhasedXPowGate):
        base_duration * 4,
        cirq.GateFamily(cirq.ops.common_gates.ZPowGate,
                        tags_to_ignore=[cirq_google.PhysicalZTag()]):
        base_duration * 5,
        cirq.GateFamily(cirq.ops.common_gates.ZPowGate,
                        tags_to_accept=[cirq_google.PhysicalZTag()]):
        base_duration * 6,
        cirq.GateFamily(cirq_google.experimental.ops.coupler_pulse.CouplerPulse):
        base_duration * 7,
        cirq.GateFamily(cirq.ops.measurement_gate.MeasurementGate):
        base_duration * 8,
        cirq.GateFamily(cirq.ops.wait_gate.WaitGate):
        base_duration * 9,
    }

    expected_target_gatesets = (
        cirq.CZTargetGateset(),
        cirq_google.SycamoreTargetGateset(),
        cirq.SqrtIswapTargetGateset(use_sqrt_iswap_inv=True),
    )

    return (
        _DeviceInfo(
            grid_qubits,
            qubit_pairs,
            expected_gateset,
            expected_gate_durations,
            expected_target_gatesets,
        ),
        spec,
    )
예제 #14
0
def test_syc_str_repr():
    assert str(cirq_google.PhysicalZTag()) == 'PhysicalZTag()'
    assert repr(cirq_google.PhysicalZTag()) == 'cirq_google.PhysicalZTag()'
    cirq.testing.assert_equivalent_repr(
        cirq_google.PhysicalZTag(), setup_code=('import cirq\nimport cirq_google\n')
    )
예제 #15
0
def test_equality():
    assert cirq_google.PhysicalZTag() == cirq_google.PhysicalZTag()
    assert hash(cirq_google.PhysicalZTag()) == hash(cirq_google.PhysicalZTag())