def main(): # In this example, we are Alice. myName = "Alice" # Initialize the connection cqc = init(myName) # Send Hello message print("App {} tells CQC: 'HELLO'".format(myName)) hdr = CQCHeader() hdr.setVals(CQC_VERSION, CQC_TP_HELLO, 0, 0) msg = hdr.pack() cqc.send(msg) # Receive return message data = cqc.recv(192) hdr = CQCHeader(data) if hdr.tp == CQC_TP_HELLO: print("CQC tells App {}: 'HELLO'".format(myName)) else: print("Did not receive a hello message, but rather: {}".format( hdr.printable())) # Close the connection cqc.close()
def construct_simple(self, tp): """Construct simple message. For example a HELLO message if tp=CQC_TP_HELLO. """ hdr = CQCHeader() hdr.setVals(CQC_VERSION, tp, self._appID, 0) msg = hdr.pack() return msg
def construct_command_headers(self, qID, command, **kwargs): """Construct a commmand consisting of a list of header objects. Extra arguments are only used if the command if of a type that needs them. - **Arguments** :qID: qubit ID :command: Command to be executed, eg CQC_CMD_H :nofify: Do we wish to be notified when done. :block: Do we want the qubit to be blocked """ # Construct extra header if needed. xtra_hdr = None if command == CQC_CMD_SEND or command == CQC_CMD_EPR: xtra_hdr = CQCCommunicationHeader() remote_appID = kwargs.get("remote_appID", 0) remote_node = kwargs.get("remote_node", 0) remote_port = kwargs.get("remote_port", 0) xtra_hdr.setVals(remote_appID, remote_node, remote_port) elif command == CQC_CMD_CNOT or command == CQC_CMD_CPHASE: xtra_hdr = CQCXtraQubitHeader() xtra_qID = kwargs.get("xtra_qID", 0) xtra_hdr.setVals(xtra_qID) elif (command == CQC_CMD_ROT_X or command == CQC_CMD_ROT_Y or command == CQC_CMD_ROT_Z): xtra_hdr = CQCRotationHeader() step = kwargs.get("step", 0) xtra_hdr.setVals(step) elif command == CQC_CMD_MEASURE or command == CQC_CMD_MEASURE_INPLACE: xtra_hdr = CQCAssignHeader() ref_id = kwargs.get("ref_id", 0) xtra_hdr.setVals(ref_id) # If xtra_hdr is None, we don't need an extra message. if xtra_hdr is None: header_length = CQCCmdHeader.HDR_LENGTH else: header_length = CQCCmdHeader.HDR_LENGTH + xtra_hdr.HDR_LENGTH # Construct Header hdr = CQCHeader() hdr.setVals(CQC_VERSION, CQC_TP_COMMAND, self._appID, header_length) # Construct Command cmd_hdr = CQCCmdHeader() notify = int(kwargs.get("notify", True)) block = int(kwargs.get("block", True)) action = int(kwargs.get("action", False)) cmd_hdr.setVals(qID, command, notify, block, action) headers = [hdr, cmd_hdr] if xtra_hdr is not None: headers.append(xtra_hdr) return headers
def create_return_message(app_id, msg_type, length=0, cqc_version=CQC_VERSION): """ Creates a messaage that the protocol should send back :param app_id: the app_id to which the message should be send :param msg_type: the type of message to return :param length: the length of additional message :param cqc_version: The cqc version of the message :return: a new header message to be send back """ hdr = CQCHeader() hdr.setVals(cqc_version, msg_type, app_id, length) return hdr.pack()
def _send_back_cqc(self, header, msgType, length=0): """ Return a simple CQC header with the specified type. header CQC header of the packet we respond to msgType Message type to return length Length of additional message """ hdr = CQCHeader() hdr.setVals(CQC_VERSION, msgType, header.app_id, length) msg = hdr.pack() self.transport.write(msg)
def insert_cqc_header(self, cqc_type: CQCType, version=CQC_VERSION) -> None: """ Inserts a CQC Header at index 0 of self._pending_headers. Invoke this method *after* all other headers are pended, so that the correct message length is calculated. """ # Count the total message length message_length = 0 for header in self._pending_headers: message_length += header.HDR_LENGTH # Build the CQC Header cqc_header = CQCHeader() cqc_header.setVals(CQC_VERSION, cqc_type, self._appID, message_length) # Insert CQC Header at the front self._pending_headers.insert(0, cqc_header)
def sendGetTime(self, qID, notify=1, block=1, action=0): """Sends get-time message - **Arguments** :qID: qubit ID :command: Command to be executed, eg CQC_CMD_H :notify: Do we wish to be notified when done. :block: Do we want the qubit to be blocked :action: Are there more commands to be executed """ # Send Header hdr = CQCHeader() hdr.setVals(CQC_VERSION, CQC_TP_GET_TIME, self._appID, CQCCmdHeader.HDR_LENGTH) msg = hdr.pack() self.commit(msg) # Send Command cmd_hdr = CQCCmdHeader() cmd_hdr.setVals(qID, 0, notify, block, action) cmd_msg = cmd_hdr.pack() self.commit(cmd_msg)