feat(quantum): Q8 circuit SVG themes and view plot chrome

Fix label/wire overlap with gutter + chip labels; default dark tech palette
with light/bw themes; restyle probs/bloch and darken view plot frames.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-08-28 15:46:02 +08:00
1 parent cf4dbdeade
commit dfb27d41dc
11 files changed
+592 -155

No files matched your search

+388 -142
View File
@@ -1,17 +1,160 @@
//! Circuit rail SVG (design §7.4).
//! Circuit rail SVG (design §7.4 + Q8 viz style).
use serde_json::Value;
use crate::sim::{parse_ops, C, Op};
const COL_W: f64 = 56.0;
const ROW_H: f64 = 40.0;
const PAD_X: f64 = 48.0;
const PAD_Y: f64 = 24.0;
const WIRE: &str = "#1a1a1a";
const COL_W: f64 = 80.0;
const ROW_H: f64 = 64.0;
const PAD_X: f64 = 24.0;
const PAD_Y: f64 = 36.0;
const LABEL_W: f64 = 52.0;
const GUTTER: f64 = 20.0;
const FONT_MONO: &str = "ui-monospace,Menlo,Consolas,monospace";
const GATE_W: f64 = 40.0;
const GATE_H: f64 = 40.0;
/// Legacy aliases used by older advanced-viz helpers until Q8c.
const BOX: &str = "#f7f4ef";
const STROKE: &str = "#1a1a1a";
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ThemeName {
Dark,
Light,
Bw,
}
impl ThemeName {
pub fn parse(raw: &str) -> Result<Self, String> {
match raw.trim().to_ascii_lowercase().as_str() {
"dark" | "暗" => Ok(Self::Dark),
"light" | "亮" | "浅" => Ok(Self::Light),
"bw" | "mono" | "黑白" => Ok(Self::Bw),
other => Err(format!("unknown theme `{other}` (dark|light|bw)")),
}
}
pub fn as_str(self) -> &'static str {
match self {
Self::Dark => "dark",
Self::Light => "light",
Self::Bw => "bw",
}
}
}
#[derive(Clone, Copy)]
struct Theme {
name: ThemeName,
bg: &'static str,
bg2: &'static str,
panel: &'static str,
wire: &'static str,
label: &'static str,
chip: &'static str,
clifford_fill: &'static str,
clifford_ink: &'static str,
phase_fill: &'static str,
phase_ink: &'static str,
rotation_fill: &'static str,
rotation_ink: &'static str,
measure_fill: &'static str,
measure_ink: &'static str,
barrier: &'static str,
ctrl: &'static str,
accent: &'static str,
muted: &'static str,
axis: &'static str,
glow: &'static str,
}
impl Theme {
fn of(name: ThemeName) -> Self {
match name {
ThemeName::Dark => Self {
name,
bg: "#0a0e14",
bg2: "#141c28",
panel: "#1a2433",
wire: "#6b7f96",
label: "#e8eef6",
chip: "#1e2a3a",
clifford_fill: "#4c6fff",
clifford_ink: "#f2f6ff",
phase_fill: "#2de2e6",
phase_ink: "#042024",
rotation_fill: "#3dd6c6",
rotation_ink: "#041a18",
measure_fill: "#8fa3b8",
measure_ink: "#0a1018",
barrier: "#9aafc0",
ctrl: "#f0f5ff",
accent: "#5b7cfa",
muted: "#93a4b8",
axis: "#7d8fa3",
glow: "#4c6fff",
},
ThemeName::Light => Self {
name,
bg: "#eef3f9",
bg2: "#ffffff",
panel: "#e2eaf3",
wire: "#4a5a6a",
label: "#15202b",
chip: "#ffffff",
clifford_fill: "#3d5afe",
clifford_ink: "#ffffff",
phase_fill: "#00838f",
phase_ink: "#ffffff",
rotation_fill: "#00897b",
rotation_ink: "#ffffff",
measure_fill: "#546e7a",
measure_ink: "#ffffff",
barrier: "#78909c",
ctrl: "#1a237e",
accent: "#3d5afe",
muted: "#5a6a7a",
axis: "#4a5560",
glow: "#3d5afe",
},
ThemeName::Bw => Self {
name,
bg: "#ffffff",
bg2: "#f7f7f7",
panel: "#eeeeee",
wire: "#222222",
label: "#111111",
chip: "#f0f0f0",
clifford_fill: "#ffffff",
clifford_ink: "#111111",
phase_fill: "#eeeeee",
phase_ink: "#111111",
rotation_fill: "#f5f5f5",
rotation_ink: "#111111",
measure_fill: "#e8e8e8",
measure_ink: "#111111",
barrier: "#666666",
ctrl: "#111111",
accent: "#333333",
muted: "#555555",
axis: "#222222",
glow: "#888888",
},
}
}
fn family_colors(self, family: &str) -> (&'static str, &'static str) {
match family {
"phase" => (self.phase_fill, self.phase_ink),
"rotation" => (self.rotation_fill, self.rotation_ink),
"measure" => (self.measure_fill, self.measure_ink),
"neutral" => (self.measure_fill, self.measure_ink),
_ => (self.clifford_fill, self.clifford_ink),
}
}
}
fn esc(s: &str) -> String {
s.replace('&', "&amp;")
.replace('<', "&lt;")
@@ -19,6 +162,17 @@ fn esc(s: &str) -> String {
.replace('"', "&quot;")
}
fn gate_family(gate: &str) -> &'static str {
match gate {
"I" | "X" | "Y" | "Z" | "H" | "HADAMARD" | "CX" | "CNOT" | "CN" | "SWAP" => "clifford",
"S" | "T" | "RZ" | "CZ" => "phase",
"RX" | "RY" => "rotation",
"MEASURE" | "M" => "measure",
"BARRIER" => "barrier",
_ => "neutral",
}
}
fn gate_label(op: &Op) -> String {
match op.gate.as_str() {
"RX" => op
@@ -39,16 +193,21 @@ fn gate_label(op: &Op) -> String {
}
}
fn rail_x0() -> f64 {
PAD_X + LABEL_W + GUTTER
}
fn y_of(q: usize) -> f64 {
PAD_Y + q as f64 * ROW_H
}
fn x_of(col: usize) -> f64 {
PAD_X + col as f64 * COL_W
rail_x0() + (col as f64 + 0.5) * COL_W
}
/// Draw a circuit diagram as standalone SVG.
pub fn circuit_svg(circuit: &Value) -> Result<String, String> {
pub fn circuit_svg(circuit: &Value, theme: ThemeName) -> Result<String, String> {
let th = Theme::of(theme);
let qubits = circuit
.get("qubits")
.or_else(|| circuit.get("比特数"))
@@ -56,23 +215,53 @@ pub fn circuit_svg(circuit: &Value) -> Result<String, String> {
.ok_or_else(|| "circuit missing qubits".to_string())? as usize;
let ops = parse_ops(circuit)?;
let cols = ops.len().max(1);
let width = PAD_X + cols as f64 * COL_W + 32.0;
let height = PAD_Y * 2.0 + (qubits.saturating_sub(1).max(0) as f64) * ROW_H + 8.0;
let height = height.max(PAD_Y * 2.0 + 24.0);
let width = (rail_x0() + cols as f64 * COL_W + PAD_X).max(320.0);
let height = PAD_Y * 2.0 + (qubits.saturating_sub(1) as f64) * ROW_H;
let height = height.max(PAD_Y * 2.0 + GATE_H + 8.0);
let mut body = String::new();
// wires
body.push_str(&svg_defs(&th));
body.push_str(&format!(
r##"<rect width="{w:.1}" height="{h:.1}" rx="14" fill="url(#mq-bg)"/>
<rect x="1.5" y="1.5" width="{wi:.1}" height="{hi:.1}" rx="12.5" fill="none" stroke="{panel}" stroke-width="1" opacity="0.55"/>"##,
w = width,
h = height,
wi = width - 3.0,
hi = height - 3.0,
panel = th.panel,
));
let wire_x0 = rail_x0();
let wire_x1 = width - PAD_X;
let label_x = PAD_X + LABEL_W - 6.0;
for q in 0..qubits {
let y = y_of(q);
// dual-tone wire for depth
body.push_str(&format!(
r#"<line x1="{x0}" y1="{y}" x2="{x1}" y2="{y}" stroke="{WIRE}" stroke-width="1.5"/>"#,
x0 = 8.0,
x1 = width - 8.0,
r##"<line x1="{x0}" y1="{y}" x2="{x1}" y2="{y}" stroke="{wire}" stroke-width="3.2" stroke-linecap="round" opacity="0.28"/>
<line x1="{x0}" y1="{y}" x2="{x1}" y2="{y}" stroke="{wire}" stroke-width="1.8" stroke-linecap="round"/>"##,
x0 = wire_x0,
x1 = wire_x1,
y = y,
wire = th.wire,
));
// label chip — wire never crosses text
let chip_w = 38.0;
let chip_h = 22.0;
body.push_str(&format!(
r#"<text x="4" y="{ty}" font-family="ui-monospace,Menlo,monospace" font-size="11" fill="{WIRE}">q{q}</text>"#,
ty = y + 4.0,
r##"<rect x="{rx}" y="{ry}" width="{chip_w}" height="{chip_h}" rx="7" fill="{chip}" stroke="{panel}" stroke-width="1"/>
<text x="{lx}" y="{y}" text-anchor="middle" dominant-baseline="middle" font-family="{font}" font-size="13" font-weight="700" letter-spacing="0.02em" fill="{fill}">q{q}</text>"##,
rx = label_x - chip_w / 2.0,
ry = y - chip_h / 2.0,
chip_w = chip_w,
chip_h = chip_h,
chip = th.chip,
panel = th.panel,
lx = label_x,
y = y,
font = FONT_MONO,
fill = th.label,
q = q,
));
}
@@ -80,7 +269,7 @@ pub fn circuit_svg(circuit: &Value) -> Result<String, String> {
let x = x_of(col);
match op.gate.as_str() {
"BARRIER" => {
draw_barrier(&mut body, x, qubits);
draw_barrier(&mut body, x, qubits, &th);
}
"MEASURE" | "M" => {
let qs = if op.qubits.is_empty() {
@@ -89,145 +278,180 @@ pub fn circuit_svg(circuit: &Value) -> Result<String, String> {
op.qubits.clone()
};
for q in qs {
draw_measure(&mut body, x, q);
draw_measure(&mut body, x, q, &th);
}
}
"CX" | "CNOT" | "CN" if op.qubits.len() >= 2 => {
draw_cx(&mut body, x, op.qubits[0], op.qubits[1]);
draw_cx(&mut body, x, op.qubits[0], op.qubits[1], &th);
}
"CZ" if op.qubits.len() >= 2 => {
draw_cz(&mut body, x, op.qubits[0], op.qubits[1]);
draw_cz(&mut body, x, op.qubits[0], op.qubits[1], &th);
}
"SWAP" if op.qubits.len() >= 2 => {
draw_swap(&mut body, x, op.qubits[0], op.qubits[1]);
draw_swap(&mut body, x, op.qubits[0], op.qubits[1], &th);
}
_ => {
let q = *op.qubits.first().unwrap_or(&0);
draw_box(&mut body, x, q, &gate_label(op));
let fam = gate_family(&op.gate);
draw_box(&mut body, x, q, &gate_label(op), fam, &th);
}
}
}
Ok(format!(
r#"<svg xmlns="http://www.w3.org/2000/svg" width="{w:.0}" height="{h:.0}" viewBox="0 0 {w:.1} {h:.1}" role="img" aria-label="quantum circuit">{body}</svg>"#,
r##"<svg xmlns="http://www.w3.org/2000/svg" width="{w:.0}" height="{h:.0}" viewBox="0 0 {w:.1} {h:.1}" data-theme="{theme}" role="img" aria-label="quantum circuit">{body}</svg>"##,
w = width,
h = height,
theme = th.name.as_str(),
body = body,
))
}
fn draw_box(out: &mut String, x: f64, q: usize, label: &str) {
fn svg_defs(th: &Theme) -> String {
format!(
r##"<defs>
<linearGradient id="mq-bg" x1="0" y1="0" x2="1" y2="1">
<stop offset="0%" stop-color="{bg}"/>
<stop offset="55%" stop-color="{bg2}"/>
<stop offset="100%" stop-color="{bg}"/>
</linearGradient>
<filter id="mq-glow" x="-40%" y="-40%" width="180%" height="180%">
<feGaussianBlur stdDeviation="2.2" result="b"/>
<feMerge><feMergeNode in="b"/><feMergeNode in="SourceGraphic"/></feMerge>
</filter>
<filter id="mq-soft" x="-20%" y="-20%" width="140%" height="140%">
<feDropShadow dx="0" dy="1.5" stdDeviation="1.8" flood-color="{glow}" flood-opacity="0.35"/>
</filter>
</defs>"##,
bg = th.bg,
bg2 = th.bg2,
glow = th.glow,
)
}
fn draw_box(out: &mut String, x: f64, q: usize, label: &str, family: &str, th: &Theme) {
let y = y_of(q);
let w = 28.0;
let h = 28.0;
let w = GATE_W;
let h = GATE_H;
let (fill, ink) = th.family_colors(family);
out.push_str(&format!(
r#"<rect x="{rx}" y="{ry}" width="{w}" height="{h}" rx="3" fill="{BOX}" stroke="{STROKE}" stroke-width="1.5"/>"#,
r##"<rect x="{rx}" y="{ry}" width="{w}" height="{h}" rx="8" fill="{fill}" stroke="{ink}" stroke-width="1.1" data-gate-family="{fam}" filter="url(#mq-soft)" opacity="0.98"/>
<text x="{x}" y="{y}" text-anchor="middle" dominant-baseline="middle" font-family="{font}" font-size="14" font-weight="700" fill="{ink}">{label}</text>"##,
rx = x - w / 2.0,
ry = y - h / 2.0,
));
out.push_str(&format!(
r#"<text x="{x}" y="{ty}" text-anchor="middle" font-family="ui-monospace,Menlo,monospace" font-size="11" fill="{STROKE}">{label}</text>"#,
ty = y + 4.0,
w = w,
h = h,
fill = fill,
ink = ink,
fam = family,
x = x,
y = y,
font = FONT_MONO,
label = esc(label),
));
}
fn draw_barrier(out: &mut String, x: f64, qubits: usize) {
let y0 = y_of(0) - 14.0;
let y1 = y_of(qubits.saturating_sub(1)) + 14.0;
fn draw_barrier(out: &mut String, x: f64, qubits: usize, th: &Theme) {
let y0 = y_of(0) - 20.0;
let y1 = y_of(qubits.saturating_sub(1)) + 20.0;
out.push_str(&format!(
r#"<line x1="{x}" y1="{y0}" x2="{x}" y2="{y1}" stroke="{STROKE}" stroke-width="1.5" stroke-dasharray="4 3"/>"#
r##"<line x1="{x}" y1="{y0}" x2="{x}" y2="{y1}" stroke="{c}" stroke-width="1.75" stroke-dasharray="6 5" data-gate-family="barrier" opacity="0.85"/>"##,
x = x,
y0 = y0,
y1 = y1,
c = th.barrier,
));
}
fn draw_measure(out: &mut String, x: f64, q: usize) {
fn draw_measure(out: &mut String, x: f64, q: usize, th: &Theme) {
let y = y_of(q);
let w = 28.0;
let h = 28.0;
let w = GATE_W;
let h = GATE_H;
let (fill, ink) = th.family_colors("measure");
out.push_str(&format!(
r#"<rect x="{rx}" y="{ry}" width="{w}" height="{h}" rx="3" fill="{BOX}" stroke="{STROKE}" stroke-width="1.5"/>"#,
r##"<rect x="{rx}" y="{ry}" width="{w}" height="{h}" rx="8" fill="{fill}" stroke="{ink}" stroke-width="1.1" data-gate-family="measure" filter="url(#mq-soft)"/>
<path d="M {x0},{y0} A 11,11 0 0 1 {x1},{y0}" fill="none" stroke="{ink}" stroke-width="1.8"/>
<line x1="{x}" y1="{y1}" x2="{x2}" y2="{y2}" stroke="{ink}" stroke-width="1.8" stroke-linecap="round"/>"##,
rx = x - w / 2.0,
ry = y - h / 2.0,
));
// meter arc + dial
out.push_str(&format!(
r#"<path d="M {x0},{y0} A 8,8 0 0 1 {x1},{y0}" fill="none" stroke="{STROKE}" stroke-width="1.5"/>"#,
x0 = x - 8.0,
y0 = y + 2.0,
x1 = x + 8.0,
));
out.push_str(&format!(
r#"<line x1="{x}" y1="{y1}" x2="{x2}" y2="{y2}" stroke="{STROKE}" stroke-width="1.5"/>"#,
y1 = y + 2.0,
x2 = x + 6.0,
y2 = y - 6.0,
w = w,
h = h,
fill = fill,
ink = ink,
x0 = x - 11.0,
y0 = y + 4.0,
x1 = x + 11.0,
x = x,
y1 = y + 4.0,
x2 = x + 8.0,
y2 = y - 8.0,
));
}
fn draw_cx(out: &mut String, x: f64, control: usize, target: usize) {
fn draw_cx(out: &mut String, x: f64, control: usize, target: usize, th: &Theme) {
let yc = y_of(control);
let yt = y_of(target);
let (fill, _) = th.family_colors("clifford");
let r = 13.0;
out.push_str(&format!(
r##"<line x1="{x}" y1="{yc}" x2="{x}" y2="{yt}" stroke="{ctrl}" stroke-width="2.25" data-gate-family="clifford"/>
<circle cx="{x}" cy="{yc}" r="5.2" fill="{ctrl}" filter="url(#mq-glow)"/>
<circle cx="{x}" cy="{yt}" r="{r}" fill="{fill}" stroke="{ctrl}" stroke-width="1.6" filter="url(#mq-soft)" opacity="0.96"/>
<line x1="{x}" y1="{a}" x2="{x}" y2="{b}" stroke="{ctrl}" stroke-width="2"/>
<line x1="{a2}" y1="{yt}" x2="{b2}" y2="{yt}" stroke="{ctrl}" stroke-width="2"/>"##,
x = x,
yc = yc,
yt = yt,
ctrl = th.ctrl,
fill = fill,
r = r,
a = yt - r,
b = yt + r,
a2 = x - r,
b2 = x + r,
));
}
fn draw_cz(out: &mut String, x: f64, control: usize, target: usize, th: &Theme) {
let yc = y_of(control);
let yt = y_of(target);
out.push_str(&format!(
r#"<line x1="{x}" y1="{yc}" x2="{x}" y2="{yt}" stroke="{STROKE}" stroke-width="1.5"/>"#
));
out.push_str(&format!(
r#"<circle cx="{x}" cy="{yc}" r="4" fill="{STROKE}"/>"#
));
out.push_str(&format!(
r#"<circle cx="{x}" cy="{yt}" r="10" fill="none" stroke="{STROKE}" stroke-width="1.5"/>"#
));
out.push_str(&format!(
r#"<line x1="{x}" y1="{a}" x2="{x}" y2="{b}" stroke="{STROKE}" stroke-width="1.5"/>"#,
a = yt - 10.0,
b = yt + 10.0,
));
out.push_str(&format!(
r#"<line x1="{a}" y1="{yt}" x2="{b}" y2="{yt}" stroke="{STROKE}" stroke-width="1.5"/>"#,
a = x - 10.0,
b = x + 10.0,
r##"<line x1="{x}" y1="{yc}" x2="{x}" y2="{yt}" stroke="{c}" stroke-width="2.25" data-gate-family="phase"/>
<circle cx="{x}" cy="{yc}" r="5.2" fill="{c}" filter="url(#mq-glow)"/>
<circle cx="{x}" cy="{yt}" r="5.2" fill="{c}" filter="url(#mq-glow)"/>"##,
x = x,
yc = yc,
yt = yt,
c = th.phase_fill,
));
}
fn draw_cz(out: &mut String, x: f64, control: usize, target: usize) {
let yc = y_of(control);
let yt = y_of(target);
out.push_str(&format!(
r#"<line x1="{x}" y1="{yc}" x2="{x}" y2="{yt}" stroke="{STROKE}" stroke-width="1.5"/>"#
));
out.push_str(&format!(
r#"<circle cx="{x}" cy="{yc}" r="4" fill="{STROKE}"/>"#
));
out.push_str(&format!(
r#"<circle cx="{x}" cy="{yt}" r="4" fill="{STROKE}"/>"#
));
}
fn draw_swap(out: &mut String, x: f64, a: usize, b: usize) {
fn draw_swap(out: &mut String, x: f64, a: usize, b: usize, th: &Theme) {
let ya = y_of(a);
let yb = y_of(b);
out.push_str(&format!(
r#"<line x1="{x}" y1="{ya}" x2="{x}" y2="{yb}" stroke="{STROKE}" stroke-width="1.5"/>"#
r##"<line x1="{x}" y1="{ya}" x2="{x}" y2="{yb}" stroke="{c}" stroke-width="2.25" data-gate-family="clifford"/>"##,
x = x,
ya = ya,
yb = yb,
c = th.ctrl,
));
for y in [ya, yb] {
out.push_str(&format!(
r#"<line x1="{x0}" y1="{y0}" x2="{x1}" y2="{y1}" stroke="{STROKE}" stroke-width="1.5"/>"#,
x0 = x - 8.0,
y0 = y - 8.0,
x1 = x + 8.0,
y1 = y + 8.0,
));
out.push_str(&format!(
r#"<line x1="{x0}" y1="{y0}" x2="{x1}" y2="{y1}" stroke="{STROKE}" stroke-width="1.5"/>"#,
x0 = x - 8.0,
y0 = y + 8.0,
x1 = x + 8.0,
y1 = y - 8.0,
r##"<line x1="{x0}" y1="{y0}" x2="{x1}" y2="{y1}" stroke="{c}" stroke-width="2" stroke-linecap="round"/>
<line x1="{x0}" y1="{y1}" x2="{x1}" y2="{y0}" stroke="{c}" stroke-width="2" stroke-linecap="round"/>"##,
x0 = x - 10.0,
y0 = y - 10.0,
x1 = x + 10.0,
y1 = y + 10.0,
c = th.ctrl,
));
}
}
/// Probability histogram from basis-label → probability map.
pub fn probs_svg(probs: &serde_json::Map<String, Value>) -> String {
pub fn probs_svg(probs: &serde_json::Map<String, Value>, theme: ThemeName) -> String {
let th = Theme::of(theme);
let mut entries: Vec<(String, f64)> = probs
.iter()
.filter_map(|(k, v)| v.as_f64().map(|p| (k.clone(), p)))
@@ -242,89 +466,108 @@ pub fn probs_svg(probs: &serde_json::Map<String, Value>) -> String {
let chart_h = 120.0;
let pad_l = 40.0;
let pad_b = 36.0;
let pad_t = 20.0;
let pad_t = 24.0;
let pad_r = 16.0;
let width = pad_l + n as f64 * (bar_w + gap) + pad_r;
let height = pad_t + chart_h + pad_b;
let mut body = String::new();
// axis
body.push_str(&svg_defs(&th));
body.push_str(&format!(
r#"<line x1="{x0}" y1="{y}" x2="{x1}" y2="{y}" stroke="{STROKE}" stroke-width="1.2"/>"#,
r##"<rect width="{w:.1}" height="{h:.1}" rx="14" fill="url(#mq-bg)"/>"##,
w = width,
h = height,
));
body.push_str(&format!(
r##"<line x1="{x0}" y1="{y}" x2="{x1}" y2="{y}" stroke="{axis}" stroke-width="1.2"/>"##,
x0 = pad_l - 4.0,
x1 = width - pad_r,
y = pad_t + chart_h,
axis = th.axis,
));
body.push_str(&format!(
r#"<line x1="{x}" y1="{y0}" x2="{x}" y2="{y1}" stroke="{STROKE}" stroke-width="1.2"/>"#,
r##"<line x1="{x}" y1="{y0}" x2="{x}" y2="{y1}" stroke="{axis}" stroke-width="1.2"/>"##,
x = pad_l - 4.0,
y0 = pad_t,
y1 = pad_t + chart_h,
axis = th.axis,
));
body.push_str(&format!(
r#"<text x="6" y="{ty}" font-family="ui-monospace,Menlo,monospace" font-size="10" fill="{STROKE}">1</text>"#,
r##"<text x="8" y="{ty}" font-family="{font}" font-size="10" fill="{lab}">1</text>"##,
ty = pad_t + 4.0,
font = FONT_MONO,
lab = th.label,
));
body.push_str(&format!(
r#"<text x="6" y="{ty}" font-family="ui-monospace,Menlo,monospace" font-size="10" fill="{STROKE}">0</text>"#,
r##"<text x="8" y="{ty}" font-family="{font}" font-size="10" fill="{lab}">0</text>"##,
ty = pad_t + chart_h + 4.0,
font = FONT_MONO,
lab = th.label,
));
for (i, (label, p)) in entries.iter().enumerate() {
let p = (*p).clamp(0.0, 1.0);
let h = p * chart_h;
let x = pad_l + i as f64 * (bar_w + gap);
let y = pad_t + chart_h - h;
let bar_fill = "#4a6fa5";
let muted = "#555";
body.push_str(&format!(
r#"<rect x="{x}" y="{y}" width="{bar_w}" height="{h}" fill="{bar_fill}" stroke="{STROKE}" stroke-width="1"/>"#,
r##"<rect x="{x}" y="{y}" width="{bar_w}" height="{h}" rx="4" fill="{fill}" stroke="{ink}" stroke-width="1" filter="url(#mq-soft)"/>"##,
x = x,
y = y,
bar_w = bar_w,
h = h,
bar_fill = bar_fill,
STROKE = STROKE,
h = h.max(0.5),
fill = th.accent,
ink = th.ctrl,
));
body.push_str(&format!(
r#"<text x="{cx}" y="{ty}" text-anchor="middle" font-family="ui-monospace,Menlo,monospace" font-size="10" fill="{STROKE}">{lab}</text>"#,
r##"<text x="{cx}" y="{ty}" text-anchor="middle" font-family="{font}" font-size="11" fill="{lab}">{lab_t}</text>"##,
cx = x + bar_w / 2.0,
ty = pad_t + chart_h + 16.0,
lab = esc(label),
STROKE = STROKE,
ty = pad_t + chart_h + 18.0,
font = FONT_MONO,
lab = th.label,
lab_t = esc(label),
));
body.push_str(&format!(
r#"<text x="{cx}" y="{ty}" text-anchor="middle" font-family="ui-monospace,Menlo,monospace" font-size="9" fill="{muted}">{pval:.2}</text>"#,
r##"<text x="{cx}" y="{ty}" text-anchor="middle" font-family="{font}" font-size="10" fill="{muted}">{pval:.2}</text>"##,
cx = x + bar_w / 2.0,
ty = y - 4.0,
ty = y - 5.0,
font = FONT_MONO,
muted = th.muted,
pval = p,
muted = muted,
));
}
format!(
r#"<svg xmlns="http://www.w3.org/2000/svg" width="{w:.0}" height="{h:.0}" viewBox="0 0 {w:.1} {h:.1}" role="img" aria-label="measurement probabilities">{body}</svg>"#,
r##"<svg xmlns="http://www.w3.org/2000/svg" width="{w:.0}" height="{h:.0}" viewBox="0 0 {w:.1} {h:.1}" data-theme="{theme}" role="img" aria-label="measurement probabilities">{body}</svg>"##,
w = width,
h = height,
theme = th.name.as_str(),
body = body,
)
}
/// 2D Bloch sphere projection with state arrow.
pub fn bloch_svg(x: f64, y: f64, z: f64) -> String {
let cx = 110.0;
let cy = 110.0;
let r = 70.0;
let width = 220.0;
let height = 240.0;
pub fn bloch_svg(x: f64, y: f64, z: f64, theme: ThemeName) -> String {
let th = Theme::of(theme);
let cx = 130.0;
let cy = 125.0;
let r = 82.0;
let width = 280.0;
let height = 290.0;
// Project: X right, Z up, Y into page (slight oblique).
let px = |vx: f64, vy: f64, _vz: f64| cx + r * (vx * 0.85 + vy * 0.35);
let py = |_vx: f64, vy: f64, vz: f64| cy - r * (vz * 0.9 - vy * 0.25);
let mut body = String::new();
body.push_str(&svg_defs(&th));
body.push_str(&format!(
r#"<circle cx="{cx}" cy="{cy}" r="{r}" fill="{fill}" stroke="{STROKE}" stroke-width="1.5"/>"#,
r##"<rect width="{w:.1}" height="{h:.1}" rx="14" fill="url(#mq-bg)"/>"##,
w = width,
h = height,
));
body.push_str(&format!(
r##"<circle cx="{cx}" cy="{cy}" r="{r}" fill="{fill}" stroke="{stroke}" stroke-width="1.5" opacity="0.28" filter="url(#mq-glow)"/>"##,
cx = cx,
cy = cy,
r = r,
fill = BOX,
STROKE = STROKE,
fill = th.clifford_fill,
stroke = th.wire,
));
// axes
let axes = [
@@ -340,19 +583,20 @@ pub fn bloch_svg(x: f64, y: f64, z: f64) -> String {
let x2 = px(vx, vy, vz);
let y2 = py(vx, vy, vz);
body.push_str(&format!(
r#"<line x1="{x1}" y1="{y1}" x2="{x2}" y2="{y2}" stroke="{axis}" stroke-width="1" stroke-dasharray="3 2"/>"#,
r##"<line x1="{x1}" y1="{y1}" x2="{x2}" y2="{y2}" stroke="{axis}" stroke-width="1" stroke-dasharray="3 2"/>"##,
x1 = x1,
y1 = y1,
x2 = x2,
y2 = y2,
axis = "#888",
axis = th.axis,
));
body.push_str(&format!(
r#"<text x="{tx}" y="{ty}" font-family="ui-monospace,Menlo,monospace" font-size="10" fill="{STROKE}">{lab}</text>"#,
r##"<text x="{tx}" y="{ty}" font-family="{font}" font-size="10" fill="{lab_c}">{lab}</text>"##,
tx = x2 + 4.0,
ty = y2 + 3.0,
font = FONT_MONO,
lab_c = th.label,
lab = lab,
STROKE = STROKE,
));
}
// state vector
@@ -360,9 +604,9 @@ pub fn bloch_svg(x: f64, y: f64, z: f64) -> String {
let sy = py(x, y, z);
let ox = px(0.0, 0.0, 0.0);
let oy = py(0.0, 0.0, 0.0);
let arrow = "#c0392b";
let arrow = th.phase_fill;
body.push_str(&format!(
r#"<line x1="{ox}" y1="{oy}" x2="{sx}" y2="{sy}" stroke="{arrow}" stroke-width="2.5"/>"#,
r##"<line x1="{ox}" y1="{oy}" x2="{sx}" y2="{sy}" stroke="{arrow}" stroke-width="2.5" stroke-linecap="round"/>"##,
ox = ox,
oy = oy,
sx = sx,
@@ -370,23 +614,26 @@ pub fn bloch_svg(x: f64, y: f64, z: f64) -> String {
arrow = arrow,
));
body.push_str(&format!(
r#"<circle cx="{sx}" cy="{sy}" r="4" fill="{arrow}"/>"#,
r##"<circle cx="{sx}" cy="{sy}" r="4.5" fill="{arrow}" stroke="{ctrl}" stroke-width="1"/>"##,
sx = sx,
sy = sy,
arrow = arrow,
ctrl = th.ctrl,
));
body.push_str(&format!(
r#"<text x="12" y="{hy}" font-family="ui-monospace,Menlo,monospace" font-size="11" fill="{STROKE}">⟨X⟩={bx:.3} ⟨Y⟩={by:.3} ⟨Z⟩={bz:.3}</text>"#,
r##"<text x="12" y="{hy}" font-family="{font}" font-size="11" fill="{lab}">⟨X⟩={bx:.3} ⟨Y⟩={by:.3} ⟨Z⟩={bz:.3}</text>"##,
hy = height - 12.0,
font = FONT_MONO,
lab = th.label,
bx = x,
by = y,
bz = z,
STROKE = STROKE,
));
format!(
r#"<svg xmlns="http://www.w3.org/2000/svg" width="{w:.0}" height="{h:.0}" viewBox="0 0 {w:.1} {h:.1}" role="img" aria-label="Bloch sphere">{body}</svg>"#,
r##"<svg xmlns="http://www.w3.org/2000/svg" width="{w:.0}" height="{h:.0}" viewBox="0 0 {w:.1} {h:.1}" data-theme="{theme}" role="img" aria-label="Bloch sphere">{body}</svg>"##,
w = width,
h = height,
theme = th.name.as_str(),
body = body,
)
}
@@ -806,8 +1053,7 @@ pub fn multibloch_svg(amps: &[C], qubits: usize) -> Result<String, String> {
body.push_str(r##"<text x="8" y="16" font-family="ui-monospace,Menlo,monospace" font-size="12" fill="#1a1a1a">multibloch</text>"##);
for q in 0..qubits {
let (x, y, z) = crate::sim::bloch_vector(amps, qubits, q)?;
let svg = bloch_svg(x, y, z);
// extract inner content roughly by offsetting — simpler: draw mini sphere
// Mini sphere (Q8c will share Theme tokens fully).
let ox = 8.0 + q as f64 * sphere_w;
let cx = ox + 60.0;
let cy = 90.0;
@@ -841,7 +1087,7 @@ pub fn multibloch_svg(amps: &[C], qubits: usize) -> Result<String, String> {
px = px,
py = py,
));
let _ = (y, svg);
let _ = y;
}
Ok(format!(
r#"<svg xmlns="http://www.w3.org/2000/svg" width="{w:.0}" height="{h:.0}" viewBox="0 0 {w:.1} {h:.1}" role="img" aria-label="multibloch">{body}</svg>"#,
+15 -3
View File
@@ -415,6 +415,17 @@ q_ffi!(quantum_state, |args: &Value| {
}))
});
fn draw_theme_of(args: &Value) -> Result<draw::ThemeName, String> {
match args
.get("theme")
.or_else(|| args.get("主题"))
.and_then(|v| v.as_str())
{
None => Ok(draw::ThemeName::Dark),
Some(s) => draw::ThemeName::parse(s),
}
}
q_ffi!(quantum_draw_circuit, |args: &Value| {
let c = circuit_of(args)?;
let kind = args
@@ -423,6 +434,7 @@ q_ffi!(quantum_draw_circuit, |args: &Value| {
.and_then(|v| v.as_str())
.unwrap_or("circuit")
.to_ascii_lowercase();
let theme = draw_theme_of(args)?;
let qubit = arg_u(args, "qubit")
.or_else(|_| arg_u(args, "比特"))
.unwrap_or(0);
@@ -433,16 +445,16 @@ q_ffi!(quantum_draw_circuit, |args: &Value| {
.filter(|s| !s.is_empty());
let (kind_out, svg) = match kind.as_str() {
"circuit" | "" => ("circuit", draw::circuit_svg(c)?),
"circuit" | "" => ("circuit", draw::circuit_svg(c, theme)?),
"probs" | "probabilities" | "概率" => {
let (qubits, amps) = sim::simulate_circuit(c)?;
let probs = sim::probabilities(&amps, qubits);
("probs", draw::probs_svg(&probs))
("probs", draw::probs_svg(&probs, theme))
}
"bloch" | "布洛赫" => {
let (qubits, amps) = sim::simulate_circuit(c)?;
let (x, y, z) = sim::bloch_vector(&amps, qubits, qubit)?;
("bloch", draw::bloch_svg(x, y, z))
("bloch", draw::bloch_svg(x, y, z, theme))
}
"hinton" | "欣顿" => {
let (n, amps) = sim::simulate_circuit(c)?;