|
| 1 | +import logging |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +from simulaqron.settings import network_config, simulaqron_settings |
| 6 | + |
| 7 | +# This is recipe to use NetQASM with simulaqron backend. |
| 8 | +from netqasm.runtime.settings import set_simulator |
| 9 | +set_simulator("simulaqron") |
| 10 | + |
| 11 | +# Importing NetQASM connection, Qubit and EPR socket must be *after* |
| 12 | +# setting the simulator for NetQASM |
| 13 | +from netqasm.logging.glob import set_log_level # noqa: E402 |
| 14 | +from netqasm.sdk.external import NetQASMConnection # noqa: E402 |
| 15 | +from netqasm.sdk import EPRSocket # noqa: E402 |
| 16 | + |
| 17 | + |
| 18 | +# This function contains the code of the classical client |
| 19 | +def quantum_program(this_node_name: str, remote_node_name: str) -> int: |
| 20 | + logging.debug("LOCAL %s: Running client side program.", this_node_name) |
| 21 | + |
| 22 | + epr_socket = EPRSocket(remote_node_name) |
| 23 | + # To start executing quantum operations, we need to create a NetQASM connection |
| 24 | + with NetQASMConnection(this_node_name, epr_sockets=[epr_socket]) as alice: |
| 25 | + # Create an entangled qubit |
| 26 | + epr = epr_socket.create_keep()[0] |
| 27 | + |
| 28 | + # And simply measure it |
| 29 | + m1 = epr.measure() |
| 30 | + # Any value that comes from NetQASM *need* to be retrieved ("casted" to int) |
| 31 | + # *after* the connection is closed (or after flushing the connection, untested) |
| 32 | + m1_val = int(m1) |
| 33 | + return m1_val |
| 34 | + |
| 35 | + |
| 36 | +if __name__ == "__main__": |
| 37 | + logging.basicConfig( |
| 38 | + format="%(asctime)s:%(levelname)s:%(name)s:%(filename)s:%(lineno)d:%(message)s", |
| 39 | + level=logging.DEBUG, |
| 40 | + force=True |
| 41 | + ) |
| 42 | + # We set the netqasm log level to "info" to avoid verbose output from the internals. |
| 43 | + set_log_level(logging.INFO) |
| 44 | + |
| 45 | + # Load the simulaqron settings file |
| 46 | + simulaqron_config_file = Path("simulaqron_settings.json") |
| 47 | + simulaqron_settings.read_from_file(simulaqron_config_file) |
| 48 | + |
| 49 | + # Load the file network configuration file |
| 50 | + # We still need this file to correctly interact with the SimulaQron backend (QNodeOS and Virtual Node) |
| 51 | + network_config_file = Path("simulaqron_network.json") |
| 52 | + network_config.read_from_file(network_config_file) |
| 53 | + |
| 54 | + # Some data for this node: |
| 55 | + network_name = "default" # A network with this name *must* exist in "simulaqron_network.json" |
| 56 | + node_name = "Alice" # A node with this name *must* exist in "simulaqron_network.json" |
| 57 | + other_node_name = "Bob" |
| 58 | + |
| 59 | + result = quantum_program(node_name, other_node_name) |
| 60 | + print(f"{node_name}: My Random Number is '{result}'") |
0 commit comments