Host

The Host component is analogous to a host, or node in a classical network. Hosts in QuNetSim act either as a routing node that can relay packets through the network, or they can act as a node that runs a specific protocol. It is up to the protocol developer to configure how the nodes behave. In most cases, once the network is established, users will run specific protocols on the nodes. In the examples we see how to accomplish this.

One other feature of a host is that they can also sniff packets, or eavesdrop on channels to manipulate the payload of the packet. For example, they can apply random noise to any qubit or change the classical messages that are routed through them. We see an example of that in the examples section.

The most commonly used methods for Hosts are:

  • add_connection(host_id)
    • Add a classical and quantum connection to the host with id host_id

  • get_classical(host_id, wait=N)
    • Get a classical message from sender with host_id host_id and wait N seconds for it

  • get_qubit(host_id, wait=N):
    • Get a data qubit from sender with host_id host_id and wait N seconds for it

  • get_qubits(host_id):
    • Get all data qubits from sender with host_id host_id

  • get_epr(host_id, q_id=q_id):
    • Get EPR pair with qubit ID q_id from sender with host_id host_id. If q_id=None then get the first free EPR pair

  • get_epr_pairs(host_id):
    • Get all EPR pairs established with host with host_id host_id

  • send_classical(host_id, message, await_ack=<bool>):
    • Send the classical message message to host with host_id host_id. Block until ACK arrives if await_ack=True

  • send_key(host_id, key_size):
    • Send a secret key via QKD of length key_size to host with ID receiver_id.

  • send_qubit(host_id, qubit, await_ack=<bool>):
    • Send qubit qubit to host with host_id host_id. Block until ACK arrives if await_ack=True.

  • send_superdense(host_id, message, await_ack=<bool>):
    • Send a message (one of ‘00’, ‘01’, ‘10’, or ‘11’) as a superdense message

  • send_teleport(host_id, qubit, await_ack=<bool>):
    • Teleport qubit qubit to host with host_id host_id.

  • send_epr(host_id, qubit_id=<None>, await_ack=<bool>):
    • Creates a shared EPR pair with the host with host_id host_id.

  • send_ghz([host_id_list], qubit_id=<None>, await_ack=<bool>):
    • Creates a shared GHZ state with all hosts in the list [host_id_list].

  • shares_epr(host_id):
    • Returns if the host shares entanglement already with host with host_id host_id

  • run_protocol(protocol, protocol_params):
    • Run the function protocol with the parameters protocol_params.

class qunetsim.components.host.Host(host_id, backend=None)

Bases: object

Host object acting as either a router node or an application host node.

_get_message_w_seq_num(sender_id, seq_num, wait)

Get a message from a sender with a specific sequence number. :param sender_id: The ID of the sender :type sender_id: str :param seq_num: The sequence number :type seq_num: int :param wait: The amount of time to wait. (-1 to wait forever) :type wait: int

Returns

The message

Return type

(Message)

_log_ack(protocol, receiver, seq)

Logs acknowledgement messages. :param protocol: The protocol for the ACK :type protocol: str :param receiver: The sender of the ACK :type receiver: str :param seq: The sequence number of the packet :type seq: int

_process_ack(sender, seq_num)

Processes an ACK msg.

Parameters
  • sender (str) – The sender of the ack

  • seq_num (int) – The sequence number of the ack

_process_packet(packet)

Processes the received packet.

Parameters

packet (Packet) – The received packet

_process_queue()

Runs a thread for processing the packets in the packet queue.

add_c_connection(receiver_id)

Adds the classical connection to host with ID receiver_id.

Parameters

receiver_id (str) – The ID of the host to connect with.

add_c_connections(receiver_ids)

Adds the classical connections to host with ID receiver_id.

Parameters

receiver_ids (list) – The IDs of the hosts to connect with.

add_checksum(qubits, size_per_qubit=2)

Generate a set of qubits that represent a quantum checksum for the set of qubits qubits

Parameters
  • qubits (list) – 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

A list of qubits that are encoded for qubits

Return type

(list)

add_connection(receiver_id)

Adds the classical and quantum connection to host with ID receiver_id.

Parameters

receiver_id (str) – The ID of the host to connect with.

add_connections(receiver_ids)

Adds the classical and quantum connections to host with ID receiver_id.

Parameters

receiver_ids (list) – A list of receiver IDs to connect with

add_data_qubit(host_id, qubit, q_id=None)

