# Exercice 3 : deux circuits soumis ensemble en mode batch, et ce qu'il faut noter
from datetime import UTC, datetime

from qiskit import QuantumCircuit
from qiskit.transpiler import generate_preset_pass_manager
from qiskit_ibm_runtime import Batch, QiskitRuntimeService
from qiskit_ibm_runtime.executor_sampler import Sampler

service = QiskitRuntimeService()
backend = service.least_busy(operational=True, simulator=False)

bell = QuantumCircuit(2)
bell.h(0)
bell.cx(0, 1)
bell.measure_all()

ghz = QuantumCircuit(3)
ghz.h(0)
ghz.cx(0, 1)
ghz.cx(1, 2)
ghz.measure_all()

pm = generate_preset_pass_manager(optimization_level=1, backend=backend, seed_transpiler=7)
circuits = {"bell": pm.run(bell), "ghz": pm.run(ghz)}

with Batch(backend=backend) as batch:
    sampler = Sampler(mode=batch)
    jobs = {name: sampler.run([isa], shots=1000) for name, isa in circuits.items()}
    for name, job in jobs.items():
        counts = job.result()[0].data.meas.get_counts()
        # Tout ce qu'il faut pour comparer plus tard : machine, date, job, shots, counts
        print(name, backend.name, datetime.now(UTC).date(), job.job_id(), 1000, dict(sorted(counts.items())))
