Run programs through QDMI

The compiler decides how a program fits a device. QDMI provides the next part of the workflow: discover that device, submit a job, wait for completion, and read its results. How do individual shots relate to a histogram? When can a simulator also return a statevector? Can you reuse a compiled program?

This notebook answers those questions with local DDSIM. It runs independently with the tutorial setup, without credentials or an external device. The QDMI guide explains the driver, client interfaces, device implementations, and SDK integrations.

Download this notebook.

Hide code cell source

1from collections import Counter
2from math import cos, pi, sin
3
4import numpy as np
5from qiskit.visualization import plot_distribution
6
7from mqt.core.mlir import compile_program, submit_program
8from mqt.core.qdmi import ProgramFormat
9from mqt.core.qdmi import device_ids, open_device

Discover a device and inspect its capabilities

Device IDs identify configured device definitions. Listing them does not submit a program. The bundled simulator has the stable ID mqt.ddsim.default:

1print("Registered devices:", device_ids())
2device = open_device("mqt.ddsim.default")
3print("Device:", device.name())
4print("Status:", device.status().name)
5formats = device.supported_program_formats()
6print("Accepted formats:", [program_format.name for program_format in formats])
Registered devices: ['mqt.ddsim.default']
Device: MQT Core DDSIM QDMI Device
Status: IDLE
Accepted formats: ['QASM2', 'QASM3', 'QIR_BASE_STRING', 'QIR_BASE_MODULE', 'QIR_ADAPTIVE_STRING', 'QIR_ADAPTIVE_MODULE']

The accepted formats tell the client how to encode a program. Device queries also expose available operations, sites, and calibration properties. The target compiler uses that information; callers do not need to reproduce its gate mapping logic.

Opening another registered device may initialize access to a provider. Keep DDSIM selected here; use the configuration guide when connecting an external library or service.

Compile a program with a predictable distribution

Choose an angle \(\theta\). Applying \(R_y(\theta)\) to qubit 0 and a controlled-X to qubit 1 produces \(\cos(\theta/2)|00\rangle + \sin(\theta/2)|11\rangle\). For \(\theta=\pi/3\), predict the probabilities of 00 and 11.

 1theta = pi / 3
 2shots = 2048
 3seed = 17
 4source = f"""OPENQASM 3.0;
 5include "stdgates.inc";
 6qubit[2] q;
 7ry({theta}) q[0];
 8cx q[0], q[1];
 9bit[2] result = measure q;
10"""
11
12compiled = compile_program(source, target=device)
13print("Selected format:", compiled.program_format.name)
14print("Payload size:", len(compiled.payload))
Selected format: QASM3
Payload size: 124

The returned CompiledProgram stores the serialized program and the target requirements used to produce it. The compiler selects a native format from those supported by the device. Reusing this object avoids recompilation for subsequent jobs.

Submit, wait, and retrieve counts

Compilation does not submit a job. Name the destination explicitly when calling submit_program:

1job = submit_program(compiled, target=device, num_shots=shots, custom1=seed)
2assert job.wait()
3print("Final job status:", job.check().name)
4counts = job.get_counts()
5assert sum(counts.values()) == shots
6assert set(counts) == {"00", "11"}
7print(counts)
Final job status: DONE
{'00': 1554, '11': 494}

job.wait() waits for completion and reports a failed execution. A finite timeout lets a client stop waiting without cancelling the job. DDSIM uses custom1 as a positive integer random seed; custom parameters are defined by each device, not by the generic QDMI interface.

Compare the sampled distribution with the analytic prediction:

1expected = {"00": cos(theta / 2) ** 2, "11": sin(theta / 2) ** 2}
2assert abs(counts["00"] / shots - expected["00"]) < 0.05
3plot_distribution([expected, counts], legend=["Analytic probabilities", "Sampled counts"])
../_images/2d699ea359dd9a054e93cb385b4cf0aa651448a9fa3c9f64e4ea03fce656acdd.png
Explain the result

