Skip to content

Commit 4efc9f4

Browse files
committed
Add Python tests for inspection methods
Assisted-by: GPT-5.6 Sol via Codex
1 parent 7c43423 commit 4efc9f4

2 files changed

Lines changed: 62 additions & 5 deletions

File tree

‎.agent/plans/extend-inspection-methods.md‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ textual MLIR.
3232
complete documentation build cannot finish because the local Graphviz
3333
`dot` executable is absent; Sphinx reached notebook execution before this
3434
unrelated environment failure.
35+
- [x] (2026-08-26 16:43Z) Fixed all nine clang-tidy findings from the draft PR
36+
and added Python binding tests for gate histograms, static depth, SCF, and
37+
dynamic register indices. The focused Python tests pass on Python 3.11
38+
through 3.14.
3539

3640
## Surprises & Discoveries
3741

@@ -52,6 +56,9 @@ textual MLIR.
5256
executable for an existing notebook. Evidence: Sphinx stopped in
5357
`docs/dd_package.md` with
5458
`ExecutableNotFound: failed to execute PosixPath('dot')`.
59+
- Observation: `getConstantIntValue` is declared by
60+
`mlir/Dialect/Utils/StaticValueUtils.h`, not `mlir/IR/Matchers.h`. The draft
61+
PR's include-cleaner check identified the indirect declaration.
5562

5663
## Decision Log
5764

@@ -73,10 +80,11 @@ textual MLIR.
7380
## Outcomes & Retrospective
7481

7582
The C++ and Python APIs are implemented. All 136 compiler tests and the full
76-
lint suite pass. A Python smoke test returns `{'ctrl': 1, 'h': 1, 'x': 1}` and
77-
depth 2 for two parallel gates followed by a controlled gate. The generated stub
78-
and Python guide expose both methods. The complete documentation build remains
79-
unavailable only because the local Graphviz executable is missing.
83+
lint suite pass. Focused Python tests pass on Python 3.11 through 3.14. A Python
84+
smoke test returns `{'ctrl': 1, 'h': 1, 'x': 1}` and depth 2 for two parallel
85+
gates followed by a controlled gate. The generated stub and Python guide expose
86+
both methods. The complete documentation build remains unavailable only because
87+
the local Graphviz executable is missing.
8088

8189
## Context and Orientation
8290

@@ -196,4 +204,5 @@ the compiler library. Do not introduce a new external dependency.
196204

197205
Plan revision note: Finalized after implementation and validation. Recorded the
198206
successful compiler tests, stub generation, Python smoke test, and lint suite,
199-
as well as the unrelated local documentation dependency failure.
207+
as well as the unrelated local documentation dependency failure. Updated after
208+
the draft PR review to record the clang-tidy fixes and Python tests.

‎test/python/test_mlir.py‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,3 +701,51 @@ def test_qc_program_num_gates() -> None:
701701
assert program.num_gates() == 2
702702
assert program.num_single_qubit_gates() == 1
703703
assert program.num_two_qubit_gates() == 1
704+
assert program.gate_counts() == {"ctrl": 1, "h": 1}
705+
assert program.static_depth() == 2
706+
707+
708+
def test_qc_program_num_gates_in_structured_control_flow() -> None:
709+
"""Gate counting includes each structured control-flow region once."""
710+
program = QCProgram.from_qasm_str("""OPENQASM 3.0;
711+
include "stdgates.inc";
712+
qubit[3] q;
713+
bit condition = measure q[0];
714+
int selector = 1;
715+
if (condition) {
716+
for int i in [0:2] {
717+
x q[i];
718+
}
719+
} else {
720+
cx q[0], q[1];
721+
}
722+
while (condition) {
723+
ctrl @ x q[0], q[1];
724+
}
725+
switch (selector) {
726+
case 1 {
727+
swap q[0], q[1];
728+
}
729+
default {
730+
z q[2];
731+
}
732+
}
733+
""")
734+
assert program.num_gates() == 5
735+
assert program.num_single_qubit_gates() == 2
736+
assert program.num_two_qubit_gates() == 3
737+
assert program.gate_counts() == {"ctrl": 2, "swap": 1, "x": 1, "z": 1}
738+
assert program.static_depth() == 3
739+
740+
741+
def test_qc_program_static_depth_with_dynamic_index() -> None:
742+
"""A dynamic register index conservatively aliases each element."""
743+
program = QCProgram.from_qasm_str("""OPENQASM 3.0;
744+
include "stdgates.inc";
745+
qubit[3] q;
746+
h q[0];
747+
for int i in [0:2] {
748+
x q[i];
749+
}
750+
""")
751+
assert program.static_depth() == 2

0 commit comments

Comments
 (0)