Land quantum Q4 visuals and gate Stream to LLM/agent imports.

Add probs/Bloch draw kinds and matches_matrix, plus feature docs; hide the view Stream panel unless llm/agent is imported, and stop rewriting method names like 绘图 through host_plot aliases.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-08-12 12:36:51 +08:00
1 parent 25b7be6739
commit eea6fe1a0e
20 files changed
+852 -23

No files matched your search

+180
View File
@@ -180,3 +180,183 @@ fn draw_swap(out: &mut String, x: f64, a: usize, b: usize) {
));
}
}
/// Probability histogram from basis-label → probability map.
pub fn probs_svg(probs: &serde_json::Map<String, Value>) -> String {
let mut entries: Vec<(String, f64)> = probs
.iter()
.filter_map(|(k, v)| v.as_f64().map(|p| (k.clone(), p)))
.collect();
entries.sort_by(|a, b| a.0.cmp(&b.0));
if entries.is_empty() {
entries.push(("∅".into(), 0.0));
}
let n = entries.len().max(1);
let bar_w = 36.0;
let gap = 12.0;
let chart_h = 120.0;
let pad_l = 40.0;
let pad_b = 36.0;
let pad_t = 20.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(&format!(
r#"<line x1="{x0}" y1="{y}" x2="{x1}" y2="{y}" stroke="{STROKE}" stroke-width="1.2"/>"#,
x0 = pad_l - 4.0,
x1 = width - pad_r,
y = pad_t + chart_h,
));
body.push_str(&format!(
r#"<line x1="{x}" y1="{y0}" x2="{x}" y2="{y1}" stroke="{STROKE}" stroke-width="1.2"/>"#,
x = pad_l - 4.0,
y0 = pad_t,
y1 = pad_t + chart_h,
));
body.push_str(&format!(
r#"<text x="6" y="{ty}" font-family="ui-monospace,Menlo,monospace" font-size="10" fill="{STROKE}">1</text>"#,
ty = pad_t + 4.0,
));
body.push_str(&format!(
r#"<text x="6" y="{ty}" font-family="ui-monospace,Menlo,monospace" font-size="10" fill="{STROKE}">0</text>"#,
ty = pad_t + chart_h + 4.0,
));
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"/>"#,
x = x,
y = y,
bar_w = bar_w,
h = h,
bar_fill = bar_fill,
STROKE = STROKE,
));
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>"#,
cx = x + bar_w / 2.0,
ty = pad_t + chart_h + 16.0,
lab = esc(label),
STROKE = STROKE,
));
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>"#,
cx = x + bar_w / 2.0,
ty = y - 4.0,
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>"#,
w = width,
h = height,
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;
// 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(&format!(
r#"<circle cx="{cx}" cy="{cy}" r="{r}" fill="{fill}" stroke="{STROKE}" stroke-width="1.5"/>"#,
cx = cx,
cy = cy,
r = r,
fill = BOX,
STROKE = STROKE,
));
// axes
let axes = [
("+X", 1.0, 0.0, 0.0),
("-X", -1.0, 0.0, 0.0),
("+Y", 0.0, 1.0, 0.0),
("+Z", 0.0, 0.0, 1.0),
("-Z", 0.0, 0.0, -1.0),
];
for (lab, vx, vy, vz) in axes {
let x1 = px(0.0, 0.0, 0.0);
let y1 = py(0.0, 0.0, 0.0);
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"/>"#,
x1 = x1,
y1 = y1,
x2 = x2,
y2 = y2,
axis = "#888",
));
body.push_str(&format!(
r#"<text x="{tx}" y="{ty}" font-family="ui-monospace,Menlo,monospace" font-size="10" fill="{STROKE}">{lab}</text>"#,
tx = x2 + 4.0,
ty = y2 + 3.0,
lab = lab,
STROKE = STROKE,
));
}
// state vector
let sx = px(x, y, z);
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";
body.push_str(&format!(
r#"<line x1="{ox}" y1="{oy}" x2="{sx}" y2="{sy}" stroke="{arrow}" stroke-width="2.5"/>"#,
ox = ox,
oy = oy,
sx = sx,
sy = sy,
arrow = arrow,
));
body.push_str(&format!(
r#"<circle cx="{sx}" cy="{sy}" r="4" fill="{arrow}"/>"#,
sx = sx,
sy = sy,
arrow = arrow,
));
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>"#,
hy = height - 12.0,
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>"#,
w = width,
h = height,
body = body,
)
}
/// Minimal single-gate glyph for `# gate`.draw.
pub fn gate_svg(name: &str) -> String {
let label = esc(name);
let width = 64.0;
let height = 48.0;
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="gate {label}"><rect x="12" y="8" width="40" height="32" rx="4" fill="{BOX}" stroke="{STROKE}" stroke-width="1.5"/><text x="32" y="29" text-anchor="middle" font-family="ui-monospace,Menlo,monospace" font-size="14" fill="{STROKE}">{label}</text></svg>"#,
w = width,
h = height,
label = label,
BOX = BOX,
STROKE = STROKE,
)
}