The probabilities are \(3/4\) for 00 and \(1/4\) for 11. Finite-shot counts fluctuate around those values. Increasing the shot count reduces sampling noise; it does not change the underlying state.

Recover individual shots

A histogram records how often each outcome occurs. get_shots() returns each individual outcome in unspecified order, and counting them must reconstruct the same histogram:

1samples = job.get_shots()
2assert len(samples) == shots
3assert dict(Counter(samples)) == counts
4assert job.get_counts() == counts
5print("First 16 shots:", samples[:16])
First 16 shots: ['00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00']

Reading results again does not rerun the program. QDMI bitstrings put the highest-index output bit first. The QIR tutorial uses asymmetric outputs to make that ordering visible.

Inspect the state behind terminal measurements

DDSIM can also return this program’s statevector and probabilities. State extraction depends on the execution path; request OpenQASM with zero shots to inspect the state without sampling:

1state_job = submit_program(source, target=device, program_format=ProgramFormat.QASM3, num_shots=0)
2assert state_job.wait()
3state = state_job.get_dense_statevector()
4probabilities = state_job.get_dense_probabilities()
5expected_state = np.array([cos(theta / 2), 0, 0, sin(theta / 2)], dtype=complex)
6np.testing.assert_allclose(state, expected_state, rtol=0, atol=1e-12)
7np.testing.assert_allclose(probabilities, np.abs(expected_state) ** 2, rtol=0, atol=1e-12)
8print("Statevector:", state)
9print("Probabilities:", probabilities)
Statevector: [0.8660254+0.j 0.       +0.j 0.       +0.j 0.5      +0.j]
Probabilities: [0.75 0.   0.   0.25]

These are simulator results, not measurements available from physical hardware. They describe the state before terminal measurement, not one randomly collapsed shot. Jobs with feedback, resets, or other ineligible execution paths do not provide this state. The DDSIM guide specifies which sampling and zero-shot extraction jobs support it.

Reuse the compiled program

The number of shots is a job parameter. Submit the same compiled artifact again with a smaller shot count; no recompilation is needed while the target requirements still match:

1small_job = submit_program(compiled, target=device, num_shots=128, custom1=seed)
2assert small_job.wait()
3assert sum(small_job.get_counts().values()) == 128
4assert job.get_counts() == counts
5plot_distribution([small_job.get_counts(), counts], legend=["128 shots", "2048 shots"])
../_images/4473ae7a33123548918a8ff8879041ecb8c1d0218a3c488e7dd7d60e62e94521.png

Experiment: change theta to pi / 2, rerun from the source cell, and predict the new probabilities. Changing the circuit requires recompilation; changing only the shot count requires another submission.

Change the program format, keep the job API

To select an exact execution format, pass program_format to submit_program. This compiles and submits the source in one call:

1qasm_job = submit_program(source, target=device, program_format=ProgramFormat.QASM3, num_shots=shots, custom1=seed)
2assert qasm_job.wait()
3qasm_counts = qasm_job.get_counts()
4assert sum(qasm_counts.values()) == shots
5assert abs(qasm_counts.get("00", 0) / shots - expected["00"]) < 0.05
6plot_distribution([counts, qasm_counts], legend=["Default QIR", "OpenQASM 3"])
../_images/55e0684f52221a8737b7470e1321d739b853213d34384331e47a0b1fff377598.png

The execution format changed, but the QDMI job workflow stayed the same. Different execution paths can use different random sequences; compare the semantics and distribution rather than requiring identical counts.

Use Qiskit or PennyLane to access QDMI through an SDK, and the MQSC Orchestrator to coordinate hybrid workflows with Slurm. A compiled artifact must still match the selected device’s capabilities; a stable ID does not make artifacts portable to an unrelated target.

Continue with Exchange structured programs with jeff to exchange a structured program before choosing its final execution format or device.