예제 #1
0
def main():
    backend = EQSNBackend()
    network = Network.get_instance()
    nodes = ["Alice", "Bob", "Eve", "Dean"]
    network.start(nodes, backend)
    network.delay = 0.0

    hosts = {'alice': Host('Alice', backend),
             'bob': Host('Bob', backend)}

    # A <-> B
    hosts['alice'].add_connection('Bob')
    hosts['bob'].add_connection('Alice')

    hosts['alice'].start()
    hosts['bob'].start()

    for h in hosts.values():
        network.add_host(h)

    ack_received_1 = hosts['alice'].send_classical(
        hosts['bob'].host_id, 'hello bob one', await_ack=True)
    hosts['alice'].max_ack_wait = 0.0
    ack_received_2 = hosts['alice'].send_classical(
        hosts['bob'].host_id, 'hello bob one', await_ack=True)
    assert ack_received_1
    assert not ack_received_2
    print("All tests succesfull!")
    network.stop(True)
    exit()
예제 #2
0
    def test_density_operator(self):
        """
        Test EQSN.
        """
        backend = EQSNBackend()
        network = Network.get_instance()
        network.start(["Alice", "Bob"], backend)
        alice = Host('Alice', backend)
        bob = Host('Bob', backend)
        alice.start()
        bob.start()
        network.add_host(alice)
        network.add_host(bob)

        q1 = backend.create_EPR(alice.host_id, bob.host_id)
        q2 = backend.receive_epr(bob.host_id, alice.host_id, q_id=q1.id)

        density_operator = backend.density_operator(q1)
        expected = np.diag([0.5, 0.5])
        self.assertTrue(np.allclose(density_operator, expected))

        # remove qubits
        backend.measure(q1, False)
        backend.measure(q2, False)

        network.stop(True)
예제 #3
0
파일: e91.py 프로젝트: tqsd/QuNetSim
def main():
    network = Network.get_instance()

    backend = EQSNBackend()

    number_of_entanglement_pairs = 50

    nodes = ['A', 'B']
    network.start(nodes, backend)
    network.delay = 0.1

    host_A = Host('A', backend)
    host_A.add_connection('B')
    host_A.delay = 0
    host_A.start()

    host_B = Host('B', backend)
    host_B.add_connection('A')
    host_B.delay = 0
    host_B.start()

    network.add_host(host_A)
    network.add_host(host_B)

    t1 = host_A.run_protocol(alice,
                             (host_B.host_id, number_of_entanglement_pairs))
    t2 = host_B.run_protocol(bob,
                             (host_A.host_id, number_of_entanglement_pairs))

    t1.join()
    t2.join()
    network.stop(True)
예제 #4
0
def test_ghz_ten_nodes(benchmark):
    backend = EQSNBackend()
    nodes = 10
    network, hosts = setup_network(nodes, backend)
    ms = benchmark.pedantic(ghz, args=(hosts[0], hosts[1:]), rounds=30)
    for m in ms:
        assert (sum(m) == nodes - 1 or sum(m) == 0)
    network.stop(True)
예제 #5
0
파일: qkd_eqsn.py 프로젝트: tqsd/QuNetSim
def main():
    # Initialize a network
    network = Network.get_instance()
    backend = EQSNBackend()

    # Define the host IDs in the network
    nodes = ['Alice', 'Bob']

    network.delay = 0.0

    # Start the network with the defined hosts
    network.start(nodes, backend)

    # Initialize the host Alice
    host_alice = Host('Alice', backend)

    # Add a one-way connection (classical and quantum) to Bob
    host_alice.add_connection('Bob')
    host_alice.delay = 0.0

    # Start listening
    host_alice.start()

    host_bob = Host('Bob', backend)
    # Bob adds his own one-way connection to Alice
    host_bob.add_connection('Alice')
    host_bob.delay = 0.0
    host_bob.start()

    # Add the hosts to the network
    # The network is: Alice <--> Bob
    network.add_host(host_alice)
    network.add_host(host_bob)

    # Generate random key
    key_size = 20  # the size of the key in bit
    secret_key = np.random.randint(2, size=key_size)

    # Concatentate functions
    def alice_func(alice):
        msg_buff = []
        alice_qkd(alice, msg_buff, secret_key, host_bob.host_id)
        alice_send_message(alice, secret_key, host_bob.host_id)

    def bob_func(eve):
        msg_buff = []
        eve_key = eve_qkd(eve, msg_buff, key_size, host_alice.host_id)
        eve_receive_message(eve, msg_buff, eve_key, host_alice.host_id)

    # Run Bob and Alice

    t1 = host_alice.run_protocol(alice_func, ())
    t2 = host_bob.run_protocol(bob_func, ())

    t1.join()
    t2.join()

    network.stop(True)
