2727
2828#include " primitives/base/base_primitive_job.hpp"
2929#include " providers/backend.hpp"
30-
31- #include " qrmi.h"
30+ #include " providers/job.hpp"
3231
3332namespace Qiskit {
3433namespace primitives {
@@ -37,70 +36,103 @@ namespace primitives {
3736// / @brief Job class for Backend Sampler primitive.
3837class BackendSamplerJob : public BasePrimitiveJob {
3938protected:
40- providers::BackendV2& backend_ ;
39+ std::shared_ptr< providers::Job> job_ = nullptr ;
4140public:
4241 // / @brief Create a new BasePrimitiveJob
43- // / @param backend a backend to be used in this job
44- // / @param id a unique id identifying the job.
45- BackendSamplerJob (providers::BackendV2& backend, std::string id, std::vector<SamplerPub>& pubs) : BasePrimitiveJob(id, pubs), backend_(backend) {}
42+ // / @param job a job pointer to run pubs
43+ // / @param pubs a list of pub
44+ BackendSamplerJob (std::shared_ptr<providers::Job> job, std::vector<SamplerPub>& pubs) : BasePrimitiveJob(pubs)
45+ {
46+ job_ = job;
47+ }
48+
49+ BackendSamplerJob (const BackendSamplerJob& other) : BasePrimitiveJob(other)
50+ {
51+ job_ = other.job_ ;
52+ }
4653
54+ ~BackendSamplerJob ()
55+ {
56+ if (job_)
57+ job_.reset ();
58+ }
4759
4860 // / @brief Return the status of the job.
4961 // / @return JobStatus enum.
5062 providers::JobStatus status (void ) override
5163 {
52- return backend_. status (job_id_ );
64+ return job_-> status ();
5365 }
5466
5567 // / @brief Return whether the job is actively running.
5668 // / @return true if job is actively running, otherwise false.
5769 bool running (void ) override
5870 {
59- return backend_. status (job_id_ ) == providers::JobStatus::RUNNING ;
71+ return job_-> status () == providers::JobStatus::RUNNING ;
6072 }
6173
6274 // / @brief Return whether the job is queued.
6375 // / @return true if job is queued, otherwise false.
6476 bool queued (void )
6577 {
66- return backend_. status (job_id_ ) == providers::JobStatus::QUEUED ;
78+ return job_-> status () == providers::JobStatus::QUEUED ;
6779 }
6880
6981 // / @brief Return whether the job has successfully run.
7082 // / @return true if successfully run, otherwise false.
7183 bool done (void ) override
7284 {
73- return backend_. status (job_id_ ) == providers::JobStatus::DONE ;
85+ return job_-> status () == providers::JobStatus::DONE ;
7486 }
7587
7688 // / @brief Return whether the job has been cancelled.
7789 // / @return true if job has been cancelled, otherwise false.
7890 bool cancelled (void ) override
7991 {
80- return backend_. status (job_id_ ) == providers::JobStatus::CANCELLED ;
92+ return job_-> status () == providers::JobStatus::CANCELLED ;
8193 }
8294
8395 // / @brief Return whether the job is in a final job state such as DONE or ERROR.
8496 // / @return true if job is in a final job state, otherwise false.
8597 bool in_final_state (void ) override
8698 {
87- auto status = backend_. status (job_id_ );
99+ auto status = job_-> status ();
88100 if (status == providers::JobStatus::DONE || status == providers::JobStatus::CANCELLED || status == providers::JobStatus::FAILED )
89101 return true ;
90102 return false ;
91103 }
92104
93-
94105 // / @brief Attempt to cancel the job.
95106 bool cancel (void ) override
96107 {
97- return backend_.stop_job (job_id_);
108+ // return job_.stop_job();
109+ return true ;
98110 }
99111
100112 PrimitiveResult result (void ) override
101113 {
102- auto result = backend_.result (job_id_);
103- result.set_pubs (pubs_);
114+ providers::JobStatus st;
115+ while (true ) {
116+ st = job_->status ();
117+ if (st == providers::JobStatus::DONE || st == providers::JobStatus::CANCELLED || st == providers::JobStatus::FAILED )
118+ break ;
119+ #ifdef _MSC_VER
120+ Sleep (1 );
121+ #else
122+ std::this_thread::sleep_for (std::chrono::seconds (1 ));
123+ #endif
124+ }
125+
126+ PrimitiveResult result;
127+ if (st == providers::JobStatus::DONE ) {
128+ uint_t num_results = job_->num_results ();
129+ result.allocate (num_results);
130+
131+ for (uint_t i = 0 ; i< num_results; i++) {
132+ result[i].set_pub (pubs_[i]);
133+ job_->result (i, result[i]);
134+ }
135+ }
104136 return result;
105137 }
106138};
0 commit comments