示例#1
0
def test_multiple_gatesets():
    halfPiGateSet = cirq_google.SerializableGateSet(
        gate_set_name='half_pi_gateset',
        serializers=cgc.SINGLE_QUBIT_HALF_PI_SERIALIZERS,
        deserializers=cgc.SINGLE_QUBIT_HALF_PI_DESERIALIZERS,
    )
    allAnglesGateSet = cirq_google.SerializableGateSet(
        gate_set_name='all_angles_gateset',
        serializers=cgc.SINGLE_QUBIT_SERIALIZERS,
        deserializers=cgc.SINGLE_QUBIT_DESERIALIZERS,
    )
    durations_dict = {'xy_pi': 20_000, 'xy_half_pi': 20_000, 'xy': 20_000}
    spec = cirq_google.devices.known_devices.create_device_proto_from_diagram(
        "aa\naa", [allAnglesGateSet, halfPiGateSet], durations_dict)
    dev = cg.SerializableDevice.from_proto(
        proto=spec, gate_sets=[allAnglesGateSet, halfPiGateSet])
    q0 = cirq.GridQubit(0, 0)
    q1 = cirq.GridQubit(1, 0)
    dev.validate_operation(cirq.X(q0))
    dev.validate_operation(cirq.X(q1))
    dev.validate_operation(cirq.XPowGate(exponent=0.1234)(q0))
    dev.validate_operation(cirq.XPowGate(exponent=0.2345)(q1))

    with pytest.raises(ValueError):
        dev.validate_operation(cirq.X(cirq.GridQubit(2, 2)))
示例#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_sycamore_circuitop_device():
    circuitop_gateset = cirq_google.SerializableGateSet(
        gate_set_name='circuitop_gateset',
        serializers=[cgc.CIRCUIT_OP_SERIALIZER],
        deserializers=[cgc.CIRCUIT_OP_DESERIALIZER],
    )
    gateset_list = [
        cirq_google.SQRT_ISWAP_GATESET,
        cirq_google.SYC_GATESET,
        circuitop_gateset,
    ]
    circuitop_proto = cirq_google.devices.known_devices.create_device_proto_from_diagram(
        known_devices._SYCAMORE23_GRID,
        gateset_list,
        known_devices._SYCAMORE_DURATIONS_PICOS,
    )
    device = cirq_google.SerializableDevice.from_proto(
        proto=circuitop_proto,
        gate_sets=gateset_list,
    )
    q0 = cirq.GridQubit(5, 3)
    q1 = cirq.GridQubit(5, 4)
    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)
    circuit_op = cirq.CircuitOperation(cirq.FrozenCircuit(syc, sqrt_iswap))
    device.validate_operation(syc)
    device.validate_operation(sqrt_iswap)
    device.validate_operation(circuit_op)
    assert device.duration_of(syc) == cirq.Duration(nanos=12)
    assert device.duration_of(sqrt_iswap) == cirq.Duration(nanos=32)
    # CircuitOperations don't have a set duration.
    assert device.duration_of(circuit_op) == cirq.Duration(nanos=0)
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))
示例#5
0
def test_multiple_fsim_gatesets():
    """This tests that gate sets with two different definitions for the same
    gate perform correctly.  In this case, we set the XPowGate to be
    half the duration of the full exponent and make sure it still works.
    """
    half_pi_gs = cirq_google.SerializableGateSet(
        gate_set_name='half_pi',
        serializers=[
            *cgc.SINGLE_QUBIT_HALF_PI_SERIALIZERS,
        ],
        deserializers=[
            *cgc.SINGLE_QUBIT_HALF_PI_DESERIALIZERS,
        ],
    )
    durations_dict = {
        'xy_pi': 20_000,
        'xy_half_pi': 10_000,
    }
    spec = cirq_google.devices.known_devices.create_device_proto_from_diagram(
        "aa\naa", [half_pi_gs], durations_dict
    )

    # The gate set defines two different serializations for PhasedXPowGate
    device = cg.SerializableDevice.from_proto(proto=spec, gate_sets=[half_pi_gs])
    q = cirq.GridQubit(0, 0)
    pi = cirq.XPowGate(exponent=1.0)
    half_pi = cirq.XPowGate(exponent=0.5)
    assert device.duration_of(pi(q)) == cirq.Duration(picos=20_000)
    assert device.duration_of(half_pi(q)) == cirq.Duration(picos=10_000)
