Leveraging parallelism through map¶
One major advantage in workflow systems is the ease of scaling computation horizontally.
Data-parallel tasks can act independently; In tierkreis this can simply be achieved through the map function.
Each map element will receive exactly one sets of inputs and can therefore be immediately dispatched.
In this example we will observe the speedup by running multiple independent graphs in parallel.
First we define a simple graph that will run a circuit in two version:
Using the qiskit aer simulator
Using the qulacs simulator
from typing import Literal, NamedTuple
from aer_worker import (
get_compiled_circuit as aer_compile,
)
from aer_worker import (
run_circuit as aer_run,
)
from qulacs_worker import (
get_compiled_circuit as qulacs_compile,
)
from qulacs_worker import (
run_circuit as qulacs_run,
)
from tierkreis.builder import Graph
from tierkreis.builtins import untuple
from tierkreis.data.models import TKR, OpaqueType
type BackendResult = OpaqueType["pytket.backends.backendresult.BackendResult"] # noqa: F821
type Circuit = OpaqueType["pytket._tket.circuit.Circuit"] # noqa: F821
class SimulateJobInputsSingle(NamedTuple):
simulator_name: TKR[Literal["aer", "qulacs"]]
circuit_shots: TKR[tuple[Circuit, int]]
compilation_optimisation_level: TKR[int]
def aer_simulate_single():
g = Graph(SimulateJobInputsSingle, TKR[BackendResult])
circuit_shots = g.task(untuple(g.inputs.circuit_shots))
compiled_circuit = g.task(
aer_compile(
circuit=circuit_shots.a,
optimisation_level=g.inputs.compilation_optimisation_level,
)
)
res = g.task(aer_run(compiled_circuit, circuit_shots.b))
return g.finish_with_outputs(res)
def qulacs_simulate_single():
g = Graph(SimulateJobInputsSingle, TKR[BackendResult])
circuit_shots = g.task(untuple(g.inputs.circuit_shots))
compiled_circuit = g.task(
qulacs_compile(
circuit=circuit_shots.a,
optimisation_level=g.inputs.compilation_optimisation_level,
)
)
res = g.task(qulacs_run(compiled_circuit, circuit_shots.b))
return g.finish_with_outputs(res)
So far these are regular graphs that compile and simulate a single circuit.
We are going to combine them into a single graph taking a parameter to decide which simulator to run using ifelse
Although we we will have two similar subgraphs in the evaluation, this is not a performance detriment as ifelse only evaluates lazily.
from tierkreis.builtins import str_eq
def compile_simulate_single():
g = Graph(SimulateJobInputsSingle, TKR[BackendResult])
aer_res = g.eval(aer_simulate_single(), g.inputs)
qulacs_res = g.eval(qulacs_simulate_single(), g.inputs)
res = g.ifelse(
g.task(str_eq(g.inputs.simulator_name, g.const("aer"))), aer_res, qulacs_res
)
return g.finish_with_outputs(res)
To make this parallel over multiple circuits we are using the map feature in a new graph.
class SimulateJobInputs(NamedTuple):
simulator_name: TKR[Literal["aer", "qulacs"]]
circuits: TKR[list[Circuit]]
n_shots: TKR[list[int]]
compilation_optimisation_level: TKR[int]
g = Graph(SimulateJobInputs, TKR[list[BackendResult]])
Each of the SimulateJobInputsSingle expects a tuple (Circuit, n_shots) which we generate by zipping
from tierkreis.builtins import tkr_zip
circuits_shots = g.task(tkr_zip(g.inputs.circuits, g.inputs.n_shots))
A convenient way to aggregate the inputs is using a map over a lambda
job_inputs = g.map(
lambda x: SimulateJobInputsSingle(
simulator_name=g.inputs.simulator_name,
circuit_shots=x,
compilation_optimisation_level=g.inputs.compilation_optimisation_level,
),
circuits_shots,
)
and finally we can map over the jobs
res = g.map(compile_simulate_single(), job_inputs)
workflow = g.finish_with_outputs(res)
Prepare JSON-compatible inputs. Circuit dictionaries are reconstructed by the worker serializer. The selected IfElse branch is loaded lazily, so the other simulator subgraph is neither loaded nor scheduled.
from pathlib import Path
from pytket.qasm.qasm import circuit_from_qasm
from tierkreis.runtime import new_default
circuit = circuit_from_qasm(Path().parent / "data" / "ghz_state_n23.qasm")
circuits = [circuit] * 3
n_shots = 1024
inputs = {
"circuits": [circuit.to_dict() for circuit in circuits],
"n_shots": [n_shots] * len(circuits),
"compilation_optimisation_level": 2,
}
runtime = await new_default()
workflow_id = await runtime.save_workflow("parallel simulators", workflow)
we can now benchmark aer by setting the simulator_name input
import time
inputs["simulator_name"] = "aer"
print("Simulating using aer...")
start = time.time()
with runtime:
run_id = await runtime.start_new_run(workflow_id, inputs)
await runtime.wait_for(run_id)
print(f"time taken: {time.time() - start}")
and
inputs["simulator_name"] = "qulacs"
print("Simulating using qulacs...")
start = time.time()
with runtime:
run_id = await runtime.start_new_run(workflow_id, inputs)
await runtime.wait_for(run_id)
print(f"time taken: {time.time() - start}")
compared against running the same graph three times:
start = time.time()
for circuit in circuits:
inputs = {
"circuit_shots": (circuit.to_dict(), n_shots),
"compilation_optimisation_level": 2,
"simulator_name": "aer",
}
sequential_id = await runtime.save_workflow(
"single simulation", compile_simulate_single()
)
with runtime:
run_id = await runtime.start_new_run(sequential_id, inputs)
await runtime.wait_for(run_id)
print(f"time taken: {time.time() - start}")