qns.entity package

Subpackages

Submodules

qns.entity.entity module

class qns.entity.entity.Entity(name: str = None)[source]

Bases: object

This is the basic entity class, including memories, channels and nodes.

handle(event: Event) None[source]

handle is called to process an receiving Event.

Parameters:

event (qns.simulator.event.Event) – the event that send to this entity

install(simulator: Simulator) None[source]

install is called before simulator runs to initialize or set initial events

Parameters:

simulator (qns.simulator.simulator.Simulator) – the simulator

Module contents

class qns.entity.Application[source]

Bases: object

Application can be deployed on the quantum nodes.

add_handler(handler, EventTypeList: List = [], ByList: List = [])[source]

Add a handler function to the dispather.

Parameters:
  • handler – The handler to process the event. It is an object method whose function signature is the same to handle function.

  • EventTypeList – a list of Event Class Type. An empty list meaning to match all events.

  • ByList – a list of Entities, QNodes or Applications, that generates this event. An empty list meaning to match all entities.

get_node()[source]

get the node that runs this application

Returns:

the quantum node

get_simulator()[source]

get the simulator

Returns:

the simulator

handle(node, event: Event) bool | None[source]

process the event on the node.

Parameters:
  • node (QNode) – the node that will handle this event

  • event (Event) – the event

Returns:

if skip is True, further applications will not handle this event

Return type:

skip (bool, None)

install(node, simulator: Simulator)[source]

install initial events for this QNode

Parameters:
  • node (QNode) – the node that will handle this event

  • simulator (Simulator) – the simulator

class qns.entity.ClassicChannel(name: str = None, node_list: List[QNode] = [], bandwidth: int = 0, delay: float | DelayModel = 0, length: float | None = 0, drop_rate: float = 0, max_buffer_size: int = 0)[source]

Bases: Entity

ClassicChannel is the channel for classic message

install(simulator: Simulator) None[source]

install is called before simulator runs to initialize or set initial events

Parameters:

simulator (qns.simulator.simulator.Simulator) – the simulator

send(packet: ClassicPacket, next_hop: QNode)[source]

Send a classic packet to the next_hop

Parameters:
Raises:

qns.entity.cchannel.cchannel.NextHopNotConnectionException – the next_hop is not connected to this channel

class qns.entity.ClassicPacket(msg: str | bytes | Any, src: QNode = None, dest: QNode = None)[source]

Bases: object

ClassicPacket is the message that transfer on a ClassicChannel

encode() bytes[source]

encode the self.msg if it is a str

Returns:

(bytes) a bytes object

get()[source]

get the message from packet

Returns:

(Union[str, bytes, Any])

class qns.entity.Entity(name: str = None)[source]

Bases: object

This is the basic entity class, including memories, channels and nodes.

handle(event: Event) None[source]

handle is called to process an receiving Event.

Parameters:

event (qns.simulator.event.Event) – the event that send to this entity

install(simulator: Simulator) None[source]

install is called before simulator runs to initialize or set initial events

Parameters:

simulator (qns.simulator.simulator.Simulator) – the simulator

Bases: QuantumChannel

Link_Decoherence_QuantumChannel is the channel in which the fidelity of entangled pairs decoherence within the quantum memory.

Creat entangled pairs.

Find the entanglement pair whose fidelity is equal to the specified fidelity.

Parameters:

fidelity – the specified fidelity.

Find the entanglement pairs with maximum and minimum fidelity.

Remove the entanglement pair whose fidelity is equal to the specified fidelity.

Remove the entanglement pair whose fidelity is equal to the specified index.

class qns.entity.MemoryReadRequestEvent(memory, key: QuantumModel | str, t: Time | None = None, name: str | None = None, by: Any | None = None)[source]

Bases: Event

MemoryReadRequestEvent is the event that request a memory read

invoke() None[source]

Invoke the event, should be implemented

class qns.entity.MemoryReadResponseEvent(node: QNode, result: QuantumModel | None = None, request: MemoryReadRequestEvent = None, t: Time | None = None, name: str | None = None, by: Any | None = None)[source]

Bases: Event

MemoryReadResponseEvent is the event that returns the memory read result

invoke() None[source]

Invoke the event, should be implemented

class qns.entity.MemoryWriteRequestEvent(memory, qubit: QuantumModel, t: Time | None = None, name: str | None = None, by: Any | None = None)[source]

Bases: Event

MemoryWriteRequestEvent is the event that request a memory write

invoke() None[source]

Invoke the event, should be implemented

class qns.entity.MemoryWriteResponseEvent(node: QNode, result: QuantumModel | None = None, request: MemoryReadRequestEvent = None, t: Time | None = None, name: str | None = None, by: Any | None = None)[source]