Adds the data qubit to the data qubit store of a host. If the qubit has an ID, adds the qubit with it, otherwise generates an ID for the qubit and adds the qubit with that ID.

Parameters
  • host_id (str) – The ID of the host to pair the qubit

  • qubit (Qubit) – The data Qubit to be added.

  • q_id (str) – the ID to set the qubit ID to

Returns

The qubit ID

Return type

(str)

add_epr(host_id, qubit, q_id=None, blocked=False)

Adds the EPR to the EPR store of a host. If the EPR has an ID, adds the EPR with it, otherwise generates an ID for the EPR and adds the qubit with that ID.

Parameters
  • host_id (str) – The ID of the host to pair the qubit

  • qubit (Qubit) – The data Qubit to be added.

  • q_id (str) – The ID of the qubit to be added.

  • blocked (bool) – If the qubit should be stored as blocked or not

Returns

The qubit ID

Return type

(str)

add_ghz_qubit(host_id, qubit, q_id=None)

Adds the GHZ qubit to the storage of the host. The host id corresponds to the generator of the GHZ state.

Parameters
  • host_id (str) – The ID of the host to pair the qubit

  • qubit (Qubit) – The data Qubit to be added.

  • q_id (str) – the ID to set the qubit ID to

Returns

The qubit ID

Return type

(str)

add_q_connection(receiver_id)

Adds the quantum connection to host with ID receiver_id.

Parameters

receiver_id (str) – The ID of the host to connect with.

add_q_connections(receiver_ids)

Adds the quantum connection to host with ID receiver_id.

Parameters

receiver_ids (list) – The IDs of the hosts to connect with.

add_w_qubit(host_id, qubit, q_id=None)

Adds the W qubit to the storage of the host. The host id corresponds to the generator of the W state.

Parameters
  • host_id (str) – The ID of the host to pair the qubit

  • qubit (Qubit) – The data Qubit to be added.

  • q_id (str) – the ID to set the qubit ID to

Returns

The qubit ID

Return type

(str)

await_ack(sequence_number, sender)

Block until an ACK for packet with sequence number arrives.

Parameters
  • sequence_number (int) – The sequence number to wait for.

  • sender (str) – The sender of the ACK

Returns

The status of the ACK

Return type

(bool)

await_remaining_acks(sender)

Awaits all remaining ACKs of one sender.

Parameters

sender (str) – sender for which to wait for all acks.

property c_relay_sniffing

If the host should sniff classical packets.

Returns

If the host should sniff classical packets.

Return type

(bool)

property c_relay_sniffing_fn

The function to apply to the qubits in transit.

Returns

The function to apply to the qubits in transit.

Return type

(function)

change_epr_qubit_id(host_id, new_id, old_id=None)

Change an EPR pair ID to another. If old_id is set, then change that specific EPR half, otherwise change the first unblocked EPR half to the new_id.

Parameters
  • host_id (str) – The partner ID of the EPR pair.

  • new_id (str) – The new ID to change the qubit too

  • old_id (str) – The old ID of the qubit

Returns

Old if of the qubit which has been changed.

Return type

(str)

property classical

Gets the received classical messages sorted with the sequence number.

Returns

Sorted list of classical messages.

Return type

(list)

property classical_connections

Gets the classical connections of the host.

Returns

classical connections

Return type

(dict)

property delay

Get the delay of the queue processor.

Returns

The delay per tick for the queue processor.

Return type

(float)

delete_key(partner_id)

Deletes a key shared with a partner by QKD. Should be used if a key was eavesdropped and/or a new key should be shared. If there is no key shared, nothing happens.

Parameters

partner_id (str) – The ID of the key partner

empty_classical(reset_seq_nums=False)

Empty the classical message buffers.

Parameters

reset_seq_nums (bool) – if all sequence number should also be reset.

get_classical(host_id, seq_num=None, wait=0)

Get the classical messages from partner host host_id. If you need the next classical message from the host, don’t pass a seq_num, but use get_next_classical instead. This is much faster.

Parameters
  • host_id (str) – The ID of the partner who sent the clasical messages

  • seq_num (int) – The sequence number of the message

  • wait (float) – How long in seconds to wait for the messages if none are set.

Returns

A list of classical messages from Host with ID host_id.

Return type

(list)

get_connections()

Get a list of the connections with the types.

Returns

The list of connections for this host.

Return type

(list)

get_qubit(host_id, q_id=None, wait=0)

Gets the data qubit received from another host in the network. If qubit ID is specified, qubit with that ID is returned, else, the last qubit received is returned.

