File size: 17,057 Bytes
9e551a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# Tensor Roll — Recursive CUDA-Q Model Quantizer
# Copyright (C) 2026 SnapKitty Collective
# SPDX-License-Identifier: AGPL-3.0-or-later
"""TensorRoll IR: the backend-neutral plan representation.

A roll plan ({tensor_name: {decision, cost_params, ...}}) compiles to a
list of IROps. ``lower(ops, backend)`` then maps the plan onto a concrete
backend:

  * ``cpu``    — REALLY executes the classical ops (LOAD/SLICE/GEMM/
                 QUANTIZE/FACTOR) with NumPy, and the quantum-family ops
                 (ROLL/ROTATE/ENTANGLE/MEASURE/RECONSTRUCT) with the real
                 qsim-classical statevector simulator. Execution records
                 carry measured milliseconds and output digests.
  * ``cuda`` / ``cuda-q`` / ``qsharp`` — return per-op records with state
                 NOT EXECUTED, a reason, and the source-verification state
                 (VERIFIED/PRESENT) of the corresponding kernel source.
                 For ``cuda-q``/``qsharp``, ROTATE/ENTANGLE/MEASURE records
                 reference the real gate-trace schema from
                 ``tensor_roll/boundary.py``. This is IR-specification
                 only: no in-memory ABI between this IR and any vendor
                 kernel is claimed — exchange happens via the checksummed
                 file artifact until a toolchain executes the kernels.

The IR never invents numbers for backends it cannot run.
"""

from __future__ import annotations

import hashlib
import time
from dataclasses import asdict, dataclass, field
from typing import Any, Dict, List, Optional, Tuple

import numpy as np

from . import boundary as BD
from . import quant as QT
from . import qsim as QSIM
from .doctor import verify_sources

IR_VERSION = 1

VALID_OPS = ("LOAD", "SLICE", "ROLL", "FACTOR", "ROTATE", "ENTANGLE",
             "MEASURE", "RECONSTRUCT", "GEMM", "QUANTIZE", "EMIT")

_CPU_BACKENDS = ("cpu-numpy", "qsim-classical")

#: IROp -> boundary kernel name for the quantum-family ops (used when
#: lowering to cuda-q / qsharp so records reference the real gate schema).
_OP_KERNEL = {
    "ROTATE": "tensor_roll_rotate",
    "ENTANGLE": "tensor_roll_entangle",
    "MEASURE": "tensor_roll_measure",
    "RECONSTRUCT": "tensor_roll_reconstruct",
    "ROLL": "tensor_roll_recursive",
}

#: which audited source file backs each non-cpu backend
_BACKEND_SOURCE = {"cuda": "CUDA source", "cuda-q": "CUDA-Q source",
                   "qsharp": "Q# source"}

_NOT_EXECUTED_REASON = {
    "cuda": ("no CUDA device, no nvidia-smi, no nvcc on this machine; "
             "lowering to CUDA is a plan only — cuda/tensor_roll_gemm.cu "
             "was not compiled or executed"),
    "cuda-q": ("no CUDA-Q toolchain on this machine; lowering to cuda-q is "
               "IR-specification only until a toolchain executes "
               "cudaq/tensor_roll_kernels.py"),
    "qsharp": ("no Q# toolchain (and no dotnet) on this machine; lowering "
               "to qsharp is IR-specification only until a toolchain "
               "executes qsharp/TensorRoll.qs"),
}


@dataclass
class IROp:
    op: str
    inputs: List[str] = field(default_factory=list)
    outputs: List[str] = field(default_factory=list)
    params: Dict[str, Any] = field(default_factory=dict)
    meta: Dict[str, Any] = field(default_factory=dict)

    def __post_init__(self):
        if self.op not in VALID_OPS:
            raise ValueError(f"unknown IR op {self.op!r}; valid: {VALID_OPS}")
        self.inputs = list(self.inputs)
        self.outputs = list(self.outputs)
        self.params = dict(self.params)
        self.meta = dict(self.meta)


# ---------------------------------------------------------------------------
# Plan -> IR
# ---------------------------------------------------------------------------

