<jupyter_start><jupyter_text>prepared by <NAME> (<EMAIL>) | December 09, 2018 I have some macros here. If there is a problem with displaying mathematical formulas, please run me to load these macros.$ \newcommand{\bra}[1]{\langle 1\rvert} $$ \newcommand{\ket}[1]{\lvert1\rangle} $$ \newcommand{\braket}[2]{\langle 1\lvert2\rangle} $$ \newcommand{\inner}[2]{\langle 1,2\rangle} $$ \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 } $  Homework (Rotations) Deadline: January 14, 2019Send your solutions to <EMAIL>.Feel free to ask questions by e-mail.  Decision problems on streaming inputs 1. Suppose that you read a series of symbols from an alphabet $ \Sigma $.        For example, $ \Sigma = \{a,b\} $, and your inputs can be $ aaabbbabababababab $ or $ aaaaaaa $ or $ bbbbbba $, etc.2. You may use one or more qubits for solving the given task. 3. At the beginning, each qubit is set to $ \ket{0} $.4. For each symbol, you fix certain operators and apply them to the quantum register whenever you read this symbol.     For example, for each $ a $, you may apply x-gate on each qubit; and, for each $ b $, you may apply z-gate and then h-gate on each qubit.5. After reading whole the input, you make a measurement.     You should make a decision on the given input.         There will be two possible outcomes.    So, you divide all possible outcomes into two sets, and give your decisions accordingly.  Example 1 Let $ \Sigma = \{a\} $.We decide whether the length of the given input is odd or even.We use a single qubit. For each symbol, we apply x-gate.If we observe $ 0 $ (resp., $1$) at the end, we output "even" (resp., "odd"). We test our program on randomly generated 10 strings of length less than 50.<jupyter_code>from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute, Aer

def parity_check(input):
    qreg =  QuantumRegister(1)
    creg = ClassicalRegister(1)
    mycircuit = QuantumCircuit(qreg,creg)
    for i in range(len(input)):
        mycircuit.x(qreg[0])
    mycircuit.measure(qreg,creg)
    job = execute(mycircuit,Aer.get_backend('qasm_simulator'),shots=100)
    counts = job.result().get_counts(mycircuit)
    return counts

from random import randrange

for i in range(10):
    length = randrange(50)
    input = ""
    for j in range(length):
        input = input + "a"
    counts = parity_check(input)
    
    print("the input is",input)
    print("its length is",length)
    print(counts)
    for key in counts:
        if key=="0": 
            print("the output 'even' is given",counts["0"],"times")
        if key=="1": 
            print("the output 'odd' is given",counts["1"],"times")
    print()<jupyter_output><empty_output><jupyter_text>Example 2 Let $ \Sigma = \{a,b\} $.We decide whether the input contains odd numbers of $a$s and odd numbers of $b$s.We use two qubits. For each $a$, we apply x-gate to the first qubit.For each $b$, we apply x-gate to the second qubit.If we observe $ 11 $ at the end, we output "yes". Otherwise, we output "no". We test our program on randomly generated 20 strings of length less than 40.<jupyter_code>from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute, Aer

def double_odd(input):
    qreg =  QuantumRegister(2)
    creg = ClassicalRegister(2)
    mycircuit = QuantumCircuit(qreg,creg)
    for i in range(len(input)):
        if input[i]=="a":
            mycircuit.x(qreg[0])
        if input[i]=="b":
            mycircuit.x(qreg[1])
    mycircuit.measure(qreg,creg)
    job = execute(mycircuit,Aer.get_backend('qasm_simulator'),shots=100)
    counts = job.result().get_counts(mycircuit)
    return counts

from random import randrange

