Skip to content

Commit

Permalink
Merge pull request #47 from tqsd/issue-46-fix
Browse files Browse the repository at this point in the history
Issue 46 fix
  • Loading branch information
stephendiadamo authored Jun 23, 2020
2 parents 9580423 + 9a1413b commit c528ce0
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 35 deletions.
2 changes: 0 additions & 2 deletions examples/send_data/send_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ def main():
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()
Expand Down
6 changes: 3 additions & 3 deletions qunetsim/components/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@ def get_ghz(self, host_id, q_id=None, wait=0):
if not isinstance(wait, float) and not isinstance(wait, int):
raise Exception('wait parameter should be a number')

return _get_qubit(self._qubit_storage, host_id, q_id, Qubit.EPR_QUBIT, wait)
return _get_qubit(self._qubit_storage, host_id, q_id, Qubit.GHZ_QUBIT, wait)

def send_teleport(self, receiver_id, q, await_ack=False, no_ack=False, payload=None, generate_epr_if_none=True):
"""
Expand Down Expand Up @@ -1057,7 +1057,7 @@ def get_data_qubits(self, host_id):
Does not remove the qubits from storage like *get_data_qubit* does.
Args:
host_id (int): The host id from which the data qubit have been received.
host_id (str): The host id from which the data qubit have been received.
Returns:
dict: If *host_id* is not set, then return the entire dictionary of data qubits.
Expand Down Expand Up @@ -1146,7 +1146,7 @@ def add_ghz_qubit(self, host_id, qubit, q_id=None):
if q_id is not None:
qubit.id = q_id

self._qubit_storage.add_qubit_from_host(qubit, Qubit.EPR_QUBIT, host_id)
self._qubit_storage.add_qubit_from_host(qubit, Qubit.GHZ_QUBIT, host_id)
return qubit.id

def add_checksum(self, qubits, size_per_qubit=2):
Expand Down
12 changes: 3 additions & 9 deletions qunetsim/components/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ def _route_quantum_info(self, sender, receiver, qubits):
qubits (List of Qubits): The qubits to be sent
"""

def transfer_qubits(r, store=False, original_sender=None):
def transfer_qubits(r, original_sender=None):
for q in qubits:
Logger.get_instance().log('transfer qubits - sending qubit ' + q.id)
x_err_var = random.random()
Expand All @@ -488,20 +488,14 @@ def transfer_qubits(r, store=False, original_sender=None):
# Unblock qubits in case they were blocked
q.blocked = False

if not store and self.ARP[r].q_relay_sniffing:
if self.ARP[r].q_relay_sniffing:
self.ARP[r].q_relay_sniffing_fn(original_sender, receiver, q)

if store and original_sender is not None:
self.ARP[r].add_data_qubit(original_sender, q)

route = self.get_quantum_route(sender, receiver)
i = 0
while i < len(route) - 1:
Logger.get_instance().log('sending qubits from ' + route[i] + ' to ' + route[i + 1])
if len(route[i:]) != 2:
transfer_qubits(route[i + 1], original_sender=route[0])
else:
transfer_qubits(route[i + 1], store=True, original_sender=route[0])
transfer_qubits(route[i + 1], original_sender=route[0])
i += 1

def _process_queue(self):
Expand Down
20 changes: 13 additions & 7 deletions qunetsim/components/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,16 @@ def _rec_qubit(packet):
Args:
packet (Packet): The packet in which to receive.
"""
from_host = packet.sender
receiver = packet.receiver
qubit = packet.payload
receiver = network.get_host(receiver)
receiver.add_data_qubit(from_host, qubit)

Logger.get_instance().log(
packet.receiver + ' received qubit ' + packet.payload.id + ' from ' + packet.sender)
# Send ACK if seq_num is not -1

if packet.seq_num != -1:
_send_ack(packet.sender, packet.receiver, packet.seq_num)

Expand Down Expand Up @@ -343,15 +350,15 @@ def _send_superdense(packet):
q_superdense = host_sender.get_epr(receiver, q_id=q_id, wait=Constants.WAIT_TIME)

else:
q_superdense = host_sender.get_epr(receiver, wait=5)
q_superdense = host_sender.get_epr(receiver)

if q_superdense is None:
Logger.get_instance().log('Failed to get EPR with ' + sender + " and " + receiver)
raise Exception("couldn't encode superdense")

_encode_superdense(packet.payload, q_superdense)

# change id, so that at receiving they are not the same
# change ID, so that at receiving they are not the same
q_superdense.id = "E" + q_superdense.id
packet.payload = q_superdense
packet.protocol = Constants.REC_SUPERDENSE
Expand All @@ -371,15 +378,14 @@ def _rec_superdense(packet):
"""
receiver = packet.receiver
sender = packet.sender
payload = packet.payload