def test_serialize_circuit_op_errors():
    constants = [default_circuit_proto()]
    raw_constants = {default_circuit(): 0}

    op = cirq.CircuitOperation(default_circuit())
    with pytest.raises(
            ValueError,
            match='CircuitOp serialization requires a constants list'):
        MY_GATE_SET.serialize_op(op)

    with pytest.raises(
            ValueError,
            match='CircuitOp serialization requires a constants list'):
        MY_GATE_SET.serialize_op(op, constants=constants)

    with pytest.raises(
            ValueError,
            match='CircuitOp serialization requires a constants list'):
        MY_GATE_SET.serialize_op(op, raw_constants=raw_constants)

    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='Cannot serialize CircuitOperation'):
        NO_CIRCUIT_OP_GATE_SET.serialize_op(op,
                                            constants=constants,
                                            raw_constants=raw_constants)
示例#7
0
def test_gateset_with_added_types_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_types(
        gate_set_name='xx', serializers=[X_SERIALIZER], deserializers=[X_DESERIALIZER]
    )

    assert xx_gateset.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
示例#8
0
def test_wait_gate_multi_qubit():
    # 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.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'
            }, {
                'id': '3_4'
            }],
        })
        op = cirq.wait(cirq.GridQubit(1, 2), cirq.GridQubit(3, 4), nanos=20)
        assert gate_set.serialize_op(op) == proto
        assert gate_set.deserialize_op(proto) == op
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)
示例#10
0
def test_adding_gates_multiple_times():
    # Deprecations: cirq_google.SerializableDevice, cirq_google.SerializableGateSet,
    # common serializers, and
    # cirq_google.devices.known_devices.create_device_proto_from_diagram
    with cirq.testing.assert_deprecated(
            'Use cirq_google.GridDevice',
            'SerializableGateSet',
            'CircuitSerializer',
            'no longer be available',
            deadline='v0.16',
            count=11,
    ):
        waiting_for_godot = cirq_google.SerializableGateSet(
            gate_set_name='wait_gateset',
            serializers=[
                cgc.WAIT_GATE_SERIALIZER,
                cgc.WAIT_GATE_SERIALIZER,
                cgc.WAIT_GATE_SERIALIZER,
            ],
            deserializers=[
                cgc.WAIT_GATE_DESERIALIZER,
                cgc.WAIT_GATE_DESERIALIZER,
                cgc.WAIT_GATE_DESERIALIZER,
            ],
        )
        wait_proto = cirq_google.devices.known_devices.create_device_proto_from_diagram(
            "aa", [waiting_for_godot])
        wait_device = cirq_google.SerializableDevice.from_proto(
            proto=wait_proto, gate_sets=[waiting_for_godot])
        q0 = cirq.GridQubit(0, 0)
        wait_op = cirq.wait(q0, nanos=25)
        wait_device.validate_operation(wait_op)

        assert (str(wait_proto) == """\
valid_gate_sets {
  name: "wait_gateset"
  valid_gates {
    id: "wait"
    number_of_qubits: 1
    valid_args {
      name: "nanos"
      type: FLOAT
    }
  }
}
valid_qubits: "0_0"
valid_qubits: "0_1"
valid_targets {
  name: "meas_targets"
  target_ordering: SUBSET_PERMUTATION
}
valid_targets {
  name: "2_qubit_targets"
  target_ordering: SYMMETRIC
  targets {
    ids: "0_0"
    ids: "0_1"
  }
}
""")
示例#11
0
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_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'
示例#13
0
def test_is_supported_operation_can_serialize_predicate():
    x_deserializer = _x_deserializer()
    # Deprecations: cirq_google.SerializableGateSet, cirq_google.GateOpSerializer, and
    # cirq_google.SerializingArg
    with cirq.testing.assert_deprecated('SerializableGateSet',
                                        'CircuitSerializer',
                                        deadline='v0.16',
                                        count=3):
        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))
