from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute, Aer
from math import pi
from random import randrange

# the angle of rotation
r = randrange(1,11)
print("the picked angle is",r,"times of 2pi/11")
print()  
theta = r*2*pi/11

# we read streams of length from 1 to 11
for i in range(1,12):
    # quantum circuit with one qubit and one bit
    qreg =  QuantumRegister(1) 
    creg = ClassicalRegister(1) 
    mycircuit = QuantumCircuit(qreg,creg)
    # the stream of length i
    for j in range(i):
        mycircuit.ry(2*theta,qreg[0]) # apply one rotation for each symbol
    # we measure after reading the whole stream
    mycircuit.measure(qreg[0],creg[0])
    # execute the circuit 1000 times
    job = execute(mycircuit,Aer.get_backend('qasm_simulator'),shots=1000)
    counts = job.result().get_counts(mycircuit)
    print("stream of lenght",i,"->",counts)

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute, Aer
from math import pi
from random import randrange

# for each stream of length from 1 to 10
for i in range(1,11):
    # we try each angle of the form k*2*pi/11 for k=1,...,10
    # we try to find the best k for which we observe 1 the most
    number_of_one_state = 0
    best_k = 1
    all_outcomes_for_i = "length "+str(i)+"-> "
    for k in range(1,11):
        theta = k*2*pi/11
        # quantum circuit with one qubit and one bit
        qreg =  QuantumRegister(1) 
        creg = ClassicalRegister(1) 
        mycircuit = QuantumCircuit(qreg,creg)
        # the stream of length i
        for j in range(i):
            mycircuit.ry(2*theta,qreg[0]) # apply one rotation for each symbol
            # we measure after reading the whole stream
        mycircuit.measure(qreg[0],creg[0])
        # execute the circuit 10000 times
        job = execute(mycircuit,Aer.get_backend('qasm_simulator'),shots=10000)
        counts = job.result().get_counts(mycircuit)
        all_outcomes_for_i = all_outcomes_for_i + str(k)+ ":" + str(counts['1']) + "  "
        if int(counts['1']) > number_of_one_state:
            number_of_one_state = counts['1']
            best_k = k
    print(all_outcomes_for_i)
    print("for length",i,", the best k is",best_k)
    print()

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute, Aer
from math import pi
from random import randrange

# the angles of rotations
theta1 = 3*2*pi/31
theta2 = 7*2*pi/31
theta3 = 11*2*pi/31

# we read streams of length from 1 to 30
for i in range(1,31):
    # quantum circuit with three qubits and three bits
    qreg =  QuantumRegister(3) 
    creg = ClassicalRegister(3) 
    mycircuit = QuantumCircuit(qreg,creg)
    # the stream of length i
    for j in range(i):
        # apply rotations for each symbol
        mycircuit.ry(2*theta1,qreg[0]) 
        mycircuit.ry(2*theta2,qreg[1]) 
        mycircuit.ry(2*theta3,qreg[2]) 
    # we measure after reading the whole stream
    mycircuit.measure(qreg,creg)
    # execute the circuit N times
    N = 1000
    job = execute(mycircuit,Aer.get_backend('qasm_simulator'),shots=N)
    counts = job.result().get_counts(mycircuit)
    print(counts)
    if '000' in counts.keys():
        c = counts['000']
    else:
        c = 0
    print('000 is observed',c,'times out of',N)
    percentange = round(c/N*100,1)
    print("the ratio of 000 is ",percentange,"%")
    print()

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute, Aer
from math import pi
from random import randrange

# randomly picked angles of rotations 
k1 = randrange(1,31)
theta1 = k1*2*pi/31
k2 = randrange(1,31)
theta2 = k2*2*pi/31
k3 = randrange(1,31)
theta3 = k3*2*pi/31
print("k1 =",k1,"k2 =",k2,"k3 =",k3)
print()

max_percentange = 0
# we read streams of length from 1 to 30
for i in range(1,31):
    # quantum circuit with three qubits and three bits
    qreg =  QuantumRegister(3) 
    creg = ClassicalRegister(3) 
    mycircuit = QuantumCircuit(qreg,creg)
    # the stream of length i
    for j in range(i):
        # apply rotations for each symbol
        mycircuit.ry(2*theta1,qreg[0]) 
        mycircuit.ry(2*theta2,qreg[1]) 
        mycircuit.ry(2*theta3,qreg[2]) 
    # we measure after reading the whole stream
    mycircuit.measure(qreg,creg)
    # execute the circuit N times
    N = 1000
    job = execute(mycircuit,Aer.get_backend('qasm_simulator'),shots=N)
    counts = job.result().get_counts(mycircuit)
    # print(counts)
    if '000' in counts.keys():
        c = counts['000']
    else:
        c = 0
    # print('000 is observed',c,'times out of',N)
    percentange = round(c/N*100,1)
    if max_percentange < percentange: max_percentange = percentange
    # print("the ration of 000 is ",percentange,"%")
    # print()
print("max percentage is",max_percentange)

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute, Aer
from math import pi
from random import randrange

number_of_qubits = 4
#number_of_qubits = 5
# randomly picked angles of rotations 
theta = []
for i in range(number_of_qubits):
    k =  randrange(1,31)
    print("k",str(i),"=",k)
    theta += [k*2*pi/31]
# print(theta)

# we count the number of zeros
zeros = ''
for i in range(number_of_qubits):
    zeros = zeros + '0'
print("zeros = ",zeros)
print()

max_percentange = 0
# we read streams of length from 1 to 30
for i in range(1,31):
    # quantum circuit with qubits and bits
    qreg =  QuantumRegister(number_of_qubits) 
    creg = ClassicalRegister(number_of_qubits) 
    mycircuit = QuantumCircuit(qreg,creg)
    # the stream of length i
    for j in range(i):
        # apply rotations for each symbol
        for k in range(number_of_qubits):
            mycircuit.ry(2*theta[k],qreg[k]) 
    # we measure after reading the whole stream
    mycircuit.measure(qreg,creg)
    # execute the circuit N times
    N = 1000
    job = execute(mycircuit,Aer.get_backend('qasm_simulator'),shots=N)
    counts = job.result().get_counts(mycircuit)
    # print(counts)
    if zeros in counts.keys():
        c = counts[zeros]
    else:
        c = 0
    # print('000 is observed',c,'times out of',N)
    percentange = round(c/N*100,1)
    if max_percentange < percentange: max_percentange = percentange
    # print("the ration of 000 is ",percentange,"%")
    # print()
print("max percentage is",max_percentange)

