-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_superdense_coding.py
More file actions
54 lines (35 loc) · 1.32 KB
/
Copy path03_superdense_coding.py
File metadata and controls
54 lines (35 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import sys
from pathlib import Path
PROJECT_FOLDER = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(PROJECT_FOLDER))
from quantum_core.circuit import apply_gate
from quantum_core.gates import CNOT, H, X, Z
from quantum_core.measurement import get_probabilities
from quantum_core.state import make_zero_state
def alice_encodes_message(state, message):
if type(message) != str:
raise TypeError("message must be a string")
if len(message) != 2:
raise ValueError("message must have exactly 2 bits")
for bit in message:
if bit != "0" and bit != "1":
raise ValueError("message can only contain 0 and 1")
if message[1] == "1":
state = apply_gate(state, X, [0])
if message[0] == "1":
state = apply_gate(state, Z, [0])
return state
def send_and_decode(message):
state = make_zero_state(2)
state = apply_gate(state, H, [0])
state = apply_gate(state, CNOT, [0, 1])
state = alice_encodes_message(state, message)
state = apply_gate(state, CNOT, [0, 1])
state = apply_gate(state, H, [0])
return get_probabilities(state)
print("Superdense coding")
print("-----------------")
messages = ["00", "01", "10", "11"]
for message in messages:
answer = send_and_decode(message)
print("Alice message:", message, "Bob receives:", answer)