|
| 1 | +/* |
| 2 | +# This code is part of Qiskit. |
| 3 | +# |
| 4 | +# (C) Copyright IBM 2017, 2026. |
| 5 | +# |
| 6 | +# This code is licensed under the Apache License, Version 2.0. You may |
| 7 | +# obtain a copy of this license in the LICENSE.txt file in the root directory |
| 8 | +# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. |
| 9 | +# |
| 10 | +# Any modifications or derivative works of this code must retain this |
| 11 | +# copyright notice, and modified files need to carry a notice indicating |
| 12 | +# that they have been altered from the originals. |
| 13 | +*/ |
| 14 | + |
| 15 | +// job class for BackendEstimator |
| 16 | + |
| 17 | +#ifndef __qiskitcpp_primitives_backend_estimator_job_def_hpp__ |
| 18 | +#define __qiskitcpp_primitives_backend_estimator_job_def_hpp__ |
| 19 | + |
| 20 | +#include <chrono> |
| 21 | +#include <thread> |
| 22 | + |
| 23 | +#include <memory> |
| 24 | +#include <stdexcept> |
| 25 | +#include <vector> |
| 26 | + |
| 27 | +#include "primitives/containers/estimator_primitive_result.hpp" |
| 28 | +#include "providers/job.hpp" |
| 29 | + |
| 30 | +// Windows headers can define ERROR after jobstatus.hpp has already been parsed. |
| 31 | +#ifdef ERROR |
| 32 | +#undef ERROR |
| 33 | +#endif |
| 34 | + |
| 35 | +namespace Qiskit { |
| 36 | +namespace primitives { |
| 37 | + |
| 38 | +/// @class BackendEstimatorJob |
| 39 | +/// @brief Job class for Backend Estimator primitive. |
| 40 | +class BackendEstimatorJob { |
| 41 | +protected: |
| 42 | + std::shared_ptr<providers::Job> job_ = nullptr; |
| 43 | + std::vector<EstimatorPub> pubs_; |
| 44 | + |
| 45 | +public: |
| 46 | + /// @brief Create a new BackendEstimatorJob. |
| 47 | + /// @param job a job pointer to run pubs. |
| 48 | + /// @param pubs a list of pubs. |
| 49 | + BackendEstimatorJob(std::shared_ptr<providers::Job> job, std::vector<EstimatorPub>& pubs) |
| 50 | + { |
| 51 | + job_ = job; |
| 52 | + pubs_ = pubs; |
| 53 | + } |
| 54 | + |
| 55 | + BackendEstimatorJob(const BackendEstimatorJob& other) |
| 56 | + { |
| 57 | + job_ = other.job_; |
| 58 | + pubs_ = other.pubs_; |
| 59 | + } |
| 60 | + |
| 61 | + ~BackendEstimatorJob() |
| 62 | + { |
| 63 | + if (job_) { |
| 64 | + job_.reset(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /// @brief get pubs for this job. |
| 69 | + /// @return a list of pub. |
| 70 | + const std::vector<EstimatorPub>& pubs(void) const |
| 71 | + { |
| 72 | + return pubs_; |
| 73 | + } |
| 74 | + |
| 75 | + /// @brief Return the status of the job. |
| 76 | + /// @return JobStatus enum. |
| 77 | + providers::JobStatus status(void) |
| 78 | + { |
| 79 | + if (!job_) { |
| 80 | + return providers::JobStatus::ERROR; |
| 81 | + } |
| 82 | + return job_->status(); |
| 83 | + } |
| 84 | + |
| 85 | + /// @brief Return whether the job is actively running. |
| 86 | + /// @return true if job is actively running, otherwise false. |
| 87 | + bool running(void) |
| 88 | + { |
| 89 | + return status() == providers::JobStatus::RUNNING; |
| 90 | + } |
| 91 | + |
| 92 | + /// @brief Return whether the job is queued. |
| 93 | + /// @return true if job is queued, otherwise false. |
| 94 | + bool queued(void) |
| 95 | + { |
| 96 | + return status() == providers::JobStatus::QUEUED; |
| 97 | + } |
| 98 | + |
| 99 | + /// @brief Return whether the job has successfully run. |
| 100 | + /// @return true if successfully run, otherwise false. |
| 101 | + bool done(void) |
| 102 | + { |
| 103 | + return status() == providers::JobStatus::DONE; |
| 104 | + } |
| 105 | + |
| 106 | + /// @brief Return whether the job has been cancelled. |
| 107 | + /// @return true if job has been cancelled, otherwise false. |
| 108 | + bool cancelled(void) |
| 109 | + { |
| 110 | + return status() == providers::JobStatus::CANCELLED; |
| 111 | + } |
| 112 | + |
| 113 | + /// @brief Return whether the job is in a final job state such as DONE or ERROR. |
| 114 | + /// @return true if job is in a final job state, otherwise false. |
| 115 | + bool in_final_state(void) |
| 116 | + { |
| 117 | + return is_final_state(status()); |
| 118 | + } |
| 119 | + |
| 120 | + /// @brief Attempt to cancel the job. |
| 121 | + /// @return false because backend cancellation is not implemented. |
| 122 | + bool cancel(void) |
| 123 | + { |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
| 127 | + /// @brief Return the results of the job. |
| 128 | + /// @return estimator primitive result. |
| 129 | + EstimatorPrimitiveResult result(void) |
| 130 | + { |
| 131 | + if (!job_) { |
| 132 | + throw std::runtime_error("BackendEstimatorJob has no provider job."); |
| 133 | + } |
| 134 | + |
| 135 | + providers::JobStatus st; |
| 136 | + while (true) { |
| 137 | + st = job_->status(); |
| 138 | + if (is_final_state(st)) { |
| 139 | + break; |
| 140 | + } |
| 141 | + std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 142 | + } |
| 143 | + |
| 144 | + if (st != providers::JobStatus::DONE) { |
| 145 | + throw std::runtime_error("BackendEstimatorJob did not finish successfully."); |
| 146 | + } |
| 147 | + |
| 148 | + uint_t num_results = job_->num_results(); |
| 149 | + if (num_results != pubs_.size()) { |
| 150 | + throw std::runtime_error("BackendEstimatorJob result count does not match the submitted pubs."); |
| 151 | + } |
| 152 | + |
| 153 | + EstimatorPrimitiveResult result; |
| 154 | + result.allocate(num_results); |
| 155 | + for (uint_t i = 0; i < num_results; i++) { |
| 156 | + result[i].set_pub(pubs_[i]); |
| 157 | + if (!job_->result(i, result[i])) { |
| 158 | + throw std::runtime_error("BackendEstimatorJob failed to read an estimator pub result."); |
| 159 | + } |
| 160 | + } |
| 161 | + return result; |
| 162 | + } |
| 163 | + |
| 164 | +protected: |
| 165 | + static bool is_final_state(providers::JobStatus status) |
| 166 | + { |
| 167 | + return status == providers::JobStatus::DONE || |
| 168 | + status == providers::JobStatus::CANCELLED || |
| 169 | + status == providers::JobStatus::FAILED || |
| 170 | + status == providers::JobStatus::ERROR; |
| 171 | + } |
| 172 | +}; |
| 173 | + |
| 174 | +} // namespace primitives |
| 175 | +} // namespace Qiskit |
| 176 | + |
| 177 | +#endif //__qiskitcpp_primitives_backend_estimator_job_def_hpp__ |
0 commit comments