Skip to content

Standardize Resource Analysis JSON Format - #3076

Open
sengthai wants to merge 33 commits into
mainfrom
ex-re/reshape-json
Open

Standardize Resource Analysis JSON Format#3076
sengthai wants to merge 33 commits into
mainfrom
ex-re/reshape-json

Conversation

@sengthai

@sengthai sengthai commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Context:
Currently, the resource analysis pass emits a JSON result when run with output-json=true, which is then consumed by the PL frontend and shown back to the user. This JSON output has grown organically: flat top-level keys, gate name encoded strings, separate function_calls(static) / var_function_calls (dynamic) fields, and PBC depth attached in the pass. This makes the format harder to parse and extend.

This PR standardizes the JSON schema in line with this ADR. Moreover, PBC depth is now contributed via the ResourceExtension framework instead of being populated separately in the pass.

Description of the Change:

  • The restructured JSON schema can be found here
  • Moved PBC depth into PBCDepthAnalysis and register it via ResourceExtensionRegistry
    Before → After (example):
// Before
{
  "basic_gates": {
    "operations": { "Hadamard(1)": 1, "CNOT(2)": 1 },
    "num_qubits": 2,
    "num_alloc_qubits": 2,
    "num_arg_qubits": 0,
    "function_calls": { "for_loop_1": 5 },
    "var_function_calls": { "dyn_for_loop_1": "a1b2..." },
    "depth": { "any_commuting_depth": 4, "qubit_disjoint_depth": 4 }
  }
}
// After
{
  "basic_gates": {
    "metadata": { "qnode": false, "has_branches": false },
    "num_qubits": { "alloc": 2, "arg": 0, "total": 2 },
    "quantum_operations": {
      "1": { "Hadamard": 1 },
      "2": { "CNOT": 1 }
    },
    "function_calls": {
      "static": { "for_loop_1": 5 },
      "dynamic": { "dyn_for_loop_1": "a1b2..." }
    },
    "extended_fields": {
      "pbc_depth": {
        "any_commuting_depth": 4,
        "qubit_disjoint_depth": 4
      }
    }
  }
}

[sc-124298]

@sengthai
sengthai changed the base branch from main to ex-re/resource-extension July 28, 2026 21:18
@sengthai
sengthai marked this pull request as ready for review July 28, 2026 21:19
@sengthai sengthai changed the title Standardize Resource-Analysis JSON Format Standardize Resource Analysis JSON Format Jul 29, 2026
@sengthai
sengthai requested a review from a team July 29, 2026 21:11
@sengthai
sengthai requested a review from jzaia18 August 3, 2026 15:24
Comment thread mlir/lib/Catalyst/Analysis/ResourceResult.cpp Outdated
Comment thread mlir/lib/Catalyst/Analysis/ResourceResult.cpp
Comment thread mlir/lib/Catalyst/Analysis/ResourceResult.cpp Outdated
Comment thread mlir/lib/Catalyst/Analysis/ResourceResult.cpp Outdated
Comment thread mlir/lib/Catalyst/Analysis/ResourceResult.cpp
Comment on lines +181 to +185
// try to insert nQubits first, if not then create empty json object.
auto [it, _] =
quantumOperationObject.try_emplace(std::to_string(nQubits), llvm::json::Object{});
(*it->getSecond().getAsObject())[opName] = countToJson(count);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of overwriting, it might be safer to sum. Right now if we somehow have 2 operators with the same name and number of wires but different number of args they will not be counted correctly. Unsure if we actually support varadic ops, but we should account for this case anyway to be safe in case we need to support them in the future

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sense, now i sum instead of overwrite them.

llvm::DenseMap<int, llvm::DenseMap<StringRef, double>> quantumOperationCounts;
for (const auto &opEntry : operations) {
StringRef opName = opEntry.getKey();
for (const auto &sizeEntry : opEntry.getValue()) {
const auto &[nQubits, nParams] = sizeEntry.first;
double count = sizeEntry.second;
quantumOperationCounts[nQubits][opName] += count;
}
}
llvm::json::Object quantumOperationObject;
for (const auto &[nQubits, opCounts] : quantumOperationCounts) {
auto [it, _] =
quantumOperationObject.try_emplace(std::to_string(nQubits), llvm::json::Object{});
for (const auto &[opName, count] : opCounts) {
(*it->getSecond().getAsObject())[opName] = countToJson(count);
}
}
funcObj["quantum_operations"] = std::move(quantumOperationObject);

@sengthai
sengthai force-pushed the ex-re/resource-extension branch from 9504a69 to c05bef1 Compare August 3, 2026 20:59
@sengthai
sengthai force-pushed the ex-re/reshape-json branch from 42948ae to ca4cebc Compare August 3, 2026 20:59
Base automatically changed from ex-re/resource-extension to main August 4, 2026 21:10
@sengthai
sengthai force-pushed the ex-re/reshape-json branch from eae737d to 13a8b0c Compare August 4, 2026 21:30
Comment thread doc/releases/changelog-dev.md Outdated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reminder: revert this before merge.

@sengthai
sengthai force-pushed the ex-re/reshape-json branch from cb5434a to bd6fae7 Compare August 5, 2026 20:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants