kronos-ml / kronos_ml /quantum_backend.py
Kronos Fusion Energy
Refresh to v0.3.0 (39 codes); clean package + docs (drop stale cache dump and deposit tooling)
2573a5a
Raw History Blame Contribute Delete
3.3 kB
"""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"),
}