Treat bare ids as variables in italic/bold, and land table/quantum follow-ups.
Prefer-var parsing in *…* / **…* (quoted strings for text), table cell expressions, digit map footnotes, and quantum steps/draw with updated golds and LLM sources. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
ff6425e6c5
commit
91d51f3f0a
63 files changed
+1542
-477
No files matched your search
@@ -0,0 +1,182 @@
|
||||
//! Circuit rail SVG (design §7.4).
|
||||
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::sim::{parse_ops, 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 BOX: &str = "#f7f4ef";
|
||||
const STROKE: &str = "#1a1a1a";
|
||||
|
||||
fn esc(s: &str) -> String {
|
||||
s.replace('&', "&")
|
||||
.replace('<', "<")
|
||||
.replace('>', ">")
|
||||
.replace('"', """)
|
||||
}
|
||||
|
||||
fn gate_label(op: &Op) -> String {
|
||||
match op.gate.as_str() {
|
||||
"RX" => op
|
||||
.theta
|
||||
.map(|t| format!("Rx({t:.2})"))
|
||||
.unwrap_or_else(|| "Rx".into()),
|
||||
"RY" => op
|
||||
.theta
|
||||
.map(|t| format!("Ry({t:.2})"))
|
||||
.unwrap_or_else(|| "Ry".into()),
|
||||
"RZ" => op
|
||||
.theta
|
||||
.map(|t| format!("Rz({t:.2})"))
|
||||
.unwrap_or_else(|| "Rz".into()),
|
||||
"HADAMARD" => "H".into(),
|
||||
"CNOT" | "CN" => "CX".into(),
|
||||
other => other.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
/// Draw a circuit diagram as standalone SVG.
|
||||
pub fn circuit_svg(circuit: &Value) -> Result<String, String> {
|
||||
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;
|
||||
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 mut body = String::new();
|
||||
// wires
|
||||
for q in 0..qubits {
|
||||
let y = y_of(q);
|
||||
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,
|
||||
y = y,
|
||||
));
|
||||
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,
|
||||
));
|
||||
}
|
||||
|
||||
for (col, op) in ops.iter().enumerate() {
|
||||
let x = x_of(col);
|
||||
match op.gate.as_str() {
|
||||
"CX" | "CNOT" | "CN" if op.qubits.len() >= 2 => {
|
||||
draw_cx(&mut body, x, op.qubits[0], op.qubits[1]);
|
||||
}
|
||||
"CZ" if op.qubits.len() >= 2 => {
|
||||
draw_cz(&mut body, x, op.qubits[0], op.qubits[1]);
|
||||
}
|
||||
"SWAP" if op.qubits.len() >= 2 => {
|
||||
draw_swap(&mut body, x, op.qubits[0], op.qubits[1]);
|
||||
}
|
||||
_ => {
|
||||
let q = *op.qubits.first().unwrap_or(&0);
|
||||
draw_box(&mut body, x, q, &gate_label(op));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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>"#,
|
||||
w = width,
|
||||
h = height,
|
||||
body = body,
|
||||
))
|
||||
}
|
||||
|
||||
fn draw_box(out: &mut String, x: f64, q: usize, label: &str) {
|
||||
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,
|
||||
));
|
||||
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,
|
||||
label = esc(label),
|
||||
));
|
||||
}
|
||||
|
||||
fn draw_cx(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="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,
|
||||
));
|
||||
}
|
||||
|
||||
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) {
|
||||
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"/>"#
|
||||
));
|
||||
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,
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
//! Marqdo quantum plugin (C ABI v2): state-vector circuit simulation.
|
||||
|
||||
mod draw;
|
||||
mod sim;
|
||||
|
||||
use std::ffi::{CStr, CString};
|
||||
@@ -42,6 +43,8 @@ pub struct MarqdoHostApi {
|
||||
|
||||
static mut HOST_FREE: Option<unsafe extern "C" fn(*mut c_void)> = None;
|
||||
static mut HOST_ALLOC: Option<unsafe extern "C" fn(usize) -> *mut c_void> = None;
|
||||
static mut HOST_QUERY: Option<HostQueryFn> = None;
|
||||
static mut HOST_USERDATA: *mut c_void = ptr::null_mut();
|
||||
|
||||
unsafe fn host_strdup(s: &str) -> *mut c_char {
|
||||
let alloc = HOST_ALLOC.expect("host alloc");
|
||||
@@ -157,6 +160,60 @@ fn circuit_of(args: &Value) -> Result<&Value, String> {
|
||||
.ok_or_else(|| "missing `circuit`".to_string())
|
||||
}
|
||||
|
||||
fn take_host_string(p: *mut c_char) -> Option<String> {
|
||||
if p.is_null() {
|
||||
return None;
|
||||
}
|
||||
let s = unsafe { CStr::from_ptr(p) }
|
||||
.to_str()
|
||||
.ok()
|
||||
.map(|s| s.to_string());
|
||||
unsafe {
|
||||
if let Some(free) = HOST_FREE {
|
||||
free(p as *mut c_void);
|
||||
}
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
fn host_query_json(name: &str, args: &Value) -> Result<Value, String> {
|
||||
let query = unsafe { HOST_QUERY }.ok_or_else(|| "host_query not available".to_string())?;
|
||||
let userdata = unsafe { HOST_USERDATA };
|
||||
let c_name = CString::new(name).map_err(|e| e.to_string())?;
|
||||
let c_args = CString::new(args.to_string()).map_err(|e| e.to_string())?;
|
||||
let mut out_ptr: *mut c_char = ptr::null_mut();
|
||||
let mut err_ptr: *mut c_char = ptr::null_mut();
|
||||
let rc = unsafe {
|
||||
query(
|
||||
userdata,
|
||||
c_name.as_ptr(),
|
||||
c_args.as_ptr(),
|
||||
&mut out_ptr,
|
||||
&mut err_ptr,
|
||||
)
|
||||
};
|
||||
let err = take_host_string(err_ptr);
|
||||
let out = take_host_string(out_ptr);
|
||||
if rc != 0 {
|
||||
return Err(err.unwrap_or_else(|| format!("host_query `{name}` failed")));
|
||||
}
|
||||
let out = out.unwrap_or_else(|| "null".into());
|
||||
serde_json::from_str(&out).map_err(|e| format!("host_query `{name}` bad JSON: {e}"))
|
||||
}
|
||||
|
||||
fn record_plot(svg: &str, path: Option<&str>) -> Result<(), String> {
|
||||
let mut args = json!({ "svg": svg });
|
||||
if let Some(p) = path {
|
||||
if !p.is_empty() {
|
||||
args.as_object_mut()
|
||||
.unwrap()
|
||||
.insert("path".into(), json!(p));
|
||||
}
|
||||
}
|
||||
host_query_json("record_plot", &args)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
q_ffi!(quantum_ping, |_args: &Value| {
|
||||
Ok(json!({
|
||||
"ok": true,
|
||||
@@ -168,11 +225,11 @@ q_ffi!(quantum_ping, |_args: &Value| {
|
||||
|
||||
q_ffi!(quantum_circuit_new, |args: &Value| {
|
||||
let qubits = arg_u(args, "qubits").or_else(|_| arg_u(args, "比特数"))?;
|
||||
sim::check_qubits(qubits)?;
|
||||
Ok(json!({
|
||||
"qubits": qubits,
|
||||
"ops": [],
|
||||
}))
|
||||
let steps = args
|
||||
.get("steps")
|
||||
.or_else(|| args.get("步骤"))
|
||||
.filter(|v| !v.is_null());
|
||||
sim::circuit_new(qubits, steps)
|
||||
});
|
||||
|
||||
q_ffi!(quantum_h, |args: &Value| {
|
||||
@@ -286,6 +343,34 @@ q_ffi!(quantum_run, |args: &Value| {
|
||||
sim::run_circuit(c, shots, seed)
|
||||
});
|
||||
|
||||
q_ffi!(quantum_draw_circuit, |args: &Value| {
|
||||
let c = circuit_of(args)?;
|
||||
let svg = draw::circuit_svg(c)?;
|
||||
let path = args
|
||||
.get("path")
|
||||
.or_else(|| args.get("路径"))
|
||||
.and_then(|v| v.as_str())
|
||||
.filter(|s| !s.is_empty());
|
||||
// Prefer view/CLI plots channel; still return structured value for authors.
|
||||
record_plot(&svg, path)?;
|
||||
let ops_n = c
|
||||
.get("ops")
|
||||
.and_then(|v| v.as_array())
|
||||
.map(|a| a.len())
|
||||
.unwrap_or(0);
|
||||
let qubits = c
|
||||
.get("qubits")
|
||||
.and_then(|v| v.as_u64())
|
||||
.unwrap_or(0);
|
||||
Ok(json!({
|
||||
"_type": "quantum_svg",
|
||||
"kind": "circuit",
|
||||
"qubits": qubits,
|
||||
"ops": ops_n,
|
||||
"svg": svg,
|
||||
}))
|
||||
});
|
||||
|
||||
fn register(host: &MarqdoHostApi, name: &str, params: &str, fn_ptr: PluginFn) -> c_int {
|
||||
let register = match host.register_fn {
|
||||
Some(f) => f,
|
||||
@@ -312,14 +397,13 @@ pub unsafe extern "C" fn marqdo_plugin_init(host: *const MarqdoHostApi) -> c_int
|
||||
let host = &*host;
|
||||
HOST_ALLOC = host.alloc;
|
||||
HOST_FREE = host.free;
|
||||
if host.host_query.is_none() {
|
||||
// v2 preferred; still allow init if only register is present
|
||||
}
|
||||
HOST_QUERY = host.host_query;
|
||||
HOST_USERDATA = host.userdata;
|
||||
let regs = [
|
||||
("quantum_ping", "", quantum_ping as PluginFn),
|
||||
(
|
||||
"quantum_circuit_new",
|
||||
"qubits",
|
||||
"qubits,steps",
|
||||
quantum_circuit_new as PluginFn,
|
||||
),
|
||||
("quantum_h", "circuit,qubit", quantum_h as PluginFn),
|
||||
@@ -354,6 +438,11 @@ 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_draw_circuit",
|
||||
"circuit,path",
|
||||
quantum_draw_circuit as PluginFn,
|
||||
),
|
||||
];
|
||||
for (name, params, f) in regs {
|
||||
if register(host, name, params, f) != 0 {
|
||||
|
||||
+207
-1
@@ -345,7 +345,8 @@ pub fn parse_ops(circuit: &Value) -> Result<Vec<Op>, String> {
|
||||
let theta = row
|
||||
.get("theta")
|
||||
.or_else(|| row.get("参数"))
|
||||
.and_then(|v| v.as_f64());
|
||||
.or_else(|| row.get("params"))
|
||||
.and_then(|v| parse_theta_value(v).ok().flatten());
|
||||
out.push(Op { gate, qubits, theta });
|
||||
}
|
||||
Ok(out)
|
||||
@@ -531,6 +532,211 @@ pub fn simulate_circuit(circuit: &Value) -> Result<(usize, Vec<C>), String> {
|
||||
Ok((qubits, amps))
|
||||
}
|
||||
|
||||
/// Build a circuit from `qubits` + optional `steps=` table (column map or row list).
|
||||
pub fn circuit_new(qubits: usize, steps: Option<&Value>) -> Result<Value, String> {
|
||||
check_qubits(qubits)?;
|
||||
let ops = match steps {
|
||||
None | Some(Value::Null) => Vec::new(),
|
||||
Some(v) => ops_from_steps(v)?,
|
||||
};
|
||||
for op in &ops {
|
||||
for &q in &op.qubits {
|
||||
if q >= qubits {
|
||||
return Err(format!("qubit {q} out of range for n={qubits}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
let ops_json: Vec<Value> = ops
|
||||
.into_iter()
|
||||
.map(|op| {
|
||||
let mut m = json!({
|
||||
"gate": op.gate,
|
||||
"qubits": op.qubits,
|
||||
});
|
||||
if let Some(t) = op.theta {
|
||||
m.as_object_mut()
|
||||
.unwrap()
|
||||
.insert("theta".into(), json!(t));
|
||||
}
|
||||
m
|
||||
})
|
||||
.collect();
|
||||
Ok(json!({
|
||||
"qubits": qubits,
|
||||
"ops": ops_json,
|
||||
}))
|
||||
}
|
||||
|
||||
/// Parse GFM `steps=` into ops.
|
||||
///
|
||||
/// Accepts:
|
||||
/// - list of row maps (`@` / `行` / `row` tables)
|
||||
/// - column-oriented map (`gate`/`门` + `qubits`/`比特` lists or scalars)
|
||||
pub fn ops_from_steps(steps: &Value) -> Result<Vec<Op>, String> {
|
||||
match steps {
|
||||
Value::Array(rows) => {
|
||||
let mut out = Vec::with_capacity(rows.len());
|
||||
for row in rows {
|
||||
out.push(op_from_step_row(row)?);
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
Value::Object(map) => {
|
||||
let gate_col = map
|
||||
.get("gate")
|
||||
.or_else(|| map.get("门"))
|
||||
.ok_or_else(|| "steps table needs `gate` / `门` column".to_string())?;
|
||||
let qubit_col = map
|
||||
.get("qubits")
|
||||
.or_else(|| map.get("qubit"))
|
||||
.or_else(|| map.get("比特"))
|
||||
.ok_or_else(|| "steps table needs `qubits` / `比特` column".to_string())?;
|
||||
let theta_col = map
|
||||
.get("theta")
|
||||
.or_else(|| map.get("参数"))
|
||||
.or_else(|| map.get("params"));
|
||||
|
||||
let gates = as_col_list(gate_col)?;
|
||||
let qubits = as_col_list(qubit_col)?;
|
||||
let thetas = match theta_col {
|
||||
Some(v) => as_col_list(v)?,
|
||||
None => vec![Value::Null; gates.len()],
|
||||
};
|
||||
if qubits.len() != gates.len() {
|
||||
return Err(format!(
|
||||
"steps column length mismatch: gate={} qubits={}",
|
||||
gates.len(),
|
||||
qubits.len()
|
||||
));
|
||||
}
|
||||
if thetas.len() != gates.len() && theta_col.is_some() {
|
||||
return Err("steps `theta`/`参数` column length mismatch".into());
|
||||
}
|
||||
let mut out = Vec::with_capacity(gates.len());
|
||||
for i in 0..gates.len() {
|
||||
let gate = gates[i]
|
||||
.as_str()
|
||||
.ok_or_else(|| format!("steps gate row {i} must be text"))?
|
||||
.to_ascii_uppercase();
|
||||
let qs = qubits_from_cell(&qubits[i])?;
|
||||
let theta = thetas
|
||||
.get(i)
|
||||
.map(parse_theta_value)
|
||||
.transpose()?
|
||||
.flatten();
|
||||
out.push(Op {
|
||||
gate,
|
||||
qubits: qs,
|
||||
theta,
|
||||
});
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
_ => Err("steps must be a table (map or list of rows)".into()),
|
||||
}
|
||||
}
|
||||
|
||||
fn as_col_list(v: &Value) -> Result<Vec<Value>, String> {
|
||||
match v {
|
||||
Value::Array(a) => Ok(a.clone()),
|
||||
other => Ok(vec![other.clone()]),
|
||||
}
|
||||
}
|
||||
|
||||
fn op_from_step_row(row: &Value) -> Result<Op, String> {
|
||||
let gate = row
|
||||
.get("gate")
|
||||
.or_else(|| row.get("门"))
|
||||
.and_then(|v| v.as_str())
|
||||
.ok_or_else(|| "step row missing `gate` / `门`".to_string())?
|
||||
.to_ascii_uppercase();
|
||||
let qs = 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)?;
|
||||
let theta = row
|
||||
.get("theta")
|
||||
.or_else(|| row.get("参数"))
|
||||
.or_else(|| row.get("params"))
|
||||
.map(parse_theta_value)
|
||||
.transpose()?
|
||||
.flatten();
|
||||
Ok(Op {
|
||||
gate,
|
||||
qubits,
|
||||
theta,
|
||||
})
|
||||
}
|
||||
|
||||
fn qubits_from_cell(v: &Value) -> Result<Vec<usize>, String> {
|
||||
match v {
|
||||
Value::Array(a) => a
|
||||
.iter()
|
||||
.map(|x| {
|
||||
x.as_u64()
|
||||
.or_else(|| x.as_i64().map(|i| i as u64))
|
||||
.ok_or_else(|| "bad qubit index".to_string())
|
||||
.map(|u| u as usize)
|
||||
})
|
||||
.collect(),
|
||||
Value::Number(n) => {
|
||||
let u = n
|
||||
.as_u64()
|
||||
.or_else(|| n.as_i64().map(|i| i as u64))
|
||||
.ok_or_else(|| "bad qubit index".to_string())? as usize;
|
||||
Ok(vec![u])
|
||||
}
|
||||
Value::String(s) => parse_qubit_list(s),
|
||||
_ => Err("bad qubits cell".into()),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_theta_value(v: &Value) -> Result<Option<f64>, String> {
|
||||
match v {
|
||||
Value::Null => Ok(None),
|
||||
Value::Number(n) => n
|
||||
.as_f64()
|
||||
.ok_or_else(|| "bad theta".to_string())
|
||||
.map(Some),
|
||||
Value::String(s) => {
|
||||
let t = s.trim();
|
||||
if t.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
parse_theta_str(t).map(Some)
|
||||
}
|
||||
_ => Err("bad theta".into()),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_theta_str(s: &str) -> Result<f64, String> {
|
||||
let t = s.trim().to_ascii_lowercase().replace('π', "pi");
|
||||
if t == "pi" {
|
||||
return Ok(std::f64::consts::PI);
|
||||
}
|
||||
if let Some(rest) = t.strip_prefix("pi/") {
|
||||
let d: f64 = rest
|
||||
.trim()
|
||||
.parse()
|
||||
.map_err(|_| format!("bad theta `{s}`"))?;
|
||||
if d == 0.0 {
|
||||
return Err(format!("bad theta `{s}`"));
|
||||
}
|
||||
return Ok(std::f64::consts::PI / d);
|
||||
}
|
||||
if let Some(rest) = t.strip_suffix("*pi") {
|
||||
let n: f64 = rest
|
||||
.trim()
|
||||
.parse()
|
||||
.map_err(|_| format!("bad theta `{s}`"))?;
|
||||
return Ok(n * std::f64::consts::PI);
|
||||
}
|
||||
t.parse::<f64>()
|
||||
.map_err(|_| format!("bad theta `{s}`"))
|
||||
}
|
||||
|
||||
pub fn push_op(circuit: &Value, gate: &str, qubits: Vec<usize>, theta: Option<f64>) -> Result<Value, String> {
|
||||
let n = circuit
|
||||
.get("qubits")
|
||||
|
||||
Reference in new issue
Block a user