def _decision_ops(name: str, cur: str, decision: str,
                  cost: Dict[str, Any]) -> Tuple[List[IROp], str]:
    """Per-decision op sequence. Returns (ops, current_value_name)."""
    ops: List[IROp] = []
    if decision == "QUANTIZE":
        ops.append(IROp("QUANTIZE", [cur], [f"q:{name}"],
                        {"bits": int(cost.get("bits", 8))},
                        {"decision": decision}))
        cur = f"q:{name}"
    elif decision == "FACTORIZE":
        ops.append(IROp("FACTOR", [cur], [f"f:{name}"],
                        {"rank": int(cost.get("rank", 4))},
                        {"decision": decision}))
        cur = f"f:{name}"
    elif decision == "QUANTUM-ENCODED":
        nq = int(cost.get("n_qubits", 3))
        angles = [float(a) for a in
                  cost.get("angles", [0.3] * nq)]
        ops.append(IROp("ROTATE", [cur], [f"qr:{name}"],
                        {"angles": angles, "n_qubits": nq},
                        {"decision": decision,
                         "backend": "qsim-classical"}))
        ops.append(IROp("ENTANGLE", [f"qr:{name}"], [f"qe:{name}"], {},
                        {"decision": decision}))
        ops.append(IROp("MEASURE", [f"qe:{name}"], [f"qm:{name}"],
                        {"shots": int(cost.get("shots", 256)),
                         "seed": int(cost.get("seed", 0))},
                        {"decision": decision}))
        ops.append(IROp("RECONSTRUCT", [f"qm:{name}"], [f"qc:{name}"],
                        {}, {"decision": decision,
                             "note": "classical reconstruction by construction"}))
        cur = f"qc:{name}"
    else:
        # PRESERVE / MERGED / ROUTED / RECONSTRUCTED / PRUNED and anything
        # else: no transform op invented; the decision rides in meta.
        ops.append(IROp("EMIT", [cur], [f"out:{name}"],
                        {"artifact": f"{name}.json"},
                        {"decision": decision,
                         "note": "pass-through: no transform op for this decision"}))
        return ops, f"out:{name}"
    ops.append(IROp("EMIT", [cur], [f"out:{name}"],
                    {"artifact": f"{name}.json"},
                    {"decision": decision}))
    return ops, f"out:{name}"


def build_ir(roll_plan: Dict[str, Dict[str, Any]]) -> List[IROp]:
    """Compile a roll plan ({tensor: {decision, cost_params, ...}}) to IR.

    Per tensor: LOAD -> ROLL -> per-decision op(s) -> EMIT.
    """
    ops: List[IROp] = []
    for name, spec in roll_plan.items():
        spec = spec or {}
        decision = str(spec.get("decision", "PRESERVE"))
        cost = dict(spec.get("cost_params", {}))
        ops.append(IROp("LOAD", [], [f"t:{name}"],
                        {"tensor": name}, {"source": "roll_plan"}))
        ops.append(IROp("ROLL", [f"t:{name}"], [f"r:{name}"],
                        {"depth": int(cost.get("depth", 1))},
                        {"note": "recursive partition/transform step"}))
        dops, _ = _decision_ops(name, f"r:{name}", decision, cost)
        ops.extend(dops)
    return ops


def to_json(ops: List[IROp]) -> Dict[str, Any]:
    return {"ir_version": IR_VERSION,
            "ops": [asdict(op) for op in ops]}


def from_json(doc: Dict[str, Any]) -> List[IROp]:
    if doc.get("ir_version") != IR_VERSION:
        raise ValueError(f"unsupported IR version {doc.get('ir_version')!r}")
    return [IROp(**item) for item in doc["ops"]]


# ---------------------------------------------------------------------------
# Lowering
# ---------------------------------------------------------------------------

def _arr_sha256(a: np.ndarray) -> str:
    return hashlib.sha256(np.ascontiguousarray(a).tobytes()).hexdigest()


