def __init__(self,slave_offset=0, myport=000, remote_port=000): self._state_vector = None self._arr_qubits = None self._basis_gates = ['u1', 'u2', 'u3', 'cx','x','y','H','z'] self._master = True self._offset = 0 self._slave_offset = slave_offset self.realchannel = SocketChannel(myport, False) TCP_IP = '127.0.0.1' self.realchannel.connect(TCP_IP, remote_port) self._circuit = None
def start_server(): num_of_clients = 2 tx_port = [1220, 1221] rx_port = [1330, 1331] clients = [] for client in range(num_of_clients): client.append(SocketChannel(tx_port[client], True)) client.connect('localhost', rx_port[client]) client.send("Hello client %d".format(client)) print("client %d sent: %s".format(client, client.recive()))
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.")
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)
class Channel: def __init__(self,slave_offset=0, myport=000, remote_port=000): self._state_vector = None self._arr_qubits = None self._basis_gates = ['u1', 'u2', 'u3', 'cx','x','y','H','z'] self._master = True self._offset = 0 self._slave_offset = slave_offset self.realchannel = SocketChannel(myport, False) TCP_IP = '127.0.0.1' self.realchannel.connect(TCP_IP, remote_port) self._circuit = None def close(self): try: self.realchannel.kill() except: print("Exception: Thread still busy") def send(self,circuit,arr_qubits): self._state_vector = Statevector.from_instruction(circuit) self._arr_qubits = arr_qubits self._circuit = circuit #From Marc ser = parser.QSerializer() ser.add_element('state_vector', self._state_vector)#self) ser.add_element('is_master', self._master)#self) ser.add_element('slave_offset', self._slave_offset)#self) ser.add_element('is_master', self._master)#self) ser.add_element('circuit', self._circuit)#self) str_to_send = ser.encode() #print(str_to_send.type()) #From Luca message = str_to_send channel = self.realchannel #SocketChannel() channel.send(message) channel.close() ## TODO: TCP THINGS return self def receive(self,circuit):#,recieve_channel): ## TODO: remove recieve as an input #TODO: TCP things #recieve_channel = TCP_STUFF #From Luca print('Wait to receive') channel = self.realchannel #SocketChannel(port=5005, listen=True) data = channel.receive() # print("received stuff \o/") #print("received data:", data) channel.close() #From Marc ser2 = parser.QSerializer() ser2.decode(data) #recieve_channel = ser2.get_element('channel_class') self._slave_offset = ser2.get_element('slave_offset') if(ser2.get_element('is_master')): self._master = False self._offset = self._slave_offset recieved_state_vector = ser2.get_element('state_vector') new_circuit = QuantumCircuit(len(recieved_state_vector.dims())) new_circuit.initialize(recieved_state_vector.data, range(len(recieved_state_vector.dims()))) new_circuit = transpile(new_circuit, basis_gates=self._basis_gates) new_circuit = new_circuit + circuit return ser2.get_element('circuit'), self._offset return new_circuit, self._offset
def main(): channel = SocketChannel(1330, False) channel.connect('localhost', 1220) channel.send("Hello server") print("Recieved msg: %s".format{channel.recieve()})