Skip to content

Commit 04534d1

Browse files
author
Axel Dahlberg
committed
Moved examples of how to use the python lib from SimulaQron repo to here
1 parent f6b00e8 commit 04534d1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1736
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
In this example Alice connects to the cqc-server, sends a hello message and prints the return message
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#
2+
# Copyright (c) 2017, Stephanie Wehner and Axel Dahlberg
3+
# All rights reserved.
4+
#
5+
# Redistribution and use in source and binary forms, with or without
6+
# modification, are permitted provided that the following conditions are met:
7+
# 1. Redistributions of source code must retain the above copyright
8+
# notice, this list of conditions and the following disclaimer.
9+
# 2. Redistributions in binary form must reproduce the above copyright
10+
# notice, this list of conditions and the following disclaimer in the
11+
# documentation and/or other materials provided with the distribution.
12+
# 3. All advertising materials mentioning features or use of this software
13+
# must display the following acknowledgement:
14+
# This product includes software developed by Stephanie Wehner, QuTech.
15+
# 4. Neither the name of the QuTech organization nor the
16+
# names of its contributors may be used to endorse or promote products
17+
# derived from this software without specific prior written permission.
18+
#
19+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ''AS IS'' AND ANY
20+
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
23+
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26+
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
30+
import logging
31+
import os
32+
import socket
33+
34+
from simulaqron.general.hostConfig import networkConfig
35+
from cqc.cqcHeader import CQCHeader, CQC_TP_HELLO, CQC_VERSION
36+
from simulaqron.toolbox import get_simulaqron_path
37+
38+
39+
#####################################################################################################
40+
#
41+
# init
42+
#
43+
def init(name, cqcFile=None):
44+
"""
45+
Initialize a connection to the cqc server with the name given as input.
46+
A path to a configure file for the cqc network can be given,
47+
if it's not given the config file 'config/cqcNodes.cfg' will be used.
48+
Returns a socket object.
49+
"""
50+
51+
# This file defines the network of CQC servers interfacing to virtual quantum nodes
52+
if cqcFile is None:
53+
simulaqron_path = get_simulaqron_path.main()
54+
cqcFile = os.path.join(simulaqron_path, "config/cqcNodes.cfg")
55+
56+
# Read configuration files for the cqc network
57+
cqcNet = networkConfig(cqcFile)
58+
59+
# Host data
60+
if name in cqcNet.hostDict:
61+
myHost = cqcNet.hostDict[name]
62+
else:
63+
logging.error("The name '%s' is not in the cqc network.", name)
64+
raise LookupError("The name '%s' is not in the cqc network.".format(name))
65+
66+
addr = myHost.addr
67+
68+
# Connect to cqc server and run protocol
69+
cqc = None
70+
try:
71+
cqc = socket.socket(addr[0], addr[1], addr[2])
72+
except socket.error:
73+
logging.error("Could not connect to cqc server: %s", name)
74+
try:
75+
cqc.connect(addr[4])
76+
except socket.error:
77+
cqc.close()
78+
logging.error("Could not connect to cqc server: %s", name)
79+
return cqc
80+
81+
82+
#####################################################################################################
83+
#
84+
# main
85+
#
86+
def main():
87+
88+
# In this example, we are Alice.
89+
myName = "Alice"
90+
91+
# Initialize the connection
92+
cqc = init(myName)
93+
94+
# Send Hello message
95+
print("App {} tells CQC: 'HELLO'".format(myName))
96+
hdr = CQCHeader()
97+
hdr.setVals(CQC_VERSION, CQC_TP_HELLO, 0, 0)
98+
msg = hdr.pack()
99+
cqc.send(msg)
100+
101+
# Receive return message
102+
data = cqc.recv(192)
103+
hdr = CQCHeader(data)
104+
if hdr.tp == CQC_TP_HELLO:
105+
print("CQC tells App {}: 'HELLO'".format(myName))
106+
else:
107+
print("Did not receive a hello message, but rather: {}".format(hdr.printable()))
108+
109+
# Close the connection
110+
cqc.close()
111+
112+
113+
##################################################################################################
114+
main()

examples/python/hello_noLib/run.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
python3 aliceTest.py &

