from random import randrange

# randomly create a 2-dimensional quantum state
def random_quantum_state():
    first_entry = randrange(100)
    first_entry = first_entry/100
    first_entry = first_entry**0.5 # we found the first value before determining its sign
    if randrange(2) == 0: 
        first_entry = -1 * first_entry
    second_entry = 1 - (first_entry**2)
    second_entry = second_entry**0.5
    if randrange(2) == 0: 
        second_entry = -1 * second_entry
    return [first_entry,second_entry]

from matplotlib.pyplot import plot, show, figure # import the useful tool for drawing figures in python

figure(figsize=(6,6), dpi=60) # size of the figure

plot(0,0,'bo') # point the origin (0,0)

for i in range(100):
    quantum_state = random_quantum_state(); # random quantum state
    plot(quantum_state[0],quantum_state[1],'bo') # put a point for the quantum state

show() # show the diagram

# import the useful tool for drawing figures in python
from matplotlib.pyplot import plot, show, figure, Circle, axis, gca, annotate, arrow, text


figure(figsize=(6,6), dpi=80) # size of the figure

gca().add_patch( Circle((0,0),1,color='black',fill=False) ) # define a circle

plot(-1.3,0)
plot(1.3,0)
plot(0,1.3)
plot(0,-1.3)

# axes
arrow(0,0,1.1,0,head_width=0.04, head_length=0.08)
arrow(0,0,-1.1,0,head_width=0.04, head_length=0.08)
arrow(0,0,0,-1.1,head_width=0.04, head_length=0.08)
arrow(0,0,0,1.1,head_width=0.04, head_length=0.08)

show() # show the diagram

def rotations(rotation_angle,number_of_rotations):
    # import all necessary objects and methods for quantum circuits
    from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute, Aer
    from qiskit.tools.visualization import matplotlib_circuit_drawer as drawer
    # import the useful tool for drawing figures in python
    from matplotlib.pyplot import plot, show, figure, Circle, axis, gca, annotate, arrow, text
    # import the constant pi
    from math import pi
    
    # we define a quantum circuit with one qubit and one bit
    qreg1 =  QuantumRegister(1) # quantum register with a single qubit
    creg1 = ClassicalRegister(1) # classical register with a single bit
    mycircuit1 = QuantumCircuit(qreg1,creg1) # quantum circuit with quantum and classical registers

    # create the plane
    figure(figsize=(6,6), dpi=80) # size of the figure
    gca().add_patch( Circle((0,0),1,color='black',fill=False) ) # draw the circle
    # auxiliary points
    plot(-1.3,0)
    plot(1.3,0)
    plot(0,1.3)
    plot(0,-1.3)
    # axes
    arrow(0,0,1.1,0,head_width=0.04, head_length=0.08)
    arrow(0,0,-1.1,0,head_width=0.04, head_length=0.08)
    arrow(0,0,0,-1.1,head_width=0.04, head_length=0.08)
    arrow(0,0,0,1.1,head_width=0.04, head_length=0.08)
    # end of create the plane

    for i in range(number_of_rotations):
        # iteratively apply the rotation 
        mycircuit1.ry(2*rotation_angle,qreg1[0]) 
        
        # the following code is used to get the quantum state of the quantum register
        job = execute(mycircuit1,Aer.get_backend('statevector_simulator'))
        current_quantum_state=job.result().get_statevector(mycircuit1) 
        x_value = current_quantum_state[0].real # get the amplitude of |0>
        y_value = current_quantum_state[1].real # get the amplitude of |1>
        # show the quantum state as an arrow on the diagram
        arrow(0,0,x_value,y_value,head_width=0.04, head_length=0.08,color='blue')

        # the following code is used to indicate the rotation number
        if x_value<0: text_x_value=x_value-0.1
        else: text_x_value=x_value+0.1
        if y_value<0: text_y_value=y_value-0.1
        else: text_y_value=y_value+0.1        
        text(text_x_value,text_y_value,'r='+str(i+1))
    # end of for-loop
    show() # show the diagram
#end of function

print("function 'rotations' is defined now, and so it can be used in the following part")

# call function rotations 8 times with angle pi/4

# import the constant pi
from math import pi
    
rotations(pi/4,8)

#
# your code is here
#


#
# your code is here
#


#
# your code is here
#


#
# your code is here
#


#
# your code is here
#


#
# your code is here
#
