Bring your own execution target to Qubernetes
A quantum simulation can start on a CPU and move to a GPU or a shared HPC system as it grows. The Python workload can stay the same even when its container image, resource requests, and scheduling rules change. In Qubernetes, an adapter supplies those settings for a computational environment.
Step 1: Register the lab-cpu adapter and provision its node pool in the Kubernetes cluster. Step 2: Select lab-cpu for the same Python script with q8sctl. Step 3: The q8s toolkit submits the Job to the cluster.
The Qubernetes team maintains adapters for Kubernetes CPU and CUDA workloads, as well as HPC workloads on CPU and AMD ROCm. CUDA adapter · HPC CPU adapter · HPC ROCm adapter
You can package and maintain an adapter independently. In this walkthrough, the adapter author and infrastructure team make lab-cpu available, the workload author selects it with q8sctl, and the q8s toolkit submits the Job to the Kubernetes cluster. Download the example files
1. Register the adapter and provision the node pool
The infrastructure team prepares a Kubernetes node pool labelled q8s.example.com/pool=lab-cpu. The adapter author uses that label as the Pod’s node selector and specifies the base image and resource requests. For this example, the workload uses a Python slim image and requests one CPU and 1 GiB of memory.
Save the adapter as src/q8s_lab_cpu/plugin.py in its own Python package.
from kubernetes import client
from q8s.constants import WORKSPACE
from q8s.plugins.job import JobPlugin
from q8s.plugins.job_template_spec import hookimpl
class LabCpuAdapter(JobPlugin):
target_name = "lab-cpu"
def get_base_image(self, python_version):
return f"python:{python_version}-slim"
@hookimpl
def makejob(
self,
name,
registry_pat,
registry_credentials_secret_name,
container_image,
workload,
env,
target,
):
if target != self.target_name:
return None
workload_env = list(env)
if workload.is_src_project:
workload_env.append(
client.V1EnvVar(name="PYTHONPATH", value=f"{WORKSPACE}/src")
)
return self.build_workload_pod_template(
workload,
name=name,
image=container_image,
env=workload_env,
image_pull_secret_name=(
registry_credentials_secret_name if registry_pat else None
),
resources=client.V1ResourceRequirements(
requests={"cpu": "1", "memory": "1Gi"}
),
node_selector={"q8s.example.com/pool": "lab-cpu"},
)get_base_image selects the starting image for q8s to build on. The makejob hook receives the resolved workload image and returns a Pod template. The shared builder adds Python launch arguments, the read-only /app mount, and image pull settings. The workload run uses the template to create and observe the Job and manages the resources it creates. If your adapter creates resources of its own, the plugin interface provides prepare and cleanup hooks. Plugin hook specification
Expose the adapter as a plugin
In your adapter repository, add a pyproject.toml that exposes the class through the q8s.targets entry point group. The adapter declares q8s 0.14.3 as its minimum because it uses the shared builder.
[build-system]
requires = ["setuptools>=61"]
build-backend = "setuptools.build_meta"
[project]
name = "q8s-lab-cpu"
version = "0.1.0"
requires-python = ">=3.10"
dependencies = ["q8s>=0.14.3"]
[project.entry-points."q8s.targets"]
lab-cpu = "q8s_lab_cpu.plugin:LabCpuAdapter"
[tool.setuptools.packages.find]
where = ["src"]In your workload project, create a requirements-dev.txt that installs q8s and the adapter into the same Python environment. This example assumes the adapter repository, which contains the pyproject.toml above, is in a sibling directory named q8s-lab-cpu.
q8s>=0.14.3
-e ../q8s-lab-cpuFrom the workload project directory, run python -m pip install -r requirements-dev.txt. The editable install lets you change the adapter locally and try it again with q8sctl.
2. Select the target for the Python workload
As the workload author, add lab-cpu to your project’s Q8Sproject. Match the target key to the installed adapter’s target_name and replace the registry username with your own. When you run the script, q8sctl selects this target via --target lab-cpu.
name: lab-demo
kubeconfig: ./kubeconfig
python_env:
python_version: "3.12"
dependencies:
- qiskit==1.1.0
targets:
lab-cpu:
python_env:
dependencies: []
docker:
username: your-dockerhub-usernamePrepare and measure a Bell state
Add a demo.py file to the workload project. A Hadamard gate on the first qubit followed by a controlled-NOT prepares the Bell state. The CPU simulator measures both qubits over 1,024 shots. Qiskit simulator documentation
from qiskit import QuantumCircuit
from qiskit.providers.basic_provider import BasicSimulator
circuit = QuantumCircuit(2, 2)
circuit.h(0)
circuit.cx(0, 1)
circuit.measure([0, 1], [0, 1])
result = BasicSimulator().run(circuit, shots=1024).result()
print(result.get_counts())3. Submit the Job to Kubernetes
With Docker authenticated to your registry and a kubeconfig for the target cluster, build and run the workload from the project directory. The q8s toolkit discovers the installed adapter through its entry point, uses its Pod template to create the Job, and submits it to the cluster. Kubernetes uses the template’s node selector to place the Pod in the labelled pool.
q8sctl build --init --target lab-cpu
q8sctl execute --target lab-cpu --kubeconfig ./kubeconfig demo.pyThe simulator should print counts for 00 and 11, with roughly half the shots in each outcome. To inspect scheduling, run it again with --submit and open the Job in your cluster’s view before it expires. Verify that the Pod was scheduled on a node in the labelled pool and that its resource requests match the adapter’s settings.
If you maintain a lab cluster, an accelerator runtime, or a shared HPC environment, you can distribute your adapter so other teams can run their Qubernetes workloads there. For a walkthrough from the workload author’s side, see How to use Catalyst with Lightning-GPU on Qubernetes .