examples/pythonLib/QBER/README.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
In this example the two nodes Alice and Bob will estimate the QBER (qubit error rate) of their produced entangled pairs.
2+
To make the below example more interesting you should try to turn on the probabilistic noise option in SimulaQron.
3+
You can do this be setting the entry :code:`noisy_qubits` to :code:`True` in the settings-file `config/settings.ini`.
4+
You can also tune the coherence time :code:`t1` of the qubits.
5+
6+
NOTE: This probabilistic noise in SimulaQron is not intended and should not be used to benchmark the performence of protocols in noisy settings, since noise is applied in a rate related to the wall clock time of your computer. Thus, if your computer or network is slower, more noise will be applied to the qubits!
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#
2+
# Copyright (c) 2017, Stephanie Wehner and Axel Dahlberg
3+
# All rights reserved.
4+
#
5+
# Redistribution and use in source and binary forms, with or without
6+
# modification, are permitted provided that the following conditions are met:
7+
# 1. Redistributions of source code must retain the above copyright
8+
# notice, this list of conditions and the following disclaimer.
9+
# 2. Redistributions in binary form must reproduce the above copyright
10+
# notice, this list of conditions and the following disclaimer in the
11+
# documentation and/or other materials provided with the distribution.
12+
# 3. All advertising materials mentioning features or use of this software
13+
# must display the following acknowledgement:
14+
# This product includes software developed by Stephanie Wehner, QuTech.
15+
# 4. Neither the name of the QuTech organization nor the
16+
# names of its contributors may be used to endorse or promote products
17+
# derived from this software without specific prior written permission.
18+
#
19+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ''AS IS'' AND ANY
20+
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
23+
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26+
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
30+
import sys
31+
import json
32+
from cqc.pythonLib import CQCConnection, CQCNoQubitError
33+
34+
35+
def main(nr_runs):
36+
meas_outcomes = {}
37+
38+
print("Estimating QBER by measuring {} produced EPR pairs.".format(nr_runs))
39+
40+
# Initialize the connection
41+
with CQCConnection("Alice") as Alice:
42+
43+
run = 0
44+
while run < nr_runs:
45+
46+
try:
47+
# Create an EPR pair
48+
q = Alice.createEPR("Bob")
49+
except CQCNoQubitError:
50+
continue
51+
52+
run += 1
53+
54+
# Get the identifier of this EPR pair such that we can relate our measurement outcomes to Bobs
55+
sequence_nr = q.get_entInfo().id_AB
56+
57+
print("Generated EPR pair number {}.".format(sequence_nr))
58+
59+
if (sequence_nr % 3) == 0:
60+
# Measure in Z
61+
basis = "Z"
62+
elif (sequence_nr % 3) == 1:
63+
# Measure in X
64+
q.H()
65+
basis = "X"
66+
else:
67+
# Measure in Y
68+
q.K()
69+
basis = "Y"
70+
71+
m = q.measure()
72+
# We save both the measurement outcome and the measurement basis
73+
meas_outcomes[sequence_nr] = (m, basis)
74+
75+
# Get the measurement outcomes from Bob
76+
msg = Alice.recvClassical(msg_size=10000)
77+
78+
# Decode the message
79+
bob_meas_outcomes = json.loads(msg.decode("utf-8"))
80+
81+
# Check the measurement outcomes
82+
errors = []
83+
for (sequence_nr, mB) in bob_meas_outcomes.items():
84+
mA, basis = meas_outcomes[int(sequence_nr)]
85+
if basis == "Y":
86+
if mA == mB: # In a noiseless situation this shouldn't happen
87+
errors.append(True)
88+
else:
89+
errors.append(False)
90+
else:
91+
if mA != mB: # In a noiseless situation this shouldn't happen
92+
errors.append(True)
93+
else:
94+
errors.append(False)
95+
96+
nr_data_points = len(errors)
97+
avg_QBER = errors.count(True) / nr_data_points
98+
to_print = "Estimated QBER is {} (from {} data-points.".format(avg_QBER, nr_data_points)
99+
print("|" + "-" * (len(to_print) + 2) + "|")
100+
print("| " + to_print + " |")
101+
print("|" + "-" * (len(to_print) + 2) + "|")
102+
103+
104+
if __name__ == "__main__":
105+
try:
106+
nr_runs = int(sys.argv[1])
107+
except Exception:
108+
nr_runs = 500
109+
if nr_runs > 1000:
110+
raise ValueError("Number of EPR pairs for this example is currently restricted to less than 1000")
111+
main(nr_runs)

examples/pythonLib/QBER/bobTest.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#
2+
# Copyright (c) 2017, Stephanie Wehner and Axel Dahlberg
3+
# All rights reserved.
4+
#
5+
# Redistribution and use in source and binary forms, with or without
6+
# modification, are permitted provided that the following conditions are met:
7+
# 1. Redistributions of source code must retain the above copyright
8+
# notice, this list of conditions and the following disclaimer.
9+
# 2. Redistributions in binary form must reproduce the above copyright
10+
# notice, this list of conditions and the following disclaimer in the
11+
# documentation and/or other materials provided with the distribution.
12+
# 3. All advertising materials mentioning features or use of this software
13+
# must display the following acknowledgement:
14+
# This product includes software developed by Stephanie Wehner, QuTech.
15+
# 4. Neither the name of the QuTech organization nor the
16+
# names of its contributors may be used to endorse or promote products
17+
# derived from this software without specific prior written permission.
18+
#
19+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ''AS IS'' AND ANY
20+
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
23+
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26+
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
30+
import sys
31+
import json
32+
from cqc.pythonLib import CQCConnection
33+
34+
35+
def main(nr_runs):
36+
meas_outcomes = {}
37+
38+
# Initialize the connection
39+
with CQCConnection("Bob") as Bob:
40+
41+
for _ in range(nr_runs):
42+
43+
# Create an EPR pair
44+
q = Bob.recvEPR()
45+
46+
# Get the identifier of this EPR pair such that Alice can relate the measuement outcomes to hers
47+
sequence_nr = q.get_entInfo().id_AB
48+
49+
if (sequence_nr % 3) == 0:
50+
# Measure in Z
51+
pass
52+
elif (sequence_nr % 3) == 1:
53+
# Measure in X
54+
q.H()
55+
else:
56+
# Measure in Y
57+
q.K()
58+
59+
m = q.measure()
60+
meas_outcomes[sequence_nr] = m
61+
62+
# Encode the measurement outcomes to bytes, such that we can send them
63+
msg = json.dumps(meas_outcomes).encode("utf-8")
64+
65+
# Send the measurement outcomes to Alice
66+
Bob.sendClassical(name="Alice", msg=msg)
67+
68+
69+
if __name__ == "__main__":
70+
try:
71+
nr_runs = int(sys.argv[1])
72+
except Exception:
73+
nr_runs = 500
74+
if nr_runs > 1000:
75+
raise ValueError("Number of EPR pairs for this example is currently restricted to less than 1000")
76+
main(nr_runs)

examples/pythonLib/QBER/run.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
3+
python3 aliceTest.py $@ &
4+
python3 bobTest.py $@ &

0 commit comments

Comments
 (0)