from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from numpy import pi
import numpy as np
from qiskit.tools.monitor import job_monitor
from qiskit import *
import time
from datetime import date, datetime
import csv

fields = ['Backend', 'Date', 'Jobs in Queue', 'Start Time', 'Finish Time']
with open('Data/Analysis_Part_One/Noise_Delay_RandomCircuit.csv', 'a+') as f:
    write = csv.writer(f)
    write.writerow(fields)
f.close()
machine_list = ['ibmq_belem','ibmq_quito','ibmq_athens','ibmq_lima','ibmq_5_yorktown','ibmq_santiago']
IBMQ.save_account('XXX',overwrite=True)
IBMQ.load_account()
provider = IBMQ.get_provider(hub='ibm-q-research', group='XXX', project='main')
while True:
    for each in machine_list:
        backend = provider.get_backend(each)
        status = backend.status()
        jobs_in_queue = status.pending_jobs
        qreg_q = QuantumRegister(5, 'q')
        creg_c = ClassicalRegister(5, 'c')
        circuit = QuantumCircuit(qreg_q, creg_c)

        circuit.cx(qreg_q[0], qreg_q[1])
        circuit.tdg(qreg_q[2])
        circuit.h(qreg_q[3])
        circuit.rz(pi/2, qreg_q[0])
        circuit.z(qreg_q[1])
        circuit.p(pi/2, qreg_q[2])
        circuit.swap(qreg_q[3], qreg_q[4])
        circuit.rx(pi/2, qreg_q[0])
        circuit.t(qreg_q[1])
        circuit.rz(pi/2, qreg_q[2])
        circuit.z(qreg_q[3])
        circuit.h(qreg_q[1])
        circuit.rz(pi/2, qreg_q[3])
        circuit.ry(pi/2, qreg_q[1])
        circuit.rxx(pi/2, qreg_q[2], qreg_q[3])
        circuit.rzz(pi/2, qreg_q[0], qreg_q[1])
        circuit.h(qreg_q[2])
        circuit.p(pi/2, qreg_q[3])
        circuit.tdg(qreg_q[0])
        circuit.s(qreg_q[1])
        circuit.s(qreg_q[2])
        circuit.s(qreg_q[3])
        circuit.rz(pi/2, qreg_q[0])
        circuit.id(qreg_q[1])
        circuit.p(pi/2, qreg_q[2])
        circuit.h(qreg_q[3])
        circuit.cx(qreg_q[1], qreg_q[2])
        circuit.h(qreg_q[3])
        circuit.ccx(qreg_q[0], qreg_q[1], qreg_q[2])
        circuit.swap(qreg_q[3], qreg_q[4])
        circuit.h(qreg_q[0])
        circuit.h(qreg_q[2])
        circuit.z(qreg_q[3])
        circuit.cx(qreg_q[0], qreg_q[1])
        circuit.tdg(qreg_q[2])
        circuit.rz(pi/2, qreg_q[3])
        circuit.rz(pi/2, qreg_q[0])
        circuit.z(qreg_q[1])
        circuit.p(pi/2, qreg_q[2])
        circuit.rx(pi/2, qreg_q[0])
        circuit.t(qreg_q[1])
        circuit.rz(pi/2, qreg_q[2])
        circuit.h(qreg_q[1])
        circuit.rxx(pi/2, qreg_q[2], qreg_q[3])
        circuit.ry(pi/2, qreg_q[1])
        circuit.h(qreg_q[2])
        circuit.p(pi/2, qreg_q[3])
        circuit.rzz(pi/2, qreg_q[0], qreg_q[1])
        circuit.s(qreg_q[2])
        circuit.s(qreg_q[3])
        circuit.tdg(qreg_q[0])
        circuit.s(qreg_q[1])
        circuit.p(pi/2, qreg_q[2])
        circuit.h(qreg_q[3])
        circuit.rz(pi/2, qreg_q[0])
        circuit.id(qreg_q[1])
        circuit.measure(qreg_q[3], creg_c[3])
        circuit.measure(qreg_q[0], creg_c[0])
        today = date.today()
        now = datetime.now().strftime("%Y_%m_%d-%I_%M_%S_%p")
        start = time.time()
        job = execute(circuit, backend)
        job_monitor(job, interval=2)
        results = job.result()
        answer = results.get_counts(circuit)
        finish_time = (time.time() - start)
        name = backend.name()
        rows = [name, now, jobs_in_queue, start, finish_time]
        with open('Data/Analysis_Part_One/Noise_Delay_RandomCircuit.csv', 'a+') as f:
            write = csv.writer(f)
            # write.writerow(fields)
            write.writerow(rows)
    time.sleep(1800)