#
# your solution is here
#

# Find expression to create the states
def expression(a, b, c):
    return (1/(a**(0.5) - (b + 2 * (c)**(0.5))**(0.5)))

# Construct the quantum state
quantum_state = [expression(3, 5, 6), expression(3, 7, 12), expression(5, 13, 40), expression(7, 15, 26)]

# Verify validity of the quantum state 
# Square root of items must add up to 1 (comes from definition of quantum state)
# Each item correspons probability of observing the system in states |00>, |01>, |10>, |11>
print("The probability of observing the states 00, 01, 10, 11:")
prob = 0
for state in quantum_state:
    print("probability of current state " + str(state))
    prob += state ** 2


print("Verify validity of state - should be approximately 1: " + str(prob) ) 



# import all necessary objects and methods for quantum circuits
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute, Aer
# import randrange for random choices
from random import randrange

#
# your code is here
#
# Registers with 5 bits
qreg1 =  QuantumRegister(5)

# define a classical register with one bit
# it stores the measurement result of the quantum part
creg1 = ClassicalRegister(5)

# define our quantum circuit
mycircuit1 = QuantumCircuit(qreg1,creg1)

# store index of random z gates
z_gate_idx = []

# apply H gate to every bit
for i in range(5):
    mycircuit1.h(qreg1[i])
    if randrange(2) == 0: # 0.5 chance apply z gate
        mycircuit1.z(qreg1[i])
        z_gate_idx.append(i)
    # apply h gate again
    mycircuit1.h(qreg1[i])
    
# measure the first qubit, and store the result in the first classical bit
mycircuit1.measure(qreg1,creg1)

# get the numbers
job = execute(mycircuit1,Aer.get_backend('qasm_simulator'),shots=1000)
counts = job.result().get_counts(mycircuit1)
print(counts) # print the outcomes
print("z gate applied to: " + str(z_gate_idx))
# It can be seen that Z-gate changes value of the q-bit

# draw the circuit
mycircuit1.draw(output='mpl',reverse_bits=True)
# reexecute this cell if you DO NOT see the circuit diagram

