Beispiel #1
0
def test_with_key_path():
    a = cirq.LineQubit(0)
    op = cirq.measure(a, key='m')

    remap_op = cirq.with_key_path(op, ('a', 'b'))
    assert cirq.measurement_keys(remap_op) == {'a:b:m'}
    assert cirq.with_key_path(remap_op, ('a', 'b')) is remap_op

    assert cirq.with_key_path(op, tuple()) is op

    assert cirq.with_key_path(cirq.X(a), ('a', 'b')) is NotImplemented
def test_with_key_path():
    mkey = cirq.MeasurementKey('key')
    mkey2 = cirq.with_key_path(mkey, ('a', ))
    assert mkey2.name == mkey.name
    assert mkey2.path == ('a', )
    assert mkey2 == mkey.with_key_path_prefix('a')

    mkey3 = mkey2.with_key_path_prefix('b')
    assert mkey3.name == mkey.name
    assert mkey3.path == ('b', 'a')
Beispiel #3
0
def test_with_key_path():
    a, b = cirq.LineQubit.range(2)
    m = cirq.Moment(cirq.measure(a, key='m1'), cirq.measure(b, key='m2'))

    new_moment = cirq.with_key_path(m, ('a', 'b'))

    assert new_moment.operations[0] == cirq.measure(
        a, key=cirq.MeasurementKey.parse_serialized('a:b:m1'))
    assert new_moment.operations[1] == cirq.measure(
        b, key=cirq.MeasurementKey.parse_serialized('a:b:m2'))
Beispiel #4
0
def test_with_key_path(mkey):
    mkey2 = cirq.with_key_path(mkey, ('a', ))
    assert mkey2.name == mkey.name
    assert mkey2.qubits == mkey.qubits
    assert mkey2.path == ('a', )
    assert mkey2 == mkey.with_key_path_prefix('a')

    mkey3 = mkey2.with_key_path_prefix('b')
    assert mkey3.name == mkey.name
    assert mkey3.qubits == mkey.qubits
    assert mkey3.path == ('b', 'a')
def test_measurement_key_path():
    class MultiKeyGate:
        def __init__(self, keys):
            self._keys = set([cirq.MeasurementKey.parse_serialized(key) for key in keys])

        def _measurement_key_names_(self):
            return {str(key) for key in self._keys}

        def _with_key_path_(self, path):
            return MultiKeyGate([str(key._with_key_path_(path)) for key in self._keys])

    assert cirq.measurement_key_names(MultiKeyGate([])) == set()
    assert cirq.measurement_key_names(MultiKeyGate(['a'])) == {'a'}

    mkg_ab = MultiKeyGate(['a', 'b'])
    assert cirq.measurement_key_names(mkg_ab) == {'a', 'b'}

    mkg_cd = cirq.with_key_path(mkg_ab, ('c', 'd'))
    assert cirq.measurement_key_names(mkg_cd) == {'c:d:a', 'c:d:b'}

    assert cirq.with_key_path(cirq.X, ('c', 'd')) is NotImplemented
def test_matrix_mixture_remap_keys():
    dp = cirq.depolarize(0.1)
    mm = cirq.MixedUnitaryChannel.from_mixture(dp)
    with pytest.raises(TypeError):
        _ = cirq.measurement_key_name(mm)
    assert cirq.with_measurement_key_mapping(mm, {'a': 'b'}) is NotImplemented

    mm_x = cirq.MixedUnitaryChannel.from_mixture(dp, key='x')
    assert cirq.with_measurement_key_mapping(mm_x, {'a': 'b'}) is mm_x
    assert cirq.measurement_key_name(cirq.with_key_path(mm_x, ('path',))) == 'path:x'

    mm_a = cirq.MixedUnitaryChannel.from_mixture(dp, key='a')
    mm_b = cirq.MixedUnitaryChannel.from_mixture(dp, key='b')
    assert mm_a != mm_b
    assert cirq.with_measurement_key_mapping(mm_a, {'a': 'b'}) == mm_b
Beispiel #7
0
def test_kraus_channel_remap_keys():
    dp = cirq.depolarize(0.1)
    kc = cirq.KrausChannel.from_channel(dp)
    with pytest.raises(TypeError):
        _ = cirq.measurement_key_name(kc)
    assert cirq.with_measurement_key_mapping(kc, {'a': 'b'}) is NotImplemented

    kc_x = cirq.KrausChannel.from_channel(dp, key='x')
    assert cirq.with_measurement_key_mapping(kc_x, {'a': 'b'}) is kc_x
    assert cirq.measurement_key_name(cirq.with_key_path(
        kc_x, ('path', ))) == 'path:x'

    kc_a = cirq.KrausChannel.from_channel(dp, key='a')
    kc_b = cirq.KrausChannel.from_channel(dp, key='b')
    assert kc_a != kc_b
    assert cirq.with_measurement_key_mapping(kc_a, {'a': 'b'}) == kc_b