|
| 1 | +import logging |
| 2 | + |
| 3 | +from asyncio import StreamReader, StreamWriter |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +from simulaqron.general.host_config import SocketsConfig |
| 7 | +from simulaqron.sdk.protocol import SimulaQronClassicalClient, SimulaQronClassicalServer |
| 8 | +from simulaqron.settings import network_config, simulaqron_settings |
| 9 | + |
| 10 | +# This is recipe to use NetQASM with simulaqron backend. |
| 11 | +from netqasm.runtime.settings import set_simulator |
| 12 | + |
| 13 | +from simulaqron.settings.network_config import NodeConfigType |
| 14 | + |
| 15 | +set_simulator("simulaqron") |
| 16 | + |
| 17 | +# Importing NetQASM connection, Qubit and EPR socket must be *after* |
| 18 | +# setting the simulator for NetQASM |
| 19 | +from netqasm.logging.glob import set_log_level # noqa: E402 |
| 20 | +from netqasm.sdk.external import NetQASMConnection # noqa: E402 |
| 21 | +from netqasm.sdk import EPRSocket # noqa: E402 |
| 22 | + |
| 23 | + |
| 24 | +async def send_to_charlie(reader: StreamReader, writer: StreamWriter): |
| 25 | + writer.write("receive_qubit".encode("utf-8")) |
| 26 | + message = await reader.read(100) |
| 27 | + assert message.decode("utf-8") == "continue" |
| 28 | + |
| 29 | + |
| 30 | +# This function contains the code of the classical client |
| 31 | +async def bob_program(reader: StreamReader, writer: StreamWriter) -> int: |
| 32 | + # This is "Bob": the middle node of the GHZ chain |
| 33 | + this_node_name = "Bob" |
| 34 | + start_node_name = "Alice" # A node with this name *must* exist in "simulaqron_network.json" |
| 35 | + end_node_name = "Charlie" # A node with this name *must* exist in "simulaqron_network.json" |
| 36 | + |
| 37 | + logging.debug("LOCAL %s: Running client side program.", this_node_name) |
| 38 | + |
| 39 | + message = await reader.read(100) |
| 40 | + assert message.decode("utf-8") == "receive_qubit" |
| 41 | + |
| 42 | + epr_socket_alice = EPRSocket(start_node_name) |
| 43 | + epr_socket_charlie = EPRSocket(end_node_name) |
| 44 | + |
| 45 | + sockets = SocketsConfig(network_config, "default", NodeConfigType.APP) |
| 46 | + |
| 47 | + charlie_client = SimulaQronClassicalClient(sockets) |
| 48 | + # To start executing quantum operations, we need to create a NetQASM connection |
| 49 | + with NetQASMConnection(this_node_name, epr_sockets=[epr_socket_alice, epr_socket_charlie]) as bob: |
| 50 | + # Receive an entangled qubit |
| 51 | + epr_alice = epr_socket_alice.recv_keep()[0] |
| 52 | + |
| 53 | + # Create a new entangled with Charlie |
| 54 | + epr_charlie = epr_socket_charlie.create_keep()[0] |
| 55 | + |
| 56 | + await charlie_client.connect_and_run(end_node_name, send_to_charlie) |
| 57 | + |
| 58 | + # Create the GHZ state by entangling the fresh qubit |
| 59 | + epr_alice.cnot(epr_charlie) |
| 60 | + |
| 61 | + writer.write("continue".encode("utf-8")) |
| 62 | + |
| 63 | + # And simply measure it |
| 64 | + m1 = epr_charlie.measure() |
| 65 | + # Any value that comes from NetQASM *need* to be retrieved ("casted" to int) |
| 66 | + # *after* the connection is closed (or after flushing the connection, untested) |
| 67 | + m1_val = int(m1) |
| 68 | + print(f"{this_node_name}: My outcome is '{m1_val}'") |
| 69 | + return 0 |
| 70 | + |
| 71 | + |
| 72 | +if __name__ == "__main__": |
| 73 | + logging.basicConfig( |
| 74 | + format="%(asctime)s:%(levelname)s:%(name)s:%(filename)s:%(lineno)d:%(message)s", |
| 75 | + level=logging.DEBUG, |
| 76 | + force=True |
| 77 | + ) |
| 78 | + # We set the netqasm log level to "info" to avoid verbose output from the internals. |
| 79 | + set_log_level(logging.INFO) |
| 80 | + |
| 81 | + # Load the simulaqron settings file |
| 82 | + simulaqron_config_file = Path("simulaqron_settings.json") |
| 83 | + simulaqron_settings.read_from_file(simulaqron_config_file) |
| 84 | + |
| 85 | + # Load the file network configuration file |
| 86 | + # We still need this file to correctly interact with the SimulaQron backend (QNodeOS and Virtual Node) |
| 87 | + network_config_file = Path("simulaqron_network.json") |
| 88 | + network_config.read_from_file(network_config_file) |
| 89 | + |
| 90 | + # Some data for this node: |
| 91 | + network_name = "default" # A network with this name *must* exist in "simulaqron_network.json" |
| 92 | + node_name = "Bob" # A node with this name *must* exist in "simulaqron_network.json" |
| 93 | + start_node_name = "Alice" # A node with this name *must* exist in "simulaqron_network.json" |
| 94 | + end_node_name = "Charlie" # A node with this name *must* exist in "simulaqron_network.json" |
| 95 | + |
| 96 | + classical_sockets = SocketsConfig(network_config, network_name, NodeConfigType.APP) |
| 97 | + |
| 98 | + server = SimulaQronClassicalServer(classical_sockets, node_name) |
| 99 | + client = SimulaQronClassicalClient(classical_sockets) |
| 100 | + |
| 101 | + server.register_client_handler(bob_program) |
| 102 | + server.start_serving() |
0 commit comments