Skip to content

Commit 16214cf

Browse files
committed
Structure of example directory; sender and receiver applications for NetQASM qubit teleportation example
1 parent b820d51 commit 16214cf

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from netqasm.runtime.settings import Simulator, get_simulator
2+
from netqasm.sdk import EPRSocket
3+
from netqasm.sdk.external import NetQASMConnection, Socket, get_qubit_state
4+
from netqasm.sdk.toolbox.sim_states import get_fidelity, qubit_from, to_dm
5+
6+
7+
def main(app_config=None):
8+
log_config = app_config.log_config
9+
10+
# Create a socket to recv classical information
11+
socket = Socket("receiver", "sender", log_config=log_config)
12+
13+
# Create a EPR socket for entanglement generation
14+
epr_socket = EPRSocket("sender")
15+
16+
# Initialize the connection
17+
receiver = NetQASMConnection(
18+
app_name=app_config.app_name, log_config=log_config, epr_sockets=[epr_socket]
19+
)
20+
with receiver:
21+
epr = epr_socket.recv_keep()[0]
22+
receiver.flush()
23+
24+
# Get the corrections
25+
m1, m2 = socket.recv_structured().payload
26+
print(f"`receiver` got corrections: {m1}, {m2}")
27+
if m2 == 1:
28+
print("`receiver` will perform X correction")
29+
epr.X()
30+
if m1 == 1:
31+
print("`receiver` will perform Z correction")
32+
epr.Z()
33+
34+
receiver.flush()
35+
36+
if get_simulator() == Simulator.NETSQUID:
37+
# Get the qubit state
38+
# NOTE only possible in simulation, not part of actual application
39+
dm = get_qubit_state(epr)
40+
print(f"`receiver` recieved the teleported state {dm}")
41+
42+
# Reconstruct the original qubit to compare with the received one.
43+
# NOTE only to check simulation results, normally the Sender does not
44+
# need to send the phi and theta values!
45+
msg = socket.recv_silent() # don't log this
46+
print(f"received silent message: {msg}")
47+
phi, theta = eval(msg)
48+
49+
original = qubit_from(phi, theta)
50+
original_dm = to_dm(original)
51+
fidelity = get_fidelity(original, dm)
52+
53+
return {
54+
"original_state": original_dm.tolist(),
55+
"correction1": "Z" if m1 == 1 else "None",
56+
"correction2": "X" if m2 == 1 else "None",
57+
"received_state": dm.tolist(),
58+
"fidelity": fidelity,
59+
}
60+
else:
61+
return {
62+
"correction1": "Z" if m1 == 1 else "None",
63+
"correction2": "X" if m2 == 1 else "None",
64+
}
65+
66+
67+
if __name__ == "__main__":
68+
main()
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from netqasm.logging.output import get_new_app_logger
2+
from netqasm.runtime.settings import Simulator, get_simulator
3+
from netqasm.sdk import EPRSocket, Qubit
4+
from netqasm.sdk.classical_communication.message import StructuredMessage
5+
from netqasm.sdk.external import NetQASMConnection, Socket
6+
from netqasm.sdk.toolbox import set_qubit_state
7+
8+
9+
def main(app_config=None, phi=0.0, theta=0.0):
10+
log_config = app_config.log_config
11+
app_logger = get_new_app_logger(app_name="sender", log_config=log_config)
12+
13+
# Create a socket to send classical information
14+
socket = Socket("sender", "receiver", log_config=log_config)
15+
16+
# Create a EPR socket for entanglement generation
17+
epr_socket = EPRSocket("receiver")
18+
19+
print("`sender` will start to teleport a qubit to `receiver`")
20+
21+
# Initialize the connection to the backend
22+
sender = NetQASMConnection(
23+
app_name=app_config.app_name, log_config=log_config, epr_sockets=[epr_socket]
24+
)
25+
with sender:
26+
# Create a qubit to teleport
27+
q = Qubit(sender)
28+
set_qubit_state(q, phi, theta)
29+
30+
# Create EPR pairs
31+
epr = epr_socket.create_keep()[0]
32+
33+
# Teleport
34+
q.cnot(epr)
35+
q.H()
36+
m1 = q.measure()
37+
m2 = epr.measure()
38+
39+
# Send the correction information
40+
m1, m2 = int(m1), int(m2)
41+
42+
app_logger.log(f"m1 = {m1}")
43+
app_logger.log(f"m2 = {m2}")
44+
print(
45+
f"`sender` measured the following teleportation corrections: m1 = {m1}, m2 = {m2}"
46+
)
47+
print("`sender` will send the corrections to `receiver`")
48+
49+
socket.send_structured(StructuredMessage("Corrections", (m1, m2)))
50+
51+
if get_simulator() == Simulator.NETSQUID:
52+
socket.send_silent(str((phi, theta)))
53+
54+
return {"m1": m1, "m2": m2}
55+
56+
57+
if __name__ == "__main__":
58+
main()

0 commit comments

Comments
 (0)