from qiskit import QuantumCircuit
from qiskit import transpiler
from qiskit import Aer,BasicAer,execute,execute_function
import time
import math

#circ.measure(1,1)
backend = Aer.get_backend('aer_simulator')

means = {
    "h":0,
    "cx":0,
    "x":0,
    "rx":0,
    "rz":0,
    "sdg":0

}

iterations = 3
sleep_time = 0.2
#Hadamard
times = 0
for i in range(iterations):
    circ = QuantumCircuit(1,1)
    circ.h(0)
    circ.measure(0,0)
    job = execute(circ, backend)
    times = times + job.result().time_taken
    time.sleep(sleep_time)
means['h'] = 100*times/iterations

#X
times = 0
for i in range(iterations):
    circ = QuantumCircuit(1,1)
    circ.x(0)
    circ.measure(0,0)
    job = execute(circ, backend)
    times = times + job.result().time_taken
    time.sleep(sleep_time)
means['x'] = 100*times/iterations

#RZ
times = 0
for i in range(iterations):
    circ = QuantumCircuit(1,1)
    circ.rz(math.pi/2,0)
    circ.measure(0,0)
    job = execute(circ, backend)
    times = times + job.result().time_taken
    time.sleep(sleep_time)
means['rz'] = 100*times/iterations

#RX
times = 0
for i in range(iterations):
    circ = QuantumCircuit(1,1)
    circ.rx(math.pi/2,0)
    circ.measure(0,0)
    job = execute(circ, backend)
    times = times + job.result().time_taken
    time.sleep(sleep_time)
means['rx'] = 100*times/iterations


#CX
times = 0
for i in range(iterations):
    circ = QuantumCircuit(2,2)
    circ.cx(0,1)
    circ.measure(0,0)
    circ.measure(1,1)
    job = execute(circ, backend)
    times = times + job.result().time_taken
    time.sleep(sleep_time)
means['cx'] = 100*times/iterations


#SDG
times = 0
for i in range(iterations):
    circ = QuantumCircuit(1,1)
    circ.sdg(0)
    circ.measure(0,0)
    job = execute(circ, backend)
    times = times + job.result().time_taken
    time.sleep(sleep_time)
means['sdg'] = 100*times/iterations


f = open('gate_costs.txt', 'w')
for gate in means:
    f.write(gate + ((4-len(gate))*" ") + "- " +str(means[gate]) + "\n")
f.close()

backend = BasicAer.get_backend('qasm_simulator')
from time import  time

circ = QuantumCircuit(1,1)
circ.h(0)
job = execute(circ, backend)
times = job.result().time_taken
print(times)


