def receive_a_qmail(port, srcAddr, srcPort, batch_size=4, adversary=False): # Initialize with Bob classicC = SocketChannel(port + 10, True) # connect to Bob classicC.connect(srcAddr, srcPort + 10) # receive otpkey from alice otpkey = classicC.receive() otpkey = pickle.loads(otpkey) print("I am Bob I received: ", otpkey) classicC.close() time.sleep(1) key_per_batch = [{ 'x': x, 'z': z } for x, z in zip(wrap(otpkey['x'], batch_size), wrap(otpkey['z'], batch_size))] # TODO: setup quantum channel n_master = batch_size n_slave = batch_size slave_offset = 0 channel = Channel(slave_offset, port, remote_port=srcPort) qcirc = None # TODO: decrypt and measure # Eve siVmulation recv = "Eve" if adversary else "Bob" bob_meas_results = [] for k in key_per_batch: circ_bob = QuantumCircuit(batch_size, batch_size) circ_bob, offset = channel.receive(circ_bob) # circ_bob.draw(output='mpl',filename="teleport_alice%s.png".format(k)) #Bob receives qubits, and decrypt them if not adversary: otp_enc_dec(circ_bob, k) #Bob measure the states, single shot simulator = Aer.get_backend('qasm_simulator') nqubit = len(otpkey['x']) # for i in range(nqubit): circ_bob.measure(np.arange(batch_size) + offset, range(batch_size)) counts = execute(circ_bob, backend=simulator, shots=1).result() output = list(counts.get_counts().keys())[0] bob_meas_results.append(output) print('%s measures' % recv, bob_meas_results[-1]) print('%ss message %s' % (recv, bins_to_str(bob_meas_results))) return bins_to_str(bob_meas_results)
def send_a_qmail(message, port, destAddr, destPort, batch_size=4): """ Alice sends to Bob a quantum email :nqubit:int, the number of qubits :message:str, the secret message that wants to be sent """ nqubit = batch_size print('Alice wants to send %s' % message) # Initialize with Bob classicC = SocketChannel(port + 10, False) # connect to Bob classicC.connect(destAddr, destPort + 10) #send message per batch bits Lbins = str_to_lbin(message, batch_size) #generate key print('generating key...') otpkey = generate_otp_key(len(Lbins) * batch_size) print('X-encryption key %s' % otpkey['x'], 'Z-encryption key %s' % otpkey['z']) # send key to Bob classicC.send(pickle.dumps(otpkey)) print("I am Alice I sent:", otpkey) # close the classic channel as we don't need it anymore classicC.close() time.sleep(2) key_per_batch = [{ 'x': x, 'z': z } for x, z in zip(wrap(otpkey['x'], batch_size), wrap(otpkey['z'], batch_size))] # TODO: setup quantum channel n_master = batch_size n_slave = batch_size slave_offset = 0 channel = Channel(slave_offset, port, remote_port=destPort) # bob_meas_results = [] # Bob for bin_batch, k in zip(Lbins, key_per_batch): print('Performing QOTP for string', bin_batch) qcirc = encode_cinfo_to_qstate(bin_batch) # Alice qotp_send(qcirc, k, channel) # print('Bob measures',bob_meas_results[-1]) # Bob print("Transmission complete.")