Exchange programs with jeff

jeff is an interchange format for quantum compilers. It represents quantum operations together with classical computation and structured control flow, so a receiving compiler can continue working on the program before choosing an execution format or device.

MQT Core imports and exports jeff through jeff-mlir, the shared MLIR dialect and serialization implementation. The Python wheels include this integration; using the APIs below requires no separate jeff installation.

To exchange a program and check that its behavior survives the round trip, use the jeff tutorial. This page describes the interfaces and the program features they support.

Where jeff fits

Representation

Role in an MQT Core workflow

OpenQASM or Qiskit circuit

Provide a program through a source language or SDK.

QC and QCO

Translate and optimize within the MQT Compiler Collection.

jeff

Exchange a structured program with another compatible compiler or process.

QIR

Express a program for a compatible execution runtime.

QDMI program

Submit the format accepted by a selected device.

The serialized jeff format uses Cap’n Proto. MQT Core’s JeffProgram holds the corresponding MLIR program in memory. Its ir property prints that MLIR representation; to_bytes() and write() produce the binary exchange format.

Compile and serialize

Pass an accepted compiler input to compile_program with OutputFormat.JEFF. The same call accepts a Qiskit QuantumCircuit, an OpenQASM source string, a supported input file, or a typed compiler program:

 1from mqt.core.mlir import JeffProgram, OutputFormat, compile_program, sample
 2
 3source = """OPENQASM 3.0;
 4include "stdgates.inc";
 5qubit[2] q;
 6h q[0];
 7cx q[0], q[1];
 8bit[2] result = measure q;
 9"""
10
11program = compile_program(source, output=OutputFormat.JEFF)
12payload = program.to_bytes()
13assert isinstance(payload, bytes)
14print(f"Serialized jeff program: {len(payload)} bytes")
Serialized jeff program: 2000 bytes

Inspect program.ir when debugging the MLIR representation. To control each stage explicitly, use qco.to_jeff(copy=True) on an existing QCOProgram. Omitting copy=True consumes it.

Load a buffer, file, or message

Deserialize bytes with JeffProgram.from_bytes. The returned program can enter the compiler pipeline again:

1received = JeffProgram.from_bytes(payload)
2qc = compile_program(received, output=OutputFormat.QC)
3counts = sample(qc, shots=64, seed=17)
4assert set(counts) <= {"00", "11"} and sum(counts.values()) == 64
5counts
{'00': 27, '11': 37}

Use write and from_file for an explicit file handoff. Alternatively, pass a Path ending in .jeff directly to compile_program:

1from pathlib import Path
2from tempfile import TemporaryDirectory
3
4with TemporaryDirectory() as directory:
5    path = Path(directory) / "bell.jeff"
6    program.write(path)
7    loaded = JeffProgram.from_file(path)
8    optimized = compile_program(path, output=OutputFormat.QCO_OPTIMIZED)
9    assert sample(loaded, shots=64, seed=17) == sample(optimized, shots=64, seed=17)

Raw byte buffers go through from_bytes before compilation. compile_program uses strings for source text; a Path makes file input explicit.

Use to_segment_views and from_segments to exchange Cap’n Proto segments without flattening them into bytes:

1segments = program.to_segment_views()
2received = JeffProgram.from_segments(segments)
3assert sample(received, shots=64, seed=17) == sample(program, shots=64, seed=17)

to_segment_views returns read-only memoryview objects that keep their message storage alive independently of the program. from_segments accepts contiguous one-dimensional byte buffers whose sizes are multiples of eight. It borrows aligned buffers and copies unaligned buffers into aligned storage. Keep input buffers unchanged until the call returns; the returned program owns its data. These segments can also be passed to pycapnp’s Module.from_segments.

In C++, use mlir::JeffProgram::toMessage and fromMessage to exchange an existing Cap’n Proto message without flattening it into bytes:

capnp::MallocMessageBuilder message;
if (mlir::failed(program.toMessage(message))) {
  return mlir::failure();
}
capnp::SegmentArrayMessageReader reader(message.getSegmentsForOutput());
auto received = mlir::JeffProgram::fromMessage(reader.getRoot<::jeff::Module>());

The caller owns the message and must keep its storage alive and unchanged until fromMessage returns. Discard the message if serialization fails. The returned program can then enter the compiler pipeline.

Continue to a device or another output format

A received jeff program is a compiler input. Select a device to obtain a program in a supported format, then submit it:

1from mqt.core.mlir import submit_program
2from mqt.core.qdmi import open_device
3
4device = open_device("mqt.ddsim.default")
5compiled = compile_program(received, target=device)
6job = submit_program(compiled, target=device, num_shots=64, custom1=17)
7assert job.wait()
8assert set(job.get_counts()) <= {"00", "11"}
9print(compiled.program_format.name, job.get_counts())
QASM3 {'00': 27, '11': 37}

Choose OutputFormat.OPENQASM3, QIR_BASE, or QIR_ADAPTIVE instead when an explicit output representation is required. The target-compilation guide covers device constraints; the QIR guide covers LLVM text and bitcode.

The command-line driver also reads and writes jeff:

mqt-core compile input.qasm --emit=jeff -o exchange.jeff
mqt-core compile exchange.jeff --emit=openqasm3 -o output.qasm
mqt-core compile exchange.jeff --emit=qir-adaptive -o output.bc

Supported conversion boundary

Exchange requires both compilers to support the program’s operations and format version. The upstream format is extensible; MQT Core does not implement every possible extension. The conversion currently supports:

  • Quantum gates, modifiers in the supported normal form, measurements, and classical computation and structured control flow used in the tutorial.

  • Integer expressions through 64 bits. Integer absolute value and power require jeff’s native widths: 1, 8, 16, 32, or 64.

  • Scalar branch results and loop state, including different loop input and result tuples. Straight-line array snapshots are preserved.

  • Defined single-block functions and calls. Imported helper functions become private, while the designated entry point remains public.

  • Source-level function input names. Restored programs can list and bind named f64 parameters. Binary interchange preserves names, but not Qiskit parameter identities or parameter-vector grouping.

The following limits affect interchange:

  • Import rejects live old array values across mutating control flow and shared array updates inside switch or while regions.

  • Quantum allocations and deallocations inside conditional regions, and mutable classical-register arguments in helpers, are unsupported.

  • Native scf.index_switch and qco.index_switch require lowering before export. Target compilation lowers them to forward branches when the selected format supports forward branching but not native multiway branching. to_jeff and target-independent compile_program with OutputFormat.JEFF do not perform this target legalization.

  • Export converts physical static qubits to allocations, so static site IDs do not survive the round trip. Exchange programs before device mapping.

  • Only selected custom operations and Pauli-product rotations are supported.

  • Unitary calls inside quantum modifiers need expansion before export. The default compiler pipeline performs modifier preparation. A direct pass requires each remaining modifier to contain one unitary using all body arguments in order.

See the conversion reference for the pass requirements and upstream jeff documentation for the format. SSA names and textual IR can change during conversion; compare program semantics when validating a handoff.