#
# your code is here
#
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute, Aer
from math import pi, cos, sin
from random import randrange

# quantum circuit with three qubits and three bits
qreg =  QuantumRegister(3) 
creg = ClassicalRegister(3) 
mycircuit = QuantumCircuit(qreg,creg)

# rotate the first qubit by random angle
r = randrange(100)
theta = 2*pi*(r/100) # radians
print("the picked angle is",r*3.6,"degrees and",theta,"radians")
a = cos(theta)
b = sin(theta)
print("a=",round(a,3),"b=",round(b,3))
print("a*a=",round(a**2,3),"b*b=",round(b**2,3))
print()
mycircuit.ry(2*theta,qreg[0])

# creating an entanglement between the second and third qubits
mycircuit.h(qreg[1])
mycircuit.cx(qreg[1],qreg[2])

# CNOT operator by Asja on her qubits where the first qubit is the control qubit 
mycircuit.cx(qreg[0],qreg[1])

# Hadamard operator by Asja on the first qubit
mycircuit.h(qreg[0])

# measurement done by Asja
mycircuit.measure(qreg[0],creg[0])
mycircuit.measure(qreg[1],creg[1])

# read the state vector
job = execute(mycircuit,Aer.get_backend('statevector_simulator'))
current_quantum_state=job.result().get_statevector(mycircuit)
print("the state vector is")
for i in range(len(current_quantum_state)):
    print(current_quantum_state[i].real)
print()

# reverse the order
def get_state(i):
    if i==0: return "000"
    if i==1: return "100"
    if i==2: return "010"
    if i==3: return "110"
    if i==4: return "001"
    if i==5: return "101"
    if i==6: return "011"
    if i==7: return "111"

balvis_state = ""
for i in range(len(current_quantum_state)):
    if current_quantum_state[i].real!=0:
        if abs(current_quantum_state[i].real-a)<0.000001: 
            balvis_state += "+a|"+ get_state(i)+">"
        elif abs(current_quantum_state[i].real+a)<0.000001:
            balvis_state += "-a|"+ get_state(i)+">"
        elif abs(current_quantum_state[i].real-b)<0.000001: 
            balvis_state += "+b|"+ get_state(i)+">"
        elif abs(current_quantum_state[i].real+b)<0.000001: 
            balvis_state += "-b|"+ get_state(i)+">"
print("which is",balvis_state)

mycircuit.draw()