simulaqron.netqasm_backend package¶
Submodules¶
simulaqron.netqasm_backend.executioner module¶
- class simulaqron.netqasm_backend.executioner.NetworkStack(executioner)[source]¶
Bases:
BaseNetworkStackThis is just a wrapper around the executioners methods for entanglement generation in order to use the correct framework as used by the netqasm executioner.
Warning
This class is candidate to be deleted! Test and delete if not needed!
- exception simulaqron.netqasm_backend.executioner.UnknownQubitError[source]¶
Bases:
RuntimeErrorRaised when the requested qubit ID could not be found.
- class simulaqron.netqasm_backend.executioner.VanillaSimulaQronExecutioner(*args, **kwargs)[source]¶
Bases:
ExecutorCreates a class that is capable of executing some “vanilla” NetQASM instructions using the SimulaQron simulator. This is the main class that bridges the “QNodeOS” world with the SimulaQron simulator. Every instruction of the NetQASM subroutine is sent to an instance of this class, so the “QNodeOS” server knows how to interact with SimulaQron’s Virtual Node to command the execution of the NetQASM instructions. To this end, this class implements the
netqasm.executor.backend.Executorclass, overwriting the methods that are invoked by the NetQASM library when executing a subroutine.- Parameters:
args (Any) – Arguments directly passed to the NetQASM
Executorconstructor.kwargs (Any) – Keyword arguments directly passed to the NetQASM
Executorconstructor.
- ROTATION_AXIS = {<class 'netqasm.lang.instr.vanilla.RotXInstruction'>: (1, 0, 0), <class 'netqasm.lang.instr.vanilla.RotYInstruction'>: (0, 1, 0), <class 'netqasm.lang.instr.vanilla.RotZInstruction'>: (0, 0, 1)}¶
- SIMULAQRON_OPS = {<class 'netqasm.lang.instr.vanilla.GateXInstruction'>: 'apply_X', <class 'netqasm.lang.instr.vanilla.GateYInstruction'>: 'apply_Y', <class 'netqasm.lang.instr.vanilla.GateZInstruction'>: 'apply_Z', <class 'netqasm.lang.instr.vanilla.GateHInstruction'>: 'apply_H', <class 'netqasm.lang.instr.vanilla.GateSInstruction'>: 'apply_S', <class 'netqasm.lang.instr.vanilla.GateKInstruction'>: 'apply_K', <class 'netqasm.lang.instr.vanilla.GateTInstruction'>: 'apply_T', <class 'netqasm.lang.instr.vanilla.CnotInstruction'>: 'cnot_onto', <class 'netqasm.lang.instr.vanilla.CphaseInstruction'>: 'cphase_onto'}¶
- add_factory(factory: NetQASMFactory)[source]¶
Sets the factory object used in the connection.
- Parameters:
factory (NetQASMFactory) – The factory object
- add_return_msg_func(func: Callable)[source]¶
Sets the function invoked for returning a message back to the client
- Parameters:
func (Callable) – The function tobe used to return a message back to the client.
- apply_rotation(axis: Tuple[int, int, int], angle: float, qubit_id: int)[source]¶
Executes a rotation of
angleradians around theaxisaxis of the specified qubit ID.- Parameters:
axis (Tuple[int, int, int]) – The axis to rotate around. This axis is expected to be a tuple of integers in the format (x, y, z).
angle (float) – The angle to rotate around. This angle should be in radians.
qubit_id (int) – The qubit to apply the rotation on.
- apply_single_qubit_gate(gate: SingleQubitInstruction, qubit_id: int)[source]¶
Applies the given single-qubit gate to the specified physical qubit ID.
- Parameters:
gate (core_instructions.SingleQubitInstruction) – The single-qubit gate to apply.
qubit_id (int) – The qubit ID of the qubit to use.
- apply_two_qubit_gate(gate: TwoQubitInstruction, qubit_id1: int, qubit_id2: int)[source]¶
Applies the given two-qubits gate to the physical qubit qubit_id1 and qubit_id2.
- Parameters:
gate (core_instructions.TwoQubitInstruction) – The two-qubit gate to apply.
qubit_id1 (int) – The qubit ID of the first qubit to use.
qubit_id2 (int) – The qubit ID of the second qubit to use.
- cmd_epr(create_id: int, remote_node_id: int, epr_socket_id: int, remote_epr_socket_id: int, qubit_id: int, create_request: LinkLayerCreate)[source]¶
Create EPR pair with another node. Depending on the IPs and ports this will either create an EPR-pair and send one part, or just receive.
- Parameters:
create_id (int) – The create ID.
remote_node_id (int) – The remote node ID.
epr_socket_id (int) – The EPR socket ID.
remote_epr_socket_id (int) – The remote EPR socket ID.
qubit_id (int) – The qubit ID.
create_request (LinkLayerCreate) – The
LinkLayerCreateobject with the EPR information.
- cmd_epr_recv(epr_socket_id: int, qubit_id: int)[source]¶
Receive half of epr from another node. Block until qubit is received.
- Parameters:
epr_socket_id (int) – The EPR socket ID.
qubit_id (int) – The qubit ID.
- cmd_measure(qubit_id: int, inplace=True)[source]¶
Executes a measure on the given qubit ID.
- Parameters:
qubit_id (int) – The qubit ID to execute measure on.
inplace (bool) – If True, execute the measurement inplace.
- cmd_new(physical_address: int)[source]¶
Request a new qubit. Since we don’t need it, this python NetQASM just provides very crude timing information (return_q_id is used internally). Additionally, ignore_max_qubits is used internally to ignore the check of number of virtual qubits at the node such that the node can temporarily create a qubit for EPR creation.
- Parameters:
physical_address (int) – The physical address of the qubit to be created.
- cmd_reset(qubit_id: int, correct: bool = True)[source]¶
Reset the given qubit to the state \(|0>\).
- Parameters:
qubit_id (int) – The qubit ID to reset.
correct (bool) – If True, apply a correction to ensure the qubit was reset.
- property factory: NetQASMFactory¶
The NetQASM Factory associated with this executioner.
- static get_error_class(remote_err)[source]¶
This is a function to get the error class of a remote thrown error when using callRemote. .. warning:: This method is candidate to be deleted.
- Parameters:
remote_err –
twisted.spread.pb.RemoteError- Returns:
class
- get_qubit_state(qubit_id: int) Generator[Deferred, Tuple[List[float], List[float]], Tuple[List[float], List[float]]][source]¶
Retrieves the state of the given qubit ID as a real and imaginary part.
- Parameters:
qubit_id (int) – The qubit ID to retrieve the state.
- Returns:
A tuple containing 2 lists of floats, representing the real and imaginary part.
- Return type:
Generator[Deferred, Tuple[List[float], List[float]], Tuple[List[float], List[float]]]
- get_virt_qubit(qubit_id: int) Referenceable[source]¶
Get reference to the virtual qubit reference in SimulaQron given app and qubit id, if it exists. If not found, raises a
UnknownQubitError.Caution
Twisted PB does not allow references to objects to be passed back between connections.
If you need to pass a qubit reference back to the Twisted PB on a _different_ connection, then use get_virt_qubit_indep below.
- Parameters:
qubit_id (int) – The qubit to get reference to.
- Returns:
The virtual qubit reference as a
twisted.spread.flavors.Referenceableobject.- Return type:
twisted.spread.flavors.Referenceable
- get_virt_qubit_num(qubit_id: int)[source]¶
Get the integer qubit ID to virtual qubit in SimulaQron specific to this connection.
Caution
This method return a qubit ID (an integer), not a
twisted.spread.flavors.Referenceableobject. If you need to get a twisted object, checkget_virt_qubit().- Parameters:
qubit_id (int) – The qubit ID to get virtual qubit ID.
- classmethod new_ent_id(epr_socket_id: int, remote_node_id: int, remote_epr_socket_id: int)[source]¶
Returns a new unique entanglement id for the specified host_app_id, remote_node and remote_app_id. Used by cmd_epr.
- Parameters:
epr_socket_id (int) – The EPR socket ID.
remote_epr_socket_id (int) – The remote EPR socket ID.
remote_node_id (int) – The remote node ID.
- property node_id¶
Returns the ID of the node on which this simulation runs on.
- remove_qubit_id(qubit_id: int)[source]¶
Removes the qubit ID from the NetQASMFactory object.
- Parameters:
qubit_id (int) – The qubit ID to remove.
- send_epr_half(qubit_id: int, epr_socket_id: int, remote_node_name: str, remote_epr_socket_id: int, ent_info: LinkLayerOKTypeK)[source]¶
Send qubit to another node.
- Parameters:
qubit_id (int) – The qubit ID.
epr_socket_id (int) – The EPR socket ID.
remote_node_name (str) – The remote node name.
remote_epr_socket_id (int) – The remote EPR socket ID.
ent_info (LinkLayerOKTypeK) – The local entanglement information.
- send_epr_outcome_half(epr_socket_id: int, remote_node_name: str, remote_epr_socket_id: int, ent_info: LinkLayerOKTypeM, remote_outcome, remote_basis: Basis)[source]¶
Send outcome from measure directly to another node.
:param epr_socket_id:The EPR socket ID. :type epr_socket_id: int :param remote_node_name: The remote node name. :type remote_node_name: str :param remote_epr_socket_id: The remote EPR socket ID. :type remote_epr_socket_id: int :param ent_info: The entanglement info objects. :type ent_info: LinkLayerOKTypeM :param remote_outcome: The outcome of the remote measurement. :type remote_outcome: :param remote_basis: The base on which the remote qubit was mesured. :type remote_basis: Basis
- class simulaqron.netqasm_backend.executioner.VirtualQubitRef(qubit_id: int, timestamp: int, virt: Referenceable)[source]¶
Bases:
objectReference to a Virtual Qubit.
- Parameters:
qubit_id (int) – The qubit ID to reference.
timestamp (int) – A timestamp.
virt (pb.Referenceable) – The Referenceable object pointing to the virtual qubit.
simulaqron.netqasm_backend.factory module¶
- exception simulaqron.netqasm_backend.factory.IncompleteMessageError[source]¶
Bases:
ValueErrorRaised when trying to parse an incomplete NetQASM message.
- class simulaqron.netqasm_backend.factory.NetQASMFactory(host: Host, name: str, qnodeos_net: SocketsConfig, backend: Type[SubroutineHandler], network_name: str = 'default')[source]¶
Bases:
FactoryFactory class that creates
NetQASMProtocolobjects. This factory will create one protocol instance per connection.This factory is handled by the internals of the twisted reactor, and should not be instantiated by the user.
- Parameters:
host (str) – The hostname to listen to new connections.
name (str) – A name for this protocol factory.
qnodeos_net (SocketsConfig) – The
SocketsConfigobject containing the QNodeOS sockets specifications.backend (Type[SubroutineHandler]) – The class of QNodeOS subroutine handlers that the protocol will forward messages to.
network_name (str) – The name of the network to handle messages from.
- is_adjacent(remote_host_name: str)[source]¶
Checks if remote host is adjacent to this node, according to the specified topology.
- Parameters:
remote_host_name (str) – The name of the remote host
- lookup(ip: int, port: int) str | None[source]¶
Lookup name of remote host used within SimulaQron given ip and port number. :param ip: The IP address to look for. This value needs to be transformed into an integer value. :param port: The port to look for. :return: The name of the node that matches the IP-port pair. None if none matches. :rtype: str | None
- class simulaqron.netqasm_backend.factory.NetQASMProtocol(factory: NetQASMFactory)[source]¶
Bases:
ProtocolImplementation of a
twisted.internet.protocol.Protocolclass that handles the connection to a server capable of handling NetQASM messages.This class is the entry point when a new NetQASM message is received. It is also responsible for calling (in a “twisted deferred” way) the message handlers that implement the logic of handling and executing the message.
- Parameters:
factory (NetQASMFactory) – The
NetQASMFactoryobject that handles this object.
- connectionLost(reason=<twisted.python.failure.Failure twisted.internet.error.ConnectionDone: Connection was closed cleanly.>)[source]¶
Implementation of the
connectionLostmethod from thetwisted.internet.protocol.Protocolclass.
- connectionMade()[source]¶
Implementation of the
connectionMademethod from thetwisted.internet.protocol.Protocolclass.
- dataReceived(data: bytes)[source]¶
We will always wait to receive enough data for the header, and then the entire packet first before commencing processing.
This method is the first entry point when the SimulaQron QNodeOS server receives a NetQASM message. Once any data is received, it attaches it to an internal buffer, and tries to parse a NetQASM message. If a message cannot be parsed, the raw data will be stored and this method will return, waiting form more data. It a message can be parsed, it will be for further processing to the message handler (usually the :py:class:´SubroutineHandler´ class).
- Parameters:
data (bytes) – The data received from the remote.
simulaqron.netqasm_backend.qnodeos module¶
- class simulaqron.netqasm_backend.qnodeos.SubroutineHandler(factory: NetQASMFactory, instr_log_dir: str | None = None, flavour: Flavour | None = None)[source]¶
Bases:
QNodeControllerClass that handles the NetQASM messages and bridges the NetQASM with the SimulaQron world.
The main responsibility of this class is to “transform” the native python generators (used by the NetQASM library) into twisted
Deferreds.Each time the QNodeOS Server (specifically, the NetQASMProtocol instance) receives and correctly parses a NetQASM message (e.g. a subroutine), it will be delegated to this class for further processing.
The main entry point that process the message is the
handle_netqasm_message()method.- Parameters:
factory (NetQASMFactory) – The
NetQASMFactoryobject.instr_log_dir (str | None) – Directory used to write log files to.
flavour (Flavour | None) – NetQASM flavour to use.
- handle_netqasm_message(msg_id: int, msg: Message)[source]¶
Handle incoming NetQASM messages by bridging two async models.
NetQASM’s executor uses Python generators (yield from) while SimulaQron uses Twisted deferred’s (@inlineCallbacks). This method bridges them by: 1. Running the parent’s generator manually 2. Detecting whether each yielded item is a Twisted Deferred or a nested generator 3. For Deferred’s: yielding to Twisted’s reactor to await completion 4. For nested generators: consuming them fully and capturing their return value
Without this bridge, nested generator return values (like physical_address from _instr_qalloc) would be lost, causing None to propagate through the system. This is also what caused the tests to fail, and probably other random weird things.
- Parameters:
msg_id (int) – The id of the message to process.
msg (Message) – The message to process.
- property protocol: Protocol¶
Returns the
NetQASMProtocolobject associated with this routine Handler.