Complex types, optional values

So far we have only build graphs with a single input and output and defined everything else as constants. Tierkreis allows for multiple inputs and outputs similar to python, by defining tuples of multiple fields. As an invariance, a graph will still always have exactly one output node which also mimics python.

In this example we will look at defining graph which compiles and runs a single circuit on an IBM simulator. For this you will need valid IBMQ credentials.

Inputs and Outputs

In tierkreis, inputs and outputs are defined as named tuples of TKR types. Were building a graph circuit -> int -> (BackendResult, int)

from typing import NamedTuple

from tierkreis.data.models import TKR, OpaqueType


class IBMInput(NamedTuple):
    circuit: TKR[OpaqueType["pytket._tket.circuit.Circuit"]]  # noqa: F821
    n_shots: TKR[int]


class IBMOutput(NamedTuple):
    results: TKR[OpaqueType["pytket._tket.backends.BackendResult"]]  # noqa: F821
    n_shots: TKR[int]

Now we construct the graph using the tkr-aer-worker. Note how we address inputs by their name g.inputs.circuit and for the output we construct an output object.

from aer_worker import get_compiled_circuit, submit_single
from tierkreis.builder import Graph, Workflow


def compile_run_single() -> Workflow[IBMInput, IBMOutput]:
    g = Graph(IBMInput, IBMOutput)

    compiled_circuit = g.task(
        get_compiled_circuit(
            circuit=g.inputs.circuit,
            optimisation_level=g.const(2),
        ),
    )
    res = g.task(submit_single(compiled_circuit, g.inputs.n_shots))
    return g.finish_with_outputs(IBMOutput(res, g.inputs.n_shots))

Run the required-input form with the Rust runtime. Opaque JSON values are supplied as dictionaries.

from pytket._tket.circuit import Circuit
from tierkreis.runtime import new_default


def ghz() -> Circuit:
    circ1 = Circuit(2)
    circ1.H(0)
    circ1.CX(0, 1)
    circ1.measure_all()
    return circ1


inputs = {
    "circuit": ghz().to_dict(),
    "n_shots": 1024,
}
graph = compile_run_single()
runtime = await new_default()
with runtime:
    workflow_id = await runtime.save_workflow("complex inputs", graph)
    run_id = await runtime.start_new_run(workflow_id, inputs)
    await runtime.wait_for(run_id)
    res = await runtime.get_outputs(run_id)

Default Values

Tierkreis can also supply graph inputs from defaults declared on the input NamedTuple. Previously we have defined the optimization level by setting a constant. Here we give optimisation_level the concrete default 2. We will also pull this up into the inputs:

class IBMInputOptional(NamedTuple):
    circuit: TKR[OpaqueType["pytket._tket.circuit.Circuit"]]  # noqa: F821
    n_shots: TKR[int]
    optimisation_level: TKR[int] = 2  # Defaulted input


def compile_optional() -> Workflow[IBMInputOptional, IBMOutput]:
    g = Graph(IBMInputOptional, IBMOutput)

    compiled_circuit = g.task(
        get_compiled_circuit(
            circuit=g.inputs.circuit,
            optimisation_level=g.inputs.optimisation_level,
        ),
    )
    res = g.task(submit_single(compiled_circuit, g.inputs.n_shots))
    return g.finish_with_outputs(IBMOutput(res, g.inputs.n_shots))  # type: ignore

The typed graph records the default for optimisation_level, so the Rust runtime can supply it when the graph input is omitted.

inputs = {
    "circuit": ghz().to_dict(),
    "n_shots": 1024,
}

graph = compile_optional()
with runtime:
    workflow_id = await runtime.save_workflow("defaulted optional input", graph)
    run_id = await runtime.start_new_run(workflow_id, inputs)
    await runtime.wait_for(run_id)
    res = await runtime.get_outputs(run_id)