diff --git a/doc/design/ext-quantum.md b/doc/design/ext-quantum.md index 2a7f533..0524008 100644 --- a/doc/design/ext-quantum.md +++ b/doc/design/ext-quantum.md @@ -161,7 +161,7 @@ $$ + `theta`=None # Rx/Ry/Rz ## matrix # → 振幅表 / 嵌套 list(文档与调试) -## draw # → quantum_svg(单门符号) +## draw # → quantum_svg(`kind=gate|matrix` 符号 / 热力图) ## matches_matrix + `matrix` + `tol`=1e-9 @@ -300,7 +300,7 @@ CLI:`draw path=bell.svg` 写沙箱文件;无 path 时不把整段 SVG 灌 st | 电路轨线(qubit 线 + 门盒 + 控点) | **必须** | `circuit.draw` | | 测量概率条形图 | **必须** | `probabilities` / `run` 后可 `draw kind=probs` | | 单比特布洛赫球(SVG) | 应有 | 仅 n=1 或指定 qubit 约化(纯态投影) | -| 门矩阵热力图 | 可后 | Q4 | +| 门矩阵热力图 | **必须** | `gate.draw kind=matrix`(幅值热图 + 小数标注) | | 交互拖拽编辑电路 | **不做** | 静态文档足够 | 视觉:内联 CSS;**无外链**字体/CDN(同 view 硬规则)。 diff --git a/doc/roadmap/ext-quantum.md b/doc/roadmap/ext-quantum.md index d82f65c..870d274 100644 --- a/doc/roadmap/ext-quantum.md +++ b/doc/roadmap/ext-quantum.md @@ -29,7 +29,7 @@ | **Q4** | 概率条 / 布洛赫;`matches_matrix`;examples + 用户文档 | **done** | | **Q5** | 教学噪声 + barrier/measure/append/state — [ext-quantum-q5.md](../design/ext-quantum-q5.md) | **done(最小)** | -验收金样:`tests/ext/quantum-bell-smoke.mq.md`、`quantum-draw-smoke.mq.md`、`quantum-gate-matrix-smoke.mq.md`、`quantum-author-api-smoke.mq.md`、`quantum-noise-smoke.mq.md`、中文对称样例。 +验收金样:`tests/ext/quantum-bell-smoke.mq.md`、`quantum-draw-smoke.mq.md`、`quantum-gate-matrix-smoke.mq.md`、`quantum-gate-heatmap-smoke.mq.md`、`quantum-author-api-smoke.mq.md`、`quantum-noise-smoke.mq.md`、中文对称样例(`quantum-author-api-zh-smoke` / `quantum-noise-zh-smoke`)。 --- diff --git a/examples/quantum-ghz/index.mq.md b/examples/quantum-ghz/index.mq.md new file mode 100644 index 0000000..77fe120 --- /dev/null +++ b/examples/quantum-ghz/index.mq.md @@ -0,0 +1,28 @@ +--- +title: GHZ state +description: 3-qubit GHZ; |000⟩/|111⟩ ≈ 1/2; probs + H matrix heatmap. +> ext/quantum/quantum.mq.md +--- + +# GHZ + +H on qubit 0, CX(0→1), CX(0→2). Ideal: |000⟩ and |111⟩ each with probability 1/2. + +# main + +`steps` = + +| step | gate | qubits | +|------|------|--------| +| 1 | H | 0 | +| 2 | CX | 0,1 | +| 3 | CX | 0,2 | + +*`qc` = > quantum.circuit qubits=3 steps=`steps` * +*`p` = > `qc`.probabilities * +*`_` = > `qc`.draw path=ghz.svg * +*`_` = > `qc`.draw kind=probs path=ghz-probs.svg * +*`H` = > quantum.gate name=H * +*`_` = > `H`.draw kind=matrix path=ghz-h-matrix.svg * + +> print text=`p` diff --git a/ext/quantum/quantum.mq.md b/ext/quantum/quantum.mq.md index f6f96a5..8d079f7 100644 --- a/ext/quantum/quantum.mq.md +++ b/ext/quantum/quantum.mq.md @@ -182,5 +182,8 @@ Named gate handle (built-in unitary). Use `matches_matrix` to check an author-su ## draw + `path`=None + + `kind`=gate -**> quantum_gate_draw gate=`self` path=`path`** +Gate glyph or complex-matrix heatmap (`kind=gate|matrix`). Records via host `record_plot`. + +**> quantum_gate_draw gate=`self` path=`path` kind=`kind`** diff --git a/ext/quantum/量子.mq.md b/ext/quantum/量子.mq.md index 9e98fef..99719b4 100644 --- a/ext/quantum/量子.mq.md +++ b/ext/quantum/量子.mq.md @@ -182,5 +182,8 @@ description: 官方量子电路模拟(中文)。门 + 电路;ABI 插件。 ## 绘图 + `路径`=None + + `种类`=gate -**> quantum_gate_draw gate=`self` path=`路径`** +门符号或复矩阵热力图(`种类=gate|matrix`)。经 `record_plot` 记录。 + +**> quantum_gate_draw gate=`self` path=`路径` kind=`种类`** diff --git a/plugins/quantum/src/draw.rs b/plugins/quantum/src/draw.rs index 196f1af..3c3aad1 100644 --- a/plugins/quantum/src/draw.rs +++ b/plugins/quantum/src/draw.rs @@ -2,7 +2,7 @@ use serde_json::Value; -use crate::sim::{parse_ops, Op}; +use crate::sim::{parse_ops, C, Op}; const COL_W: f64 = 56.0; const ROW_H: f64 = 40.0; @@ -405,3 +405,94 @@ pub fn gate_svg(name: &str) -> String { STROKE = STROKE, ) } + +fn mag(c: C) -> f64 { + (c.re * c.re + c.im * c.im).sqrt() +} + +fn heat_fill(m: f64) -> String { + // pale → indigo by |amp| + let t = m.clamp(0.0, 1.0); + let r = (232.0 + (55.0 - 232.0) * t).round() as i32; + let g = (236.0 + (66.0 - 236.0) * t).round() as i32; + let b = (241.0 + (250.0 - 241.0) * t).round() as i32; + format!("rgb({r},{g},{b})") +} + +/// Complex matrix heatmap (magnitude fill + re/im labels for small dims). +pub fn matrix_heatmap_svg(m: &[Vec], title: &str) -> String { + let n = m.len().max(1); + let cell = if n <= 2 { + 72.0 + } else if n <= 4 { + 56.0 + } else { + 40.0 + }; + let left = 36.0; + let top = 28.0; + let width = left + cell * n as f64 + 16.0; + let height = top + cell * n as f64 + 20.0; + let mut body = String::new(); + body.push_str(&format!( + r#"{title}"#, + title = esc(title), + STROKE = STROKE, + )); + for i in 0..n { + body.push_str(&format!( + r#"{i}"#, + y = top + (i as f64 + 0.55) * cell, + i = i, + muted = "#666", + )); + body.push_str(&format!( + r#"{i}"#, + x = left + (i as f64 + 0.5) * cell, + y = top - 6.0, + i = i, + muted = "#666", + )); + for j in 0..n { + let c = m + .get(i) + .and_then(|row| row.get(j)) + .copied() + .unwrap_or(C { re: 0.0, im: 0.0 }); + let x = left + j as f64 * cell; + let y = top + i as f64 * cell; + let fill = heat_fill(mag(c)); + body.push_str(&format!( + r#""#, + x = x, + y = y, + cell = cell, + fill = fill, + STROKE = STROKE, + )); + if n <= 4 { + let label = if c.im.abs() < 1e-9 { + format!("{:.3}", c.re) + } else if c.re.abs() < 1e-9 { + format!("{:.3}i", c.im) + } else { + format!("{:.2}{:+.2}i", c.re, c.im) + }; + let ink = if mag(c) > 0.55 { "#fff" } else { STROKE }; + body.push_str(&format!( + r#"{lab}"#, + tx = x + cell * 0.5, + ty = y + cell * 0.55, + lab = esc(&label), + ink = ink, + )); + } + } + } + format!( + r#"{body}"#, + w = width, + h = height, + body = body, + ) +} diff --git a/plugins/quantum/src/lib.rs b/plugins/quantum/src/lib.rs index e74282b..019ca9e 100644 --- a/plugins/quantum/src/lib.rs +++ b/plugins/quantum/src/lib.rs @@ -542,16 +542,37 @@ q_ffi!(quantum_gate_draw, |args: &Value| { .or_else(|| gate.get("名")) .and_then(|v| v.as_str()) .unwrap_or("?"); + let theta = gate + .get("theta") + .or_else(|| gate.get("参数")) + .and_then(|v| v.as_f64()); + let kind = args + .get("kind") + .or_else(|| args.get("种类")) + .or_else(|| args.get("类型")) + .and_then(|v| v.as_str()) + .unwrap_or("gate") + .to_ascii_lowercase(); let path = args .get("path") .or_else(|| args.get("路径")) .and_then(|v| v.as_str()) .filter(|s| !s.is_empty()); - let svg = draw::gate_svg(name); + let (kind_out, svg) = if matches!(kind.as_str(), "matrix" | "heatmap" | "矩阵") { + let m = sim::named_gate_matrix(name, theta)?; + ( + "matrix", + draw::matrix_heatmap_svg(&m, &format!("{name} matrix")), + ) + } else if matches!(kind.as_str(), "gate" | "glyph" | "门") { + ("gate", draw::gate_svg(name)) + } else { + return Err(format!("unknown gate draw kind `{kind}` (gate|matrix)")); + }; record_plot(&svg, path)?; Ok(json!({ "_type": "quantum_svg", - "kind": "gate", + "kind": kind_out, "svg": svg, })) }); diff --git a/public/features/06-quantum.mq.md b/public/features/06-quantum.mq.md index 4cd4633..b437656 100644 --- a/public/features/06-quantum.mq.md +++ b/public/features/06-quantum.mq.md @@ -1,6 +1,6 @@ --- title: Quantum circuits (ext/quantum) -description: Official quantum simulator — tables, gates, probs/Bloch SVG +description: Official quantum simulator — tables, gates, probs/Bloch/matrix SVG > ext/quantum/quantum.mq.md --- @@ -8,7 +8,7 @@ description: Official quantum simulator — tables, gates, probs/Bloch SVG Optional package (not stdlib). Install: `marqdo ext add quantum` (or build `marqdo_plugin_quantum`). -Bell state: H then CX. Ideal |00⟩ / |11⟩ each ≈ 1/2. `draw kind=probs` embeds a probability bar chart in view. +GHZ (3 qubits): H on q0, then CX 0→1 and 0→2. Ideal |000⟩ / |111⟩ each ≈ 1/2. `draw kind=probs` embeds probability bars; `gate.draw kind=matrix` shows a heatmap. `steps` = @@ -16,10 +16,13 @@ Bell state: H then CX. Ideal |00⟩ / |11⟩ each ≈ 1/2. `draw kind=probs` emb |------|------|--------| | 1 | H | 0 | | 2 | CX | 0,1 | +| 3 | CX | 0,2 | -*`qc` = > quantum.circuit qubits=2 steps=`steps` * +*`qc` = > quantum.circuit qubits=3 steps=`steps` * *`p` = > `qc`.probabilities * *`_` = > `qc`.draw kind=probs * +*`H` = > quantum.gate name=H * +*`__` = > `H`.draw kind=matrix * > print text=`p` > print text=quantum-feature-ok diff --git a/public/features/06-量子.mq.md b/public/features/06-量子.mq.md index cd7e36c..bea1efa 100644 --- a/public/features/06-量子.mq.md +++ b/public/features/06-量子.mq.md @@ -1,6 +1,6 @@ --- title: 量子电路(ext/quantum) -description: 官方量子模拟 — 表、门、概率/布洛赫 SVG +description: 官方量子模拟 — 表、门、概率/布洛赫/矩阵热图 SVG > ext/quantum/量子.mq.md --- @@ -8,7 +8,7 @@ description: 官方量子模拟 — 表、门、概率/布洛赫 SVG 可选扩展(非 stdlib)。安装:`marqdo ext add quantum`(或编译 `marqdo_plugin_quantum`)。 -贝尔态:先哈达玛再控非。理想 |00⟩ / |11⟩ 各约 1/2。`绘图 种类=probs` 在 view 嵌入概率条。 +GHZ(3 比特):q0 哈达玛,再控非 0→1、0→2。理想 |000⟩ / |111⟩ 各约 1/2。`绘图 种类=probs` 嵌入概率条;`门.绘图 种类=matrix` 为矩阵热图。 `步骤` = @@ -16,10 +16,13 @@ description: 官方量子模拟 — 表、门、概率/布洛赫 SVG |----|----|------| | 1 | H | 0 | | 2 | CX | 0,1 | +| 3 | CX | 0,2 | -*`qc` = > 量子.电路 比特数=2 步骤=`步骤` * +*`qc` = > 量子.电路 比特数=3 步骤=`步骤` * *`p` = > `qc`.概率 * *`_` = > `qc`.绘图 种类=probs * +*`H` = > 量子.门 名=H * +*`__` = > `H`.绘图 种类=matrix * > 打印 内容=`p` > 打印 内容=量子特性-ok diff --git a/tests/ext/quantum-author-api-zh-smoke.mq.md b/tests/ext/quantum-author-api-zh-smoke.mq.md new file mode 100644 index 0000000..bdb85ca --- /dev/null +++ b/tests/ext/quantum-author-api-zh-smoke.mq.md @@ -0,0 +1,37 @@ +--- +title: quantum author API zh smoke +description: 屏障 / 测量 / 追加 / 态 / 绘图. +> ext/quantum/量子.mq.md +--- + +# main + +*`qc` = > 量子.电路 比特数=2 * +*`qc` = > `qc`.哈达玛 比特=0 * +*`qc` = > `qc`.屏障 * +*`qc` = > `qc`.控非 控制=0 目标=1 * +*`qc` = > `qc`.测量 * + +*`st` = > `qc`.态 * +*`dim` = `st`[^dim] * +1. `dim` == 4 + > print text=state-ok +2. * + > print text=state-fail + +*`img` = > `qc`.绘图 路径=quantum-author-zh-draw.svg * +*`kind` = `img`[^kind] * +1. `kind` == circuit + > print text=draw-ok +2. * + > print text=draw-fail + +*`other` = > 量子.电路 比特数=2 * +*`other` = > `other`.泡利X 比特=1 * +*`qc2` = > `qc`.追加 操作=`other` * +*`ops` = `qc2`[^ops] * +*`n` = > len `ops` * +1. `n` == 5 + > print text=append-ok +2. * + > print text=append-fail diff --git a/tests/ext/quantum-gate-heatmap-smoke.mq.md b/tests/ext/quantum-gate-heatmap-smoke.mq.md new file mode 100644 index 0000000..f57d257 --- /dev/null +++ b/tests/ext/quantum-gate-heatmap-smoke.mq.md @@ -0,0 +1,16 @@ +--- +title: quantum gate matrix heatmap smoke +description: gate.draw kind=matrix writes SVG heatmap. +> ext/quantum/quantum.mq.md +--- + +# main + +*`H` = > quantum.gate name=H * +*`img` = > `H`.draw kind=matrix path=quantum-gate-heatmap.svg * +*`kind` = `img`[^kind] * + +1. `kind` == matrix + > print text=heatmap-ok +2. * + > print text=heatmap-fail diff --git a/tests/ext/quantum-noise-zh-smoke.mq.md b/tests/ext/quantum-noise-zh-smoke.mq.md new file mode 100644 index 0000000..65b64c4 --- /dev/null +++ b/tests/ext/quantum-noise-zh-smoke.mq.md @@ -0,0 +1,18 @@ +--- +title: quantum noise zh smoke +description: 噪声 bitflip p=1 后 I 使全部射中 |1⟩. +> ext/quantum/量子.mq.md +--- + +# main + +*`qc` = > 量子.电路 比特数=1 * +*`qc` = > `qc`.单位 比特=0 * +*`qc` = > `qc`.噪声 种类=bitflip 概率=1 * +*`r` = > `qc`.运行 次数=32 种子=3 * +*`c1` = `r`[^counts][^1] * + +1. `c1` == 32 + > print text=noise-ok +2. * + > print text=noise-fail diff --git a/tests/gold.rs b/tests/gold.rs index c02829b..ed44a0a 100644 --- a/tests/gold.rs +++ b/tests/gold.rs @@ -1558,6 +1558,56 @@ fn ext_quantum_noise_smoke() { assert_out("tests/ext/quantum-noise-smoke.mq.md", "noise-ok"); } +#[test] +fn ext_quantum_gate_heatmap_smoke() { + let status = Command::new("cargo") + .args(["build", "-p", "marqdo_plugin_quantum"]) + .current_dir(env!("CARGO_MANIFEST_DIR")) + .status() + .expect("build quantum plugin"); + assert!(status.success(), "failed to build marqdo_plugin_quantum"); + assert_out("tests/ext/quantum-gate-heatmap-smoke.mq.md", "heatmap-ok"); + let svg_path = "tests/ext/quantum-gate-heatmap.svg"; + let svg = std::fs::read_to_string(svg_path).unwrap_or_default(); + assert!( + svg.contains("