%matplotlib inline
import qiskit as qsk
import numpy as np
import matplotlib.pyplot as plt

qreg = qsk.QuantumRegister(2, name='q')
circ = qsk.QuantumCircuit(qreg)
circ.draw('mpl')

circ.x(qreg[0]); #Add x on 0
circ.draw('mpl')

circ.h(qreg[0]); #Add x on 0
circ.draw('mpl')

circ.cx(qreg[0], qreg[1]); #Add CNOT on 0 and 1
circ.draw(output='mpl')

backend = qsk.Aer.get_backend('statevector_simulator')
job = qsk.execute(circ, backend)
result = job.result()

outputstate = result.get_statevector(circ, decimals=3)
print(outputstate)

from qiskit.visualization import plot_state_city
plot_state_city(outputstate)

creg = qsk.ClassicalRegister(2, name='c')
circ.add_register(creg)
circ.draw('mpl')

circ.measure(qreg,creg)
circ.draw('mpl')

backend_sim = qsk.Aer.get_backend('qasm_simulator')
job_sim = qsk.execute(circ, backend_sim, shots=1024)
result_sim = job_sim.result()

counts = result_sim.get_counts(circ)
print(counts)

from qiskit.visualization import plot_histogram
plot_histogram(counts)

from qiskit import IBMQ
token = np.loadtxt("Token.txt", unpack=True, dtype=str)
IBMQ.save_account(token, overwrite=True)

provider = IBMQ.load_account();

backend_list = provider.backends()
for i in backend_list:
    print(f'{i} \n')

from qiskit.providers.ibmq import least_busy

num_qubits = 2

backend = least_busy(provider.backends(filters=lambda x: x.configuration().n_qubits >= num_qubits and not x.configuration().simulator and x.status().operational==True))
backend


from qiskit.tools.monitor import job_monitor

job_exp = qsk.execute(circ, backend=backend)
job_monitor(job_exp)

result_exp = job_exp.result()

from qiskit.visualization import plot_histogram
counts_exp = result_exp.get_counts(circ)
plot_histogram([counts_exp,counts], legend=['Device', 'Simulator'])

job_id = job_exp.job_id()

print('JOB ID: {}'.format(job_id))

retrieved_job = backend.retrieve_job(job_id)

retrieved_job.result().get_counts(circ)

qsk.__qiskit_version__

from qiskit.tools.jupyter import *
%qiskit_version_table