def _lower_cpu(ops: List[IROp]) -> List[Dict[str, Any]]:
    vals: Dict[str, np.ndarray] = {}
    payloads: Dict[str, Dict] = {}
    records: List[Dict[str, Any]] = []

    def payload_for(name: str) -> Dict:
        if name in payloads:
            return payloads[name]
        arr = vals.get(name)
        if arr is None:
            raise KeyError(f"no array or payload named {name!r}")
        if arr.size > 256:
            raise ValueError(
                f"quantum-family op on {arr.size} elements exceeds the "
                "256-element eligibility cap; refusing")
        p, _ = QSIM.tensor_roll_encode(arr, encoding="amplitude")
        return p

    for seq, op in enumerate(ops):
        t0 = time.perf_counter()
        rec: Dict[str, Any] = {"seq": seq, "op": op.op,
                               "inputs": op.inputs, "outputs": op.outputs}
        try:
            if op.op == "LOAD":
                data = op.params.get("data")
                if data is None:
                    raise ValueError(
                        "LOAD has no params['data'] — nothing to execute")
                arr = np.asarray(data, dtype=np.float64)
                if "shape" in op.params:
                    arr = arr.reshape(op.params["shape"])
                vals[op.outputs[0]] = arr
                rec.update(state="EXECUTED", backend="cpu-numpy",
                           detail={"shape": list(arr.shape)})
            elif op.op == "SLICE":
                src = vals[op.inputs[0]]
                # params["slices"]: per-dim [lo, hi] pair, or null for full dim
                sel = tuple(slice(p[0], p[1]) if isinstance(p, (list, tuple))
                            else slice(None)
                            for p in op.params["slices"])
                vals[op.outputs[0]] = src[sel]
                rec.update(state="EXECUTED", backend="cpu-numpy",
                           detail={"shape": list(vals[op.outputs[0]].shape)})
            elif op.op == "GEMM":
                C = vals[op.inputs[0]] @ vals[op.inputs[1]]
                vals[op.outputs[0]] = C
                rec.update(state="EXECUTED", backend="cpu-numpy",
                           detail={"shape": list(C.shape),
                                   "checksum": float(C.sum()),
                                   "sha256": _arr_sha256(C)})
            elif op.op == "QUANTIZE":
                q, meta = QT.quantize_int8(vals[op.inputs[0]])
                vals[op.outputs[0]] = q.astype(np.float64)
                rec.update(state="EXECUTED", backend="cpu-numpy",
                           detail={"fmt": meta["fmt"], "scale": meta["scale"],
                                   "codes_sha256": _arr_sha256(q)})
            elif op.op == "FACTOR":
                W = vals[op.inputs[0]]
                r = int(op.params.get("rank", 2))
                U, S, Vt = np.linalg.svd(W.astype(np.float64),
                                         full_matrices=False)
                What = (U[:, :r] * S[:r]) @ Vt[:r, :]
                vals[op.outputs[0]] = What
                denom = float(np.linalg.norm(W))
                err = float(np.linalg.norm(W - What) / denom) if denom else 0.0
                rec.update(state="EXECUTED", backend="cpu-numpy",
                           detail={"rank": r,
                                   "relative_error": err,
                                   "sha256": _arr_sha256(What)})
            elif op.op == "ROLL":
                arr = vals[op.inputs[0]]
                if arr.size > 256:
                    raise ValueError(
                        f"ROLL on {arr.size} elements exceeds the 256-element "
                        "quantum-eligibility cap; refusing")
                What, rep = QSIM.tensor_roll_recursive(
                    arr, depth=int(op.params.get("depth", 1)),
                    shots=int(op.params.get("shots", 256)),
                    seed=int(op.params.get("seed", 0)))
                vals[op.outputs[0]] = What
                rec.update(state="EXECUTED", backend="qsim-classical",
                           detail={"kernel": "tensor_roll_recursive",
                                   "reconstruction_error":
                                       rep.get("reconstruction_error"),
                                   "sha256": _arr_sha256(What)})
            elif op.op == "ROTATE":
                p = payload_for(op.inputs[0])
                p2, rep = QSIM.tensor_roll_rotate(
                    p, angles=op.params["angles"])
                payloads[op.outputs[0]] = p2
                rec.update(state="EXECUTED", backend="qsim-classical",
                           detail={"kernel": "tensor_roll_rotate"})
            elif op.op == "ENTANGLE":
                p = payload_for(op.inputs[0])
                p2, _ = QSIM.tensor_roll_entangle(p)
                payloads[op.outputs[0]] = p2
                rec.update(state="EXECUTED", backend="qsim-classical",
                           detail={"kernel": "tensor_roll_entangle"})
            elif op.op == "MEASURE":
                p = payload_for(op.inputs[0])
                p2, rep = QSIM.tensor_roll_measure(
                    p, shots=int(op.params.get("shots", 256)),
                    seed=int(op.params.get("seed", 0)))
                payloads[op.outputs[0]] = p2
                stats = rep.get("measurement_stats") or {}
                rec.update(state="EXECUTED", backend="qsim-classical",
                           detail={"kernel": "tensor_roll_measure",
                                   "shots": int(op.params.get("shots", 256)),
                                   "n_outcomes": stats.get("n_outcomes"),
                                   "top_p": stats.get("top_p")})
            elif op.op == "RECONSTRUCT":
                p = payload_for(op.inputs[0])
                if "counts" not in p:
                    raise ValueError(
                        "RECONSTRUCT needs a measured payload (run MEASURE first)")
                What, _ = QSIM.tensor_roll_reconstruct(p)
                vals[op.outputs[0]] = What
                rec.update(state="EXECUTED", backend="qsim-classical",
                           detail={"kernel": "tensor_roll_reconstruct",
                                   "shape": list(What.shape),
                                   "sha256": _arr_sha256(What)})
            elif op.op == "EMIT":
                arr = vals.get(op.inputs[0])
                if arr is None:
                    raise ValueError(
                        f"EMIT input {op.inputs[0]!r} was never produced")
                vals[op.outputs[0]] = arr
                rec.update(state="EXECUTED", backend="cpu-numpy",
                           detail={"artifact": op.params.get("artifact"),
                                   "shape": list(arr.shape),
                                   "sha256": _arr_sha256(arr)})
            else:  # pragma: no cover - constructor guards VALID_OPS
                raise ValueError(f"no cpu lowering for {op.op}")
        except Exception as exc:  # noqa: BLE001 - record, don't crash
            rec.update(state="FAILED",
                       reason=f"{type(exc).__name__}: {exc}")
        rec["ms"] = round((time.perf_counter() - t0) * 1000.0, 3)
        records.append(rec)
    return records


