I have a code that looks like this,:
import random
import numpy as np
from operator import itemgetter
import sys
equal_to = {"a":"u_plus", "b":"u_minus", "c":"v_plus", "d":"v_minus"} #bell state
p = 8 #length of generated binary
key1 = [] #list of generated binary
for i in range(p):
temp = random.randint(0,1)
key1.append(temp)
tmplist2 = [] #list of random sample_letters
for i in range(p):
while True:
attempt = str(random.choice(list(equal_to)))
tmplist2.append(attempt)
if attempt == 'b':
break
#evaluate result of alice binary and bell state
def eva(alice, bell):
if alice == 1:
if bell == 'a' or bell == 'b':
return 1
elif bell == 'c' or bell == 'd':
return 0
elif alice == 0:
if bell == 'c' or bell == 'd':
return 1
elif bell == 'a' or bell == 'b':
return 0
for_bob = [] #list of generated binary and bell state through logic gate
for k in key1:
for t in tmplist2:
e = eva(k, t)
for_bob.append(e)
#tr = [[eva(k,t) for t in tmplist2] for k in key1] #list comprehension split the key properly
print("generated random binary strings:", key1)
print("generated bell states:", tmplist2)
print("encrypted strings:", for_bob)
It printed out something like this:
generated random binary strings:
[0, 0, 0, 0, 0, 1, 1, 0]
generated bell states:
['b', 'c', 'b', 'a', 'a', 'c', 'b', 'b', 'd', 'd', 'd', 'c', 'c', 'b', 'a', 'd', 'd', 'b', 'c', 'c', 'd', 'c', 'd', 'b', 'd', 'c', 'a', 'd', 'c', 'd', 'c', 'c', 'b']
encrypted strings:
[0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0]
I was looking a way to loop within tmplist2
without starting again from the beginning, if the string 'b' came out.
The loop is supposed to function as follows:
key1[0]
shall evaluate with 'b', returning to 0. Since 'b' came out, outer loop should continue to next element key1[1]
and it should evaluate with 'c' and 'b', returning 1 0. Again because 'b' came out, outer loop shall proceed to next element key[2]
and evaluate itself with 'a', 'a', 'c', and 'b', returning 0 0 1 0. This should continue until tmplist2
finished.
Anyone got solutions for the issue? I was searching the solution of how to loop within a loop and not returning to the beginning when a certain condition is given. In this case 'b'.
n.b.: this program was meant to simulate quantum teleportation.
Main data
for_bob = []
key1 = [0, 0, 0, 0, 0, 1, 1, 0]
tmplist2 = ['b', 'c', 'b', 'a', 'a', 'c', 'b', 'b', 'd', 'd', 'd', 'c', 'c', 'b', 'a', 'd', 'd', 'b', 'c', 'c', 'd', 'c', 'd', 'b', 'd', 'c', 'a', 'd', 'c', 'd', 'c', 'c', 'b']
Approach 1
_tmp = tmplist2[:]
for k in key1:
while _tmp:
if _tmp[:1] != ['b']:
for_bob.append(eva(k, *_tmp[:1]))
_tmp = _tmp[1:]
else:
for_bob.append(eva(k, *_tmp[:1]))
_tmp = _tmp[1:]
break
Approach 2:
_tmp = iter(tmplist2)
for i in key1:
for j in iter(lambda: next(_tmp), None):
if j != 'b':
for_bob.append(eva(i, j))
else:
for_bob.append(eva(i, j))
break
generated random binary strings: [0, 0, 0, 0, 0, 1, 1, 0]
generated bell states: ['b', 'c', 'b', 'a', 'a', 'c', 'b', 'b', 'd', 'd', 'd', 'c', 'c', 'b', 'a', 'd', 'd', 'b', 'c', 'c', 'd', 'c', 'd', 'b', 'd', 'c', 'a', 'd', 'c', 'd', 'c', 'c', 'b']
encrypted strings: [0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0]