netqasm.sdk.classical_communication
netqasm.sdk.classical_communication.broadcast_channel
Interface for classical broadcasting between Hosts.
This module contains the BroadcastChannel class which is a base for representing classical broadcasting (sending classical messages to all other nodes in the network) between Hosts.
- class netqasm.sdk.classical_communication.broadcast_channel.BroadcastChannel(app_name, timeout=None, use_callbacks=False)
Bases:
ABCSocket for sending messages to all nodes in the network (broadcasting).
A BroadcastChannel can be used by Hosts to broadcast a message to all other nodes in the network. It is very similar to a Socket object, just without an explicit single remote node.
A BroadcastChannel is a local object that each node (that wants to send and receive broadcast messages) must instantiate themselves.
- Parameters:
app_name (
str)timeout (
Optional[float])use_callbacks (
bool)
- __init__(app_name, timeout=None, use_callbacks=False)
BroadcastChannel constructor.
- Parameters:
app_name (
str) – application/Host name of this channel’s constructortimeout (
Optional[float]) – maximum time to try and create this channel before abortinguse_callbacks (
bool) – whether to use the recv_callback and conn_lost_callback callback methods
- abstract send(msg)
Broadcast a message to all remote nodes.
- Parameters:
msg (
str)- Return type:
None
- abstract recv(block=True)
Receive a message that was broadcast and from whom.
- Parameters:
block (
bool)- Return type:
Tuple[str,str]
- recv_callback(remote_app_name, msg)
This method gets called when a message is received.
Subclass to define behaviour.
NOTE: This only happens if self.use_callbacks is set to True.
- Parameters:
remote_app_name (
str)msg (
str)
- Return type:
None
- conn_lost_callback()
This method gets called when the connection is lost.
Subclass to define behaviour.
NOTE: This only happens if self.use_callbacks is set to True.
- Return type:
None
- class netqasm.sdk.classical_communication.broadcast_channel.BroadcastChannelBySockets(app_name, remote_app_names, **kwargs)
Bases:
BroadcastChannelImplementation of a BroadcastChannel using a Socket for each remote node.
Technically this is a multicast channel since the receiving nodes must be explicitly listed. It simply uses one-to-one sockets for every remote node.
- Parameters:
app_name (
str)remote_app_names (
List[str])
- __init__(app_name, remote_app_names, **kwargs)
BroadcastChannel constructor.
- Parameters:
app_name (
str) – application/Host name of selfremote_app_names (
List[str]) – list of receiving remote Hosts
- send(msg)
Broadcast a message to all remote nodes.
- Parameters:
msg (
str)- Return type:
None
- recv(block=True, timeout=None)
Receive a message that was broadcast.
- Parameters:
block (bool) – Whether to block for an available message
timeout (float, optional) – Optionally use a timeout for trying to recv a message. Only used if block=True.
- Returns:
(remote_node_name, msg)
- Return type:
tuple
- Raises:
RuntimeError – If block=False and there is no available message
netqasm.sdk.classical_communication.thread_socket.broadcast_channel
BroadcastChannel implementation using ThreadSockets.
- class netqasm.sdk.classical_communication.thread_socket.broadcast_channel.ThreadBroadcastChannel(app_name, remote_app_names, **kwargs)
Bases:
BroadcastChannelBySockets- Parameters:
app_name (
str)remote_app_names (
List[str])
netqasm.sdk.classical_communication.message
netqasm.sdk.classical_communication.socket
Interface for classical communication between Hosts.
This module contains the Socket class which is a base for representing classical communication (sending and receiving classical messages) between Hosts.
- class netqasm.sdk.classical_communication.socket.Socket(app_name, remote_app_name, socket_id=0, timeout=None, use_callbacks=False, log_config=None)
Bases:
ABCBase class for classical sockets.
Classical communication is modelled by sockets, which are also widely used in purely classical applications involving communication.
If a node wants to communicate arbitrary classical messages with another node, this communication must be done between the Hosts of these nodes. Both Hosts should instantiate a Socket object with the other Host as ‘remote’. Upon creation, the Socket objects try to connect to each other. Only after this has succeeded, the sockets can be used.
The main methods of a Socket are send and recv, which are used to send a message to the remote Host, and wait to receive a message from the other Host, respectively. Messages are str (string) objects. To send a number, convert it to a string before sending, and convert it back after receiving.
There are some variations on the send and recv methods which may be useful in specific scenarios. See their own documentation for their use.
NOTE: At the moment, Sockets are not part of the compilation process yet. Therefore, they don’t need to be part of a Connection, and operations on Sockets do not need to be flushed before they are executed (they are executed immediately). This also means that e.g. a recv operation, which is blocking by default, acutally blocks the whole application script. So, if any quantum operations should be executed before such a `recv` statement, make sure that these operations are flushed before blocking on `recv`.
Implementations (subclasses) of Sockets may be quite different, depending on the runtime environment. A physical setup (with real hardware) may implement this as TCP sockets. A simulator might use inter-thread communication (see e.g. ThreadSocket), or another custom object type.
- Parameters:
app_name (
str)remote_app_name (
str)socket_id (
int)timeout (
Optional[float])use_callbacks (
bool)log_config (
Optional[LogConfig])
- __init__(app_name, remote_app_name, socket_id=0, timeout=None, use_callbacks=False, log_config=None)
Socket constructor.
- Parameters:
app_name (
str) – application/Host name of this socket’s ownerremote_app_name (
str) – application/Host name of this socket’s remotesocket_id (
int) – local ID to use for this sockettimeout (
Optional[float]) – maximum amount of real time to try to connect to the remote socketuse_callbacks (
bool) – whether to call the recv_callback method upon receiving a messagelog_config (
Optional[LogConfig]) – logging configuration for this socket
- abstract send(msg)
Send a message to the remote node.
- Parameters:
msg (
str)- Return type:
None
- abstract recv(block=True, timeout=None, maxsize=None)
Receive a message from the remote node.
- Parameters:
block (
bool)timeout (
Optional[float])maxsize (
Optional[int])
- Return type:
str
- send_structured(msg)
Sends a structured message (with header and payload) to the remote node.
- Parameters:
msg (
StructuredMessage)- Return type:
None
- recv_structured(block=True, timeout=None, maxsize=None)
Receive a message (with header and payload) from the remote node.
- Parameters:
block (
bool)timeout (
Optional[float])maxsize (
Optional[int])
- Return type:
- send_silent(msg)
Sends a message without logging
- Parameters:
msg (
str)- Return type:
None
- recv_silent(block=True, timeout=None, maxsize=None)
Receive a message without logging
- Parameters:
block (
bool)timeout (
Optional[float])maxsize (
Optional[int])
- Return type:
str
- recv_callback(msg)
This method gets called when a message is received.
Subclass to define behaviour.
NOTE: This only happens if self.use_callbacks is set to True.
- Parameters:
msg (
str)- Return type:
None
- conn_lost_callback()
This method gets called when the connection is lost.
Subclass to define behaviour.
NOTE: This only happens if self.use_callbacks is set to True.
- Return type:
None
netqasm.sdk.classical_communication.thread_socket.socket
Classical socket implementation using a hub that is shared across threads.
This module contains the ThreadSocket class which is an implementation of the Socket interface that can be used by Hosts that run in a multi-thread simulation.
- netqasm.sdk.classical_communication.thread_socket.socket.trim_msg(msg)
- Parameters:
msg (
str)- Return type:
str
- netqasm.sdk.classical_communication.thread_socket.socket.log_send(method)
- netqasm.sdk.classical_communication.thread_socket.socket.log_send_structured(method)
- netqasm.sdk.classical_communication.thread_socket.socket.log_recv(method)
- netqasm.sdk.classical_communication.thread_socket.socket.log_recv_structured(method)
- class netqasm.sdk.classical_communication.thread_socket.socket.ThreadSocket(app_name, remote_app_name, socket_id=0, timeout=None, use_callbacks=False, log_config=None)
Bases:
SocketClassical socket implementation for multi-threaded simulations.
This implementation should be used when the simulation consists of a single process, with a thread for each Host. A global SocketHub instance must be available and shared between all threads. The ThreadSocket instance for a Host can then ‘send’ and ‘receive’ messages to/from other Hosts by writing/reading from the shared SocketHub memory.
Currently only used with the SquidASM simulator backend.
- Parameters:
app_name (
str)remote_app_name (
str)socket_id (
int)timeout (
Optional[float])use_callbacks (
bool)log_config (
Optional[LogConfig])
- __init__(app_name, remote_app_name, socket_id=0, timeout=None, use_callbacks=False, log_config=None)
ThreadSocket constructor.
- Parameters:
app_name (
str) – application/Host name of this socket’s ownerremote_app_name (
str) – remote application/Host namesocket_id (
int) – local ID to use for this sockettimeout (
Optional[float]) – maximum amount of real time to try to connect to the remote socketuse_callbacks (
bool) – whether to call the recv_callback method upon receiving a messagelog_config (
Optional[LogConfig]) – logging configuration for this socket
- classmethod get_comm_logger(app_name, comm_log_dir)
- Parameters:
app_name (
str)comm_log_dir (
str)
- Return type:
- property app_name: str
- property remote_app_name: str
- property id: int
- property key: Tuple[str, str, int]
- property remote_key: Tuple[str, str, int]
- property connected: bool
- property use_callbacks: bool
- send(msg: str) None
Send a message to the remote node.
- Parameters:
msg (
str)- Return type:
None
- recv(*args, **kwargs)
Receive a message from the remote node.
- send_structured(msg: StructuredMessage)
Sends a structured message (with header and payload) to the remote node.
- Parameters:
msg (
StructuredMessage)
- recv_structured(*args, **kwargs)
Receive a message (with header and payload) from the remote node.
- wait()
Waits until the connection gets lost
- Return type:
None
- send_silent(msg)
Sends a message without logging
- Parameters:
msg (
str)- Return type:
None
- recv_silent(block=True, timeout=None, maxsize=None)
Receive a message without logging
- Parameters:
block (
bool)timeout (
Optional[float])maxsize (
Optional[int])
- Return type:
str
- class netqasm.sdk.classical_communication.thread_socket.socket.StorageThreadSocket(app_name, remote_app_name, **kwargs)
Bases:
ThreadSocket- Parameters:
app_name (
str)remote_app_name (
str)
- __init__(app_name, remote_app_name, **kwargs)
ThreadSocket that simply stores any message comming in
- Parameters:
app_name (
str)remote_app_name (
str)
- recv_callback(msg)
This method gets called when a message is received.
Subclass to define behaviour.
NOTE: This only happens if self.use_callbacks is set to True.
- Parameters:
msg (
str)- Return type:
None
netqasm.sdk.classical_communication.thread_socket.socket_hub
Global management for classical sockets that live in separate threads.
This module contains the _SocketHub that manages sockets that are in separate threads and need to communicate with each other.
- netqasm.sdk.classical_communication.thread_socket.socket_hub.reset_socket_hub()
- Return type:
None