import numpy as np

from qiskit import QuantumCircuit, Aer, IBMQ, assemble, execute, QuantumRegister, ClassicalRegister
from qiskit.quantum_info import Statevector
from qiskit.visualization import plot_histogram, plot_bloch_multivector 

# Loading your IBM Quantum account(s)
provider = IBMQ.load_account()

qc = QuantumCircuit(1)
qc.x(0)

state = Statevector.from_instruction(qc)
plot_bloch_multivector(state)

qc = QuantumCircuit(1)
qc.h(0)

state = Statevector.from_instruction(qc)
plot_bloch_multivector(state)

qc = QuantumCircuit(1)
qc.x(0)
qc.h(0)

state = Statevector.from_instruction(qc)
plot_bloch_multivector(state)

qcX = QuantumCircuit(1)
qcX.u(np.pi, 0, np.pi, 0)
qcX.draw('mpl')

qcZ = QuantumCircuit(1)
qcZ.u(0, 0, np.pi, 0)
qcZ.draw('mpl')

qcY = QuantumCircuit(1)
qcY.u(np.pi, np.pi/2, np.pi/2, 0)
qcY.draw('mpl')

qcH = QuantumCircuit(1)
qcH.u(np.pi/2, 0, np.pi, 0)
qcH.draw('mpl')

n_qubits = 1 #number of qubits
n_bits = 1 #number of bits

q = QuantumRegister(n_qubits, 'qbit' )
c = ClassicalRegister(n_bits, 'cbit' )

qc = QuantumCircuit(q,c)
qc.u(np.pi/4,np.pi/4,np.pi/4, q[0]) #or just 0

qc.measure(0,0)

qc.draw('mpl')

# we choose the simulator as our backend
backend = Aer.get_backend('qasm_simulator') 

# we run the simulation and get the counts
Counts = execute(qc, backend, shots = 1024,  seed_simulator=312).result().get_counts() 

# let us plot a histogram to see the possible outcomes and corresponding probabilities
plot_histogram(Counts)

qc=QuantumCircuit(2,2)
qc.cx(0,1)

qc.draw('mpl')

qc=QuantumCircuit(2,2)
qc.x(0)
qc.cx(0,1)

qc.draw('mpl')

qc = QuantumCircuit(2,2)
qc.h(0)
qc.cx(0,1)

for i in range(2):
    qc.measure(i,i)

qc.draw('mpl')

# we run the simulation and get the counts
# this will be better exaplained in the notebook 'qasm_simulator_and_visualization.ipynb' 
counts = execute(qc, backend, shots = 1024).result().get_counts() 

#plot histogram
plot_histogram(counts) 

qc = QuantumCircuit(2,2)
qc.h(0)
qc.x(1)
qc.z(0)
qc.cx(0,1)

for i in range(2):
    qc.measure(i,i)

qc.draw('mpl')

counts = execute(qc, backend, shots = 1024).result().get_counts()
plot_histogram(counts) 