Esempio n. 1
0
def cancel_job(job: IBMQJob, verify: bool = False) -> bool:
    """Cancel a job.

    Args:
        job: Job to cancel.
        verify: Verify job status.

    Returns:
        Whether job has been cancelled.
    """
    cancelled = False
    for _ in range(2):
        # Try twice in case job is not in a cancellable state
        try:
            cancelled = job.cancel()
            if cancelled:
                if verify:
                    status = job.status()
                    assert status is JobStatus.CANCELLED, \
                        'cancel() was successful for job {} but its ' \
                        'status is {}.'.format(job.job_id(), status)
                break
        except JobError:
            pass

    return cancelled
Esempio n. 2
0
def run_on_backend(backend,
                   qobj,
                   backend_options=None,
                   noise_config=None,
                   skip_qobj_validation=False):
    if skip_qobj_validation:
        job_id = str(uuid.uuid4())
        if is_aer_provider(backend):
            from qiskit.providers.aer.aerjob import AerJob
            temp_backend_options = backend_options[
                'backend_options'] if backend_options != {} else None
            temp_noise_config = noise_config[
                'noise_model'] if noise_config != {} else None
            job = AerJob(backend, job_id, backend._run_job, qobj,
                         temp_backend_options, temp_noise_config, False)
            job._future = job._executor.submit(job._fn, job._job_id, job._qobj,
                                               *job._args)
        elif is_basicaer_provider(backend):
            backend._set_options(qobj_config=qobj.config, **backend_options)
            job = BasicAerJob(backend, job_id, backend._run_job, qobj)
            job._future = job._executor.submit(job._fn, job._job_id, job._qobj)
        elif is_ibmq_provider(backend):
            # TODO: IBMQJob performs validation during the constructor. the following lines does not
            # skip validation but run as is.
            from qiskit.providers.ibmq.job import IBMQJob
            job = IBMQJob(backend, None, backend._api, qobj=qobj)
            job._future = job._executor.submit(job._submit_callback)
        else:
            logger.info(
                "Can't skip qobj validation for the third-party provider.")
            job = backend.run(qobj, **backend_options, **noise_config)
        return job
    else:
        job = backend.run(qobj, **backend_options, **noise_config)
        return job
Esempio n. 3
0
def update_job_tags_and_verify(
        job_to_update: IBMQJob,
        tags_after_update: List[str],
        replacement_tags: Optional[List[str]] = None,
        additional_tags: Optional[List[str]] = None,
        removal_tags: Optional[List[str]] = None) -> None:
    """Update the tags for a job and assert that the update was successful.

    Args:
        job_to_update: The job to update.
        tags_after_update: The list of tags a job should be associated after updating.
        replacement_tags: The tags that should replace the current tags
            associated with this job set.
        additional_tags: The new tags that should be added to the current tags
            associated with this job set.
        removal_tags: The tags that should be removed from the current tags
            associated with this job set.
    """
    # Update the job tags.
    _ = job_to_update.update_tags(replacement_tags=replacement_tags,
                                  additional_tags=additional_tags,
                                  removal_tags=removal_tags)

    # Cached results may be returned if quickly refreshing,
    # after an update, so wait some time.
    time.sleep(2)
    job_to_update.refresh()

    assert set(job_to_update.tags()) == set(tags_after_update), (
        'Updating the tags for job {} was unsuccessful. '
        'The tags are {}, but they should be {}.'.format(
            job_to_update.job_id(), job_to_update.tags(), tags_after_update))