Skip to content

Commit 4190426

Browse files
committed
Add corrRNG example using new SDK
1 parent b703014 commit 4190426

7 files changed

Lines changed: 217 additions & 0 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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}'")
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 bob_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+
# Receive an entangled qubit
26+
epr = epr_socket.recv_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 = "Bob" # A node with this name *must* exist in "simulaqron_network.json"
57+
other_node_name = "Alice"
58+
59+
result = bob_program(node_name, other_node_name)
60+
print(f"{node_name}: My Random Number is '{result}'")

examples/new-sdk/corrRNG/doNew.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
3+
./terminate.sh
4+
sleep 1
5+
./run.sh

examples/new-sdk/corrRNG/run.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
3+
# Flag used to determine whether to start simulaqron backend or not
4+
# This is useful when using this script to run the application in different machines.
5+
START_SIMULAQRON=true
6+
7+
# Process the arguments, if any
8+
while [[ $# -gt 0 ]]; do
9+
case $1 in
10+
-n|--no-start-simulaqron)
11+
START_SIMULAQRON=false
12+
shift
13+
;;
14+
esac
15+
done
16+
17+
if [ "$START_SIMULAQRON" = true ]; then
18+
# Check if SimulaQron is already running
19+
if [ ! -f ~/.simulaqron_pids/simulaqron_network_default.pid ]; then
20+
# If not, start simulaqron backend for both nodes
21+
simulaqron start --nodes=Alice,Bob --network-config-file simulaqron_network.json --simulaqron-config-file simulaqron_settings.json
22+
fi
23+
fi
24+
25+
python3 aliceTest.py &
26+
python3 bobTest.py
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[
2+
{
3+
"name": "default",
4+
"nodes": [
5+
{
6+
"Alice": {
7+
"app_socket": ["localhost", 8821],
8+
"qnodeos_socket": ["localhost", 8822],
9+
"vnode_socket": ["localhost", 8823]
10+
}
11+
},
12+
{
13+
"Bob": {
14+
"app_socket": ["localhost", 8831],
15+
"qnodeos_socket": ["localhost", 8832],
16+
"vnode_socket": ["localhost", 8833]
17+
}
18+
},
19+
{
20+
"Charlie": {
21+
"app_socket": ["localhost", 8841],
22+
"qnodeos_socket": ["localhost", 8842],
23+
"vnode_socket": ["localhost", 8843]
24+
}
25+
},
26+
{
27+
"David": {
28+
"app_socket": ["localhost", 8871],
29+
"qnodeos_socket": ["localhost", 8872],
30+
"vnode_socket": ["localhost", 8873]
31+
}
32+
}
33+
],
34+
"topology": null
35+
}
36+
]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"max_qubits": 20,
3+
"max_registers": 1000,
4+
"conn_retry_time": 0.5,
5+
"conn_max_retries": 10,
6+
"recv_timeout": 100,
7+
"recv_retry_time": 0.1,
8+
"recv_max_retries": 10,
9+
"log_level": 30,
10+
"sim_backend": "projectq",
11+
"noisy_qubits": false,
12+
"max_app_waiting_time": -1.0,
13+
"t1": 1.0
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env sh
2+
TEST_PIDS=$(ps aux | grep python | grep -E "Test" | awk {'print $2'})
3+
if [ "$TEST_PIDS" != "" ]
4+
then
5+
kill -9 $TEST_PIDS
6+
fi
7+
8+
# Check if SimulaQron is running
9+
if [ -f ~/.simulaqron_pids/simulaqron_network_default.pid ]; then
10+
if ! simulaqron stop
11+
then
12+
# Kill the process, only if simulaqron could not be stopped gracefully
13+
cat $HOME/.simulaqron_pids/simulaqron_network_default.pid | xargs kill -9
14+
fi
15+
fi
16+

0 commit comments

Comments
 (0)