from may4_challenge.ex4 import get_unitary
U = get_unitary()
print(U)
print("U has shape", U.shape)

from qiskit import QuantumCircuit, Aer, QuantumRegister
from may4_challenge.ex4 import check_circuit, submit_circuit
from qiskit.compiler import transpile
from qiskit.extensions.quantum_initializer.isometry import Isometry
from qiskit.quantum_info import Operator
import numpy as np
from scipy.linalg import hadamard
from qiskit.quantum_info.operators.predicates import is_isometry

##### build your quantum circuit here
qc = QuantumCircuit(4)
# apply operations to your quantum circuit here
def recursiveKronecker(k, hmat):
    if 2**(k-1) == 1:
        return hmat
    else:
        return np.kron(hmat, recursiveKronecker(k-1, hmat))



h2 = np.matrix([[1,1],[1,-1]])
h2 = h2/(2**0.5)
h = recursiveKronecker(4, h2)

v = h*U*h

qc.h([0,1,2,3])
qc.isometry(v,[0,1,2,3],[])
qc.h([0,1,2,3])
sim = Aer.get_backend('qasm_simulator') 
new_qc = transpile(qc,
                   backend = sim,
                   seed_transpiler=11,
                   optimization_level=2, 
                   basis_gates = ['u3','cx'])

qc_basis = new_qc.decompose()

qc.draw()

##### check your quantum circuit by running the next line
check_circuit(qc_basis)

# Send the circuit as the final answer, can re-submit at any time
submit_circuit(qc_basis) 