from pprint import pprint
import numpy as np
import matplotlib.pyplot as plt

from qiskit_cold_atom.providers import ColdAtomProvider

# save an account to disk
# ColdAtomProvider.save_account(url = ["url_of_backend_1", "url_of_backend_2"], username="JohnDoe",token="123456") 

# load the stored account
provider = ColdAtomProvider.load_account()

print(provider.backends())

spin_device_backend = provider.get_backend("SYNQS_SoPa_backend")

spin_device_backend.status().to_dict()

spin_device_backend.configuration().supported_instructions

from qiskit.circuit import QuantumCircuit, Parameter
import numpy as np

t = Parameter("t")

circuit = QuantumCircuit(1, 1)
circuit.delay(duration = t, unit='s', qarg=0)
circuit.measure(0, 0)
circuit.draw(output='mpl')

# create a list of circuits with variable loading times:
load_times = np.arange(0.1, 15, 2)
circuit_list =[circuit.bind_parameters({t: load_time}) for load_time in load_times]

# send the list of circuits to the backend and execute with 5 shots each
# demo_job = spin_device_backend.run(circuit_list, shots = 5)

job_retrieved = spin_device_backend.retrieve_job(job_id = "20210520_171502_89aec")

print("job status: ", job_retrieved.status())

result = job_retrieved.result()
print(type(result))

outcomes = [result.get_memory(i) for i in range(len(circuit_list))]

atom_numbers = [np.mean(np.array(counts, dtype=float)) for counts in outcomes]
atom_stds = [np.std(np.array(counts, dtype=float)) for counts in outcomes]

import matplotlib.pyplot as plt

plt.errorbar(load_times, atom_numbers, yerr=atom_stds, fmt='x--')
plt.grid(alpha=0.5)
plt.title("loading of atoms in cold atomic device through Qiskit")
plt.xlabel("loading time [s]")
plt.ylabel("atom number in trap [a.u.]")
plt.show()

result.to_dict()

import qiskit.tools.jupyter
%qiskit_version_table
%qiskit_copyright