Bases: Event

MemoryWriteResponseEvent is the event that returns the memory write result

invoke() None[source]

Invoke the event, should be implemented

class qns.entity.Monitor(name: str | None = None, network=None)[source]

Bases: Entity

add_attribution(name: str, calculate_func: Callable[[Simulator, Any, Event | None], Any]) None[source]

Set an attribution that will be recorded. For example, an attribution could be the throughput, or the fidelity.

Parameters:
  • name (str) – the column’s name, e.g., fidelity, throughput, time …

  • calculate_func (Callable[[Simulator, Optional[QuantumNetwork], Optional[Event]]) – a function to calculate the value, it has three input parameters (Simulator, QuantumNetwork, Event), and it returns the value.

Usage:

m = Monitor()

# record the event happening time m.add_attribution(“time”, lambda s,n,e: e.t)

# get the ‘name’ attribution of the last node m.add_attribution(“count”, lambda s,network,e: network.nodes[-1].name)

at_event(event_type) None[source]

Watch network status whenever the event happends

Parameters:

event_type (Event) – the watching event

Usage:

# record network status when a node receives a qubit m.at_event(RecvQubitPacket)

at_finish() None[source]

Watch the final status after the simulation.

Usage:

m.at_finish()

at_period(period_time: float) None[source]

Watch network status at a constant period.

Parameters:

period_time (float) – the period of watching network status [s]

Usage:

# record network status every 3 seconds. m.at_period(3)

at_start() None[source]

Watch the initial status before the simulation starts.

Usage:

m.at_start()

calculate_date(event: Event)[source]
get_date()[source]

Get the collected data.

Returns:

the collected data, as a pd.DataFrame.

handle(event: Event) None[source]

handle is called to process an receiving Event.

Parameters:

event (qns.simulator.event.Event) – the event that send to this entity

install(simulator: Simulator) None[source]

install is called before simulator runs to initialize or set initial events

Parameters:

simulator (qns.simulator.simulator.Simulator) – the simulator

class qns.entity.MonitorEvent(t: Time | None, monitor, name: str | None = None, by: Any | None = None)[source]

Bases: Event

the event that notify the monitor to write down network status

invoke() None[source]

Invoke the event, should be implemented

class qns.entity.OperateRequestEvent(operator, qubits: List[QuantumModel] = [], t: Time | None = None, name: str | None = None, by: Any | None = None)[source]

Bases: Event

OperateRequestEvent is the event that request a operator to handle

invoke() None[source]

Invoke the event, should be implemented

class qns.entity.OperateResponseEvent(node: QNode, result: int | List[int] = None, request: OperateRequestEvent = None, t: Time | None = None, name: str | None = None, by: Any | None = None)[source]

Bases: Event

OperateResponseEvent is the event that returns the operating result

invoke() None[source]

Invoke the event, should be implemented

class qns.entity.QNode(name: str = None, apps: List[Application] = None)[source]

Bases: Entity

QNode is a quantum node in the quantum network

add_apps(app: Application)[source]

Insert an Application into the app list.

Parameters:

app (Application) – the inserting application.

add_cchannel(cchannel)[source]

Add a classic channel in this QNode

Parameters:

cchannel (ClassicChannel) – the classic channel

add_memory(memory)[source]

Add a quantum memory in this QNode

Parameters:

memory (Memory) – the quantum memory

add_network(network)[source]

add a network object to this node

Parameters:

network (qns.network.network.Network) – the network object

add_operator(operator)[source]

Add a quantum operator in this node

Parameters:

operator (QuantumOperator) – the quantum operator

add_qchannel(qchannel)[source]

Add a quantum channel in this QNode

Parameters:

qchannel (QuantumChannel) – the quantum channel

add_request(request)[source]

add a request to this node

Parameters:

request (Request) – the inserting request

clear_request()[source]

clear all requests

get_apps(app_type)[source]

Get an Application that is app_type

Parameters:

app_type – the class of app_type

get_cchannel(dst: QNode)[source]

Get the classic channel that connects to the dst

Parameters:

dst (QNode) – the destination

get_memory(memory: str | int)[source]

Get the memory by index (in memories) or its name

Parameters:

memory (Union[str, int]) – the index or name of the memory

get_qchannel(dst: QNode)[source]

Get the quantum channel that connects to the dst

Parameters:

dst (QNode) – the destination

handle(event: Event) None[source]

This function will handle an Event. This event will be passed to every applications in apps list in order.

Parameters:

event (Event) – the event that happens on this QNode

install(simulator: Simulator) None[source]

install is called before simulator runs to initialize or set initial events

Parameters:

simulator (qns.simulator.simulator.Simulator) – the simulator

class qns.entity.QuantumChannel(name: str = None, node_list: List[QNode] = [], fidelity: float = 0.8, bandwidth: int = 1, delay: float | DelayModel = 0, drop_rate: float = 0, max_buffer_size: int = 0, length: float = 0, decoherence_rate: float | None = 0, transfer_error_model_args: dict = {})[source]

Bases: Entity

QuantumChannel is the channel for transmitting qubit

install(simulator: Simulator) None[source]

install is called before simulator runs to initialize or set initial events

Parameters:

simulator (Simulator) – the simulator

send(qubit: QuantumModel, next_hop: QNode)[source]

Send a qubit to the next_hop

Parameters:
  • qubit (QuantumModel) – the transmitting qubit

  • next_hop (QNode) – the next hop QNode

Raises:

NextHopNotConnectionException – the next_hop is not connected to this channel

class qns.entity.QuantumMemory(name: str = None, node: QNode = None, capacity: int = 0, decoherence_rate: float | None = 0, store_error_model_args: dict = {}, delay: float | DelayModel = 0)[source]

Bases: Entity

Quantum memory stores qubits or entangled pairs.

It has two modes:

Synchronous mode, users can use the read and write function to operate the memory directly without delay Asynchronous mode, users can use events to operate memories asynchronously

property count: int

return the current memory usage

get(key: QuantumModel | str | int) QuantumModel | None[source]

get a qubit from the memory but without removing it from the memory

Parameters:

key (Union[QuantumModel, str, int]) – the key. It can be a QuantumModel object, its name or the index number.

get_store_time(key: QuantumModel | str | int) QuantumModel | None[source]

get the store time of a qubit from the memory

Parameters:

key (Union[QuantumModel, str, int]) – the key. It can be a QuantumModel object, its name or the index number.

handle(event: Event) None[source]

handle is called to process an receiving Event.

Parameters:

event (qns.simulator.event.Event) – the event that send to this entity

install(simulator: Simulator) None[source]

install is called before simulator runs to initialize or set initial events

Parameters:

simulator (qns.simulator.simulator.Simulator) – the simulator

is_full() bool[source]

check whether the memory is full

read(key: QuantumModel | str) QuantumModel | None[source]

The API for reading a qubit from the memory

Parameters:

key (Union[QuantumModel, str]) – the key. It can be a QuantumModel object, its name or the index number.

write(qm: QuantumModel) bool[source]

The API for storing a qubit to the memory

Parameters:

qm (QuantumModel) – the QuantumModel, could be a qubit or an entangled pair

Returns:

whether the qubit is stored successfully

Return type:

bool

class qns.entity.QuantumOperator(name: str = None, node: QNode = None, gate: Callable[[...], None | int | List[int]] = None, delay: float | DelayModel = 0)[source]

Bases: Entity

Quantum operator can perfrom quantum operation or measurements on qubits. It has two modes:

Synchronous mode, users can use the operate function to operate qubits directly without delay Asynchronous mode, users will use events to operate quantum operations asynchronously

handle(event: Event) None[source]

handle is called to process an receiving Event.

Parameters:

event (qns.simulator.event.Event) – the event that send to this entity

install(simulator: Simulator) None[source]

install is called before simulator runs to initialize or set initial events

Parameters:

simulator (qns.simulator.simulator.Simulator) – the simulator

operate(*qubits) int | List[int] | None[source]

operate on qubits and return the measure result

Parameters:

qubits – the operating qubits

Returns:

the measure result

set_own(node: QNode)[source]

set the owner of this quantum operator

class qns.entity.RecvClassicPacket(t: Time | None = None, name: str | None = None, cchannel: ClassicChannel = None, packet: ClassicPacket = None, dest: QNode = None, by: Any | None = None)[source]

Bases: Event

The event for a QNode to receive a classic packet

invoke() None[source]

Invoke the event, should be implemented

class qns.entity.RecvQubitPacket(t: Time | None = None, qchannel: QuantumChannel = None, qubit: QuantumModel = None, dest: QNode = None, name: str | None = None, by: Any | None = None)[source]

Bases: Event

The event for a QNode to receive a classic packet

invoke() None[source]

Invoke the event, should be implemented

class qns.entity.Timer(name: str, start_time: float, end_time: float = 0, step_time: float = 1, trigger_func=None)[source]

Bases: Entity

A Timer is an Entity that trigger the function trigger_func one-shot or periodically.

install(simulator: Simulator) None[source]

install is called before simulator runs to initialize or set initial events

Parameters:

simulator (qns.simulator.simulator.Simulator) – the simulator

trigger()[source]