
    i~                     R    S r SSKJrJr  SSKJr  SSKJr  SSKJ	r	   " S S\	5      r
g	)
zQiskit Runtime batch mode.    )OptionalUnion)	BackendV2)QiskitRuntimeService   )Sessionc            	          ^  \ rS rSrSr SSS.S\S\\\\	4      S\\
   4U 4S jjjjrSS.S\\
   S	\\	   4S
 jjrSrU =r$ )Batch   a	  Class for running jobs in batch execution mode.

The ``batch`` mode is designed to efficiently perform experiments that comprise multiple
independent jobs.

Using the ``batch`` mode provides the following benefits:
    - The jobs' classical computation, such as compilation, is run in parallel.
      Thus, running multiple jobs in a batch is significantly faster than running them serially.

    - There is usually minimal delay between job, which can help avoid drift.

    - If you partition your workload into multiple jobs and run them in ``batch`` mode, you can
      get results from individual jobs, which makes them more flexible to work with. For example,
      if a job's results do not meet your expectations, you can cancel the remaining jobs, or
      simply re-submit that individual job and avoid re-running the entire workload.

Batch mode can shorten processing time if all jobs are provided at the outset.
If you want to submit iterative jobs, use ``session`` mode instead.

You can open a Qiskit Runtime batch by using this ``Batch`` class, then submit jobs
to one or more primitives.

For example::

    import numpy as np
    from qiskit.circuit.library import IQP
    from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
    from qiskit.quantum_info import random_hermitian
    from qiskit_ibm_runtime import QiskitRuntimeService, SamplerV2 as Sampler, Batch

    n_qubits = 127

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

    rng = np.random.default_rng()
    mats = [np.real(random_hermitian(n_qubits, seed=rng)) for _ in range(30)]
    circuits = [IQP(mat) for mat in mats]
    for circuit in circuits:
        circuit.measure_all()

    pm = generate_preset_pass_manager(backend=backend, optimization_level=1)
    isa_circuits = pm.run(circuits)

    max_circuits = 10
    all_partitioned_circuits = []
    for i in range(0, len(isa_circuits), max_circuits):
        all_partitioned_circuits.append(isa_circuits[i : i + max_circuits])
    jobs = []
    start_idx = 0

    with Batch(backend=backend):
        sampler = Sampler()
        for partitioned_circuits in all_partitioned_circuits:
            job = sampler.run(partitioned_circuits)
            jobs.append(job)

For more details, check the "`Run jobs in a batch
<https://quantum.cloud.ibm.com/docs/guides/run-jobs-batch>`_" page.
T)
create_newbackendmax_timer   c                "   > [         TU ]  XUS9  g)ag  Batch constructor.

Args:
    backend: Instance of ``Backend`` class.

    max_time:
        Maximum amount of time a runtime session can be open before being
        forcibly closed. Can be specified as seconds (int) or a string like "2h 30m 40s".
        This value must be less than the
        `system imposed maximum
        <https://quantum.cloud.ibm.com/docs/guides/max-execution-time>`_.
    create_new: If True, the POST session API endpoint will be called to create a new session.
        Prevents creating a new session when ``from_id()`` is called.
Raises:
    ValueError: If an input value is invalid.
)r   r   r   N)super__init__)selfr   r   r   	__class__s       R/home/james-whalen/.local/lib/python3.13/site-packages/qiskit_ibm_runtime/batch.pyr   Batch.__init__U   s    0 	
S    returnc                $   [        U R                  [        5      (       aq  U(       aj  U R                  R                  U R                  5      R                  U R                  5       U R                  U R                  S5      nUR                  S5      $ g)zCreate a session.batchidN)	
isinstance_servicer   _get_api_client	_instancecreate_sessionr   	_max_timeget)r   r   sessions      r   _create_sessionBatch._create_sessiono   sd    dmm%9::zmm33DNNCRRG ;;t$$r    )N)__name__
__module____qualname____firstlineno____doc__r   r   r   intstrboolr   r#   __static_attributes____classcell__)r   s   @r   r
   r
      s|    ;@ /3T
 &*TT 5c?+T
 TNT T4 ?C Xd^ xPS}  r   r
   N)r*   typingr   r   qiskit.providers.backendr   qiskit_ibm_runtimer   r"   r   r
   r%   r   r   <module>r3      s$    ! " . 3 _G _r   