示例#14
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)
def test_multiple_fsim_gatesets():
    """This tests that gate sets with two different definitions for the same
    gate perform correctly.  In this case, we set the XPowGate to be
    half the duration of the full exponent and make sure it still works.
    """
    # Deprecations: cirq_google.SerializableDevice, cirq_google.SerializableGateSet,
    # common serializers, and
    # cirq_google.devices.known_devices.create_device_proto_from_diagram,
    with cirq.testing.assert_deprecated(
            'Use cirq_google.GridDevice',
            'SerializableGateSet',
            'CircuitSerializer',
            'no longer be available',
            deadline='v0.16',
            count=7,
    ):
        half_pi_gs = cirq_google.SerializableGateSet(
            gate_set_name='half_pi',
            serializers=[*cgc.SINGLE_QUBIT_HALF_PI_SERIALIZERS],
            deserializers=[*cgc.SINGLE_QUBIT_HALF_PI_DESERIALIZERS],
        )
        durations_dict = {'xy_pi': 20_000, 'xy_half_pi': 10_000}
        spec = cirq_google.devices.known_devices.create_device_proto_from_diagram(
            "aa\naa", [half_pi_gs], durations_dict)

        # The gate set defines two different serializations for PhasedXPowGate
        device = cg.SerializableDevice.from_proto(proto=spec,
                                                  gate_sets=[half_pi_gs])
        q = cirq.GridQubit(0, 0)
        pi = cirq.XPowGate(exponent=1.0)
        half_pi = cirq.XPowGate(exponent=0.5)
        assert device.duration_of(pi(q)) == cirq.Duration(picos=20_000)
        assert device.duration_of(half_pi(q)) == cirq.Duration(picos=10_000)
示例#16
0
def test_multiple_serializers():
    # Deprecations: cirq_google.SerializableGateSet, cirq_google.GateOpSerializer, and
    # cirq_google.SerializingArg
    with cirq.testing.assert_deprecated('SerializableGateSet',
                                        'CircuitSerializer',
                                        deadline='v0.16',
                                        count=5):
        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'
示例#17
0
def test_serialize_circuit_op_errors():
    constants = [default_circuit_proto()]
    raw_constants = {default_circuit(): 0}

    op = cirq.CircuitOperation(default_circuit())
    with pytest.raises(
            ValueError,
            match='CircuitOp serialization requires a constants list'):
        _my_gate_set().serialize_op(op)

    with pytest.raises(
            ValueError,
            match='CircuitOp serialization requires a constants list'):
        _my_gate_set().serialize_op(op, constants=constants)

    with pytest.raises(
            ValueError,
            match='CircuitOp serialization requires a constants list'):
        _my_gate_set().serialize_op(op, raw_constants=raw_constants)

    x_serializer = _x_serializer()
    x_deserializer = _x_deserializer()
    with cirq.testing.assert_deprecated('SerializableGateSet',
                                        deadline='v0.16',
                                        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='Cannot serialize CircuitOperation'):
            NO_CIRCUIT_OP_GATE_SET.serialize_op(op,
                                                constants=constants,
                                                raw_constants=raw_constants)
示例#18
0
def test_gateset_with_added_types():
    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_types(
        gate_set_name='xy', serializers=[Y_SERIALIZER], deserializers=[Y_DESERIALIZER]
    )
    assert x_gateset.name == 'x'
    assert x_gateset.is_supported_operation(cirq.X(q))
    assert not x_gateset.is_supported_operation(cirq.Y(q))
    assert xy_gateset.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
示例#19
0
def test_proto_with_waitgate():
    wait_gateset = cirq_google.SerializableGateSet(
        gate_set_name='wait_gateset',
        serializers=[cgc.WAIT_GATE_SERIALIZER],
        deserializers=[cgc.WAIT_GATE_DESERIALIZER],
    )
    wait_proto = cirq_google.devices.known_devices.create_device_proto_from_diagram(
        "aa\naa", [wait_gateset])
    wait_device = cirq_google.SerializableDevice.from_proto(
        proto=wait_proto, gate_sets=[wait_gateset])
    q0 = cirq.GridQubit(1, 1)
    wait_op = cirq.wait(q0, nanos=25)
    wait_device.validate_operation(wait_op)

    assert (str(wait_proto) == """\
valid_gate_sets {
  name: "wait_gateset"
  valid_gates {
    id: "wait"
    number_of_qubits: 1
    valid_args {
      name: "nanos"
      type: FLOAT
    }
  }
}
valid_qubits: "0_0"
valid_qubits: "0_1"
valid_qubits: "1_0"
valid_qubits: "1_1"
valid_targets {
  name: "meas_targets"
  target_ordering: SUBSET_PERMUTATION
}
valid_targets {
  name: "2_qubit_targets"
  target_ordering: SYMMETRIC
  targets {
    ids: "0_0"
    ids: "0_1"
  }
  targets {
    ids: "0_0"
    ids: "1_0"
  }
  targets {
    ids: "0_1"
    ids: "1_1"
  }
  targets {
    ids: "1_0"
    ids: "1_1"
  }
}
""")
示例#20
0
def _my_gate_set():
    x_serializer = _x_serializer()
    x_deserializer = _x_deserializer()
    with cirq.testing.assert_deprecated('SerializableGateSet',
                                        deadline='v0.16',
                                        count=1):
        return cg.SerializableGateSet(
            gate_set_name='my_gate_set',
            serializers=[x_serializer, CIRCUIT_OP_SERIALIZER],
            deserializers=[x_deserializer, CIRCUIT_OP_DESERIALIZER],
        )
