Add quantum Q5 teaching noise and finish author-facing circuit APIs.

Barrier/measure/append/state plus bitflip/depolarizing run trajectories let docs show noisy sampling without a full open-system simulator.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-08-12 13:46:56 +08:00
1 parent eea6fe1a0e
commit 60d570756f
13 files changed
+663 -51

No files matched your search

+45
View File
@@ -79,6 +79,19 @@ pub fn circuit_svg(circuit: &Value) -> Result<String, String> {
for (col, op) in ops.iter().enumerate() {
let x = x_of(col);
match op.gate.as_str() {
"BARRIER" => {
draw_barrier(&mut body, x, qubits);
}
"MEASURE" | "M" => {
let qs = if op.qubits.is_empty() {
(0..qubits).collect::<Vec<_>>()
} else {
op.qubits.clone()
};
for q in qs {
draw_measure(&mut body, x, q);
}
}
"CX" | "CNOT" | "CN" if op.qubits.len() >= 2 => {
draw_cx(&mut body, x, op.qubits[0], op.qubits[1]);
}
@@ -119,6 +132,38 @@ fn draw_box(out: &mut String, x: f64, q: usize, label: &str) {
));
}
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;
out.push_str(&format!(
r#"<line x1="{x}" y1="{y0}" x2="{x}" y2="{y1}" stroke="{STROKE}" stroke-width="1.5" stroke-dasharray="4 3"/>"#
));
}
fn draw_measure(out: &mut String, x: f64, q: usize) {
let y = y_of(q);
let w = 28.0;
let h = 28.0;
out.push_str(&format!(
r#"<rect x="{rx}" y="{ry}" width="{w}" height="{h}" rx="3" fill="{BOX}" stroke="{STROKE}" stroke-width="1.5"/>"#,
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,
));
}
fn draw_cx(out: &mut String, x: f64, control: usize, target: usize) {
let yc = y_of(control);
let yt = y_of(target);
+80
View File
@@ -343,6 +343,77 @@ q_ffi!(quantum_run, |args: &Value| {
sim::run_circuit(c, shots, seed)
});
q_ffi!(quantum_barrier, |args: &Value| {
let c = circuit_of(args)?;
sim::push_op(c, "BARRIER", vec![], None)
});
q_ffi!(quantum_measure, |args: &Value| {
let c = circuit_of(args)?;
let qs = match args.get("qubits").or_else(|| args.get("比特")) {
None | Some(Value::Null) => vec![],
Some(Value::Array(a)) => a
.iter()
.map(|v| {
v.as_u64()
.or_else(|| v.as_i64().map(|i| i as u64))
.ok_or_else(|| "bad qubit".to_string())
.map(|u| u as usize)
})
.collect::<Result<Vec<_>, _>>()?,
Some(Value::Number(n)) => {
vec![n.as_u64().or_else(|| n.as_i64().map(|i| i as u64)).unwrap() as usize]
}
Some(Value::String(s)) => {
if s.trim().is_empty() {
vec![]
} else {
s.split(|c| c == ',' || c == ' ' || c == ',')
.filter(|t| !t.is_empty())
.map(|t| {
t.trim()
.parse::<usize>()
.map_err(|_| format!("bad qubit `{t}`"))
})
.collect::<Result<Vec<_>, _>>()?
}
}
_ => return Err("bad qubits for measure".into()),
};
sim::push_op(c, "MEASURE", qs, None)
});
q_ffi!(quantum_append, |args: &Value| {
let c = circuit_of(args)?;
let op = args
.get("op")
.or_else(|| args.get("操作"))
.ok_or_else(|| "append needs `op`".to_string())?;
sim::append(c, op)
});
q_ffi!(quantum_noise, |args: &Value| {
let c = circuit_of(args)?;
let kind = args
.get("kind")
.or_else(|| args.get("种类"))
.and_then(|v| v.as_str())
.ok_or_else(|| "noise needs `kind`".to_string())?;
let p = arg_f(args, "p").or_else(|_| arg_f(args, "概率"))?;
sim::set_noise(c, kind, p)
});
q_ffi!(quantum_state, |args: &Value| {
let c = circuit_of(args)?;
let (qubits, amps) = sim::simulate_circuit(c)?;
Ok(json!({
"_type": "quantum_state",
"qubits": qubits,
"dim": amps.len(),
"amplitudes": sim::amps_to_json(&amps),
}))
});
q_ffi!(quantum_draw_circuit, |args: &Value| {
let c = circuit_of(args)?;
let kind = args
@@ -552,6 +623,15 @@ pub unsafe extern "C" fn marqdo_plugin_init(host: *const MarqdoHostApi) -> c_int
quantum_probabilities as PluginFn,
),
("quantum_run", "circuit,shots,seed", quantum_run as PluginFn),
("quantum_barrier", "circuit", quantum_barrier as PluginFn),
(
"quantum_measure",
"circuit,qubits",
quantum_measure as PluginFn,
),
("quantum_append", "circuit,op", quantum_append as PluginFn),
("quantum_noise", "circuit,kind,p", quantum_noise as PluginFn),
("quantum_state", "circuit", quantum_state as PluginFn),
(
"quantum_draw_circuit",
"circuit,path,kind,qubit",
+299 -38
View File
@@ -320,11 +320,20 @@ pub fn parse_ops(circuit: &Value) -> Result<Vec<Op>, String> {
let u = n.as_u64().or_else(|| n.as_i64().map(|i| i as u64)).unwrap() as usize;
vec![u]
}
Some(Value::String(s)) => parse_qubit_list(s)?,
_ => {
if let Some(q) = row
Some(Value::String(s)) => {
let t = s.trim();
if t.is_empty() {
vec![]
} else {
parse_qubit_list(s)?
}
}
Some(Value::Null) => vec![],
None => {
if is_meta_gate(&gate) {
vec![]
} else if let Some(q) = row
.get("qubit")
.or_else(|| row.get("比特"))
.and_then(|v| v.as_u64().or_else(|| v.as_i64().map(|i| i as u64)))
{
vec![q as usize]
@@ -341,6 +350,7 @@ pub fn parse_ops(circuit: &Value) -> Result<Vec<Op>, String> {
return Err(format!("op `{gate}` missing qubits"));
}
}
_ => return Err(format!("op `{gate}` bad qubits")),
};
let theta = row
.get("theta")
@@ -370,74 +380,177 @@ pub fn apply_op(amps: &mut [C], qubits: usize, op: &Op) -> Result<(), String> {
}
}
match op.gate.as_str() {
"BARRIER" | "MEASURE" | "M" => Ok(()),
"I" | "ID" | "IDENTITY" => {
let q = *op.qubits.first().ok_or("I needs qubit")?;
apply_i(amps, q);
Ok(())
}
"X" | "NOT" => {
let q = *op.qubits.first().ok_or("X needs qubit")?;
apply_x(amps, q);
Ok(())
}
"Y" => {
let q = *op.qubits.first().ok_or("Y needs qubit")?;
apply_y(amps, q);
Ok(())
}
"Z" => {
let q = *op.qubits.first().ok_or("Z needs qubit")?;
apply_z(amps, q);
Ok(())
}
"S" => {
let q = *op.qubits.first().ok_or("S needs qubit")?;
apply_s(amps, q);
Ok(())
}
"T" => {
let q = *op.qubits.first().ok_or("T needs qubit")?;
apply_t(amps, q);
Ok(())
}
"H" | "HADAMARD" => {
let q = *op.qubits.first().ok_or("H needs qubit")?;
apply_h(amps, q);
Ok(())
}
"RX" => {
let q = *op.qubits.first().ok_or("Rx needs qubit")?;
let th = op.theta.ok_or("Rx needs theta")?;
apply_rx(amps, q, th);
Ok(())
}
"RY" => {
let q = *op.qubits.first().ok_or("Ry needs qubit")?;
let th = op.theta.ok_or("Ry needs theta")?;
apply_ry(amps, q, th);
Ok(())
}
"RZ" => {
let q = *op.qubits.first().ok_or("Rz needs qubit")?;
let th = op.theta.ok_or("Rz needs theta")?;
apply_rz(amps, q, th);
Ok(())
}
"CX" | "CNOT" | "CN" => {
if op.qubits.len() < 2 {
return Err("CX needs control and target".into());
}
apply_cx(amps, op.qubits[0], op.qubits[1]);
Ok(())
}
"CZ" => {
if op.qubits.len() < 2 {
return Err("CZ needs control and target".into());
}
apply_cz(amps, op.qubits[0], op.qubits[1]);
Ok(())
}
"SWAP" => {
if op.qubits.len() < 2 {
return Err("SWAP needs two qubits".into());
}
apply_swap(amps, op.qubits[0], op.qubits[1]);
Ok(())
}
other => Err(format!(
"unsupported gate `{other}` (I/X/Y/Z/H/S/T/Rx/Ry/Rz/CX/CZ/SWAP/BARRIER/MEASURE)"
)),
}
}
pub fn is_meta_gate(gate: &str) -> bool {
matches!(
gate.to_ascii_uppercase().as_str(),
"BARRIER" | "MEASURE" | "M"
)
}
pub fn is_unitary_op(op: &Op) -> bool {
!is_meta_gate(&op.gate)
}
#[derive(Clone, Debug)]
pub struct NoiseSpec {
pub kind: String, // bitflip | depolarizing
pub p: f64,
}
pub fn parse_noise(circuit: &Value) -> Result<Option<NoiseSpec>, String> {
let kind = circuit
.get("noise_kind")
.or_else(|| circuit.get("噪声种类"))
.and_then(|v| v.as_str())
.map(|s| s.to_ascii_lowercase());
let p = circuit
.get("noise_p")
.or_else(|| circuit.get("噪声概率"))
.and_then(|v| v.as_f64());
match (kind, p) {
(None, None) => Ok(None),
(Some(k), Some(p)) => {
if !(0.0..=1.0).contains(&p) {
return Err("noise p must be in [0,1]".into());
}
let kind = match k.as_str() {
"bitflip" | "bit_flip" | "比特翻转" => "bitflip".into(),
"depolarizing" | "depolarise" | "退极化" => "depolarizing".into(),
other => {
return Err(format!(
"unknown noise kind `{other}` (bitflip|depolarizing)"
));
}
};
Ok(Some(NoiseSpec { kind, p }))
}
_ => Err("noise needs both kind and p".into()),
}
}
pub fn set_noise(circuit: &Value, kind: &str, p: f64) -> Result<Value, String> {
if !(0.0..=1.0).contains(&p) {
return Err("noise p must be in [0,1]".into());
}
let k = match kind.trim().to_ascii_lowercase().as_str() {
"bitflip" | "bit_flip" | "比特翻转" => "bitflip",
"depolarizing" | "depolarise" | "退极化" => "depolarizing",
other => {
return Err(format!(
"unsupported gate `{other}` (I/X/Y/Z/H/S/T/Rx/Ry/Rz/CX/CZ/SWAP)"
"unknown noise kind `{other}` (bitflip|depolarizing)"
));
}
};
let mut obj = circuit.as_object().cloned().unwrap_or_default();
obj.insert("noise_kind".into(), json!(k));
obj.insert("noise_p".into(), json!(p));
Ok(Value::Object(obj))
}
/// Qubits to read out: union of MEASURE ops; empty MEASURE list = all qubits; no MEASURE = all.
pub fn measure_targets(ops: &[Op], n: usize) -> Vec<usize> {
let mut measured = Vec::new();
let mut any = false;
for op in ops {
let g = op.gate.to_ascii_uppercase();
if g == "MEASURE" || g == "M" {
any = true;
if op.qubits.is_empty() {
return (0..n).collect();
}
for &q in &op.qubits {
if !measured.contains(&q) {
measured.push(q);
}
}
}
}
Ok(())
if !any {
return (0..n).collect();
}
measured.sort_unstable();
measured
}
struct Rng {
@@ -447,7 +560,11 @@ struct Rng {
impl Rng {
fn new(seed: u64) -> Self {
Self {
state: if seed == 0 { 0x9e37_79b9_7f4a_7c15 } else { seed },
state: if seed == 0 {
0x9e37_79b9_7f4a_7c15
} else {
seed
},
}
}
fn next_u64(&mut self) -> u64 {
@@ -463,8 +580,33 @@ impl Rng {
}
}
/// Sample computational-basis shots; returns counts map.
pub fn sample_counts(amps: &[C], qubits: usize, shots: usize, seed: u64) -> Map<String, Value> {
fn apply_noise_on_qubits(
amps: &mut [C],
qs: &[usize],
noise: &NoiseSpec,
rng: &mut Rng,
) {
if noise.p <= 0.0 {
return;
}
for &q in qs {
if rng.next_f64() >= noise.p {
continue;
}
match noise.kind.as_str() {
"bitflip" => apply_x(amps, q),
"depolarizing" => match rng.next_u64() % 3 {
0 => apply_x(amps, q),
1 => apply_y(amps, q),
_ => apply_z(amps, q),
},
_ => {}
}
}
}
fn sample_label(amps: &[C], n: usize, targets: &[usize], rng: &mut Rng) -> String {
// Build CDF over full basis, then project bits onto targets (ascending, LSB = targets[0]).
let mut cdf = Vec::with_capacity(amps.len());
let mut acc = 0.0;
for a in amps {
@@ -472,31 +614,41 @@ pub fn sample_counts(amps: &[C], qubits: usize, shots: usize, seed: u64) -> Map<
cdf.push(acc);
}
if acc <= 0.0 {
return Map::new();
return "0".repeat(targets.len().max(1));
}
// normalize cdf tail to 1
let inv = 1.0 / acc;
for x in &mut cdf {
*x *= inv;
}
let r = rng.next_f64();
let mut idx = 0usize;
for (i, &c) in cdf.iter().enumerate() {
if r <= c {
idx = i;
break;
}
idx = i;
}
if targets.len() == n {
return basis_label(idx, n);
}
let mut bits = String::with_capacity(targets.len());
// label: targets[0] is rightmost (LSB of the label string)
for &q in targets.iter().rev() {
bits.push(if bit(idx, q) { '1' } else { '0' });
}
bits
}
/// Sample computational-basis shots; returns counts map.
#[allow(dead_code)]
pub fn sample_counts(amps: &[C], qubits: usize, shots: usize, seed: u64) -> Map<String, Value> {
let targets: Vec<usize> = (0..qubits).collect();
let mut counts: Map<String, Value> = Map::new();
let mut rng = Rng::new(seed);
for _ in 0..shots {
let r = rng.next_f64();
let mut idx = 0usize;
for (i, &c) in cdf.iter().enumerate() {
if r <= c {
idx = i;
break;
}
idx = i;
}
let label = basis_label(idx, qubits);
let n = counts
.get(&label)
.and_then(|v| v.as_u64())
.unwrap_or(0)
+ 1;
let label = sample_label(amps, qubits, &targets, &mut rng);
let n = counts.get(&label).and_then(|v| v.as_u64()).unwrap_or(0) + 1;
counts.insert(label, json!(n));
}
counts
@@ -506,14 +658,51 @@ pub fn run_circuit(circuit: &Value, shots: usize, seed: u64) -> Result<Value, St
if shots == 0 {
return Err("shots must be >= 1".into());
}
let (qubits, amps) = simulate_circuit(circuit)?;
let counts = sample_counts(&amps, qubits, shots, seed);
Ok(json!({
let qubits = circuit
.get("qubits")
.or_else(|| circuit.get("比特数"))
.and_then(|v| v.as_u64().or_else(|| v.as_i64().map(|i| i as u64)))
.ok_or_else(|| "circuit missing qubits".to_string())? as usize;
check_qubits(qubits)?;
let ops = parse_ops(circuit)?;
let noise = parse_noise(circuit)?;
let targets = measure_targets(&ops, qubits);
let mut counts: Map<String, Value> = Map::new();
let mut rng = Rng::new(seed);
for _ in 0..shots {
let mut amps = zero_state(qubits);
for op in &ops {
if !is_unitary_op(op) {
continue;
}
apply_op(&mut amps, qubits, op)?;
if let Some(ref nspec) = noise {
if nspec.p > 0.0 {
apply_noise_on_qubits(&mut amps, &op.qubits, nspec, &mut rng);
}
}
}
renormalize(&mut amps);
let label = sample_label(&amps, qubits, &targets, &mut rng);
let c = counts.get(&label).and_then(|v| v.as_u64()).unwrap_or(0) + 1;
counts.insert(label, json!(c));
}
let mut out = json!({
"qubits": qubits,
"shots": shots,
"seed": seed,
"counts": counts,
}))
"measure": targets,
});
if let Some(nspec) = noise {
out.as_object_mut().unwrap().insert(
"noise".into(),
json!({ "kind": nspec.kind, "p": nspec.p }),
);
}
Ok(out)
}
pub fn simulate_circuit(circuit: &Value) -> Result<(usize, Vec<C>), String> {
@@ -650,12 +839,17 @@ fn op_from_step_row(row: &Value) -> Result<Op, String> {
.and_then(|v| v.as_str())
.ok_or_else(|| "step row missing `gate` / `门`".to_string())?
.to_ascii_uppercase();
let qs = row
let qubits = match row
.get("qubits")
.or_else(|| row.get("qubit"))
.or_else(|| row.get("比特"))
.ok_or_else(|| "step row missing `qubits` / `比特`".to_string())?;
let qubits = qubits_from_cell(qs)?;
{
None if is_meta_gate(&gate) => vec![],
None => {
return Err("step row missing `qubits` / `比特`".into());
}
Some(v) => qubits_from_cell(v)?,
};
let theta = row
.get("theta")
.or_else(|| row.get("参数"))
@@ -672,6 +866,7 @@ fn op_from_step_row(row: &Value) -> Result<Op, String> {
fn qubits_from_cell(v: &Value) -> Result<Vec<usize>, String> {
match v {
Value::Null => Ok(vec![]),
Value::Array(a) => a
.iter()
.map(|x| {
@@ -688,7 +883,13 @@ fn qubits_from_cell(v: &Value) -> Result<Vec<usize>, String> {
.ok_or_else(|| "bad qubit index".to_string())? as usize;
Ok(vec![u])
}
Value::String(s) => parse_qubit_list(s),
Value::String(s) => {
if s.trim().is_empty() {
Ok(vec![])
} else {
parse_qubit_list(s)
}
}
_ => Err("bad qubits cell".into()),
}
}
@@ -743,11 +944,19 @@ pub fn push_op(circuit: &Value, gate: &str, qubits: Vec<usize>, theta: Option<f6
.and_then(|v| v.as_u64().or_else(|| v.as_i64().map(|i| i as u64)))
.ok_or_else(|| "circuit missing qubits".to_string())? as usize;
check_qubits(n)?;
for &q in &qubits {
if q >= n {
return Err(format!("qubit {q} out of range for n={n}"));
let g = gate.to_ascii_uppercase();
if !is_meta_gate(&g) || !qubits.is_empty() {
for &q in &qubits {
if q >= n {
return Err(format!("qubit {q} out of range for n={n}"));
}
}
}
if is_meta_gate(&g) {
// ok with empty qubits (barrier / measure-all)
} else if qubits.is_empty() {
return Err(format!("gate `{g}` needs qubits"));
}
let mut obj = circuit.as_object().cloned().unwrap_or_default();
let mut ops = obj
.get("ops")
@@ -755,7 +964,7 @@ pub fn push_op(circuit: &Value, gate: &str, qubits: Vec<usize>, theta: Option<f6
.cloned()
.unwrap_or_default();
let mut op = json!({
"gate": gate.to_ascii_uppercase(),
"gate": g,
"qubits": qubits,
});
if let Some(t) = theta {
@@ -766,6 +975,58 @@ pub fn push_op(circuit: &Value, gate: &str, qubits: Vec<usize>, theta: Option<f6
Ok(Value::Object(obj))
}
/// Append another circuit's ops, a single op map, or reject bare gate handles without qubits.
pub fn append(circuit: &Value, op: &Value) -> Result<Value, String> {
let n = circuit
.get("qubits")
.and_then(|v| v.as_u64().or_else(|| v.as_i64().map(|i| i as u64)))
.ok_or_else(|| "circuit missing qubits".to_string())? as usize;
let mut obj = circuit.as_object().cloned().unwrap_or_default();
let mut ops = obj
.get("ops")
.and_then(|v| v.as_array())
.cloned()
.unwrap_or_default();
if let Some(arr) = op.get("ops").and_then(|v| v.as_array()) {
let other_n = op
.get("qubits")
.and_then(|v| v.as_u64())
.unwrap_or(n as u64) as usize;
if other_n > n {
return Err(format!(
"append: other circuit qubits={other_n} exceeds this n={n}"
));
}
for row in arr {
ops.push(row.clone());
}
} else if op.get("gate").or_else(|| op.get("门")).is_some() {
let parsed = parse_ops(&json!({ "ops": [op] }))?;
for p in parsed {
for &q in &p.qubits {
if q >= n {
return Err(format!("append qubit {q} out of range for n={n}"));
}
}
let mut row = json!({ "gate": p.gate, "qubits": p.qubits });
if let Some(t) = p.theta {
row.as_object_mut()
.unwrap()
.insert("theta".into(), json!(t));
}
ops.push(row);
}
} else if op.get("name").or_else(|| op.get("名")).is_some() {
return Err("append gate handle needs an op with qubits; pass a circuit or {{gate,qubits}}".into());
} else {
return Err("append needs a circuit (ops) or op {{gate,qubits}}".into());
}
obj.insert("ops".into(), Value::Array(ops));
Ok(Value::Object(obj))
}
/// Bloch vector (x,y,z) for reduced density of `qubit` (LSB = qubit 0).
pub fn bloch_vector(amps: &[C], qubits: usize, qubit: usize) -> Result<(f64, f64, f64), String> {
if qubit >= qubits {