def main(): input_data = sys.argv[1:] # Set node numbers min_tel = int(input_data[0]) max_tel = int(input_data[1]) number = int(input_data[2]) # Initialize the connection Bob = CQCConnection("Bob") for n in range(min_tel, max_tel + 1): for _ in range(number): # Start teleporting back and fourth for _ in range(n): # Make an EPR pair with other node q = Bob.recvEPR() # Receive info about corrections data = Bob.recvClassical(timout=3600) Bob.closeClassicalServer() message = list(data) a = message[0] b = message[1] # Apply corrections if b == 1: q.X() if a == 1: q.Z() # Make an EPR pair with next node qEPR = Bob.recvEPR() # Apply the local teleportation operations q.cnot(qEPR) q.H() # Measure the qubits a = q.measure() b = qEPR.measure() to_print = "App {}: Measurement outcomes are: a={}, b={}".format( Bob.name, a, b) print("|" + "-" * (len(to_print) + 2) + "|") print("| " + to_print + " |") print("|" + "-" * (len(to_print) + 2) + "|") # Send corrections to other node Bob.sendClassical("Alice", [a, b]) Bob.closeClassicalChannel("Alice") # Stop the connection Bob.close()
def apply_on_target(self, cqc: CQCConnection, control_cqc_name: str, target: qubit): # Receive qubit q = cqc.recvEPR() remote_result = binStrToInt(cqc.recvClassical()) print("Bob received message: ", remote_result) if remote_result == 1: q.X() q.cnot(target) q.H() local_result = q.measure(inplace=True) q.reset() print("Bob sent message: ", local_result) cqc.sendClassical(control_cqc_name, msg=local_result)
def apply_on_control(self, cqc: CQCConnection, target_cqc_name: str, control: qubit): # Create an EPR pair q = cqc.createEPR(target_cqc_name) control.cnot(q) local_result = q.measure(inplace=True) q.reset() cqc.sendClassical(name=target_cqc_name, msg=local_result) print("\n ") print("Alice sent message: ", local_result) remote_result = binStrToInt(cqc.recvClassical()) print("Alice received message: ", remote_result) if remote_result == 1: control.Z()
class Node: def __init__(self, name): self.conn = CQCConnection(name) self.name = name self.N = 0 self.bases = [] self.raw_key = [] self.sifted_key = [] self.remote_bases = [] def __del__(self): self.conn.__exit__(None, None, None) def set_size(self, N): self.N = N def send_classical_integer(self, to, integer): self.conn.sendClassical( to, integer.to_bytes((integer.bit_length() + 7) // 8, byteorder="big")) def recv_classical_integer(self): return int.from_bytes(self.conn.recvClassical(), byteorder="big") def send_bases(self, to): msg = b'' for i, base in enumerate(self.bases): msg += base.to_bytes(1, byteorder="big") self.conn.sendClassical(to, msg) def recv_bases(self): res = [] msg = self.conn.recvClassical() for i in range(self.N): res.append(msg[i]) return res def compare_bases(self): for i in range(self.N): if self.bases[i] == self.remote_bases[i]: self.sifted_key.append(self.raw_key[i]) def encode_qubit(self, bit, basis): q = qubit(self.conn) if bit == 1: q.X() if basis == 1: q.H() return q def send_qubits(self, to): for i in range(self.N): basis = random.randint(0, 1) bit = random.randint(0, 1) self.bases.append(basis) self.raw_key.append(bit) q = self.encode_qubit(bit, basis) self.conn.sendQubit(q, to) def recv_qubits(self): for i in range(self.N): basis = random.randint(0, 1) self.bases.append(basis) q = self.conn.recvQubit() if basis == 1: q.H() self.raw_key.append(q.measure()) def __str__(self): return TEMPLATE.format(self.name, self.bases, self.raw_key, self.sifted_key) def main(self): pass
def main(): input_data = sys.argv[1:] # Set node numbers node_nr = int(input_data[0]) tot_nr = int(input_data[1]) next_node_nr = (node_nr + 1) % tot_nr # Initialize the connection node = CQCConnection("n" + str(node_nr)) # Create EPR pairs with previous and next node if node_nr == 0: # start timer t1 = timer() qNext = node.createEPR("n" + str(next_node_nr)) qPrev = node.recvEPR() else: qPrev = node.recvEPR() qNext = node.createEPR("n" + str(next_node_nr)) if node_nr == 0: # this is the first node so create qubit # Create a qubit to teleport q = qubit(node) # Prepare the qubit to teleport in |+> q.H() # ------ # Qubit is created, send it to next node # ------ else: # we are node in chain so receive classical corrections # Receive info about corrections data = node.recvClassical() message = list(data) a = message[0] b = message[1] # Apply corrections if b == 1: qPrev.X() if a == 1: qPrev.Z() # ------ # Qubit is receive, send it to next node # ------ # Apply the local teleportation operations qPrev.cnot(qNext) qPrev.H() # Measure the qubits a = qPrev.measure() b = qNext.measure() to_print = "App {}: Measurement outcomes are: a={}, b={}".format( node.name, a, b) print("|" + "-" * (len(to_print) + 2) + "|") print("| " + to_print + " |") print("|" + "-" * (len(to_print) + 2) + "|") # Send corrections to next node node.sendClassical("n" + str(next_node_nr), [a, b]) if node_nr == 0: # this is first node, so receive again after qubit traversed chain # Receive info about corrections data = node.recvClassical() message = list(data) a = message[0] b = message[1] # Apply corrections if b == 1: qPrev.X() if a == 1: qPrev.Z() # ------ # Qubit is receive, so measure it # ------ # measure the qubit, print the outcome and record the time it took m = q.measure() t2 = timer() to_print = "App {}: Measurement outcome is: m={}".format(node.name, m) print("|" + "-" * (len(to_print) + 2) + "|") print("| " + to_print + " |") print("|" + "-" * (len(to_print) + 2) + "|") to_print = "App {}: Time elapsed: t={}".format(node.name, t2 - t1) print("|" + "-" * (len(to_print) + 2) + "|") print("| " + to_print + " |") print("|" + "-" * (len(to_print) + 2) + "|") with open("times_v2.txt", "a") as f: f.write("{}, {}\n".format(tot_nr, t2 - t1)) # Stop the connection node.close()
def main(): input_data = sys.argv[1:] # Set node numbers min_tel = int(input_data[0]) max_tel = int(input_data[1]) number = int(input_data[2]) # Clear file with open("times{}_{}_{}.txt".format(min_tel, max_tel, number), "w") as f: pass # Initialize the connection Alice = CQCConnection("Alice") times = [] for n in range(min_tel, max_tel + 1): print("========") print(n) print("========") for _ in range(number): # start timer t1 = timer() # Create a qubit to teleport q = qubit(Alice) # Prepare the qubit to teleport in |+> q.H() # Start teleporting back and fourth for _ in range(n): # Make an EPR pair with next node qEPR = Alice.createEPR("Bob") # Apply the local teleportation operations q.cnot(qEPR) q.H() # Measure the qubits a = q.measure() b = qEPR.measure() to_print = "App {}: Measurement outcomes are: a={}, b={}".format( Alice.name, a, b) print("|" + "-" * (len(to_print) + 2) + "|") print("| " + to_print + " |") print("|" + "-" * (len(to_print) + 2) + "|") # Send corrections to other node Alice.sendClassical("Bob", [a, b]) Alice.closeClassicalChannel("Bob") # Make an EPR pair with other node q = Alice.createEPR("Bob") # Receive info about corrections data = Alice.recvClassical(timout=3600) Alice.closeClassicalServer() message = list(data) a = message[0] b = message[1] # Apply corrections if b == 1: q.X() if a == 1: q.Z() # measure the qubit, print the outcome and record the time it took m = q.measure() t2 = timer() to_print = "App {}: Measurement outcome is: m={}".format( Alice.name, m) print("|" + "-" * (len(to_print) + 2) + "|") print("| " + to_print + " |") print("|" + "-" * (len(to_print) + 2) + "|") to_print = "App {}: Time elapsed: t={}".format(Alice.name, t2 - t1) print("|" + "-" * (len(to_print) + 2) + "|") print("| " + to_print + " |") print("|" + "-" * (len(to_print) + 2) + "|") times.append((n, t2 - t1)) with open("times{}_{}_{}.txt".format(min_tel, max_tel, number), "a") as f: for (n, t) in times: f.write("{}, {}\n".format(n, t)) # Stop the connection Alice.close()