from qiskit import *
Aer.backends()

IBMQ.load_accounts()
IBMQ.backends()

from pprint import pprint
backend_name = 'ibmqx4'
backend = IBMQ.get_backend(backend_name)
backend.status()

backend.configuration()

backend.properties()

from qiskit.backends.ibmq import least_busy
devices = IBMQ.backends(simulator=False)
least_busy_device = least_busy(devices)
least_busy_device.status()

last_10_jobs = least_busy_device.jobs(limit=10)
for j in last_10_jobs:
    print(j.job_id())

from qiskit.tools.visualization import circuit_drawer
qr = QuantumRegister(4)
cr = ClassicalRegister(4)
circ = QuantumCircuit(qr, cr)
circ.h(qr[0])
circ.cx(qr[0], qr[1])
circ.tdg(qr[2])
circ.cx(qr[2], qr[1])
circ.x(qr[2])
circ.cx(qr[0], qr[2])
circ.cx(qr[1], qr[3])
circ.measure(qr, cr)
circuit_drawer(circ, scale=0.5)

%matplotlib inline
qiskit.tools.visualization.matplotlib_circuit_drawer(circ)

simulator_backend = IBMQ.get_backend(simulator=True, hub=None)
job = execute(circ, simulator_backend)

import time
from qiskit.backends import JobStatus
print(job.status())
while job.status() != JobStatus.DONE:
    time.sleep(1)
    print(job.status())

qobj = compile(circ, simulator_backend)
compiled_circ = qobj_to_circuits(qobj)[0]
circuit_drawer(compiled_circ, scale=0.5)

qobj.as_dict()

job = simulator_backend.run(qobj)
result = job.result()

import qiskit.wrapper.jupyter

%%qiskit_job_status
job = execute(circ, backend)

%%qiskit_progress_bar
qobj = compile(circ, backend)

from qiskit import *
from qiskit.tools.visualization.interactive import *
q = QuantumRegister(5)
circ1 = QuantumCircuit(q)
circ1.h(q)
circ1.ry(1.3, q[0])
circ1.cx(q[0], q[1])

statevector_simulator = Aer.get_backend('statevector_simulator')
result = execute(circ1, statevector_simulator).result()
psi = result.get_statevector()
iplot_state(psi, method='qsphere') # method could be city, paulivec, qsphere, bloch, wigner
# Not displayable in github

c = ClassicalRegister(5)
circ2 = QuantumCircuit(q, c)
circ2.h(q)
circ2.measure(q, c)
circ3 = QuantumCircuit(q, c)
circ3.h(q[0])
circ3.y(q[1])
circ3.cx(q[1], q[0])
circ3.measure(q, c)

qasm_simulator = Aer.get_backend('qasm_simulator')
result = execute([circ2, circ3], qasm_simulator).result()
counts_list = [result.get_counts(circ) for circ in [circ2, circ3]]
# Needs to be run in Jupyter, will not render on github
iplot_histogram(counts_list, legend=['first circuit', 'second circuit'])
# Not displayable in github

