from qiskit import QuantumCircuit

qc = QuantumCircuit(2, 1)
qc.sx(0)
qc.delay(100, 0)
qc.cx(0, 1)
qc.measure(1, 0)

qc.draw()

from qiskit.test.mock import FakeWashington

FakeWashington().configuration().timing_constraints

from qiskit.transpiler.passmanager import PassManager
from qiskit.transpiler.passes import ALAPSchedule, AlignMeasures
from qiskit.transpiler.instruction_durations import InstructionDurations

durations = InstructionDurations([("sx", (0, ), 160), ("cx", (0, 1), 800), ("measure", (1, ), 1600)])

# this is as of Qiskit v0.19
pm = PassManager([ALAPSchedule(durations)])
pm.run(qc).draw()

# this is as of Qiskit v0.19
pm = PassManager([ALAPSchedule(durations), AlignMeasures(alignment=16)])
pm.run(qc).draw()

FakeWashington().configuration().conditional_latency

FakeWashington().configuration().acquisition_latency

from qiskit import QuantumCircuit

qc = QuantumCircuit(2, 1)
qc.x(0)
qc.measure(0, 0)
qc.x(0).c_if(0, 0)
qc.measure(1, 0)

qc.draw()

from qiskit.transpiler.passmanager import PassManager
from qiskit.transpiler.passes import ALAPSchedule
from qiskit.transpiler.instruction_durations import InstructionDurations
from qiskit.visualization import timeline_drawer

durations = InstructionDurations([("x", None, 160), ("measure", None, 1600)])

# this is as of Qiskit v0.19
pm = PassManager([ALAPSchedule(durations)])
qc_scheduled = pm.run(qc)
qc_scheduled.draw()

# this is as of Qiskit v0.19
timeline_drawer(qc_scheduled)

from qiskit import QuantumCircuit

qc = QuantumCircuit(2, 1)
qc.x(0)
qc.measure(0, 0)
qc.measure(1, 0)

qc.draw()

# this is as of Qiskit v0.19
pm = PassManager([ALAPSchedule(durations)])
qc_scheduled = pm.run(qc)
qc_scheduled.draw()

# this is as of Qiskit v0.19
timeline_drawer(qc_scheduled)

# this is as of Qiskit v0.19

from qiskit import schedule
from qiskit.test.mock import FakeBelem

try:
    sched = schedule(qc, FakeBelem())
except Exception as ex:
    print(ex)

from qiskit import QuantumCircuit

qc = QuantumCircuit(2, 1)
qc.sx(0)
qc.delay(100, 0)
qc.cx(0, 1)
qc.measure(1, 0)

qc.draw()

from qiskit.transpiler.passmanager import PassManager
from qiskit.transpiler.passes import ALAPSchedule, PadDelay, ConstrainedReschedule
from qiskit.transpiler.instruction_durations import InstructionDurations

durations = InstructionDurations([("sx", (0, ), 160), ("cx", (0, 1), 800), ("measure", (1, ), 1600)])

pm = PassManager([ALAPSchedule(durations)])
pm.run(qc).draw()

for node, t in pm.property_set["node_start_time"].items():
    print(f"{t :4d} dt: {node.name} ({node.qargs} {node.cargs})")

pm = PassManager(
    [
        ALAPSchedule(durations), 
        ConstrainedReschedule(acquire_alignment=16, pulse_alignment=16),
    ]
)
pm.run(qc).draw()

for node, t in pm.property_set["node_start_time"].items():
    print(f"{t :4d} dt: {node.name} ({node.qargs} {node.cargs})")

pm = PassManager(
    [
        ALAPSchedule(durations), 
        ConstrainedReschedule(acquire_alignment=16, pulse_alignment=16),
        PadDelay(),
    ]
)
pm.run(qc).draw()

from qiskit import QuantumCircuit
from qiskit.visualization import timeline_drawer

durations = InstructionDurations([("x", None, 160), ("measure", None, 1600)])

qc = QuantumCircuit(2, 1)
qc.x(0)
qc.measure(0, 0)
qc.x(0).c_if(0, 0)
qc.measure(1, 0)

pm = PassManager(
    [
        ALAPSchedule(durations), 
        PadDelay(),
    ]
)
qc_scheduled = pm.run(qc)
qc_scheduled.draw()

timeline_drawer(qc_scheduled)

pm = PassManager(
    [
        ALAPSchedule(durations, clbit_write_latency=300, conditional_latency=200), 
        PadDelay(),
    ]
)
qc_scheduled = pm.run(qc)
timeline_drawer(qc_scheduled)

pm = PassManager(
    [
        ALAPSchedule(durations, clbit_write_latency=1600, conditional_latency=0), 
        PadDelay(),
    ]
)
qc_scheduled = pm.run(qc)
timeline_drawer(qc_scheduled)

from qiskit.transpiler.passes import DynamicalDecoupling
from qiskit.circuit.library import XGate, YGate

ghz4 = QuantumCircuit(4)

ghz4.h(0)
ghz4.cx(0, 1)
ghz4.cx(1, 2)
ghz4.cx(2, 3)

ghz4.draw()

durations = InstructionDurations([("h", None, 160), ("x", None, 160), ("y", None, 160), ("cx", None, 800)])
dd_sequence = [XGate(), YGate(), XGate(), YGate()]

pm = PassManager(
    [
        ALAPSchedule(durations, clbit_write_latency=1600, conditional_latency=0), 
        PadDelay(),
    ]
)
qc_scheduled = pm.run(ghz4)

timeline_drawer(qc_scheduled)

pm = PassManager(
    [
        ALAPSchedule(durations, clbit_write_latency=1600, conditional_latency=0), 
        DynamicalDecoupling(durations, dd_sequence),
    ]
)
qc_scheduled = pm.run(ghz4)

timeline_drawer(qc_scheduled)

qc_scheduled.draw()

pm = PassManager(
    [
        ALAPSchedule(durations, clbit_write_latency=1600, conditional_latency=0), 
        DynamicalDecoupling(durations, dd_sequence, pulse_alignment=16),
    ]
)
qc_scheduled = pm.run(ghz4)

qc_scheduled.draw()