q1 = packet.payload
host_receiver = network.get_host(receiver)

q1 = host_receiver.get_data_qubit(sender, payload.id, wait=Constants.WAIT_TIME)
# the shared EPR id is the DATA id without the first letter.
q2 = host_receiver.get_epr(sender, payload.id[1:], wait=Constants.WAIT_TIME)
q2 = host_receiver.get_epr(sender, q1.id[1:], wait=Constants.WAIT_TIME)

assert q1 is not None and q2 is not None
assert q1 is not None
assert q2 is not None

# Send ACK if seq_num is not -1
if packet.seq_num != -1:
Expand Down
6 changes: 6 additions & 0 deletions qunetsim/objects/packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ def __init__(self, sender, receiver, protocol, payload_type, payload,
self._seq_num = sequence_number
self._await_ack = await_ack

def __str__(self):
return 'Sender: %s\nReceiver: ' \
'%s\nProtocol: %s\nSequence number: %d\n' \
'Payload type: %s' % \
(self._sender, self._receiver, self._protocol, self._seq_num, str(self._payload_type))

@property
def sender(self):
"""
Expand Down
34 changes: 20 additions & 14 deletions qunetsim/objects/quantum_storage.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from qunetsim.backends.rw_lock import RWLock
from qunetsim.objects import Logger
import queue


Expand Down Expand Up @@ -28,6 +29,8 @@ def __init__(self):
# read write lock, for threaded access
self.lock = RWLock()

self.logger = Logger.get_instance()

# for tracking pending requests
# dictionary tracks the request made by a pending request.
self._pending_request_dict = {}
Expand Down Expand Up @@ -197,7 +200,8 @@ def add_qubit_from_host(self, qubit, purpose, from_host_id):

self.lock.acquire_write()
if self._check_qubit_in_system(qubit, from_host_id, purpose=purpose):
print(self)
self.logger.log("Qubit with id %s, purpose %s and from host %s"
" already in storage" % (qubit.id, purpose, from_host_id))
raise ValueError("Qubit with these parameters already in storage!")
if from_host_id not in self._host_dict:
self._add_new_host(from_host_id)
Expand Down Expand Up @@ -244,11 +248,11 @@ def _check_all_requests(self):
If a request is fullfilled, the request is handeled and the function
returns the qubit of this request.
"""
for id, args in self._pending_request_dict.items():
for req_id, args in self._pending_request_dict.items():
ret = self._get_qubit_from_host(args[1], args[2], args[3])
if ret is not None:
args[0].put(ret)
self._remove_request(id)
self._remove_request(req_id)
return ret

def _add_request(self, args):
Expand All @@ -264,15 +268,15 @@ def _add_request(self, args):
self._amount_pending_requests += 1
return self._request_id

def _remove_request(self, id):
def _remove_request(self, req_id):
"""
Removes a pending request from the request dict.
Args:
id (int): The id of the request to remove.
req_id (int): The id of the request to remove.
"""
if id in self._pending_request_dict:
del self._pending_request_dict[id]
if req_id in self._pending_request_dict:
del self._pending_request_dict[req_id]
self._amount_pending_requests -= 1

def get_qubit_from_host(self, from_host_id, q_id=None, purpose=None, wait=0):
Expand Down Expand Up @@ -335,13 +339,15 @@ def _get_qubit_from_host(self, from_host_id, q_id, purpose):
if from_host_id not in self._host_dict:
return None
if self._host_dict[from_host_id]:
qubit = self._host_dict[from_host_id].pop(0)
out = self._pop_qubit_with_id_and_host_from_qubit_dict(
qubit.id, from_host_id, purpose=purpose)
if out is not None:
self._decrease_qubit_counter(from_host_id)
return out[0]
self._host_dict[from_host_id].append(qubit)
# check purposes of all qubits
for _ in range(len(self._host_dict[from_host_id])):
qubit = self._host_dict[from_host_id].pop(0)
out = self._pop_qubit_with_id_and_host_from_qubit_dict(
qubit.id, from_host_id, purpose=purpose)
if out is not None:
self._decrease_qubit_counter(from_host_id)
return out[0]
self._host_dict[from_host_id].append(qubit)
return None

def _pop_qubit_with_id_and_host_from_qubit_dict(self, q_id, from_host_id, purpose=None):
Expand Down

0 comments on commit c528ce0

Please sign in to comment.