<jupyter_start><jupyter_text>prepared by <NAME> (QLatvia)                 This cell contains some macros. If there is a problem with displaying mathematical formulas, please run this cell to load these macros. $ \newcommand{\bra}[1]{\langle 1|} $$ \newcommand{\ket}[1]{|1\rangle} $$ \newcommand{\braket}[2]{\langle 1|2\rangle} $$ \newcommand{\dot}[2]{ 1 \cdot 2} $$ \newcommand{\biginner}[2]{\left\langle 1,2\right\rangle} $$ \newcommand{\mymatrix}[2]{\left( \begin{array}{1} 2\end{array} \right)} $$ \newcommand{\myvector}[1]{\mymatrix{c}{1}} $$ \newcommand{\myrvector}[1]{\mymatrix{r}{1}} $$ \newcommand{\mypar}[1]{\left( 1 \right)} $$ \newcommand{\mybigpar}[1]{ \Big( 1 \Big)} $$ \newcommand{\sqrttwo}{\frac{1}{\sqrt{2}}} $$ \newcommand{\dsqrttwo}{\dfrac{1}{\sqrt{2}}} $$ \newcommand{\onehalf}{\frac{1}{2}} $$ \newcommand{\donehalf}{\dfrac{1}{2}} $$ \newcommand{\hadamard}{ \mymatrix{rr}{ \sqrttwo & \sqrttwo \\ \sqrttwo & -\sqrttwo }} $$ \newcommand{\vzero}{\myvector{1\\0}} $$ \newcommand{\vone}{\myvector{0\\1}} $$ \newcommand{\vhadamardzero}{\myvector{ \sqrttwo \\  \sqrttwo } } $$ \newcommand{\vhadamardone}{ \myrvector{ \sqrttwo \\ -\sqrttwo } } $$ \newcommand{\myarray}[2]{ \begin{array}{1}2\end{array}} $$ \newcommand{\X}{ \mymatrix{cc}{0 & 1 \\ 1 & 0}  } $$ \newcommand{\Z}{ \mymatrix{rr}{1 & 0 \\ 0 & -1}  } $$ \newcommand{\Htwo}{ \mymatrix{rrrr}{ \frac{1}{2} & \frac{1}{2} & \frac{1}{2} & \frac{1}{2} \\ \frac{1}{2} & -\frac{1}{2} & \frac{1}{2} & -\frac{1}{2} \\ \frac{1}{2} & \frac{1}{2} & -\frac{1}{2} & -\frac{1}{2} \\ \frac{1}{2} & -\frac{1}{2} & -\frac{1}{2} & \frac{1}{2} } } $$ \newcommand{\CNOT}{ \mymatrix{cccc}{1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0} } $$ \newcommand{\norm}[1]{ \left\lVert 1 \right\rVert } $  Coin Flip: A Probabilistic Bit  A fair coin A coin has two sides: Head and Tail.After flipping a coin, we can get a Head or a Tail. We can represent these two cases by a single bit:     0 represents Head      1 represents Tail   Flipping a fair coin If our coin is fair, then the probabilities of obtaining a Head or a Tail are equal:$ p= \dfrac{1}{2} = 0.5 $.Coin-flipping can be defined as an operator:     $ FairCoin(Head) = \frac{1}{2} Head + \frac{1}{2}Tail $      $ FairCoin(Tail) = \frac{1}{2} Head + \frac{1}{2}Tail $ $FairCoin = \begin{array}{c|cc} & \mathbf{Head} & \mathbf{Tail} \\ \hline \mathbf{Head} & \dfrac{1}{2} & \dfrac{1}{2} \\  \mathbf{Tail} & \dfrac{1}{2} & \dfrac{1}{2}  \end{array}$Or, by using 0 and 1:$FairCoin = \begin{array}{c|cc} & \mathbf{0} & \mathbf{1} \\ \hline \mathbf{0} & \dfrac{1}{2} & \dfrac{1}{2} \\  \mathbf{1} & \dfrac{1}{2} & \dfrac{1}{2}  \end{array}$  Task 1: Simulating FairCoin in PythonFlip a fair coin 100 times. Calculate the total number of heads and tails, and then check the ratio of the number of heads and the number of tails.Do the same experiment 1000 times.Do the same experiment 10,000 times.Do the same experiment 100,000 times.Do your results get close to the ideal case (the numbers of heads and tails are equal)?<jupyter_code>from random import randrange
#
# you may use method 'randrange' for this task
# randrange(n) return a value from {0,1,...,n} randomly

for task_1 in [100,1000,10000,100000]:
    head = 0
    tail = 0
    for i in range(task_1):
        if randrange(2) == 0: head = head + 1
        else: tail = tail + 1
    print("experiment:",task_1)
    print("the ratio of heads/tails is",(head/tail),"heads =",head,"tails = ",tail)
    print()<jupyter_output>experiment: 100
the ratio of heads/tails is 0.9607843137254902 heads = 49 tails =  51

experiment: 1000
the ratio of heads/tails is 0.9960079840319361 heads = 499 tails =  501

