|
| 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 | +def quantum_program(this_node_name: str) -> int: |
| 21 | + # To start executing quantum operations, we need to create a NetQASM connection |
| 22 | + with NetQASMConnection(this_node_name, epr_sockets=[epr_socket]) as alice: |
| 23 | + # Create a qubit |
| 24 | + q = Qubit(alice) |
| 25 | + |
| 26 | + # Perform some local quantum operations |
| 27 | + q.H() |
| 28 | + q.X() |
| 29 | + m1 = q.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 | + # Load the simulaqron settings file |
| 38 | + simulaqron_config_file = Path("simulaqron_settings.json") |
| 39 | + simulaqron_settings.read_from_file(simulaqron_config_file) |
| 40 | + |
| 41 | + # Load the file network configuration file |
| 42 | + # We still need this file to correctly interact with the SimulaQron backend (QNodeOS and Virtual Node) |
| 43 | + network_config_file = Path("simulaqron_network.json") |
| 44 | + network_config.read_from_file(network_config_file) |
| 45 | + |
| 46 | + # Some data for this node: |
| 47 | + network_name = "default" # A network with this name *must* exist in "simulaqron_network.json" |
| 48 | + node_name = "YourName" # A node with this name *must* exist in "simulaqron_network.json" |
| 49 | + |
| 50 | + # Get the socket configuration for the sockets used for the application layer |
| 51 | + sockets_config = SocketsConfig(network_config, network_name, NodeConfigType.APP) |
| 52 | + |
| 53 | + # Name of one node to classically connect to |
| 54 | + server_name = "Bob" |
| 55 | + |
| 56 | + result = quantum_program() |
| 57 | + print(result) |
0 commit comments