# a T1 circuit
from qiskit.circuit import QuantumCircuit
qc = QuantumCircuit(1, 1)
qc.x(0)
qc.delay(10, 0, unit="us")
qc.measure(0, 0)
qc.draw(output="mpl")

from qiskit import IBMQ
IBMQ.load_account()
provider = IBMQ.get_provider(hub='ibm-q', group='open', project='main')
device_backend = provider.get_backend('ibmq_armonk')
device_backend

from qiskit import transpile
from qiskit.providers.aer import AerSimulator
from qiskit.providers.aer.noise import NoiseModel
from qiskit_experiments.library import T1 as T1Experiment

print(f"(Backend: {device_backend.name()}, Qubit: 0)")
print(f"Reported T1={device_backend.properties().t1(0) * 1e+3:.3f}[ms]")

t1exp = T1Experiment(0, delays=list(range(1, 400, 10)), unit="us")

t1exp.circuits()[0].draw(output="mpl")  # the first T1 circuit

t1exp.circuits()[1].draw(output="mpl")  # the second T1 circuit

t1exp.circuits()[-1].draw(output="mpl")  # the last T1 circuit

old_noise_model = NoiseModel.from_backend(device_backend)
old_noise_model._custom_noise_passes = []
old_sim = AerSimulator.from_backend(device_backend, noise_model=old_noise_model)
t1exp.set_transpile_options(scheduling_method="alap")
exp_data = t1exp.run(old_sim, seed_simulator=123).block_for_results()
result = exp_data.analysis_results()[0]
print(f"Ref: Simulated T1 with old noise model={result.value}")
display(exp_data.figure(0))

new_sim = AerSimulator.from_backend(device_backend)  # call NoiseModel.from_backend() behind the scene
t1exp.set_transpile_options(scheduling_method="alap")
exp_data = t1exp.run(new_sim, seed_simulator=123).block_for_results()
result = exp_data.analysis_results()[0]
print(f"Simulated T1={result.value}")
print(f"Ref: Reported T1={device_backend.properties().t1(0) * 1e+3:.3f}[ms]")
display(exp_data.figure(0))

# original circuit
circ = t1exp.circuits()[1]
circ.draw(output="mpl")

# transpiled circuit (scheduled circuit)
transpiled_circ = transpile(circ, device_backend, scheduling_method="alap")
transpiled_circ.draw(output="mpl")

# noisy circuit
noise_model = NoiseModel.from_backend(device_backend)
noisy_circ = noise_model._pass_manager().run(transpiled_circ)  # within assembler
noisy_circ.draw(output="mpl")

noise_model = NoiseModel.from_backend(device_backend)
nmp = noise_model._pass_manager()
print(nmp.passes())