experiment: 10000
the ratio of heads/tails is 0.9817677368212445 heads = 4954 tails =  5046

experiment: 100000
the ratio of heads/tails is 0.9943360855171314 heads = 49858 tails =  50142<jupyter_text>click for our solution  Flipping a biased coin Our coin may have a bias. For example, the probability of getting head is greater than the probability of getting tail.Here is an example:$BiasedCoin = \begin{array}{c|cc} & \mathbf{Head} & \mathbf{Tail} \\ \hline \mathbf{Head} & 0.6 & 0.6 \\  \mathbf{Tail} & 0.4 & 0.4  \end{array}$Or, by using 0 and 1 as the states:$BiasedCoin = \begin{array}{c|cc} & \mathbf{0} & \mathbf{1} \\ \hline \mathbf{0} & 0.6 & 0.6\\  \mathbf{1} & 0.4 & 0.4 \end{array}$  Task 2: Simulating BiasedCoin in PythonFlip the following biased coin 100 times. Calculate the total numbers of heads and tails, and then check the ratio of the number of heads and the number of tails.$BiasedCoin = \begin{array}{c|cc} & \mathbf{Head} & \mathbf{Tail} \\ \hline \mathbf{Head} & 0.6 & 0.6 \\  \mathbf{Tail} & 0.4 & 0.4  \end{array}$Do the same experiment 1000 times.Do the same experiment 10,000 times.Do the same experiment 100,000 times.Do your results get close to the ideal case $ \mypar{ \dfrac{ \mbox{ of heads} }{ \mbox{ of tails} } = \dfrac{0.6}{0.4} = 1.50000000 } $?<jupyter_code>from random import randrange

for task_2 in [100,1000,10000,100000]:
    head = 0
    tail = 0
    for i in range(task_2):
        if randrange(100) < 60: head = head + 1
        else: tail = tail + 1
    print("experiment:",task_2)
    print("the ratio of heads/tails is",(head/tail),"heads =",head,"tails = ",tail)
    print()<jupyter_output>experiment: 100
the ratio of heads/tails is 1.3255813953488371 heads = 57 tails =  43

experiment: 1000
the ratio of heads/tails is 1.4691358024691359 heads = 595 tails =  405

experiment: 10000
the ratio of heads/tails is 1.5290844714213454 heads = 6046 tails =  3954

experiment: 100000
the ratio of heads/tails is 1.491342584518797 heads = 59861 tails =  40139<jupyter_text>click for our solution  Programming a biased coin [extra] We use a simple method to create a biased coin.First, we pick a range for the precision of probabilities, say $ N $, as $ N = 11, 101, 1001, \mbox{ or }, 10^k+1 $ for some $ k > 4 $.Second, we pick the bias, say $ B $, as an integer between 0 and $ N $.We fix $ N $ and $ B $.Third, we pick a random integer between 0 and $ N $:     if it is less than $ B $, we say "Head" and      if it is equal to $ B $ or greater than $ B $, we say "Tail"     In this way, we can have a biased coin "landing" head with probability $ \frac{B}{N} $.Remark that we pick $ N = 10^k+1 $. In this way, the coin cannot be fair if $ B $ is an integer. ($ \frac{10^k+1}{2} $ is not an integer.)  Task 3 Write a function to implement the described biased coin,The inputs are integers $ N >0 $ and $ 0 \leq B < N $.The output is either "Head" or "Tail".<jupyter_code>def biased_coin(N,B):
    from random import randrange
    r = randrange(N)
    if r<B:
        return "head"
    else:
        return "tail"<jupyter_output><empty_output><jupyter_text>click for our solution  Task 4We use the biased coin defined in Task 3. (You may use the one given in the solution.)We pick $ N $ as 101.We determine $ B $ randomly, and we do not check its value immedeately.The task is to guess the bias by using the biased coin at most 500 times.Check the real bias, and calculate the error.<jupyter_code>from random import randrange

def biased_coin(N,B):
    from random import randrange
    r = randrange(N)
    if r<B:
        return "head"
    else:
        return "tail"

N = 101
B = randrange(100)
head = 0
for i in range(500):
    if biased_coin(N,B) == "Head":
        head = head + 1

k =  head/500        
real_bias = B/N
error = abs(k-real_bias)/real_bias*100 

print("k is",k)
print("real bias is",real_bias)
print("error (%) is",error)<jupyter_output>k is 0.0
real bias is 0.6831683168316832
error (%) is 100.0
{'fraction_non_alphanumeric': 0.158356236094752, 'fraction_numerical': 0.07499018453082057, 'mean_word_length': 3.5353115727002966, 'pattern_counts': {'":': 1, '<': 19, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '287795', 'license': 'Apache-2.0', 'n_tokens_mistral': 3066, 'n_tokens_neox': 2622, 'n_words': 902, 'path': 'bronze/.ipynb_checkpoints/B06_Coin_Flip-checkpoint.ipynb', 'repo_name': 'dilyaraahmetshina/quantum_computings', 'size': '7653'}