-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws_braket_hello_world
98 lines (67 loc) · 2.59 KB
/
aws_braket_hello_world
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from braket.circuits import Circuit
from braket.circuits import ascii_circuit_diagram
from braket.aws import AwsDevice
import boto3
import binascii
def convert_string_to_bit_pairs(wordstring):
print ("received " + wordstring)
binary_string = ' '.join(format(x, '08b') for x in bytearray(wordstring, 'utf-8'))
return get_array_of_bit_pairs(binary_string)
def get_array_of_bit_pairs(binary_string):
print ("received " + binary_string)
n=9
binary_array = [(binary_string[i:i+n]) for i in range(0, len(binary_string), n)]
bitpair_array = []
for bit_string in binary_array:
n=2
tmp_bitpair_array = [(bit_string[i:i+n]) for i in range(0, len(bit_string), n)]
for bit_pair in tmp_bitpair_array :
bitpair_array.append(bit_pair)
return bitpair_array
aws_account_id = boto3.client("sts").get_caller_identity()["Account"]
print(aws_account_id)
#s3_folder = (f"amazon-braket-", "scia-firstq")
s3_folder = (f"amazon-braket-{aws_account_id}", "scia-firstq")
device = AwsDevice("arn:aws:braket:::device/quantum-simulator/amazon/sv1")
#device = AwsDevice("arn:aws:braket:::device/qpu/ionq/ionQdevice")
bitpair_array = convert_string_to_bit_pairs("hello quantum world")
# recvd bit pair string
bitpairstring = ""
recvd_string = ""
loopcount = 0
for bitpair in bitpair_array :
my_circuit = Circuit().h(range(1))
my_circuit.cnot(control=0, target=1)
print (bitpair)
if (bitpair == ' '):
continue
loopcount = loopcount + 1
m = bitpair
print ("sender's sent message =", m)
# okay this is caca
if (m == "01"):
my_circuit.x(0)
if (m == "10"):
my_circuit.z(0)
if (m == "11"):
my_circuit.x(0), my_circuit.z(0)
# receiver measures the bell state
my_circuit.cnot(control=0, target=1)
my_circuit.h(0)
diagram = ascii_circuit_diagram.AsciiCircuitDiagram()
results = device.run(my_circuit, s3_folder, shots=1)
print(diagram.build_diagram(my_circuit))
counts = results.result().measurement_counts
for k, v in counts.items():
bitpairstring = bitpairstring + k
print ("recv'ers recv'd message =", bitpairstring)
task = device.run(my_circuit, s3_folder, shots=100)
status = None
while status != 'COMPLETED':
status = task.state()
print('Status:', status)
if (len(bitpairstring) == 8):
print (bitpairstring)
recvd_string = recvd_string + chr(int(bitpairstring[:8], 2))
bitpairstring = ""
print("super encoded string is: " + recvd_string)