for i in range(20):
    length = randrange(40)
    input = ""
    number_of_as=0
    number_of_bs=0
    for j in range(length):
        if randrange(2)==0: 
            input = input + "a"
            number_of_as = number_of_as + 1
        else:
            input = input + "b"
            number_of_bs = number_of_bs + 1
            
    counts = double_odd(input)
    
    print("the input is",input)
    print("the number of as is",number_of_as)
    print("the number of bs is",number_of_bs)
    print(counts)
    number_of_yes = 0
    number_of_no = 0
    for key in counts:
        if key=="11": 
            number_of_yes = counts["11"]
        elif key=="00":
            number_of_no = number_of_no + counts["00"]
        elif key=="01":
            number_of_no = number_of_no + counts["01"]
        elif key=="11":
            number_of_no = number_of_no + counts["10"]
    print("number of yes is",number_of_yes,"and number of no is",number_of_no)
    print()<jupyter_output><empty_output><jupyter_text>Task 1 Let $ \Sigma = \{a\} $.You will read an input of length which is a multiple of $ 8 $: $ 8i \in \{8,16,24,\ldots\} $.Use a single qubit and determine whether the multiple ($ i $) is odd or even.For each $a$, you can apply a rotation.Test your program with the inputs of lengths $ 8, 16, 24, 32, 40, 48, 56, 64, 72, 80 $.<jupyter_code>#
# your solution
#<jupyter_output><empty_output><jupyter_text>Task 2 Let $ \Sigma= \{a\} $.Determine whether the length of the input is a multiple of 7 or not in the following manner:1. If it is a multiple of 7, then output "yes" with probability 1.2. If it is not a multiple of 7, then output "yes" with probability less than 1.For each $a$, you can apply a rotation.Test your program with all inputs of lengths less than 29.Determine the inputs for which you output "yes" nearly three times less than the output "no".<jupyter_code>#
# your solution
#<jupyter_output><empty_output><jupyter_text>Task 3 Write down possible six different rotation angles that would work for Task 2.  Rotations:(Double click to this cell for editing.)1. $ \cdot\frac{\pi}{\cdot} $2. $ \cdot\frac{\pi}{\cdot} $3. $ \cdot\frac{\pi}{\cdot} $4. $ \cdot\frac{\pi}{\cdot} $5. $ \cdot\frac{\pi}{\cdot} $6. $ \cdot\frac{\pi}{\cdot} $  Task 4 Experimentially test each of these rotations for Task 2.<jupyter_code>#
# your solution
#<jupyter_output><empty_output><jupyter_text>Task 5We can improve the algorihtm for Task 2.Let $ \Sigma= \{a\} $.Determine whether the length of input is a multiple of 91.There are 90 different rotations that you can use.Randomly pick four of these rotations and fix them.Use four qubits. In each qubit, apply one of these rotations.Test your program with all inputs of lengths less than 92.If the input length is 91, then your program should output "yes" with probability 1.If the input length is not 91, then your program should output "yes" with probability no more than $ \epsilon $, where $ \epsilon < \frac{1}{2}$. [*]Experimentially verify both cases, and also determine the approximate value of $\epsilon$.[*] Remark that the randomly picked rotations would work high likely. But, there is still a small chance that $\epsilon$ can be more than $ \frac{1}{2} $ because of certain sets of ratations.<jupyter_code>#
# your solution
#<jupyter_output><empty_output><jupyter_text>Task 6 Repeat Task 5 with five and then six rotations by using five and six qubits, respectively.The value of $ \epsilon $ is expected to decrease if we use more rotations and qubits.<jupyter_code>#
# your solution
#<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1431894847039665, 'fraction_numerical': 0.028381993718739094, 'mean_word_length': 3.5540254237288136, 'pattern_counts': {'":': 8, '<': 33, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '328378', 'license': 'Apache-2.0', 'n_tokens_mistral': 2947, 'n_tokens_neox': 2714, 'n_words': 952, 'path': 'bronze/.ipynb_checkpoints/B96_Homework-checkpoint.ipynb', 'repo_name': 'dilyaraahmetshina/quantum_computings', 'size': '8618'}