from IPython.display import Image
Image("runtime.png")

from IPython.display import HTML

HTML('<iframe width="560" height="315" src="https://www.youtube.com/embed/1kRfHNUbkrg?rel=0&amp;controls=0&amp;showinfo=0" frameborder="0" allowfullscreen></iframe>')


#!pip install 'qiskit[all]'
#!pip install qiskit-ibm-runtime
#!pip install matplotlib
#!pip install pylatexenc


from qiskit_ibm_runtime import IBMRuntimeService

# Save account on disk. if you need to overwrite the account info, please add overwrite=True)
IBMRuntimeService.save_account(auth="cloud", token="Your Token", instance="your_crn", overwrite=True)

service = IBMRuntimeService()

service.pprint_programs()

service.backends()

from qiskit.test.reference_circuits import ReferenceCircuits

program_inputs = {'iterations': 1}
options = {"backend_name": "ibmq_qasm_simulator"}
job = service.run(program_id="hello-world",
                  #options=options,
                  inputs=program_inputs
                 )
print(f"job id: {job.job_id}")
result = job.result()
print(result)

from qiskit import QuantumCircuit

N = 10
qc = QuantumCircuit(N)

qc.h(0)

for kk in range(0, N -1):
    qc.cx(kk, kk + 1)

qc.measure_all()
qc.draw('mpl', fold=-1)

from qiskit import Aer, transpile
from qiskit.visualization import plot_histogram


simulator = Aer.get_backend('aer_simulator_statevector')
circ = transpile(qc, simulator)

job_statevector = simulator.run(circ, shots=20000)
counts_statevector = job_statevector.result().get_counts(0)
plot_histogram(counts_statevector)

from qiskit.test.mock import FakeMontreal
fake_backend = FakeMontreal()

from qiskit.tools.jupyter import *
fake_backend

job_noisy = fake_backend.run(circ, shots=20000)
counts_moisy = job_noisy.result().get_counts(0)
plot_histogram(counts_moisy)

service = IBMRuntimeService()
# Specify the program inputs here.
program_inputs = {
    "circuits": qc,
    "circuit_indices": [0],
#    "shots":20000
}

# Specify the backend name.

job = service.run(
    program_id="sampler",
    inputs=program_inputs,
)

# Printing the job ID in case we need to retrieve it later.
print(f"Job ID: {job.job_id}")

# Get the job result - this is blocking and control may not return immediately.
result = job.result()
print(result)

plot_histogram(result.get('quasi_dists'))

# Specify the program inputs here.
program_inputs = {
    "circuits": qc,
    "circuit_indices": [0],
#    "shots":2048
}

# Specify the backend name.
options = {"backend_name": "ibmq_qasm_simulator"}

job = service.run(
    program_id="sampler",
    options=options,
    inputs=program_inputs,
)

# Printing the job ID in case we need to retrieve it later.
print(f"Job ID: {job.job_id}")

# Get the job result - this is blocking and control may not return immediately.
qasm_result = job.result()
print(qasm_result)

plot_histogram(qasm_result.get('quasi_dists'))

IBMRuntimeService.save_account(auth="cloud", token="your token", instance="your _ crn", overwrite=True)


service = IBMRuntimeService()
service.backends()

# Specify the program inputs here.
program_inputs = {
    "circuits": qc,
    "circuit_indices": [0],
#    "shots":10000
}

# Specify the backend name.
options = {"backend_name": "ibm_algiers"}

job = service.run(
    program_id="sampler",
    options=options,
    inputs=program_inputs,
)

# Printing the job ID in case we need to retrieve it later.
print(f"Job ID: {job.job_id}")

# Get the job result - this is blocking and control may not return immediately.
real_result = job.result()
print(real_result)

plot_histogram(real_result.get('quasi_dists'))