示例#21
0
def test_cz_pow_non_integer_does_not_serialize():
    # 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.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)
示例#22
0
def test_fsim_gate_not_allowed(gate):
    # Deprecations: cirq_google.SerializableGateSet and common serializers.
    with cirq.testing.assert_deprecated('SerializableGateSet',
                                        'CircuitSerializer',
                                        deadline='v0.16',
                                        count=3):
        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))
示例#23
0
def _half_pi_gate_set():
    # Deprecations: cirq_google.SerializableGateSet and common serializers.
    with cirq.testing.assert_deprecated('SerializableGateSet',
                                        'CircuitSerializer',
                                        deadline='v0.16',
                                        count=5):
        return cg.SerializableGateSet(
            gate_set_name='test_half_pi',
            serializers=([cgc.MEASUREMENT_SERIALIZER] +
                         cgc.SINGLE_QUBIT_HALF_PI_SERIALIZERS),
            deserializers=([cgc.MEASUREMENT_DESERIALIZER] +
                           cgc.SINGLE_QUBIT_HALF_PI_DESERIALIZERS),
        )
示例#24
0
def test_proto_with_circuitop():
    # Deprecations: cirq_google.SerializableGateSets class and
    # cirq_google.devices.known_devices.create_device_proto_from_diagram
    with cirq.testing.assert_deprecated('SerializableGateSet',
                                        'no longer be available',
                                        deadline='v0.16',
                                        count=3):
        circuitop_gateset = cirq_google.SerializableGateSet(
            gate_set_name='circuitop_gateset',
            serializers=[cgc.CIRCUIT_OP_SERIALIZER],
            deserializers=[cgc.CIRCUIT_OP_DESERIALIZER],
        )
        circuitop_proto = cirq_google.devices.known_devices.create_device_proto_from_diagram(
            "aa\naa", [circuitop_gateset])

        assert (str(circuitop_proto) == """\
valid_gate_sets {
  name: "circuitop_gateset"
  valid_gates {
    id: "circuit"
  }
}
valid_qubits: "0_0"
valid_qubits: "0_1"
valid_qubits: "1_0"
valid_qubits: "1_1"
valid_targets {
  name: "meas_targets"
  target_ordering: SUBSET_PERMUTATION
}
valid_targets {
  name: "2_qubit_targets"
  target_ordering: SYMMETRIC
  targets {
    ids: "0_0"
    ids: "0_1"
  }
  targets {
    ids: "0_0"
    ids: "1_0"
  }
  targets {
    ids: "0_1"
    ids: "1_1"
  }
  targets {
    ids: "1_0"
    ids: "1_1"
  }
}
""")
示例#25
0
def test_coupler_pulse():
    # 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.COUPLER_PULSE_SERIALIZER],
                                          [cgc.COUPLER_PULSE_DESERIALIZER])
        proto = op_proto({
            'gate': {
                'id': 'coupler_pulse'
            },
            'args': {
                'hold_time_ns': {
                    'arg_value': {
                        'float_value': 16.0
                    }
                },
                'padding_time_ns': {
                    'arg_value': {
                        'float_value': 12.0
                    }
                },
                'rise_time_ns': {
                    'arg_value': {
                        'float_value': 32.0
                    }
                },
                'coupling_mhz': {
                    'arg_value': {
                        'float_value': 20.0
                    }
                },
            },
            'qubits': [{
                'id': '1_2'
            }, {
                'id': '2_2'
            }],
        })
        q = cirq.GridQubit(1, 2)
        q2 = cirq.GridQubit(2, 2)
        op = cg.experimental.ops.CouplerPulse(
            hold_time=cirq.Duration(nanos=16),
            padding_time=cirq.Duration(nanos=12),
            rise_time=cirq.Duration(nanos=32),
            coupling_mhz=20,
        )(q, q2)
        assert gate_set.serialize_op(op) == proto
        assert gate_set.deserialize_op(proto) == op