Parameters
  • host_id (str) – The ID of the host that data qubit to be returned is received from.

  • q_id (str) – The qubit ID of the data qubit to get.

  • wait (float) – The amount of time to wait for the a qubit to arrive

Returns

Qubit received from the host with host_id and q_id.

Return type

(Qubit)

get_qubits(host_id, remove_from_storage=False)

Return the dictionary of data qubits stored, just for the information regarding which qubits are stored. Optional to remove the qubits from storage like get_qubit does with remove_from_storage field.

Parameters
  • host_id (str) – The host id from which the data qubit have been received.

  • remove_from_storage (bool) – Get and remove from storage.

Returns

If host_id is not set, then return the entire dictionary of data qubits.

Else If host_id is set, then return the data qubits for that particular host if there are any. Return an empty list otherwise.

Return type

(dict)

get_epr(host_id, q_id=None, wait=0)

Gets the EPR that is entangled with another host in the network. If qubit ID is specified, EPR with that ID is returned, else, the last EPR added is returned.

Parameters
  • host_id (str) – The ID of the host that returned EPR is entangled to.

  • q_id (str) – The qubit ID of the EPR to get.

  • wait (float) – the amount of time to wait

Returns

Qubit shared with the host with host_id and q_id.

Return type

(Qubit)

get_epr_pairs(host_id)

Return the dictionary of EPR pairs stored, just for the information regarding which qubits are stored. Does not remove the qubits from storage like get_epr_pair does.

Parameters

host_id (str) – Get the EPR pairs established with host with host_id

Returns

If host_id is not set, then return the entire dictionary of EPR pairs.

Else If host_id is set, then return the EPRs for that particular host if there are any. Return an empty list otherwise.

Return type

(dict)

get_ghz(host_id, q_id=None, wait=0)

Gets the GHZ qubit which has been created by the host with the host ID host_id. It is not necessary to know with whom the states are shared.

Parameters
  • host_id (str) – The ID of the host that creates the GHZ state.

  • q_id (str) – The qubit ID of the GHZ to get.

  • wait (float) – the amount of time to wait

Returns

Qubit shared with the host with host_id and q_id.

Return type

(Qubit)

get_key(receiver_id, wait=-1)

Should be called after send_key is called. Blocks, till the key sharing is finished and returns the key and the amount of attemptes needed in QKD. From this value, the user can decide if the transmission was assumed safe or not.

Parameters
  • receiver_id (str) – The ID of the key partner

  • wait (float) – The amount to wait for the key, -1 to wait forever.

Returns

The key, as a list of ints, and the amount of attempts needed

Return type

(key, int)

get_next_classical(sender_id, wait=-1)

Gets the next classical message available from a sender. If wait is -1 (default), it is waited till a message arrives.

Parameters
  • sender_id (str) – ID of the sender from the returned message.

  • wait (int) – waiting time, default forever.

Returns

The message or None

Return type

(str)

get_next_sequence_number(host)

Get and set the next sequence number of connection with a receiver.

Parameters

host (str) – The ID of the receiver

Returns

The next sequence number of connection with a receiver.

Return type

(int)

get_number_of_data_qubits(host_id)

Return the number of data qubits associated with host host_id.

Parameters

host_id (str) –

Returns

Return type

(int)

get_qubit_by_id(q_id)

Return the qubit that has the id q_id from the quantum storage.

Parameters

q_id (str) – The ID of the qubit

Returns

The qubit with the id q_id or None if it does not exist

Return type

(Qubit)

get_sequence_number(host)

Get the sequence number on the sending side of connection with a host host.

Parameters

host (str) – The ID of the sender

Returns

The next sequence number of connection with a receiver.

Return type

(int)

get_sequence_number_receiver(host)

Get the sequence number on the receiving side of the connection with host host.

Parameters

host (str) – The ID of the connected host

Returns

The receiver sequence number.

Return type

(int)

get_w(host_id, q_id=None, wait=0)

Gets the W qubit which has been created by the host with the host ID host_id. It is not necessary to know with whom the states are shared.

Parameters
  • host_id (str) – The ID of the host that creates the GHZ state.

  • q_id (str) – The qubit ID of the W to get.

  • wait (float) – the amount of time to wait

Returns

Qubit shared with the host with host_id and q_id.

Return type

(Qubit)

property host_id

Get the host_id of the host.

Returns

The host ID of the host.

Return type

(str)

is_idle()

Returns if the host has packets to process or is idle.

Returns

If the host is idle or not.

