Query Computing Task Results/Status#

Blocking Mode#

Core Interface#

QTaskManager.get_result_sync

Characteristics#

  • Blocks the current thread until timeout or all tasks are executed and returns the result

    • For example, in the example code below, only after the computing task is executed and the result is returned to the variable res, will the next line of code print(f'res:{res}') be executed

Example#

  • The example code below uses sampling task as an example

  • QTaskManager.get_result_sync also applies to expectation estimation tasks

# python code

from qpanda3_runtime import RuntimeService
from pyqpanda3.core import *

service = RuntimeService()
service.login('your_valid_api_key')
prog = QProg()
prog << X(0)<<CNOT(0,1)<<measure(0,0)<<measure(1,1)
task = service.sample(
    prog,
    service.device('WK_C180') # Here we use the quantum computing chip with id WK_C180
)
res = task.get_result_sync() # Query the task computing result
print(f'res:{res}')

Non-Blocking Mode#

Does not block the current thread

Method 1> Try#

Core Interface#

QTaskManager.try_get_result

Characteristics#

  • Only queries the current status of the computing task and returns the results obtained so far

    • For example, in the example code below, after try_get_result queries the result, it immediately returns the result to the variable res, even if the computing task may not have finished and produced a result.

  • QTaskManager.try_get_result returns the result directly, which is a tuple.

    • The first element of the tuple corresponds to the computing result of the task (if there is no result, it is filled with the prompt string Status:Processing),

    • The second element is a boolean value indicating whether all computing results have been generated and returned (True: all computing results have been generated and returned; otherwise False)

    • The third element is a percentage indicating the proportion of results that have been generated and returned to the total

      • For example: in batch sampling tasks, when the system enables the sub-task strategy, multiple tasks are actually generated in the backend, each with its own id. try_get_result internally pulls the execution results of all these tasks.

  • QTaskManager.try_get_result only queries unfinished sub-tasks, but returns all currently obtained result data

    • The QTaskManager.try_get_result interface of the same QTaskManager object uses an incremental strategy when pulling result data, and each pulled result data is stored internally in the QTaskManager object

Example#

  • The example code below uses sampling task as an example

  • QTaskManager.try_get_result also applies to expectation estimation tasks

# python code
import time

from qpanda3_runtime import RuntimeService
from pyqpanda3.core import *

service = RuntimeService()
service.login('your_valid_api_key')
prog = QProg()
prog << X(0) << CNOT(0, 1) << measure(0, 0) << measure(1, 1)
task = service.sample(
    prog,
    service.device('WK_C180'),  # Here we use the quantum computing chip with id WK_C180
    shots=1000,
)
res = task.try_get_result()
print(f'res:{res}')
time.sleep(5)
res2 = task.try_get_result()
print(f'res2:{res2}')

A result of a certain execution is as follows

res:(['Status:Processing'], False, 'finished 0.0%')
res2:([[{'key': ['0x1', '0x2', '0x3'], 'value': [0.042, 0.025, 0.933]}]], True, 'finished 100%')

Simple analysis of results

  • When first queried, the computing task had not finished executing, and no result data was produced.

  • When second queried, the computing task had finished executing and produced result data.

Method 2> asyncio Coroutine#

Core Interface#

QTaskManager.get_result_async

Characteristics#

A coroutine function wrapped for the asyncio asynchronous library

Example#

The code below is only used to demonstrate the functionality of QTaskManager.get_result_async. In actual application, it needs to be carefully modified according to business needs

import asyncio
from qpanda3_runtime import RuntimeService
from pyqpanda3.core import *

async def fun():
    for i in range(10):
      i += 1
      print(f'i:{i}')
async def fun2(service,task_id):
    task2 = service.get_task(task_id)
    res = await task2.get_result_async()
    await asyncio.sleep(1)
    print('res:',res)
async def process(service,task_id):
    async_tasks = []
    async_tasks.append(fun2(service,task_id))
    async_tasks.append(fun())
    await asyncio.gather(*async_tasks)

if __name__ == '__main__':
    service = RuntimeService()
    service.login('your_valid_api_key')
    prog = QProg()
    prog << X(0)<<CNOT(0,1)<<measure(0,0)<<measure(1,1)
    task = service.sample(
        circuits=prog.originir(),
        device=service.device('WK_C180'),
        measure_qubits=[0,1],
        shots=100
    )
    asyncio.run(process(service,task.id()))

Method 3> Background Sub-Thread#

Core Interface#

QTaskManager.get_result_background

Characteristics#

Uses a sub-thread to pull the execution results of computing tasks in the background

  • End of sub-thread

    • The sub-thread ends after returning all result data

    • Or the sub-thread terminates when the main thread ends

  • Status monitoring of sub-thread

    • QTaskManager.get_result_background returns a detection function,

    • The return value of the detection function is a boolean value. True means the background sub-thread has ended, otherwise returns False

  • Use callback function for post-processing results

    • You can pass a callback function to QTaskManager.get_result_background

    • Like print_res in the example code below

Example#

import os
import time

from qpanda3_runtime import RuntimeService
from pyqpanda3.core import *

if __name__ == '__main__':
    service = RuntimeService()
    service.login('your_valid_api_key')

    prog = QProg()
    prog << X(0)<<CNOT(0,1)<<measure(0,0)<<measure(1,1)
    task = service.sample(
        circuits=prog.originir(),
        device=service.device('WK_C180'),
        shots=10
    )

    task2 = service.get_task(task.id())
    def print_res(res):
        print(res)
    check_status = task2.get_result_background(print_res)
    for i in range(5):
        print(f'i:{i}')

    while check_status() == False:
        time.sleep(0.1)

A result of a certain program execution

i:0
i:1
i:2
i:3
i:4
[[{'key': ['0x1', '0x3'], 'value': [0.1, 0.9]}]]