File size: 3,296 Bytes
f34dc51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2573a5a
 
 
f34dc51
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
"""KODEX quantum backend layer — the honest "connect into a real quantum computer" hook.

Every KODEX quantum code builds its circuit ONCE and runs it through `get_device()`.
By default that is a LOCAL simulator (exact statevector, free, CPU, works today). The
SAME circuit runs on real hardware or a shot-based simulator by setting env vars — no
code change:

    KODEX_QC_BACKEND unset / "default"  -> default.qubit          exact statevector (free)
    KODEX_QC_BACKEND=aer                -> qiskit.aer             realistic shot noise (local)
    KODEX_QC_BACKEND=ibm                -> qiskit.remote          REAL IBM Quantum hardware
        also set KODEX_QC_TOKEN=<your IBM Quantum API token>
        optionally KODEX_QC_IBM_BACKEND=<backend name>  (default: least-busy)

HONEST FRAMING (carry it on every card): a genuine quantum *advantage* for fusion kernels
is ~8-10 years out (fault tolerance). What is real and runnable TODAY is the *tooling and
testing* — you can execute these fusion quantum programs on a simulator now, and on real
quantum hardware the moment you plug in an account, on one code path. That is what KODEX
offers: be first to test earnestly, honest about the timeline.
"""
from __future__ import annotations

import os


def qc_backend() -> str:
    return os.environ.get("KODEX_QC_BACKEND", "default").strip().lower()


def get_device(wires, shots=None):
    """Return a PennyLane device for `wires`, routed by KODEX_QC_BACKEND. Defaults to the
    free local exact simulator; falls back to it safely if a hardware plugin is missing."""
    import pennylane as qml
    b = qc_backend()
    if b in ("", "default", "sim", "statevector"):
        return qml.device("default.qubit", wires=wires, shots=shots)
    if b == "aer":
        try:
            return qml.device("qiskit.aer", wires=wires, shots=shots or 4096)
        except Exception:
            return qml.device("default.qubit", wires=wires, shots=shots)
    if b in ("ibm", "qiskit", "hardware"):
        try:
            kw = {"wires": wires, "shots": shots or 4096}
            tok = os.environ.get("KODEX_QC_TOKEN")
            if tok:
                kw["token"] = tok
            ibm = os.environ.get("KODEX_QC_IBM_BACKEND")
            if ibm:
                kw["backend"] = ibm
            return qml.device("qiskit.remote", **kw)      # real IBM Quantum hardware
        except Exception:
            return qml.device("default.qubit", wires=wires, shots=shots)
    return qml.device("default.qubit", wires=wires, shots=shots)


def backend_note() -> dict:
    """A small honest descriptor of where a circuit actually ran — for benchmark cards."""
    b = qc_backend()
    real = b in ("ibm", "qiskit", "hardware")
    return {
        "active_backend": b or "default",
        "ran_on_real_hardware": real,
        "how_to_use_real_qc": ("set KODEX_QC_BACKEND=ibm and KODEX_QC_TOKEN=<your IBM Quantum "
                               "token> to run this exact circuit on real quantum hardware"),
        "honest_timeline": ("fault-tolerant quantum ADVANTAGE for fusion kernels is ~8-10 yr out — a "
                            "meaningful role in fusion only after ~2036; this tooling makes the TESTING "
                            "real and runnable TODAY, same code path"),
    }