예제 #6
0
    def setUp(self):
        network = Network.get_instance()
        network.start(["host_1"], EQSNBackend())

        self.controller_host = ControllerHost(
            host_id="host_1",
            computing_host_ids=["QPU_1"])
        network.add_host(self.controller_host)

        self._network = network
예제 #7
0
파일: send_w.py 프로젝트: AleMuzzi/QuNetSim
def main():
    network = Network.get_instance()
    nodes = ["Alice", "Bob", "Eve", "Dean"]
    back = EQSNBackend()
    network.start(nodes, back)

    network.delay = 0.1

    host_alice = Host('Alice', back)
    host_alice.add_connection('Bob')
    host_alice.add_connection('Eve')
    host_alice.start()

    host_bob = Host('Bob', back)
    host_bob.add_connection('Alice')
    host_bob.add_connection('Eve')
    host_bob.start()

    host_eve = Host('Eve', back)
    host_eve.add_connection('Bob')
    host_eve.add_connection('Dean')
    host_eve.add_connection('Alice')
    host_eve.start()

    host_dean = Host('Dean', back)
    host_dean.add_connection('Eve')
    host_dean.start()

    network.add_host(host_alice)
    network.add_host(host_bob)
    network.add_host(host_eve)
    network.add_host(host_dean)

    share_list = ["Bob", "Eve", "Dean"]
    q_id1, ack_received = host_alice.send_w(share_list, await_ack=True)

    print("Alice received ACK from all? " + str(ack_received))

    q1 = host_alice.get_w('Alice', q_id1, wait=10)
    q2 = host_bob.get_w('Alice', q_id1, wait=10)
    q3 = host_eve.get_w('Alice', q_id1, wait=10)
    q4 = host_dean.get_w('Alice', q_id1, wait=10)

    print("System density matrix:")
    print(q1._qubit[0].data)

    m1 = q1.measure()
    m2 = q2.measure()
    m3 = q3.measure()
    m4 = q4.measure()

    print("\nResults of measurements are %d, %d, %d, %d." % (m1, m2, m3, m4))

    network.stop(True)
    exit()
예제 #8
0
    def start(self, nodes=None, backend=None):
        """
        Starts the network.

        """
        if backend is None:
            self._backend = EQSNBackend()
        else:
            self._backend = backend
        if nodes is not None:
            self._backend.start(nodes=nodes)
        self._queue_processor_thread = DaemonThread(target=self._process_queue)
예제 #9
0
 def setUpClass(cls):
     global network
     global hosts
     nodes = ["Alice", "Bob"]
     backend = EQSNBackend()
     network.start(nodes=nodes, backend=backend)
     hosts = {'alice': Host('Alice', backend), 'bob': Host('Bob', backend)}
     hosts['alice'].add_connection('Bob')
     hosts['bob'].add_connection('Alice')
     hosts['alice'].start()
     hosts['bob'].start()
     for h in hosts.values():
         network.add_host(h)
예제 #10
0
def main():
    network = Network.get_instance()
    nodes = ["Alice", "Bob", "Eve", "Dean"]
    back = EQSNBackend()
    network.start(nodes, back)

    network.delay = 0.1

    host_alice = Host('Alice', back)
    host_alice.add_connection('Bob')
    host_alice.add_connection('Eve')
    host_alice.start()

    host_bob = Host('Bob', back)
    host_bob.add_connection('Alice')
    host_bob.add_connection('Eve')
    host_bob.start()

    host_eve = Host('Eve', back)
    host_eve.add_connection('Bob')
    host_eve.add_connection('Dean')
    host_eve.add_connection('Alice')
    host_eve.start()

    host_dean = Host('Dean', back)
    host_dean.add_connection('Eve')
    host_dean.start()

    network.add_host(host_alice)
    network.add_host(host_bob)
    network.add_host(host_eve)
    network.add_host(host_dean)

    share_list = ["Bob", "Eve", "Dean"]
    q_id1 = host_alice.send_w(share_list, no_ack=True)

    q1 = host_alice.get_w('Alice', q_id1, wait=10)
    q2 = host_bob.get_w('Alice', q_id1, wait=10)
    q3 = host_eve.get_w('Alice', q_id1, wait=10)
    q4 = host_dean.get_w('Alice', q_id1, wait=10)

    m1 = q1.measure()
    m2 = q2.measure()
    m3 = q3.measure()
    m4 = q4.measure()

    print("\nResults of measurements are %d, %d, %d, %d." % (m1, m2, m3, m4))

    network.stop(True)
    exit()
