Asynchronous jobs
In this tutorial, we will learn how to run jobs asynchronously using Julia tasks. The use of asynchronous jobs allows other computations to continue while waiting for results from the QPU.
Julia tasks
Practical applications of quantum computing typically involve both classical and quantum computation. A quantum processor is a hardware accelerator in this paradigm. It may therefore be desirable to continue some of the classical computations while the program waits for the quantum hardware to complete its tasks. This is an example of asynchronous programming. We recommend that you consult Julia's page on asynchronous programming if you are unfamiliar with this concept.
In Snowflurry
, the function that communicates with a quantum processor will yield execution while it waits for a response from the quantum computer. This allows other computations to continue while the quantum computer is running our job.
Code
To provide maximum flexibility, Snowflurry does not impose any restrictions on how you parallelize your code. We cannot know what will be best for your code. That is up to you!
As shown in the Running a Circuit on a Real Hardware tutorial, we will start by importing Snowflurry, building our circuit, and defining our QPU:
using Snowflurry
circuit = QuantumCircuit(qubit_count = 2, instructions = [
hadamard(1),
control_x(1, 2),
readout(1, 1),
readout(2, 2),
])
user = ENV["THUNDERHEAD_USER"]
token = ENV["THUNDERHEAD_API_TOKEN"]
host = ENV["THUNDERHEAD_HOST"]
project = ENV["THUNDERHEAD_PROJECT_ID"]
realm = ENV["THUNDERHEAD_REALM"]
qpu = AnyonYukonQPU(host = host, user = user, access_token = token, project_id = project, realm = realm)
Next, we are going to define and schedule our task:
shot_count = 200
task = Task(() -> run_job(qpu, circuit, shot_count))
schedule(task)
Do not forget the last line in the previous code block! It is important to schedule
the task
, otherwise it will not start!
We then need to yield execution of the current thread to the newly scheduled task:
yieldto(task)
# Simulate work by calculating the nth Fibonacci number slowly
function fibonacci(n)
if n <= 2
return 1
end
return fibonacci(n - 1) + fibonacci(n - 2)
end
fibonacci(30)
This ensures that the scheduler starts the task. Otherwise, it might take a while for our task to start and for our jobs to be submitted to the quantum computer! Our program can also perform other computations after yielding. We can then fetch the results from our task:
result = fetch(task)
println(result)
The full code is available at tutorials/asynchronous_jobs.jl.