def _lower_not_executed(ops: List[IROp], backend: str) -> List[Dict[str, Any]]:
    sources = verify_sources()
    src_state = sources[_BACKEND_SOURCE[backend]]["state"]
    records: List[Dict[str, Any]] = []
    for seq, op in enumerate(ops):
        rec: Dict[str, Any] = {
            "seq": seq, "op": op.op, "inputs": op.inputs,
            "outputs": op.outputs, "backend": backend,
            "state": "NOT EXECUTED",
            "reason": _NOT_EXECUTED_REASON[backend],
            "source": src_state,
            "source_path": sources[_BACKEND_SOURCE[backend]]["path"],
        }
        if backend in ("cuda-q", "qsharp") and op.op in _OP_KERNEL:
            # Reference the REAL gate-trace schema: this is what the vendor
            # kernel would have to implement. IR-specification only — no
            # ABI compatibility is asserted (see module docstring).
            kernel = _OP_KERNEL[op.op]
            nq = int(op.params.get("n_qubits",
                                   len(op.params.get("angles", [0.3, 0.3]))))
            rec["gate_trace_schema"] = BD.TRACE_SCHEMA
            rec["gate_trace"] = BD.trace_for_kernel(kernel, max(1, nq))
            rec["note"] = ("IR-specification only: references the boundary "
                           "gate-trace schema; asserts no in-memory ABI "
                           "compatibility with vendor kernels. Exchange "
                           "happens via the checksummed file artifact "
                           "(tensor_roll/boundary.py) until a toolchain "
                           "executes the kernels.")
        records.append(rec)
    return records


def lower(ops: List[IROp], backend: str) -> List[Dict[str, Any]]:
    """Lower IR ops to *backend*. Returns per-op execution records.

    ``cpu`` executes for real (NumPy + qsim-classical). ``cuda``,
    ``cuda-q`` and ``qsharp`` return NOT EXECUTED records with reasons —
    a plan, not a fabrication.
    """
    if backend == "cpu":
        return _lower_cpu(ops)
    if backend in ("cuda", "cuda-q", "qsharp"):
        return _lower_not_executed(ops, backend)
    raise ValueError(f"unknown backend {backend!r}; "
                     "expected 'cpu', 'cuda', 'cuda-q' or 'qsharp'")