Ejemplo n.º 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
Ejemplo n.º 2
0
def test_Memory_excite():
    NUM_TESTS = 1000

    tl = Timeline()
    rec = DumbReceiver()
    mem = Memory("mem",
                 tl,
                 fidelity=1,
                 frequency=0,
                 efficiency=1,
                 coherence_time=-1,
                 wavelength=500)
    mem.owner = rec

    # test with perfect efficiency

    for _ in range(NUM_TESTS):
        mem.excite()

    assert len(rec.photon_list) == NUM_TESTS
    null_photons = [p for p in rec.photon_list if p.is_null]
    null_ratio = len(null_photons) / NUM_TESTS
    assert null_ratio == 1

    # test with imperfect efficiency

    rec.photon_list = []
    mem.efficiency = 0.7
    mem.update_state([complex(0), complex(1)])

    for _ in range(NUM_TESTS):
        mem.excite()

    assert abs(len(rec.photon_list) / NUM_TESTS - 0.7) < 0.1
    null_photons = [p for p in rec.photon_list if p.is_null]
    null_ratio = len(null_photons) / len(rec.photon_list)
    assert null_ratio == 0

    # test with perfect efficiency, + state

    rec.photon_list = []
    mem.efficiency = 1
    plus = [math.sqrt(1 / 2), math.sqrt(1 / 2)]

    for _ in range(NUM_TESTS):
        mem.update_state(plus)
        mem.excite()

    assert len(rec.photon_list) == NUM_TESTS
    null_photons = [p for p in rec.photon_list if p.is_null]
    null_ratio = len(null_photons) / NUM_TESTS
    assert abs(null_ratio - 0.5) < 0.1
Ejemplo n.º 3
0
def test_Memory_update_state():
    new_state = [complex(0), complex(1)]

    tl = Timeline()
    mem = Memory("mem",
                 tl,
                 fidelity=1,
                 frequency=0,
                 efficiency=1,
                 coherence_time=-1,
                 wavelength=500)

    mem.update_state(new_state)

    assert len(tl.quantum_manager.states) == 1
    assert (tl.quantum_manager.get(mem.qstate_key) == np.array(new_state)).all