qscript_to_meta

qscript_to_meta(qscript, precision=8, parameter_order=None, was_multi_obs=None)[source]

Shared helper: convert a PennyLane QuantumScript to a MetaCircuit.

Used by PennyLaneSpecStage and by the program-layer factories in divi.qprog.algorithms. Builds the circuit body from the qscript and derives the measurement observable (SparsePauliOp) or measured-wire tuple from the qscript’s single measurement.

Parameters:
  • qscript (QuantumScript) –

    PennyLane QuantumScript. Accepts:

    • a single probs/counts measurement;

    • one or more expval measurements — sets observable to a tuple[SparsePauliOp, ...], one entry per measurement. Mixing expval with probs/counts in one QuantumScript is not supported.

  • precision (int) – MetaCircuit.precision for numeric gate parameters.

  • parameter_order (tuple[Parameter, ...] | None) – Explicit parameter ordering for the resulting MetaCircuit. Use when the qscript’s first-appearance order doesn’t match env.param_sets columns (e.g. ansatz builds gates in a different order than the flat weight array). When None, ordering is inferred from the qscript (first appearance).

  • was_multi_obs (bool | None) – Optional override for the resulting MetaCircuit’s _was_multi_obs flag. When None (default), inferred from the script: more than one expval measurement → True, otherwise False. Pass True when the higher-level caller knows the user opted into the multi-observable API (e.g. observable=[O] in TimeEvolution) even when only one expval ends up in the script.