File size: 2,266 Bytes
e66189d 725a720 e66189d 725a720 e66189d 725a720 e66189d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | """Tiny metric-family plugin: tally gate types in the current circuit.
Demonstrates:
* Reading the upstream circuit from inputs.
* Returning the basic ``summary`` + ``scalars`` for the KV table.
* Emitting a typed ``figures`` list — markdown narrative + a bar
chart — so the result card renders rich output instead of just a
flat dict.
"""
from __future__ import annotations
import io
def run(inputs: dict, params: dict) -> dict:
from qiskit import qpy
blob = inputs.get("circuit_qpy_bytes")
if not blob:
return {"error": "No upstream circuit. Place this block downstream of an Input."}
loaded = qpy.load(io.BytesIO(blob))
qc = loaded[0] if isinstance(loaded, list) else loaded
ops = dict(qc.count_ops())
if params.get("include_barriers", "yes") == "no":
ops.pop("barrier", None)
total = sum(int(v) for v in ops.values())
# Sort bars by descending count for a more readable chart.
sorted_ops = sorted(ops.items(), key=lambda kv: kv[1], reverse=True)
bar_data = [{"label": name, "value": int(count)} for name, count in sorted_ops]
# Build a short markdown narrative summarising the result. Plugin
# authors get a place to write 1-2 sentences of "what this means".
narrative = (
f"The circuit has **{qc.num_qubits} qubits** at depth "
f"**{qc.depth()}**, with **{total} total gates** across "
f"**{len(ops)} distinct gate types**."
)
return {
"summary": {
"num_qubits": qc.num_qubits,
"depth": qc.depth(),
"total_gates": total,
},
"scalars": {"total_gates": total},
"figures": [
{
"type": "markdown",
"title": "Analysis",
"content": narrative,
},
{
"type": "bar",
"title": "Gates by type",
"x_label": "Gate",
"y_label": "Count",
"data": bar_data,
},
{
"type": "table",
"title": "Raw counts",
"headers": ["gate", "count"],
"rows": [[name, int(count)] for name, count in sorted_ops],
},
],
}
|