예제 #11
0
def main():
    backend = EQSNBackend()
    network = Network.get_instance()
    nodes = ["Alice", "Bob", "Eve", "Dean"]
    network.start(nodes, backend)
    network.delay = 0.7

    hosts = {'alice': Host('Alice', backend), 'bob': Host('Bob', backend)}

    network.delay = 0
    # A <-> B
    hosts['alice'].add_connection('Bob')
    hosts['bob'].add_connection('Alice')

    hosts['alice'].start()
    hosts['bob'].start()

    for h in hosts.values():
        network.add_host(h)

    # send messages to Bob without waiting for ACKs
    hosts['alice'].send_classical(hosts['bob'].host_id,
                                  'hello bob one',
                                  await_ack=False)
    hosts['alice'].send_classical(hosts['bob'].host_id,
                                  'hello bob two',
                                  await_ack=False)
    hosts['alice'].send_classical(hosts['bob'].host_id,
                                  'hello bob three',
                                  await_ack=False)
    hosts['alice'].send_classical(hosts['bob'].host_id,
                                  'hello bob four',
                                  await_ack=False)

    # Wait for all Acks from Bob
    hosts['alice'].await_remaining_acks(hosts['bob'].host_id)

    saw_ack = [False, False, False, False]
    messages = hosts['alice'].classical
    for m in messages:
        if m.content == Constants.ACK:
            saw_ack[m.seq_num] = True

    for ack in saw_ack:
        assert ack
    print("All tests succesfull!")
    network.stop(True)
예제 #12
0
def main():
    backend = EQSNBackend()
    network = Network.get_instance()
    nodes = ['Alice', 'Eve', 'Bob']
    network.start(nodes)

    host_alice = Host('Alice', backend)
    host_alice.add_connection('Eve')
    host_alice.start()

    host_eve = Host('Eve', backend)
    host_eve.add_connections(['Alice', 'Bob'])
    host_eve.start()

    host_bob = Host('Bob', backend)
    host_bob.add_connection('Eve')
    host_bob.start()

    network.add_host(host_alice)
    network.add_host(host_eve)
    network.add_host(host_bob)
    # network.draw_classical_network()
    # network.draw_quantum_network()
    network.start()

    alice_basis, bob_basis = preparation()
    print("Alice bases: {}".format(alice_basis))
    print("Bob bases: {}".format(bob_basis))

    if INTERCEPTION:
        host_eve.q_relay_sniffing = True
        host_eve.q_relay_sniffing_fn = eve_sniffing_quantum

    t1 = host_alice.run_protocol(alice, (
        host_bob.host_id,
        alice_basis,
    ))
    t2 = host_bob.run_protocol(bob, (
        host_alice.host_id,
        bob_basis,
    ))
    t1.join()
    t2.join()

    network.stop(True)
    exit()
def main():
    backend = EQSNBackend()
    network = Network.get_instance()
    nodes = ["Alice", "Bob", "Eve", "Dean"]
    network.start(nodes, backend)
    network.delay = 0.0

    hosts = {'alice': Host('Alice', backend), 'bob': Host('Bob', backend)}

    network.start(nodes, backend)
    network.packet_drop_rate = 0.5
    network.delay = 0

    hosts['alice'].add_connection('Bob')
    hosts['bob'].add_connection('Alice')

    hosts['alice'].start()
    hosts['bob'].start()

    for h in hosts.values():
        network.add_host(h)

    # ACKs for 1 hop take at most 2 seconds
    hosts['alice'].max_ack_wait = 0.5
    num_acks = 0
    # don't make more then 10 attempts, since of receiver window.
    num_messages = 20
    for _ in range(num_messages):
        ack = hosts['alice'].send_classical(hosts['bob'].host_id,
                                            'Hello Bob',
                                            await_ack=True)
        if ack:
            num_acks += 1

    num_messages_bob_received = len(hosts['bob'].classical)
    assert num_acks != num_messages
    assert num_acks < num_messages
    assert num_messages_bob_received < num_messages

    # ACKs can also get dropped
    assert num_messages_bob_received > num_acks
    assert float(num_acks) / num_messages < 0.9
    print("All tests succesfull!")
    network.stop(True)
    exit()
