def create_EPR(self, host_a_id, host_b_id, q_id=None, block=False): """ Creates an EPR pair for two qubits and returns one of the qubits. Args: host_a_id (String): ID of the first host who gets the EPR state. host_b_id (String): ID of the second host who gets the EPR state. q_id (String): Optional id which both qubits should have. block (bool): Determines if the created pair should be blocked or not. Returns: Returns a qubit. The qubit belongs to host a. To get the second qubit of host b, the receive_epr function has to be called. """ q1 = self.create_qubit(host_a_id) q2 = self.create_qubit(host_b_id) projectq.ops.H | q1 projectq.ops.CNOT | (q1, q2) host_a = self._hosts.get_from_dict(host_a_id) host_b = self._hosts.get_from_dict(host_b_id) qubit_b = Qubit(host_b, qubit=q2, q_id=q_id, blocked=block) qubit = Qubit(host_a, qubit=q1, q_id=q_id, blocked=block) self.store_ent_pair(host_a.host_id, host_b.host_id, qubit_b) return qubit
def main(): network = Network.get_instance() nodes = ["Alice", "Bob", "Eve", "Dean"] backend = CQCBackend() network.start(nodes, backend) network.delay = 0.7 hosts = {'alice': Host('Alice', backend), 'bob': Host('Bob', backend)} network.delay = 0 # A <-> B hosts['alice'].add_connection('Bob') hosts['bob'].add_connection('Alice') hosts['alice'].start() hosts['bob'].start() for h in hosts.values(): network.add_host(h) q1 = Qubit(hosts['alice']) hosts['alice'].send_qubit('Bob', q1, await_ack=True) q1 = Qubit(hosts['alice']) hosts['alice'].send_qubit('Bob', q1, await_ack=True) q1 = Qubit(hosts['alice']) hosts['alice'].send_qubit('Bob', q1, await_ack=True) q1 = Qubit(hosts['bob']) hosts['bob'].send_qubit('Alice', q1, await_ack=True) network.stop(True) exit()
def create_EPR(self, host_a_id, host_b_id, q_id=None, block=False): """ Creates an EPR pair for two qubits and returns one of the qubits. Args: host_a_id (String): ID of the first host who gets the EPR state. host_b_id (String): ID of the second host who gets the EPR state. q_id (String): Optional id which both qubits should have. block (bool): Determines if the created pair should be blocked or not. Returns: Returns a qubit. The qubit belongs to host a. To get the second qubit of host b, the receive_epr function has to be called. """ uid1 = uuid.uuid4() uid2 = uuid.uuid4() host_a = self._hosts.get_from_dict(host_a_id) host_b = self._hosts.get_from_dict(host_b_id) self.eqsn.new_qubit(uid1) self.eqsn.new_qubit(uid2) self.eqsn.H_gate(uid1) self.eqsn.cnot_gate(uid2, uid1) q1 = Qubit(host_a, qubit=uid1, q_id=q_id, blocked=block) q2 = Qubit(host_b, qubit=uid2, q_id=q1.id, blocked=block) self.store_ent_pair(host_a.host_id, host_b.host_id, q2) return q1
def qubit_send_w_retransmission(host, q_size, receiver_id, checksum_size_per_qubit): """ Sends the data qubits along with checksum qubits , with the possibility of retransmission. :param host: Sender of qubits :param q_size: Number of qubits to be sent :param receiver_id: ID of the receiver :param checksum_size_per_qubit: Checksum qubit per data qubit size :return: """ bit_arr = np.random.randint(2, size=q_size) print('Bit array to be sent: ' + str(bit_arr)) qubits = [] for i in range(q_size): q_tmp = Qubit(host) if bit_arr[i] == 1: q_tmp.X() qubits.append(q_tmp) check_qubits = host.add_checksum(qubits, checksum_size_per_qubit) checksum_size = int(q_size / checksum_size_per_qubit) qubits.append(check_qubits) checksum_cnt = 0 for i in range(q_size + checksum_size): if i < q_size: q = qubits[i] else: q = qubits[q_size][checksum_cnt] checksum_cnt = checksum_cnt + 1 q_success = False got_ack = False number_of_retransmissions = 0 while not got_ack and number_of_retransmissions < MAX_NUM_OF_TRANSMISSIONS: print('Alice prepares qubit') err_1 = Qubit(host) # encode logical qubit q.cnot(err_1) _, ack_received = host.send_qubit(receiver_id, q, await_ack=True) if ack_received: err_1.release() got_ack = True q_success = True if not q_success: print('Alice: Bob did not receive the qubit') # re-introduce a qubit to the system and correct the error q = Qubit(host) err_1.cnot(q) number_of_retransmissions += 1 if number_of_retransmissions == 10: print("Alice: too many attempts made") return False return True
def main(): backend = CQCBackend() network = Network.get_instance() nodes = ["Alice", "Bob", "Eve", "Dean"] network.start(nodes, backend) network.delay = 0.0 hosts = {'alice': Host('Alice', backend), 'bob': Host('Bob', backend)} # A <-> B hosts['alice'].add_connection('Bob') hosts['bob'].add_connection('Alice') hosts['alice'].start() hosts['bob'].start() for h in hosts.values(): network.add_host(h) q = Qubit(hosts['alice']) q.X() hosts['alice'].send_teleport(hosts['bob'].host_id, q) q2 = hosts['bob'].get_data_qubit(hosts['alice'].host_id) i = 0 while q2 is None and i < 5: q2 = hosts['bob'].get_data_qubit(hosts['alice'].host_id) i += 1 time.sleep(1) assert q2 is not None assert q2.measure() == 1 print("All tests succesfull!") network.stop(True) exit()
def create_EPR(self, host_a_id, host_b_id, q_id=None, block=False): """ Creates an EPR pair for two qubits and returns one of the qubits. Args: host_a_id (String): ID of the first host who gets the EPR state. host_b_id (String): ID of the second host who gets the EPR state. q_id (String): Optional id which both qubits should have. block (bool): Determines if the created pair should be blocked or not. Returns: Returns a qubit. The qubit belongs to host a. To get the second qubit of host b, the receive_epr function has to be called. """ cqc_host_a = self._cqc_connections.get_from_dict(host_a_id) cqc_host_b = self._cqc_connections.get_from_dict(host_b_id) host_a = self._hosts.get_from_dict(host_a_id) q = cqc_host_a.createEPR(cqc_host_b.name) qubit = Qubit(host_a, qubit=q, q_id=q_id, blocked=block) # add the ID to a list, so the next returned qubit from recv EPR # gets assigned the right id key = cqc_host_a.name + ':' + cqc_host_b.name list = self._entaglement_ids.get_from_dict(key) if list is not None: list.append(qubit.id) else: list = [qubit.id] self._entaglement_ids.add_to_dict(key, list) return qubit
def main(): network = Network.get_instance() nodes = ['Alice', 'Bob', 'Eve'] network.delay = 0.2 network.start(nodes) host_alice = Host('Alice') host_bob = Host('Bob') host_eve = Host('Eve') host_alice.add_connection('Bob') host_bob.add_connection('Alice') host_bob.add_connection('Eve') host_eve.add_connection('Bob') host_alice.start() host_bob.start() host_eve.start() network.add_host(host_alice) network.add_host(host_bob) network.add_host(host_eve) q = Qubit(host_alice) q.X() host_alice.send_teleport('Eve', q, await_ack=True) q_eve = host_eve.get_data_qubit(host_alice.host_id, q.id, wait=5) assert q_eve is not None print('Eve measures: %d' % q_eve.measure())
def test_teleport_superdense_combination(self): global hosts hosts['alice'].send_superdense(hosts['bob'].host_id, '11') messages = hosts['bob'].classical i = 0 while i < TestOneHop.MAX_WAIT and len(messages) == 0: messages = hosts['bob'].classical i += 1 time.sleep(1) q = Qubit(hosts['alice']) q.X() hosts['alice'].send_teleport(hosts['bob'].host_id, q) q2 = hosts['bob'].get_data_qubit(hosts['alice'].host_id) i = 0 while q2 is None and i < TestOneHop.MAX_WAIT: q2 = hosts['bob'].get_data_qubit(hosts['alice'].host_id) i += 1 time.sleep(1) self.assertIsNotNone(messages) self.assertTrue(len(messages) > 0) self.assertEqual(messages[0].sender, hosts['alice'].host_id) self.assertEqual(messages[0].content, '11') self.assertIsNotNone(q2) self.assertEqual(q2.measure(), 1)
def test_send_qubit_bob_to_alice(self): hosts = {'alice': Host('Alice'), 'bob': Host('Bob')} self.hosts = hosts # A <-> B hosts['bob'].add_connection('00000000') hosts['alice'].start() hosts['bob'].start() for h in hosts.values(): self.network.add_host(h) q = Qubit(hosts['bob']) q.X() q_id = hosts['bob'].send_qubit(hosts['alice'].host_id, q) i = 0 rec_q = hosts['alice'].get_data_qubit(hosts['bob'].host_id, q_id) while i < TestOneHop.MAX_WAIT and rec_q is None: rec_q = hosts['alice'].get_data_qubit(hosts['bob'].host_id, q_id) i += 1 time.sleep(1) self.assertIsNotNone(rec_q) self.assertEqual(rec_q.measure(), 1)
def test_teleport(self): hosts = {'alice': Host('Alice'), 'bob': Host('Bob')} self.hosts = hosts # A <-> B hosts['alice'].add_connection('Bob') hosts['bob'].add_connection('Alice') hosts['alice'].start() hosts['bob'].start() for h in hosts.values(): self.network.add_host(h) q = Qubit(hosts['alice']) q.X() hosts['alice'].send_teleport(hosts['bob'].host_id, q) q2 = hosts['bob'].get_data_qubit(hosts['alice'].host_id) i = 0 while q2 is None and i < TestOneHop.MAX_WAIT: q2 = hosts['bob'].get_data_qubit(hosts['alice'].host_id) i += 1 time.sleep(1) self.assertIsNotNone(q2) self.assertEqual(q2.measure(), 1)
def receive_epr(self, host_id, sender_id, q_id=None, block=False): """ Called after create EPR in the receiver, to receive the other EPR pair. Args: host_id (String): ID of the first host who gets the EPR state. sender_id (String): ID of the sender of the EPR pair. q_id (String): Optional id which both qubits should have. block (bool): Determines if the created pair should be blocked or not. Returns: Returns an EPR qubit with the other Host. """ cqc_host = self._cqc_connections.get_from_dict(host_id) host = self._hosts.get_from_dict(host_id) q = cqc_host.recvEPR() key = sender_id + ':' + cqc_host.name ent_list = self._entaglement_ids.get_from_dict(key) if ent_list is None: raise Exception("Internal Error!") id = None id = ent_list.pop(0) if q_id is not None and q_id != id: raise ValueError("q_id doesn't match id!") self._entaglement_ids.add_to_dict(key, ent_list) return Qubit(host, qubit=q, q_id=id, blocked=block)
def alice(host): for _ in range(amount_transmit): s = 'Hi Eve.' print("Alice sends: %s" % s) host.send_classical('Eve', s, await_ack=True) for _ in range(amount_transmit): print("Alice sends qubit in the |1> state") q = Qubit(host) q.X() host.send_qubit('Eve', q, await_ack=False)
def _establish_epr(self, sender, receiver, q_id, o_seq_num, blocked): """ Instead doing an entanglement swap, for efficiency we establish EPR pairs directly for simulation, if an entanglement swap would have been possible. Args: sender (Host): Sender of the EPR pair receiver (Host): Receiver of the EPR pair q_id (str): Qubit ID of the sent EPR pair o_seq_num (int): The original sequence number blocked (bool): If the pair being distributed is blocked or not """ host_sender = self.get_host(sender) host_receiver = self.get_host(receiver) q1 = Qubit(host_sender) q2 = Qubit(host_sender) q1.H() q1.cnot(q2) host_sender.add_epr(receiver, q1, q_id, blocked) host_receiver.add_epr(sender, q2, q_id, blocked) host_receiver.send_ack(sender, o_seq_num)
def test_teleport(self): q = Qubit(hosts['alice']) q.X() hosts['alice'].send_teleport(hosts['eve'].host_id, q) q2 = None i = 0 while i < TestTwoHop.MAX_WAIT and q2 is None: q2 = hosts['eve'].get_data_qubit(hosts['alice'].host_id) i += 1 time.sleep(1) self.assertIsNotNone(q2) self.assertEqual(q2.measure(), 1)
def preparation_and_distribution(): for serial in range(NO_OF_SERIALS): for bit_no in range(QUBITS_PER_MONEY): random_bit = randint(0, 1) random_base = randint(0, 1) bank_bits[serial].append(random_bit) bank_basis[serial].append(random_base) q = Qubit(host) if random_bit == 1: q.X() if random_base == 1: q.H() host.send_qubit(customer, q, await_ack=False)
def main(): backend = CQCBackend() network = Network.get_instance() nodes = ["Alice", "Bob", "Eve", "Dean"] network.start(nodes, backend) network.delay = 0.7 hosts = {'alice': Host('Alice', backend), 'bob': Host('Bob', backend)} # A <-> B hosts['alice'].add_connection('Bob') hosts['bob'].add_connection('Alice') hosts['alice'].start() hosts['bob'].start() for h in hosts.values(): network.add_host(h) hosts['alice'].send_superdense(hosts['bob'].host_id, '11') messages = hosts['bob'].classical i = 0 while i < 5 and len(messages) == 0: messages = hosts['bob'].classical i += 1 time.sleep(1) q = Qubit(hosts['alice']) q.X() hosts['alice'].send_teleport(hosts['bob'].host_id, q) q2 = hosts['bob'].get_data_qubit(hosts['alice'].host_id) i = 0 while q2 is None and i < 5: q2 = hosts['bob'].get_data_qubit(hosts['alice'].host_id) i += 1 time.sleep(1) assert messages is not None assert len(messages) > 0 assert (messages[0].sender == hosts['alice'].host_id) assert (messages[0].content == '11') assert q2 is not None assert (q2.measure() == 1) print("All tests succesfull!") network.stop(True) exit()
def main(): network = Network.get_instance() nodes = ["Alice", "Bob", "Eve", "Dean"] network.start(nodes) network.delay = 0.1 host_alice = Host('Alice') host_alice.add_connection('Bob') host_alice.start() host_bob = Host('Bob') host_bob.add_connection('Alice') host_bob.add_connection('Eve') host_bob.start() host_eve = Host('Eve') host_eve.add_connection('Bob') host_eve.add_connection('Dean') host_eve.start() host_dean = Host('Dean') host_dean.add_connection('Eve') host_dean.start() network.add_host(host_alice) network.add_host(host_bob) network.add_host(host_eve) network.add_host(host_dean) # Create a qubit owned by Alice q = Qubit(host_alice) # Put the qubit in the excited state q.X() # Send the qubit and await an ACK from Dean q_id, _ = host_alice.send_qubit('Dean', q, await_ack=True) # Get the qubit on Dean's side from Alice q_rec = host_dean.get_data_qubit('Alice', q_id) # Ensure the qubit arrived and then measure and print the results. if q_rec is not None: m = q_rec.measure() print("Results of the measurements for q_id are ", str(m)) else: print('q_rec is none') network.stop(True) exit()
def test_send_qubit_alice_to_bob(self): global hosts q = Qubit(hosts['alice']) q.X() q_id = hosts['alice'].send_qubit(hosts['bob'].host_id, q) i = 0 rec_q = hosts['bob'].get_data_qubit(hosts['alice'].host_id, q_id) while i < TestOneHop.MAX_WAIT and rec_q is None: rec_q = hosts['bob'].get_data_qubit(hosts['alice'].host_id, q_id) i += 1 time.sleep(1) self.assertIsNotNone(rec_q) self.assertEqual(rec_q.measure(), 1)
def test_teleport(self): global hosts q = Qubit(hosts['alice']) q.X() hosts['alice'].send_teleport(hosts['bob'].host_id, q) q2 = hosts['bob'].get_data_qubit(hosts['alice'].host_id) i = 0 while q2 is None and i < TestOneHop.MAX_WAIT: q2 = hosts['bob'].get_data_qubit(hosts['alice'].host_id) i += 1 time.sleep(1) self.assertIsNotNone(q2) self.assertEqual(q2.measure(), 1)
def _send_key(packet): receiver = network.get_host(packet.receiver) sender = network.get_host(packet.sender) key_size = packet.payload['keysize'] packet.protocol = REC_KEY network.send(packet) secret_key = np.random.randint(2, size=key_size) msg_buff = [] sender.qkd_keys[receiver.host_id] = secret_key.tolist() sequence_nr = 0 # iterate over all bits in the secret key. for bit in secret_key: ack = False while not ack: # get a random base. 0 for Z base and 1 for X base. base = random.randint(0, 1) # create qubit q_bit = Qubit(sender) # Set qubit to the bit from the secret key. if bit == 1: q_bit.X() # Apply basis change to the bit if necessary. if base == 1: q_bit.H() # Send Qubit to Receiver sender.send_qubit(receiver.host_id, q_bit, await_ack=True) # Get measured basis of Receiver message = sender.get_next_classical_message( receiver.host_id, msg_buff, sequence_nr) # Compare to send basis, if same, answer with 0 and set ack True and go to next bit, # otherwise, send 1 and repeat. if message == ("%d:%d") % (sequence_nr, base): ack = True sender.send_classical(receiver.host_id, ("%d:0" % sequence_nr), await_ack=True) else: ack = False sender.send_classical(receiver.host_id, ("%d:1" % sequence_nr), await_ack=True) sequence_nr += 1
def test_teleport_superdense_combination(self): hosts = {'alice': Host('Alice'), 'bob': Host('Bob')} self.hosts = hosts # A <-> B hosts['alice'].add_connection('Bob') hosts['bob'].add_connection('Alice') hosts['alice'].start() hosts['bob'].start() for h in hosts.values(): self.network.add_host(h) hosts['alice'].send_superdense(hosts['bob'].host_id, '11') messages = hosts['bob'].classical i = 0 while i < TestOneHop.MAX_WAIT and len(messages) == 0: messages = hosts['bob'].classical i += 1 time.sleep(1) q = Qubit(hosts['alice']) q.X() hosts['alice'].send_teleport(hosts['bob'].host_id, q) q2 = hosts['bob'].get_data_qubit(hosts['alice'].host_id) i = 0 while q2 is None and i < TestOneHop.MAX_WAIT: q2 = hosts['bob'].get_data_qubit(hosts['alice'].host_id) i += 1 time.sleep(1) self.assertIsNotNone(messages) self.assertTrue(len(messages) > 0) self.assertEqual(messages[0]['sender'], hosts['alice'].host_id) self.assertEqual(messages[0]['message'], '11') self.assertIsNotNone(q2) self.assertEqual(q2.measure(), 1)
def main(): network = Network.get_instance() nodes = ["Alice", "Bob", "Eve", "Dean"] network.start(nodes) hosts = {'alice': Host('Alice'), 'bob': Host('Bob'), 'eve': Host('Eve')} network.delay = 0 # A <-> B hosts['alice'].add_connection('Bob') hosts['bob'].add_connection('Alice') # B <-> E hosts['bob'].add_connection('Eve') hosts['eve'].add_connection('Bob') hosts['alice'].start() hosts['bob'].start() hosts['eve'].start() for h in hosts.values(): network.add_host(h) q = Qubit(hosts['alice']) q.X() q_id = hosts['alice'].send_qubit(hosts['eve'].host_id, q) i = 0 q1 = None while i < MAX_WAIT and q1 is None: q1 = hosts['eve'].get_data_qubit(hosts['alice'].host_id, q_id) i += 1 time.sleep(1) assert q1 != None assert q1.measure() == 1 print("All tests succesfull!") network.stop(True) exit()
def sender(host, distributor, r, epr_id): q = host.get_ghz(distributor, wait=10) b = random.choice(['0', '1']) host.send_broadcast(b) if b == '1': q.Z() host.add_epr(r, q, q_id=epr_id) sending_qubit = Qubit(host) sending_qubit.X() print('Sending %s' % sending_qubit.id) # Generate EPR if none shouldn't change anything, but if there is # no shared entanglement between s and r, then there should # be a mistake in the protocol host.send_teleport(r, sending_qubit, generate_epr_if_none=False, await_ack=False) host.empty_classical()
def Alice_qkd(alice, msg_buff): sequence_nr = 0 # iterate over all bits in the secret key. for bit in secret_key: ack = False while not ack: print("Alice sequence nr is %d." % sequence_nr) # get a random base. 0 for Z base and 1 for X base. base = random.randint(0, 1) # create qubit q_bit = Qubit(alice) # Set qubit to the bit from the secret key. if bit == 1: q_bit.X() # Apply basis change to the bit if necessary. if base == 1: q_bit.H() # Send Qubit to Bob alice.send_qubit(hosts['Eve'].host_id, q_bit, await_ack=True) # Get measured basis of Bob message = get_next_classical_message(alice, hosts['Eve'].host_id, msg_buff, sequence_nr) # Compare to send basis, if same, answer with 0 and set ack True and go to next bit, # otherwise, send 1 and repeat. if message == ("%d:%d") % (sequence_nr, base): ack = True alice.send_classical(hosts['Eve'].host_id, ("%d:0" % sequence_nr), await_ack=True) else: ack = False alice.send_classical(hosts['Eve'].host_id, ("%d:1" % sequence_nr), await_ack=True) sequence_nr += 1
def test_epr_teleport_combination(self): q = Qubit(hosts['alice']) q.X() q_id = hosts['alice'].send_epr(hosts['eve'].host_id) hosts['alice'].send_teleport(hosts['eve'].host_id, q) q1_epr = None q2_epr = None q_teleport = None i = 0 while q1_epr is None and i < TestTwoHop.MAX_WAIT: q1_epr = hosts['alice'].get_epr(hosts['eve'].host_id, q_id) if q1_epr is not None: q1_epr = q1_epr i += 1 time.sleep(1) i = 0 while q2_epr is None and i < TestTwoHop.MAX_WAIT: q2_epr = hosts['eve'].get_epr(hosts['alice'].host_id, q_id) if q2_epr is not None: q2_epr = q2_epr i += 1 time.sleep(1) i = 0 while q_teleport is None and i < TestTwoHop.MAX_WAIT: q_teleport = hosts['eve'].get_data_qubit(hosts['alice'].host_id) if q_teleport is not None: q_teleport = q_teleport i += 1 time.sleep(1) self.assertIsNotNone(q1_epr) self.assertIsNotNone(q2_epr) self.assertIsNotNone(q_teleport) self.assertEqual(q1_epr.measure(), q2_epr.measure()) self.assertEqual(q_teleport.measure(), 1)
def checksum_sender(host, q_size, receiver_id, checksum_size_per_qubit): bit_arr = np.random.randint(2, size=q_size) Logger.get_instance().log('Bit array to be sent: ' + str(bit_arr)) qubits = [] for i in range(q_size): q_tmp = Qubit(host) if bit_arr[i] == 1: q_tmp.X() qubits.append(q_tmp) check_qubits = host.add_checksum(qubits, checksum_size_per_qubit) checksum_size = int(q_size / checksum_size_per_qubit) qubits.append(check_qubits) checksum_cnt = 0 for i in range(q_size + checksum_size): if i < q_size: q = qubits[i] else: q = qubits[q_size][checksum_cnt] checksum_cnt = checksum_cnt + 1 host.send_qubit(receiver_id, q, await_ack=True)
def add_checksum(self, qubits, size_per_qubit=2): """ Generate a set of qubits that represent a quantum checksum for the set of qubits *qubits* Args: qubits: The set of qubits to encode size_per_qubit (int): The size of the checksum per qubit (i.e. 1 qubit encoded into *size*) Returns: list: A list of qubits that are encoded for *qubits* """ i = 0 check_qubits = [] while i < len(qubits): check = Qubit(self.host_id) j = 0 while j < size_per_qubit: qubits[i + j].cnot(check) j += 1 check_qubits.append(check) i += size_per_qubit return check_qubits
def test_teleport(self): with CQCConnection("Alice") as Alice, CQCConnection( "Bob") as Bob, CQCConnection("Eve") as Eve: hosts = { 'alice': Host('00000000', Alice), 'bob': Host('00000001', Bob), 'eve': Host('00000011', Eve) } self.hosts = hosts # A <-> B <-> E hosts['alice'].add_connection('00000001') hosts['bob'].add_connection('00000000') hosts['bob'].add_connection('00000011') hosts['eve'].add_connection('00000001') hosts['alice'].start() hosts['bob'].start() hosts['eve'].start() for h in hosts.values(): self.network.add_host(h) q = Qubit(hosts['alice']) q.X() hosts['alice'].send_teleport(hosts['eve'].host_id, q) q2 = None i = 0 while i < TestTwoHop.MAX_WAIT and q2 is None: q2 = hosts['eve'].get_data_qubit(hosts['alice'].host_id) i += 1 time.sleep(1) self.assertIsNotNone(q2) self.assertEqual(q2.measure(), 1)
def qudp_sender(host, q_size, receiver_id): bit_arr = np.random.randint(2, size=q_size) data_qubits = [] checksum_size = 3 checksum_per_qubit = int(q_size / checksum_size) print('---------') print('Sender sends the classical bits: ' + str(bit_arr)) print('---------') for i in range(q_size): q_tmp = Qubit(host) if bit_arr[i] == 1: q_tmp.X() data_qubits.append(q_tmp) checksum_qubits = host.add_checksum(data_qubits, checksum_per_qubit) data_qubits.extend(checksum_qubits) host.send_classical(receiver_id, checksum_size, await_ack=False) for q in data_qubits: host.send_qubit(receiver_id, q, await_ack=False)
def test_maximum_data_qubit_limit(self): global hosts hosts['alice'].set_data_qubit_memory_limit(1) hosts['bob'].set_data_qubit_memory_limit(1) q_alice_id_1 = hosts['alice'].send_qubit(hosts['bob'].host_id, Qubit(hosts['alice'])) time.sleep(2) q_alice_id_2 = hosts['alice'].send_qubit(hosts['bob'].host_id, Qubit(hosts['alice'])) time.sleep(2) q_bob_id_1 = hosts['bob'].send_qubit(hosts['alice'].host_id, Qubit(hosts['bob'])) time.sleep(2) q_bob_id_2 = hosts['bob'].send_qubit(hosts['alice'].host_id, Qubit(hosts['bob'])) time.sleep(2) # Allow the network to process the requests # TODO: remove the need for this time.sleep(2) i = 0 while len(hosts['alice'].get_data_qubits( hosts['bob'].host_id)) < 1 and i < TestOneHop.MAX_WAIT: time.sleep(1) i += 1 i = 0 while len(hosts['bob'].get_data_qubits( hosts['alice'].host_id)) < 1 and i < TestOneHop.MAX_WAIT: time.sleep(1) i += 1 self.assertTrue( len(hosts['alice'].get_data_qubits(hosts['bob'].host_id)) == 1) self.assertTrue(hosts['alice'].get_data_qubit( hosts['bob'].host_id, q_bob_id_1).measure() == 0) self.assertIsNone(hosts['alice'].get_data_qubit( hosts['bob'].host_id, q_bob_id_2)) self.assertTrue( len(hosts['bob'].get_data_qubits(hosts['alice'].host_id)) == 1) self.assertTrue(hosts['bob'].get_data_qubit( hosts['alice'].host_id, q_alice_id_1).measure() == 0) self.assertIsNone(hosts['bob'].get_data_qubit(hosts['alice'].host_id, q_alice_id_2)) hosts['alice'].set_data_qubit_memory_limit(2, hosts['bob'].host_id) hosts['bob'].set_data_qubit_memory_limit(2) q_alice_id_1 = hosts['alice'].send_qubit(hosts['bob'].host_id, Qubit(hosts['alice'])) time.sleep(2) q_alice_id_2 = hosts['alice'].send_qubit(hosts['bob'].host_id, Qubit(hosts['alice'])) time.sleep(2) q_alice_id_3 = hosts['alice'].send_qubit(hosts['bob'].host_id, Qubit(hosts['alice'])) time.sleep(2) q_bob_id_1 = hosts['bob'].send_qubit(hosts['alice'].host_id, Qubit(hosts['bob'])) time.sleep(2) q_bob_id_2 = hosts['bob'].send_qubit(hosts['alice'].host_id, Qubit(hosts['bob'])) time.sleep(2) q_bob_id_3 = hosts['bob'].send_qubit(hosts['alice'].host_id, Qubit(hosts['bob'])) time.sleep(2) # Allow the network to process the requests time.sleep(3) i = 0 while len(hosts['alice'].get_data_qubits( hosts['bob'].host_id)) < 2 and i < TestOneHop.MAX_WAIT: time.sleep(1) i += 1 i = 0 while len(hosts['bob'].get_data_qubits( hosts['alice'].host_id)) < 2 and i < TestOneHop.MAX_WAIT: time.sleep(1) i += 1 self.assertTrue( len(hosts['alice'].get_data_qubits(hosts['bob'].host_id)) == 2) self.assertTrue(hosts['alice'].get_data_qubit( hosts['bob'].host_id, q_bob_id_1).measure() == 0) self.assertTrue(hosts['alice'].get_data_qubit( hosts['bob'].host_id, q_bob_id_2).measure() == 0) self.assertIsNone(hosts['alice'].get_data_qubit( hosts['bob'].host_id, q_bob_id_3)) self.assertTrue( len(hosts['bob'].get_data_qubits(hosts['alice'].host_id)) == 2) self.assertTrue(hosts['bob'].get_data_qubit( hosts['alice'].host_id, q_alice_id_1).measure() == 0) self.assertTrue(hosts['bob'].get_data_qubit( hosts['alice'].host_id, q_alice_id_2).measure() == 0) self.assertIsNone(hosts['bob'].get_data_qubit(hosts['alice'].host_id, q_alice_id_3))