<jupyter_start><jupyter_text>Algorithm BB84 for key exchange.  First let's define some imports.<jupyter_code>%matplotlib inline
# Importing standard Qiskit libraries
from qiskit import QuantumCircuit, execute, Aer, IBMQ
from qiskit.compiler import transpile, assemble
from qiskit.tools.jupyter import *
from random import randint
import hashlib<jupyter_output><empty_output><jupyter_text>In the next cell Alice choses a random binary string from which the Shared key will be form later. Also she selects random basis and encripts the string accordingly to the selected basis.<jupyter_code>#Alice cell, Bob can't see what's going in on here
m0 = ''.join([str(randint(0,1)) for x in range(256)])
Alice_bases = [randint(0,1) for x in range(256)]
qubits = list()
for i in range(len(Alice_bases)):
    mycircuit = QuantumCircuit(1,1)
    if(Alice_bases[i] == 0):
        if(m0[i] == "1"):
            mycircuit.x(0)
    else:
        if(m0[i] == "0"):
            mycircuit.h(0)
        else:
            mycircuit.x(0)
            mycircuit.h(0)
    qubits.append(mycircuit)<jupyter_output><empty_output><jupyter_text>In the next cell Bob measures the qubits that Alice sent. Then He receives the basis of Alice. Finally He send to Alice the index of the basis that they meassure correctly.<jupyter_code>#Bob cell, Alice can't see what's going in on here
Bob_bases = [randint(0,1) for x in range(256)]
backend = Aer.get_backend('qasm_simulator')
measurements = list()
for i in range(len(Bob_bases)):
    qubit = qubits[i]
    if(Bob_bases[i] == 0):
        qubit.measure(0,0)
    else:
        qubit.h(0)
        qubit.measure(0,0)
    result = execute(qubit, backend, shots=1, memory=True).result()
    measurements.append(int(result.get_memory()[0]))
#Bob cell, Alice can't see what's going in on here
I0 = list()
for i in range(len(Alice_bases)):
    if(Alice_bases[i] == Bob_bases[i]):
        I0.append(i)<jupyter_output><empty_output><jupyter_text>In the final step, both of them take from their strings the bits which they share.<jupyter_code>#Test cell to see that both of them have the same key
keyAlice = ''.join([m0[x] for x in I0])
keyBob = ''.join([str(measurements[x]) for x in I0])
print(keyAlice)
print(keyBob)
print(keyAlice == keyBob)<jupyter_output>01001100001001000000011100011010111101000111110101101011111111011000101100001101010111011101110001110011001110111000010000000010111000
01001100001001000000011100011010111101000111110101101011111111011000101100001101010111011101110001110011001110111000010000000010111000
True
{'fraction_non_alphanumeric': 0.10126083530338849, 'fraction_numerical': 0.12214342001576044, 'mean_word_length': 4.8637413394919164, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 6, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '451448', 'license': 'Apache-2.0', 'n_tokens_mistral': 1091, 'n_tokens_neox': 862, 'n_words': 241, 'path': 'BB84.ipynb', 'repo_name': 'mentesniker/Quantum-Cryptography', 'size': '2538'}