예제 #1
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)
예제 #2
0
    def test_adding_hosts_to_backend(self):
        for b in TestBackend.backends:
            backend = b()
            network = Network.get_instance()
            network.start(["Alice"], backend)
            alice = Host('Alice', backend)
            alice.start()

            network.add_host(alice)
            network.stop(True)
예제 #3
0
def test_adding_hosts_to_backend(backend_generator):
    print("Starrting adding a host test...")
    backend = backend_generator()
    network = Network.get_instance()
    network.start(["Alice"], backend)
    alice = Host('Alice', backend)
    alice.start()

    network.add_host(alice)
    network.stop(True)
    print("Test was successfull!")
예제 #4
0
def test_epr_generation(backend_generator):
    print("Starting EPR generation backend.")
    backend = backend_generator()
    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)

    # Test multiple times to eliminate probabilistic effects
    for _ in range(10):
        q1 = backend.create_EPR(alice.host_id, bob.host_id)
        q2 = backend.receive_epr(bob.host_id, alice.host_id, q_id=q1.id)
        assert q1.id == q2.id
        assert backend.measure(q1, False) == backend.measure(q2, False)

    network.stop(True)
    print("Test was successful")
예제 #5
0
    def test_epr_generation(self):
        for b in TestBackend.backends:
            backend = b()
            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)

            # Test multiple times to eliminate probabilistic effects
            for _ in range(5):
                q1 = backend.create_EPR(alice.host_id, bob.host_id)
                q2 = backend.receive_epr(bob.host_id,
                                         alice.host_id,
                                         q_id=q1.id)
                assert q1.id == q2.id
                assert backend.measure(q1, False) == backend.measure(q2, False)

            network.stop(True)
예제 #6
0
    def test_single_gates(self):
        for b in TestBackend.backends:
            backend = b()
            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)

            q = Qubit(alice)

            q.X()
            self.assertEqual(1, q.measure())

            q = Qubit(alice)

            q.H()
            q.H()
            self.assertEqual(0, q.measure())

            network.stop(True)
예제 #7
0
def main():
    network = Network.get_instance()
    nodes = ['Alice', 'Eve', 'Bob']
    network.start(nodes)
    network.delay = 0.1

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

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

    host_bob = Host('Bob')
    host_bob.add_connection('Eve')
    host_bob.delay = 0.2
    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, alice_bits, alice_encoded = preparation()
    print("Alice bases: {}".format(alice_basis))
    print("Bob bases: {}".format(bob_basis))
    print("Alice bits: {}".format(alice_bits))
    print("Alice encoded: {}".format(alice_encoded))

    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,
        alice_bits,
        alice_encoded,
    ))
    t2 = host_bob.run_protocol(bob, (
        host_alice.host_id,
        bob_basis,
    ))
    t1.join()
    t2.join()

    network.stop(True)
    exit()
예제 #8
0
def main():
    # Testing
    eve_interception = False  # Set it as true to test interception
    b = BB84()
    # Network initialization
    network = Network.get_instance()
    nodes = ['Alice', 'Bob', 'Eve']
    network.start(nodes)
    host_alice = Host('Alice')
    host_alice.add_connection('Eve')
    host_alice.start()

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

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

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

    # Eve intercepts
    if (eve_interception):
        host_eve.q_relay_sniffing = True
        host_eve.q_relay_sniffing_fn = b.eve_sniffing_quantum

    network.draw_quantum_network()
    network.draw_classical_network()

    KEY_LENGTH = 500  # the size of the key in bit
    np.random.seed(0)
    secret_key = np.random.randint(2, size=KEY_LENGTH)
    print("Secret Key: ", secret_key)

    # Step 1: Alice's Prepares encoding basis and choose a random bitstring
    alice_bitstring, alice_bases = b.select_encoding(KEY_LENGTH)
    print("alice_bitstring: ", alice_bitstring[:10])
    print("alice_bases: ", alice_bases[:10])

    # Step 2: Bob selects bases for measurement
    bob_bases = b.select_measurement(KEY_LENGTH)
    print("Bob_bases: ", bob_bases[:10])

    # Step 3: Alice encodes the qubits
    encoded_qubits = b.encode(host_alice, alice_bitstring, alice_bases)
    print('Encoded bits: ', encoded_qubits)

    # Step 4: Alice sends the qubits
    q_ids = b.send(host_alice, host_bob, encoded_qubits)
    print(q_ids)

    # Step 5: Bob measures the received qubits
    Bob_received_message = b.receive(host_bob, host_alice, q_ids)
    print('Bob received qubits', Bob_received_message)
    bob_bits, bob_bitstring = b.measurement(bob_bases, Bob_received_message)
    print('Bob measured ', bob_bitstring)

    # Step 6: Bob uses Alice's announced bases to see where they agreed with
    # his decoding bases
    seq_no = b.send_classically(host_alice, alice_bases, KEY_LENGTH, host_bob)
    print(seq_no)
    bases_bob_received_arr, bases_bob_received_string = b.receive_classically(
        host_bob, host_alice, seq_no)
    print('Bob Received Bases from Alice', bases_bob_received_string)
    Bob_agreeing_bases = b.compare_bases(bases_bob_received_string, bob_bases)
    print(Bob_agreeing_bases)

    # Bob announces where they agreed on encoding and
    # decoding bases classically.
    seq_no = b.send_classically(host_bob, bob_bases, KEY_LENGTH, host_alice)
    print(seq_no)
    Alice_received_bases, Alice_received_bases_string = b.receive_classically(
        host_alice, host_bob, seq_no)
    print('Alice Received Bases from Bob', Alice_received_bases_string)
    Alice_agreeing_bases = b.compare_bases(alice_bases,
                                           Alice_received_bases_string)
    print(Alice_agreeing_bases)

    # Step 7: Alice and Bob construct their keys
    alice_key = b.construct_key_from_indices(
        alice_bitstring, Alice_agreeing_bases)  # Alice Side
    bob_key = b.construct_key_from_indices(bob_bitstring, Bob_agreeing_bases)

    # Preview the first 10 elements of each Key:
    print("alice_key: ", alice_key[:10])
    print("bob_key: ", bob_key[:10])
    print("Alice's key is equal to Bob's key: ", alice_key == bob_key)

    if (alice_key == bob_key):
        message = "QKD is cool!"
        print("Original Messge:", message)
        encrypted_message = b.encrypt_message(message, alice_key)
        print("Encrypted message:", encrypted_message)
        decrypted_message = b.decrypt_message(encrypted_message, bob_key)
        print("Decrypted message:", decrypted_message)