示例#26
0
def test_proto_with_circuitop():
    circuitop_gateset = cirq_google.SerializableGateSet(
        gate_set_name='circuitop_gateset',
        serializers=[cgc.CIRCUIT_OP_SERIALIZER],
        deserializers=[cgc.CIRCUIT_OP_DESERIALIZER],
    )
    circuitop_proto = cirq_google.devices.known_devices.create_device_proto_from_diagram(
        "aa\naa",
        [circuitop_gateset],
    )

    assert (
        str(circuitop_proto)
        == """\
valid_gate_sets {
  name: "circuitop_gateset"
  valid_gates {
    id: "circuit"
  }
}
valid_qubits: "0_0"
valid_qubits: "0_1"
valid_qubits: "1_0"
valid_qubits: "1_1"
valid_targets {
  name: "meas_targets"
  target_ordering: SUBSET_PERMUTATION
}
valid_targets {
  name: "2_qubit_targets"
  target_ordering: SYMMETRIC
  targets {
    ids: "0_0"
    ids: "0_1"
  }
  targets {
    ids: "0_0"
    ids: "1_0"
  }
  targets {
    ids: "0_1"
    ids: "1_1"
  }
  targets {
    ids: "1_0"
    ids: "1_1"
  }
}
"""
    )
示例#27
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_multiple_gatesets():
    # Deprecations: cirq_google.SerializableDevice, cirq_google.SerializableGateSet,
    # common serializers, and
    # cirq_google.devices.known_devices.create_device_proto_from_diagram,
    with cirq.testing.assert_deprecated(
            'Use cirq_google.GridDevice',
            'SerializableGateSet',
            'CircuitSerializer',
            'no longer be available',
            deadline='v0.16',
            count=10,
    ):
        halfPiGateSet = cirq_google.SerializableGateSet(
            gate_set_name='half_pi_gateset',
            serializers=cgc.SINGLE_QUBIT_HALF_PI_SERIALIZERS,
            deserializers=cgc.SINGLE_QUBIT_HALF_PI_DESERIALIZERS,
        )
        allAnglesGateSet = cirq_google.SerializableGateSet(
            gate_set_name='all_angles_gateset',
            serializers=cgc.SINGLE_QUBIT_SERIALIZERS,
            deserializers=cgc.SINGLE_QUBIT_DESERIALIZERS,
        )
        durations_dict = {'xy_pi': 20_000, 'xy_half_pi': 20_000, 'xy': 20_000}
        spec = cirq_google.devices.known_devices.create_device_proto_from_diagram(
            "aa\naa", [allAnglesGateSet, halfPiGateSet], durations_dict)
        dev = cg.SerializableDevice.from_proto(
            proto=spec, gate_sets=[allAnglesGateSet, halfPiGateSet])
        q0 = cirq.GridQubit(0, 0)
        q1 = cirq.GridQubit(1, 0)
        dev.validate_operation(cirq.X(q0))
        dev.validate_operation(cirq.X(q1))
        dev.validate_operation(cirq.XPowGate(exponent=0.1234)(q0))
        dev.validate_operation(cirq.XPowGate(exponent=0.2345)(q1))

        with pytest.raises(ValueError):
            dev.validate_operation(cirq.X(cirq.GridQubit(2, 2)))
示例#29
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)
示例#30
0
def test_coupler_pulse():
    gate_set = cg.SerializableGateSet('test', [cgc.COUPLER_PULSE_SERIALIZER],
                                      [cgc.COUPLER_PULSE_DESERIALIZER])
    proto = op_proto({
        'gate': {
            'id': 'coupler_pulse'
        },
        'args': {
            'hold_time_ns': {
                'arg_value': {
                    'float_value': 16.0
                }
            },
            'padding_time_ns': {
                'arg_value': {
                    'float_value': 12.0
                }
            },
            'rise_time_ns': {
                'arg_value': {
                    'float_value': 32.0
                }
            },
            'coupling_mhz': {
                'arg_value': {
                    'float_value': 20.0
                }
            },
        },
        'qubits': [{
            'id': '1_2'
        }, {
            'id': '2_2'
        }],
    })
    q = cirq.GridQubit(1, 2)
    q2 = cirq.GridQubit(2, 2)
    op = cg.experimental.ops.CouplerPulse(
        hold_time=cirq.Duration(nanos=16),
        padding_time=cirq.Duration(nanos=12),
        rise_time=cirq.Duration(nanos=32),
        coupling_mhz=20,
    )(q, q2)
    assert gate_set.serialize_op(op) == proto
    assert gate_set.deserialize_op(proto) == op