Example #1
0
def test_Memory_expire():
    class FakeProtocol(EntanglementProtocol):
        def __init__(self, name):
            super().__init__(None, name)
            self.is_expire = False

        def set_others(self, other: "EntanglementProtocol") -> None:
            pass

        def start(self) -> None:
            pass

        def is_ready(self) -> bool:
            pass

        def memory_expire(self, memory) -> None:
            self.is_expire = True

        def received_message(self, src: str, msg: "Message"):
            pass

        def expire(self, memory: Memory):
            self.memory_expire(memory)

    tl = Timeline()
    mem = Memory("mem",
                 tl,
                 fidelity=1,
                 frequency=0,
                 efficiency=1,
                 coherence_time=-1,
                 wavelength=500)
    parent = DumbParent(mem)
    protocol = FakeProtocol("upper_protocol")
    mem.attach(protocol)
    mem.update_state([math.sqrt(1 / 2), math.sqrt(1 / 2)])
    entangled_memory = {"node_id": "node", "memo_id": 0}
    mem.entangled_memory = entangled_memory

    # expire when the protocol controls memory
    mem.detach(parent)
    assert len(parent.pop_log) == 0 and protocol.is_expire is False
    mem.expire()
    assert (tl.quantum_manager.get(mem.qstate_key).state == np.array(
        [1, 0])).all  # check if collapsed to |0> state
    assert mem.entangled_memory == {"node_id": None, "memo_id": None}
    assert len(parent.pop_log) == 0 and protocol.is_expire is True

    # expire when the resource manager controls memory
    mem.attach(parent)
    mem.detach(protocol)
    mem.update_state([math.sqrt(1 / 2), math.sqrt(1 / 2)])
    entangled_memory = {"node_id": "node", "memo_id": 0}
    mem.entangled_memory = entangled_memory
    mem.expire()
    assert len(parent.pop_log) == 1
def create_scenario(state1, state2, seed):
    tl = Timeline()
    tl.seed(seed)
    a1 = FakeNode("a1", tl)
    a2 = FakeNode("a2", tl)
    a3 = FakeNode("a3", tl)
    cc0 = ClassicalChannel("a2-a1", tl, 0, 1e5)
    cc1 = ClassicalChannel("a2-a3", tl, 0, 1e5)
    cc0.set_ends(a2, a1)
    cc1.set_ends(a2, a3)
    tl.init()

    memo1 = Memory("a1.0", timeline=tl, fidelity=0.9, frequency=0, efficiency=1, coherence_time=1, wavelength=500)
    memo2 = Memory("a2.0", tl, 0.9, 0, 1, 1, 500)
    memo3 = Memory("a2.1", tl, 0.9, 0, 1, 1, 500)
    memo4 = Memory("a3.0", tl, 0.9, 0, 1, 1, 500)

    memo1.fidelity = memo2.fidelity = memo3.fidelity = memo4.fidelity = 1
    memo1.entangled_memory = {'node_id': 'a2', 'memo_id': memo2.name}
    memo2.entangled_memory = {'node_id': 'a1', 'memo_id': memo1.name}
    memo3.entangled_memory = {'node_id': 'a3', 'memo_id': memo4.name}
    memo4.entangled_memory = {'node_id': 'a2', 'memo_id': memo3.name}

    tl.quantum_manager.set([memo1.qstate_key, memo2.qstate_key], state1)
    tl.quantum_manager.set([memo3.qstate_key, memo4.qstate_key], state2)

    es1 = EntanglementSwappingB(a1, "a1.ESb0", memo1)
    a1.protocols.append(es1)
    es2 = EntanglementSwappingA(a2, "a2.ESa0", memo2, memo3)
    a2.protocols.append(es2)
    es3 = EntanglementSwappingB(a3, "a3.ESb1", memo4)
    a3.protocols.append(es3)

    es1.set_others(es2)
    es3.set_others(es2)
    es2.set_others(es1)
    es2.set_others(es3)

    es2.start()

    tl.run()

    ket1, ket2, ket3, ket4 = map(tl.quantum_manager.get,
                                 [memo1.qstate_key, memo2.qstate_key, memo3.qstate_key, memo4.qstate_key])

    assert id(ket1) == id(ket4)
    assert id(ket2) != id(ket3)
    assert len(ket1.keys) == 2 and memo1.qstate_key in ket1.keys and memo4.qstate_key in ket1.keys
    assert len(ket2.keys) == 1

    assert memo2.entangled_memory == memo3.entangled_memory == {'node_id': None, 'memo_id': None}
    assert memo1.entangled_memory["node_id"] == "a3" and memo4.entangled_memory["node_id"] == "a1"
    assert a1.resource_manager.log[-1] == (memo1, "ENTANGLED")
    assert a3.resource_manager.log[-1] == (memo4, "ENTANGLED")
    return ket1, ket2, ket3, ket4, a3
def create_scenario(state1, state2, seed):
    tl = Timeline()
    tl.seed(seed)
    a1 = FakeNode("a1", tl)
    a2 = FakeNode("a2", tl)
    cc0 = ClassicalChannel("cc0", tl, 0, 1e5)
    cc1 = ClassicalChannel("cc1", tl, 0, 1e5)
    cc0.delay = ONE_MILLISECOND
    cc1.delay = ONE_MILLISECOND
    cc0.set_ends(a1, a2)
    cc1.set_ends(a2, a1)

    kept1 = Memory('kept1', tl, fidelity=1, frequency=0, efficiency=1, coherence_time=1, wavelength=HALF_MICRON)
    kept2 = Memory('kept2', tl, fidelity=1, frequency=0, efficiency=1, coherence_time=1, wavelength=HALF_MICRON)
    meas1 = Memory('mea1', tl, fidelity=1, frequency=0, efficiency=1, coherence_time=1, wavelength=HALF_MICRON)
    meas2 = Memory('mea2', tl, fidelity=1, frequency=0, efficiency=1, coherence_time=1, wavelength=HALF_MICRON)

    tl.init()

    tl.quantum_manager.set([kept1.qstate_key, kept2.qstate_key], state1)
    tl.quantum_manager.set([meas1.qstate_key, meas2.qstate_key], state2)

    kept1.entangled_memory = {'node_id': 'a2', 'memo_id': 'kept2'}
    kept2.entangled_memory = {'node_id': 'a1', 'memo_id': 'kept1'}
    meas1.entangled_memory = {'node_id': 'a2', 'memo_id': 'meas2'}
    meas2.entangled_memory = {'node_id': 'a1', 'memo_id': 'meas1'}
    kept1.fidelity = kept2.fidelity = meas1.fidelity = meas2.fidelity = 1

    ep1 = BBPSSW(a1, "a1.ep1", kept1, meas1)
    ep2 = BBPSSW(a2, "a2.ep2", kept2, meas2)
    a1.protocols.append(ep1)
    a2.protocols.append(ep2)
    ep1.set_others(ep2)
    ep2.set_others(ep1)

    ep1.start()
    ep2.start()

    tl.run()

    assert meas1.entangled_memory == meas2.entangled_memory == {'node_id': None, 'memo_id': None}

    return tl, kept1, kept2, meas1, meas2, ep1, ep2