Return type

(bool)

property max_ack_wait

Get the maximum amount of time to wait for an ACK

Returns

The maximum amount of time to wait for an ACK

Return type

(float)

property q_relay_sniffing

If the host should sniff quantum packets.

Returns

If the host should sniff quantum packets.

Return type

(bool)

property q_relay_sniffing_fn

The function to apply to the qubits in transit.

Returns

The function to apply to the qubits in transit.

Return type

(function)

property quantum_connections

Get the quantum connections for the host.

Returns

The quantum connections for the host.

Return type

(dict)

quantum_relay_sniffing_function(sender, receiver, qubit)

Calls the quantum relay sniffing function if one is set.

rec_packet(packet)

Puts the packet into the packet queue of the host.

Parameters

packet – Received packet.

relay_sniffing_function(sender, receiver, transport_packet)

The function called for packet sniffing.

Parameters
  • sender (str) – the sender of the packet

  • receiver (str) – the receiver of the packet

  • transport_packet (Packet) – the packet itself

remove_c_connection(receiver_id)

Remove the classical connection with receiver with receiver ID receiver_id. :param receiver_id: The ID of the receiving side of the classical connection :type receiver_id: str

Returns

Success status of the removal

Return type

(bool)

remove_connection(receiver_id)

Remove a classical and quantum connection from a host. :param receiver_id: The ID of the connection to remove :type receiver_id: str

Returns

a two element list of the status of the removals.

Return type

(list)

remove_q_connection(receiver_id)

Remove the quantum connection with receiver with receiver ID receiver_id. :param receiver_id: The ID of the receiving side of the quantum connection :type receiver_id: str

Returns

Success status of the removal

Return type

(bool)

reset_data_qubits(host_id=None)

Remove all qubits associated with host_id else remove all qubits if host_id not set.

Parameters

host_id (str) – The host ID to remove associated qubits with.

reset_sequence_numbers()

Reset all sequence numbers.

run_protocol(protocol, arguments=(), blocking=False)

Run the protocol protocol.

Parameters
  • protocol (function) – The protocol that the host should run.

  • arguments (tuple) – The set of (ordered) arguments for the protocol

  • blocking (bool) – Wait for thread to stop before proceeding

Returns

The thread the protocol is running on

Return type

(DaemonThread)

send_ack(receiver, seq_number)

Sends the classical message to the receiver host with ID:receiver

Parameters
  • receiver (str) – The ID of the host to send the message.

  • seq_number (int) – Sequence number of the acknowleged packet.

send_broadcast(message)

Send a broadcast message to all of the network.

Parameters

message (str) – The message to broadcast

send_classical(receiver_id, message, await_ack=False, no_ack=False)

Sends the classical message to the receiver host with ID:receiver

Parameters
  • receiver_id (str) – The ID of the host to send the message.

  • message (str) – The classical message to send.

  • await_ack (bool) – If sender should wait for an ACK.

  • no_ack (bool) – If this message should not use any ACK and sequencing.

Returns

(bool) If await_ack=True, return the status of the ACK

send_epr(receiver_id, q_id=None, await_ack=False, no_ack=False, block=False)

Establish an EPR pair with the receiver and return the qubit ID of pair.

Parameters
  • receiver_id (str) – The receiver ID

  • q_id (str) – The ID of the qubit

  • await_ack (bool) – If sender should wait for an ACK.

  • no_ack (bool) – If this message should not use any ACK and sequencing.

  • block (bool) – If the created EPR pair should be blocked or not.

Returns

If await_ack=True, return the ID of the EPR pair and the status of the ACK

Return type

(str, bool)

send_ghz(receiver_list, q_id=None, await_ack=False, no_ack=False, distribute=False)

Share GHZ state with all receiver ids in the list. GHZ state is generated locally.

Parameters
  • receiver_list (list) – A List of all Host IDs with which a GHZ state should be shared.

  • q_id (str) – The ID of the GHZ qubits

  • await_ack (bool) – If the sender should await an ACK from all receivers

  • no_ack (bool) – If this message should not use any ACK and sequencing.

  • distribute (bool) – If the sender should keep part of the GHZ state, or just distribute one

Returns

Qubit ID of the shared GHZ and ACK status

Return type

(str, bool)

send_key(receiver_id, key_size, await_ack=True)

Send a secret key via QKD of length key_size to host with ID receiver_id. The ACK is returned before the QKD protocol is completley finished!

