|
| 1 | +import logging |
| 2 | +import threading |
| 3 | +from queue import Queue |
| 4 | +from eqsn.shared_dict import SharedDict |
| 5 | +from eqsn.qubit_thread import SINGLE_GATE, MERGE_SEND, MERGE_ACCEPT, MEASURE,\ |
| 6 | + MEASURE_NON_DESTRUCTIVE, GIVE_QUBITS_AND_TERMINATE, \ |
| 7 | + CONTROLLED_GATE, NEW_QUBIT, ADD_MERGED_QUBITS_TO_DICT, QubitThread |
| 8 | + |
| 9 | + |
| 10 | +class WorkerProcess(object): |
| 11 | + |
| 12 | + def __init__(self, queue): |
| 13 | + self.queue = queue |
| 14 | + # Get a new instance, since their might be one from the old process |
| 15 | + self.shared_dict = SharedDict.get_new_instance() |
| 16 | + |
| 17 | + def run(self): |
| 18 | + """ |
| 19 | + Run in loop and wait to receive tasks to perform. |
| 20 | + """ |
| 21 | + amount_single_gate = 0 |
| 22 | + while True: |
| 23 | + item = self.queue.get() |
| 24 | + if item is None: |
| 25 | + self.stop_all() |
| 26 | + return |
| 27 | + elif item[0] == NEW_QUBIT: |
| 28 | + self.new_qubit(item[1]) |
| 29 | + elif item[0] == SINGLE_GATE: |
| 30 | + self.apply_single_gate(item[1], item[2]) |
| 31 | + amount_single_gate += 1 |
| 32 | + elif item[0] == CONTROLLED_GATE: |
| 33 | + self.apply_controlled_gate(item[1], item[2], item[3]) |
| 34 | + elif item[0] == MEASURE: |
| 35 | + self.measure(item[1], item[2]) |
| 36 | + elif item[0] == MERGE_ACCEPT: |
| 37 | + self.merge_accept(item[1], item[2]) |
| 38 | + elif item[0] == MERGE_SEND: |
| 39 | + self.merge_send(item[1], item[2]) |
| 40 | + elif item[0] == GIVE_QUBITS_AND_TERMINATE: |
| 41 | + self.give_qubits_and_terminate(item[1], item[2]) |
| 42 | + elif item[0] == MEASURE_NON_DESTRUCTIVE: |
| 43 | + self.measure_non_destructive(item[1], item[2]) |
| 44 | + elif item[0] == ADD_MERGED_QUBITS_TO_DICT: |
| 45 | + self.add_merged_qubits_to_thread(item[1], item[2]) |
| 46 | + else: |
| 47 | + raise ValueError("Command does not exist!") |
| 48 | + |
| 49 | + def new_qubit(self, q_id): |
| 50 | + """ |
| 51 | + Creates a new qubit with an id. |
| 52 | +
|
| 53 | + Args: |
| 54 | + id (String): Id of the new qubit. |
| 55 | + """ |
| 56 | + q = Queue() |
| 57 | + thread = QubitThread(q_id, q) |
| 58 | + p = threading.Thread(target=thread.run, args=()) |
| 59 | + self.shared_dict.set_thread_with_id(q_id, p, q) |
| 60 | + p.start() |
| 61 | + logging.debug("Created new qubit with id %s.", q_id) |
| 62 | + |
| 63 | + def measure(self, q_id, channel): |
| 64 | + temp_queue = Queue() |
| 65 | + q = self.shared_dict.get_queues_for_ids([q_id])[0] |
| 66 | + q.put([MEASURE, q_id, temp_queue]) |
| 67 | + res = temp_queue.get() |
| 68 | + channel.put(res) |
| 69 | + self.shared_dict.delete_id_and_check_to_join_thread(q_id) |
| 70 | + |
| 71 | + def measure_non_destructive(self, q_id, channel): |
| 72 | + q = self.shared_dict.get_queues_for_ids([q_id])[0] |
| 73 | + q.put([MEASURE_NON_DESTRUCTIVE, q_id, channel]) |
| 74 | + |
| 75 | + def give_qubits_and_terminate(self, q_id, queue): |
| 76 | + temp_queue = Queue() |
| 77 | + q = self.shared_dict.get_queues_for_ids([q_id])[0] |
| 78 | + q.put([GIVE_QUBITS_AND_TERMINATE, temp_queue]) |
| 79 | + qubits = temp_queue.get() |
| 80 | + # remove all qubits |
| 81 | + for c in qubits: |
| 82 | + self.shared_dict.delete_id_and_check_to_join_thread(c) |
| 83 | + # send the qubits to the main process |
| 84 | + queue.put(qubits) |
| 85 | + |
| 86 | + def add_merged_qubits_to_thread(self, q_id, qubits): |
| 87 | + q, t = self.shared_dict.get_queues_and_threads_for_ids([q_id])[0] |
| 88 | + for qubit in qubits: |
| 89 | + self.shared_dict.set_thread_with_id(qubit, t, q) |
| 90 | + |
| 91 | + def stop_all(self): |
| 92 | + """ |
| 93 | + Stops the simulator from running. |
| 94 | + """ |
| 95 | + self.shared_dict.send_all_threads(None) |
| 96 | + self.shared_dict.stop_all_threads() |
| 97 | + self.shared_dict.stop_shared_dict() |
| 98 | + |
| 99 | + def apply_single_gate(self, gate, q_id): |
| 100 | + """ |
| 101 | + Applies a single gate to a Qubit. |
| 102 | + """ |
| 103 | + q = self.shared_dict.get_queues_for_ids([q_id])[0] |
| 104 | + q.put([SINGLE_GATE, gate, q_id]) |
| 105 | + |
| 106 | + def apply_controlled_gate(self, gate, q_id1, q_id2): |
| 107 | + """ |
| 108 | + Applies a controlled gate, where the gate is applied to |
| 109 | + q_id1 and controlled by q_id2. |
| 110 | +
|
| 111 | + Args: |
| 112 | + q_id1 (String): Id of the Qubit on which the X gate is applied. |
| 113 | + q_id2 (String): Id of the Qubit which controls the gate. |
| 114 | + """ |
| 115 | + self.merge_qubits(q_id1, q_id2) |
| 116 | + q = self.shared_dict.get_queues_for_ids([q_id1])[0] |
| 117 | + q.put([CONTROLLED_GATE, gate, q_id1, q_id2]) |
| 118 | + |
| 119 | + def merge_send(self, q_id, queue): |
| 120 | + q = self.shared_dict.get_queues_for_ids([q_id])[0] |
| 121 | + q.put([MERGE_SEND, queue]) |
| 122 | + |
| 123 | + def merge_accept(self, q_id, queue): |
| 124 | + q = self.shared_dict.get_queues_for_ids([q_id])[0] |
| 125 | + q.put([MERGE_ACCEPT, queue]) |
| 126 | + |
| 127 | + def merge_qubits(self, q_id1, q_id2): |
| 128 | + """ |
| 129 | + Merges two qubits to one process, if they are not already |
| 130 | + running in the same process. |
| 131 | +
|
| 132 | + Args: |
| 133 | + q_id1 (String): Id of the Qubit merged into q_id2. |
| 134 | + q_id2 (String): Id of the Qubit merged with q_id1. |
| 135 | + """ |
| 136 | + l = self.shared_dict.get_queues_for_ids([q_id1, q_id2]) |
| 137 | + if len(l) == 1: |
| 138 | + return # Already merged |
| 139 | + else: |
| 140 | + logging.debug("Merge Qubits %s and %s.", q_id1, q_id2) |
| 141 | + q1 = l[0] |
| 142 | + q2 = l[1] |
| 143 | + merge_q = Queue() |
| 144 | + q1.put([MERGE_SEND, merge_q]) |
| 145 | + q2.put([MERGE_ACCEPT, merge_q]) |
| 146 | + qubits_q = Queue() |
| 147 | + q1.put([GIVE_QUBITS_AND_TERMINATE, qubits_q]) |
| 148 | + qubits = qubits_q.get() |
| 149 | + self.shared_dict.change_thread_and_queue_of_ids_and_join( |
| 150 | + qubits, q_id2) |
0 commit comments