Example #1
0
        public void GetJobResult(IbmQX_SendResult job_result, IbmQXSendInput options)
        {
            try
            {
                if (job_result.JobResult.Qasms.Count < 1)
                {
                    return;
                }
                if (job_result.JobResult.InfoQueue != null)
                {
                    Task.Delay(job_result.JobResult.InfoQueue.EstimatedTimeInQueue * 1000).Wait();
                }
                var    client = new HttpClient();
                string uri    = string.Format(ApiUrl + ApiJobResult, job_result.JobResult.Id, AuthenticationData.Id);
                for (int i = 0; i < options.NumRetries; i++)
                {
                    using (var streamTask = client.GetStreamAsync(uri))
                    {
                        DataContractJsonSerializerSettings settings = new DataContractJsonSerializerSettings();
                        settings.UseSimpleDictionaryFormat = true;
                        var serializer = new DataContractJsonSerializer(typeof(IbmJobMeasurmentResult), settings);
                        var data       = serializer.ReadObject(streamTask.Result) as IbmJobMeasurmentResult;
                        if (data != null && data.Status.ToUpper() == "COMPLETED" && data.Qasms.Count > 0 && data.Qasms[0].Status.ToUpper() == "DONE")
                        {
                            job_result.JobMeasurmentResult = data;
                            job_result.Results             = data.Qasms[0].result.Data.Counts;
                            job_result.State = IbmQXSendState.Ok;
                            return;
                        }
                    }

                    Task.Delay(options.SecInterval * 1000).Wait();
                }
                job_result.State = IbmQXSendState.MeasurmentTimeOut;
            }
            catch (Exception en)
            {
                job_result.ErrorMessage = en.Message;
                job_result.State        = IbmQXSendState.Exception;
            }
        }
Example #2
0
        public async Task <IbmQX_SendResult> Send(IbmQXSendInput Options)
        {
            IbmQX_SendResult result = new IbmQX_SendResult();

            try
            {
                CallLogEvent("Ibm send function has started ");
                if (Options.Device != IbmQXDevices.Simulator && await IsDeviceOnline(Options.Device) == false)
                {
                    CallLogEvent("Target device is currenlty offline");
                    result.State = IbmQXSendState.Device_is_offline;
                    return(result);
                }
                if (AuthenticationData.TokenEndTime <= DateTime.Now)
                {
                    CallLogEvent("Getting a new authentificaton token");
                    bool response_state = await Login(Options.Email, Options.Password);

                    if (response_state == false)
                    {
                        CallLogEvent("Process of getting a new authentication token failed");
                        result.State = IbmQXSendState.AuthentificationFailed;
                        return(result);
                    }
                }
                var    client         = new HttpClient();
                string uri            = string.Format(ApiUrl + ApiJobs, AuthenticationData.Id, Options.Device.ToString(), Options.Cache.ToString().ToLower(), Options.Shots);
                var    PostDataString = "{ \"qasms\": [{ \"qasm\":\"" + Options.QasmCode.Replace("\"", "\\\"") + "\"}], \"shots\":" + Options.Shots + ", \"maxCredits\":" + Options.MaxCredits + ", \"backend\":{ \"name\": \"" + Options.Device.ToString().ToLower() + "\" }}";
                var    response       = (await client.PostAsync(uri, new StringContent(PostDataString, Encoding.UTF8, "application/json")));
                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    using (HttpContent content = response.Content)
                    {
                        var serializer = new DataContractJsonSerializer(typeof(IbmJobResult));
                        result.JobResult = serializer.ReadObject(await content.ReadAsStreamAsync()) as IbmJobResult;
                        if (result.JobResult == null)
                        {
                            result.State = IbmQXSendState.CommunicationError;
                            return(result);
                        }
                        CallLogEvent("Program is executing");
                        if (result.JobResult.InfoQueue != null)
                        {
                            CallLogEvent("Queue position: " + result.JobResult.InfoQueue.Position + ", Estimated time in queue: " + result.JobResult.InfoQueue.EstimatedTimeInQueue + "s");
                            CallLogEvent("Estimated finish time: " + DateTime.Now.AddSeconds(result.JobResult.InfoQueue.EstimatedTimeInQueue).ToString("yyyy-MM-dd HH:mm:ss"));
                        }
                    }
                }
                else
                {
                    result.State = IbmQXSendState.IbmError;
                    using (HttpContent content = response.Content)
                    {
                        var serializer = new DataContractJsonSerializer(typeof(IbmErrorMessage));
                        var error_msg  = serializer.ReadObject(await content.ReadAsStreamAsync()) as IbmErrorMessage;
                        if (error_msg != null && error_msg.Error != null)
                        {
                            result.ErrorMessage = error_msg.Error.Code + ":" + error_msg.Error.Message;
                            CallLogEvent("Ibm send function has finished with error:" + result.ErrorMessage);
                            return(result);
                        }
                    }
                    result.ErrorMessage = "Unknow error";
                    CallLogEvent("Ibm send function has finished with error:" + result.ErrorMessage);
                }
                GetJobResult(result, Options);
                CallLogEvent("Ibm send function has finished succesfuly ");
                return(result);
            }
            catch (Exception en)
            {
                CallLogEvent("Ibm send function threw exception, message:" + en.Message);
                result.State        = IbmQXSendState.Exception;
                result.ErrorMessage = en.Message;
                return(result);
            }
        }