Parameters
  • receiver_id (str) – The ID of the receiver

  • key_size (int) – The size of the key

  • await_ack (bool) – If the host should wait for an ACk

Returns

Status of ACK, returned at the beginning of the protocol

Return type

(bool)

send_qubit(receiver_id, q, await_ack=False, no_ack=False)

Send the qubit q to the receiver with ID receiver_id.

Parameters
  • receiver_id (str) – The receiver ID to send the message to

  • q (Qubit) – The qubit to send

  • await_ack (bool) – If sender should wait for an ACK.

  • no_ack (bool) – If this message should not use any ACK and sequencing.

Returns

If await_ack=True, return the ID of the qubit and the status of the ACK

Return type

(str, bool)

send_superdense(receiver_id, message, await_ack=False, no_ack=False)

Send the two bit binary (i.e. ‘00’, ‘01’, ‘10’, ‘11) message via superdense coding to the receiver with receiver ID receiver_id.

Parameters
  • receiver_id (str) – The receiver ID to send the message to

  • message (str) – The two bit binary message

  • await_ack (bool) – If sender should wait for an ACK.

  • no_ack (bool) – If this message should not use any ACK and sequencing.

Returns

(bool) If await_ack=True, return the status of the ACK

send_teleport(receiver_id, q, await_ack=False, no_ack=False, payload=None, generate_epr_if_none=True)

Teleports the qubit q with the receiver with host ID receiver

Parameters
  • receiver_id (str) – The ID of the host to establish the EPR pair with

  • q (Qubit) – The qubit to teleport

  • await_ack (bool) – If sender should wait for an ACK.

  • no_ack (bool) – If this message should not use any ACK and sequencing.

  • payload

  • generate_epr_if_none – Generate an EPR pair with receiver if one doesn’t exist

Returns

(bool) If await_ack=True, return the status of the ACK

send_w(receiver_list, q_id=None, await_ack=False, no_ack=False, distribute=False)

Share W state with all receiver ids in the list. W state is generated locally.

Parameters
  • receiver_list (list) – A List of all Host IDs with which a W state should be shared.

  • q_id (str) – The ID of the W qubits

  • await_ack (bool) – If the sender should await an ACK from all receivers

  • no_ack (bool) – If this message should not use any ACK and sequencing.

  • distribute (bool) – If the sender should keep part of the W state, or just distribute one

Returns

Qubit ID of the shared W and ACK status

Return type

(str, bool)

set_data_qubit_memory_limit(limit, host_id=None)

Set the limit to how many data qubits can be stored from host_id, or if host_id is not set, use the limit for all connections.

Parameters
  • limit (int) – The maximum number of qubits for the memory

  • host_id (str) – (optional) The partner ID to set the limit with

set_epr_memory_limit(limit, host_id=None)

Set the limit to how many EPR pair halves can be stored from host_id, or if host_id is not set, use the limit for all connections.

Parameters
  • limit (int) – The maximum number of qubits for the memory

  • host_id (str) – (optional) The partner ID to set the limit with

shares_epr(receiver_id)

Returns boolean value dependent on if the host shares an EPR pair with receiver with ID receiver_id

Parameters

receiver_id (str) – The receiver ID to check.

Returns

Whether the host shares an EPR pair with receiver with ID receiver_id

Return type

(bool)

property sniff_full_packet

If the eavesdropper should get the whole packet or just the payload.

Returns

If the eavesdropper should get the whole packet or just the

payload.

Return type

(bool)

start()

Starts the host.

stop(release_qubits=True)

Stops the host. If release_qubit is true, clear the quantum memories.

Parameters

(boolean) – If release_qubit is true, clear the quantum memories.

property storage_epr_limit

Get the maximum number of qubits that can be held in EPR memory.

Returns

The maximum number of qubits that can be held in EPR memory.

Return type

(int)

property storage_limit

Get the maximum number of qubits that can be held in data qubit memory.

Returns

The maximum number of qubits that can be held in data qubit memory.

Return type

(int)

qunetsim.components.host._get_qubit(store, host_id, q_id, purpose, wait=0)

Gets the data qubit received from another host in the network. If qubit ID is specified, qubit with that ID is returned, else, the last qubit received is returned.

Parameters
  • store – The qubit storage to retrieve the qubit

  • host_id (str) – The ID of the host that data qubit to be returned is received from.

  • q_id (str) – The qubit ID of the data qubit to get.

  • purpose (str) – The intended use of the qubit

Returns

Qubit received from the host with host_id and q_id.

Return type

(Qubit)