예제 #14
0
    def setUp(self):
        network = Network.get_instance()
        network.start(["host_1", "QPU_1", "QPU_2", "QPU_3"], EQSNBackend())

        clock = Clock()

        self.computing_host_1 = ComputingHost(host_id="QPU_1",
                                              controller_host_id="host_1",
                                              clock=clock,
                                              total_qubits=2)

        self.computing_host_2 = ComputingHost(host_id="QPU_2",
                                              controller_host_id="host_1",
                                              clock=clock,
                                              total_qubits=2)

        self.computing_host_3 = ComputingHost(host_id="QPU_3",
                                              controller_host_id="host_1",
                                              clock=clock,
                                              total_qubits=2)

        self.controller_host = ControllerHost(
            host_id="host_1",
            clock=clock,
            computing_host_ids=["QPU_1", "QPU_2", "QPU_3"])

        self.computing_host_1.add_connections(['QPU_2', 'QPU_3'])
        self.computing_host_1.start()
        self.computing_host_2.add_connections(['QPU_1', 'QPU_3'])
        self.computing_host_2.start()
        self.computing_host_3.add_connections(['QPU_1', 'QPU_2'])
        self.computing_host_3.start()

        self.controller_host.start()

        network.add_hosts([
            self.controller_host, self.computing_host_1, self.computing_host_2,
            self.computing_host_3
        ])

        self.network = network
        self.clock = clock
예제 #15
0
def main():
    backend = EQSNBackend()
    network = Network.get_instance()
    nodes = ["Alice", "Bob", "Eve", "Dean"]
    network.start(nodes, backend)
    network.delay = 0.7
    hosts = {'alice': Host('Alice', backend), 'bob': Host('Bob', backend)}

    # A <-> B
    hosts['alice'].add_connection('Bob')
    hosts['bob'].add_connection('Alice')

    hosts['alice'].start()
    hosts['bob'].start()

    for h in hosts.values():
        network.add_host(h)

    q_id = hosts['alice'].send_epr(hosts['bob'].host_id)
    q1 = hosts['alice'].get_epr(hosts['bob'].host_id, q_id)
    i = 0
    while q1 is None and i < 5:
        q1 = hosts['alice'].get_epr(hosts['bob'].host_id, q_id)
        i += 1
        time.sleep(1)

    assert q1 is not None
    i = 0
    q2 = hosts['bob'].get_epr(hosts['alice'].host_id, q_id)
    while q2 is None and i < 5:
        q2 = hosts['bob'].get_epr(hosts['alice'].host_id, q_id)
        i += 1
        time.sleep(1)

    assert q2 is not None
    assert (q1.measure() == q2.measure())
    print("All tests succesfull!")
    network.stop(True)
    exit()
예제 #16
0
def main():
    network = Network.get_instance()

    # backend = ProjectQBackend()
    # backend = CQCBackend()
    backend = EQSNBackend()

    nodes = ['A', 'B']
    network.delay = 0.1
    network.start(nodes, backend)

    host_A = Host('A', backend)
    host_A.add_connection('B')
    host_A.delay = 0
    host_A.start()

    host_B = Host('B', backend)
    host_B.add_connection('A')
    host_B.delay = 0
    host_B.start()

    network.add_host(host_A)
    network.add_host(host_B)

    m = 2
    n = 4
    rot_angle = np.pi / 9

    t1 = host_A.run_protocol(quantum_coin_flipping,
                             arguments=(m, n, host_B.host_id, rot_angle))
    t2 = host_B.run_protocol(quantum_coin_flipping,
                             arguments=(m, n, host_A.host_id, rot_angle))

    t1.join()
    t2.join()

    network.stop(True)
예제 #17
0
def test_teleport_ten_hops(benchmark):
    backend = EQSNBackend()
    network, hosts = setup_network(11, backend)
    benchmark.pedantic(superdense, args=(hosts[0], hosts[-1]), rounds=30)
    network.stop(True)