|
| 1 | +from asyncio import StreamReader, StreamWriter |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +from simulaqron.general.host_config import SocketsConfig |
| 5 | +from simulaqron.sdk.protocol import SimulaQronClassicalClient |
| 6 | +from simulaqron.settings import network_config, simulaqron_settings |
| 7 | +from simulaqron.settings.network_config import NodeConfigType |
| 8 | + |
| 9 | +# This is recipe to use NetQASM with simulaqron backend. |
| 10 | +from netqasm.runtime.settings import set_simulator |
| 11 | +set_simulator("simulaqron") |
| 12 | + |
| 13 | +# Importing NetQASM connection, Qubit and EPR socket must be *after* |
| 14 | +# setting the simulator for NetQASM |
| 15 | +from netqasm.sdk.external import NetQASMConnection # noqa: E402 |
| 16 | +from netqasm.sdk import Qubit, EPRSocket # noqa: E402 |
| 17 | + |
| 18 | + |
| 19 | +# This function contains the code of the classical client |
| 20 | +# "reader" is an object connected to the server, which can be used to read data from the server |
| 21 | +# "writer" is an object connected to the server, which can be used to send data to the server |
| 22 | +async def run_client(reader: StreamReader, writer: StreamWriter): |
| 23 | + # To send a messsage, we can simply use the "wirte" method from the "writer" object |
| 24 | + # The argument *must* be a python bytes object, which we can get by encoding (using |
| 25 | + # the UTF-8 charmap) any python string |
| 26 | + message = "Hello World" |
| 27 | + writer.write(message.encode("utf-8")) |
| 28 | + |
| 29 | + # If you want to receive a message (such a response) from the server, you can |
| 30 | + # use the "read" method from the "reader" object. The argument is an integer that |
| 31 | + # configures the maximum bytes that we are allowed to read in a single operation. |
| 32 | + # Note: Since "read" is a python coroutine, we need to "await" it, so python can |
| 33 | + # execute other coroutines until the data becomes available to read. |
| 34 | + answer = await reader.read(100) |
| 35 | + |
| 36 | + this_node_name = "Alice" |
| 37 | + other_node_name = "Bob" |
| 38 | + |
| 39 | + # We ca create an EPR socket with the other node |
| 40 | + epr_socket = EPRSocket(other_node_name) |
| 41 | + # To start executing quantum operations, we need to create a NetQASM connection |
| 42 | + with NetQASMConnection(this_node_name, epr_sockets=[epr_socket]) as alice: |
| 43 | + # Create a qubit |
| 44 | + q = Qubit(alice) |
| 45 | + q.H() |
| 46 | + # Create an entangled qubit with the other node |
| 47 | + epr = epr_socket.create_keep()[0] |
| 48 | + # Teleport |
| 49 | + q.cnot(epr) |
| 50 | + q.H() |
| 51 | + m1 = q.measure() |
| 52 | + m2 = epr.measure() |
| 53 | + # Any value that comes from NetQASM *need* to be retrieved ("casted" to int) |
| 54 | + # *after* the connection is closed (or after flushing the connection, untested) |
| 55 | + m1_val = int(m1) |
| 56 | + m2_val = int(m2) |
| 57 | + return m1_val, m2_val |
| 58 | + |
| 59 | + |
| 60 | +if __name__ == "__main__": |
| 61 | + # Load the simulaqron settings file |
| 62 | + simulaqron_config_file = Path("simulaqron_settings.json") |
| 63 | + simulaqron_settings.read_from_file(simulaqron_config_file) |
| 64 | + |
| 65 | + # Load the file network configuration file |
| 66 | + network_config_file = Path("simulaqron_network.json") |
| 67 | + network_config.read_from_file(network_config_file) |
| 68 | + |
| 69 | + # Some data for this node: |
| 70 | + network_name = "default" # A network with this name *must* exist in "simulaqron_network.json" |
| 71 | + node_name = "YourName" # A node with this name *must* exist in "simulaqron_network.json" |
| 72 | + |
| 73 | + # Get the socket configuration for the sockets used for the application layer |
| 74 | + sockets_config = SocketsConfig(network_config, network_name, NodeConfigType.APP) |
| 75 | + |
| 76 | + # Name of one node to classically connect to |
| 77 | + server_name = "Bob" |
| 78 | + |
| 79 | + # Create the client |
| 80 | + client = SimulaQronClassicalClient(sockets_config) |
| 81 | + # Run a classical client invoking the `run_client` method. This also has the effect to |
| 82 | + # immediately execute the `run_client` method. |
| 83 | + results = client.run_client(server_name, run_client) |
0 commit comments