import numpy as np
from qiskit import QuantumCircuit, Aer, execute
from qiskit.circuit.random import random_circuit
from qiskit.visualization import plot_state_qsphere
from qiskit.quantum_info.operators import Operator

def pretty_print(x):
    with np.printoptions(precision=3, suppress=True):
        print(x)

n = 3
circ = random_circuit(n,n)
circ.draw(output='mpl')


circ.draw(output='latex')

result_statevec_sim = execute(circ, Aer.get_backend('statevector_simulator')).result()
psi = result_statevec_sim.get_statevector(circ, decimals=3)
pretty_print(psi)

plot_state_qsphere(psi)

result_unitary_sim = execute(circ, Aer.get_backend('unitary_simulator')).result()
U = result_unitary_sim.get_unitary(circ, decimals=3)
pretty_print(U)

op = Operator(circ) # implicitly uses unitary simulator
op.dim

x = np.zeros(2**n)
x[0] = 1
pretty_print(op.data.dot(x))

