Skip to content

External/verbatim gates report the depth of the decomposition they skipped #352

Description

@ryanhill1

Description

_visit_external_gate_operation calls _visit_basic_gate_operation(operation) "just for validation" (src/pyqasm/visitor.py:1351), and that call still runs _update_qubit_depth_for_gate over the decomposition. So a gate that is not decomposed because it is external still contributes the depth of the decomposition that was skipped.

The _recording_ext_gate_depth suppression on the neighbouring line guards only the custom-gate path, not the basic-gate path.

Reproduction

from pyqasm import loads

qasm = """OPENQASM 3.0;
include "stdgates.inc";
qubit[2] q;
crz(0.5) q[0], q[1];
"""
m = loads(qasm)
m.unroll(external_gates=["crz"])
print(m.depth())   # 12, but exactly one crz statement is emitted

Only gates with a decomposition rule are affected — unroll(external_gates=["ccx"]) reports 1 correctly, as does a body of rx + cz (2).

Why it matters more now

Before #341 this required opting in per gate name via external_gates. #341 routes every gate inside a #pragma braket verbatim box through the same path, so any Braket verbatim program containing a decomposable gate silently reports an inflated depth without the user naming anything:

#pragma braket verbatim
box { crz(0.5) q[0], q[1]; }

emits one instruction and reports depth == 12.

Suggested fix

Suppress depth recording around the validation-only _visit_basic_gate_operation call, the same way _recording_ext_gate_depth already does for the custom-gate path, and record the external gate's own depth instead.

Found in review of #341 (M1).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions