Add quantum Q2 gate set, seeded run, and EN/ZH L1 APIs.
Expand the simulator beyond H/CX with Y/Z/S/T/R*/CZ/SWAP and deterministic shot sampling so authors can write full circuits and reproducible measurement checks. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
e853b00fe2
commit
ff6425e6c5
9 files changed
+567
-6
No files matched your search
@@ -2,7 +2,7 @@
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **Accepted · 实现中**(Q0–Q1:电路 + H/CX + 概率;Q2+ 见路线图) |
|
||||
| 状态 | **Accepted · 实现中**(Q0–Q2:门集 + `run`;Q3+ 电路表/绘图见路线图) |
|
||||
| 日期 | 2026-08-12 |
|
||||
| 相关 | [stdlib-math.md](stdlib-math.md) · [ext-abi.md](ext-abi.md) · [ext-cli.md](ext-cli.md) · [ext-web.md](ext-web.md) · [view.md](view.md) · [stdlib-i18n.md](stdlib-i18n.md) · [roadmap/ext-quantum.md](../roadmap/ext-quantum.md) |
|
||||
| 安装(目标) | `marqdo ext add quantum`(中英:`quantum` / `量子`) |
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **Q0–Q1 落地中**(设计 Accepted) |
|
||||
| 状态 | **Q0–Q2 已落地**(设计 Accepted;Q3+ 待做) |
|
||||
| 日期 | 2026-08-12 |
|
||||
| **锁定设计** | [design/ext-quantum.md](../design/ext-quantum.md) |
|
||||
| 相关 | [ext-cli.md](../design/ext-cli.md) · [ext-abi.md](../design/ext-abi.md) · [stdlib-math.md](../design/stdlib-math.md) · [view.md](../design/view.md) |
|
||||
@@ -24,7 +24,7 @@
|
||||
|------|------|------|
|
||||
| **Q0** | CATALOG + `plugins/quantum` ping + L1 `ensure_plugin` | **done** |
|
||||
| **Q1** | 态向量;I/X/H/CX;`simulate` / `probabilities`;贝尔态 gold | **done** |
|
||||
| **Q2** | Y/Z/S/T/R\* /CZ/SWAP;`run seed=`;中英 API | pending |
|
||||
| **Q2** | Y/Z/S/T/R\* /CZ/SWAP;`run seed=`;中英 API | **done** |
|
||||
| **Q3** | `steps=` 电路表;`draw` 轨线 SVG;view `record_plot` 或值识别 | pending |
|
||||
| **Q4** | 概率条 / 布洛赫;`matches_matrix`;examples + 用户文档 | pending |
|
||||
| **Q5** | (可选)噪声 / 更大 n —— 另文 | pending |
|
||||
|
||||
@@ -36,17 +36,67 @@ State-vector circuit (default |0…0⟩). Methods append gates and return an upd
|
||||
|
||||
**> quantum_x circuit=`self` qubit=`qubit`**
|
||||
|
||||
## y
|
||||
+ `qubit`=0
|
||||
|
||||
**> quantum_y circuit=`self` qubit=`qubit`**
|
||||
|
||||
## z
|
||||
+ `qubit`=0
|
||||
|
||||
**> quantum_z circuit=`self` qubit=`qubit`**
|
||||
|
||||
## s
|
||||
+ `qubit`=0
|
||||
|
||||
**> quantum_s circuit=`self` qubit=`qubit`**
|
||||
|
||||
## t
|
||||
+ `qubit`=0
|
||||
|
||||
**> quantum_t circuit=`self` qubit=`qubit`**
|
||||
|
||||
## i
|
||||
+ `qubit`=0
|
||||
|
||||
**> quantum_i circuit=`self` qubit=`qubit`**
|
||||
|
||||
## rx
|
||||
+ `qubit`=0
|
||||
+ `theta`
|
||||
|
||||
**> quantum_rx circuit=`self` qubit=`qubit` theta=`theta`**
|
||||
|
||||
## ry
|
||||
+ `qubit`=0
|
||||
+ `theta`
|
||||
|
||||
**> quantum_ry circuit=`self` qubit=`qubit` theta=`theta`**
|
||||
|
||||
## rz
|
||||
+ `qubit`=0
|
||||
+ `theta`
|
||||
|
||||
**> quantum_rz circuit=`self` qubit=`qubit` theta=`theta`**
|
||||
|
||||
## cx
|
||||
+ `control`=0
|
||||
+ `target`=1
|
||||
|
||||
**> quantum_cx circuit=`self` control=`control` target=`target`**
|
||||
|
||||
## cz
|
||||
+ `control`=0
|
||||
+ `target`=1
|
||||
|
||||
**> quantum_cz circuit=`self` control=`control` target=`target`**
|
||||
|
||||
## swap
|
||||
+ `a`=0
|
||||
+ `b`=1
|
||||
|
||||
**> quantum_swap circuit=`self` a=`a` b=`b`**
|
||||
|
||||
## simulate
|
||||
|
||||
Return state vector amplitudes `{re,im}`.
|
||||
@@ -58,3 +108,11 @@ Return state vector amplitudes `{re,im}`.
|
||||
Return map of basis label → probability (qubit 0 = rightmost bit).
|
||||
|
||||
**> quantum_probabilities circuit=`self`**
|
||||
|
||||
## run
|
||||
+ `shots`=1024
|
||||
+ `seed`=1
|
||||
|
||||
Sample computational-basis shots (deterministic with `seed=`).
|
||||
|
||||
**> quantum_run circuit=`self` shots=`shots` seed=`seed`**
|
||||
@@ -36,17 +36,67 @@ description: 官方量子电路模拟(中文)。门 + 电路;ABI 插件。
|
||||
|
||||
**> quantum_x circuit=`self` qubit=`比特`**
|
||||
|
||||
## 泡利Y
|
||||
+ `比特`=0
|
||||
|
||||
**> quantum_y circuit=`self` qubit=`比特`**
|
||||
|
||||
## 泡利Z
|
||||
+ `比特`=0
|
||||
|
||||
**> quantum_z circuit=`self` qubit=`比特`**
|
||||
|
||||
## 相位S
|
||||
+ `比特`=0
|
||||
|
||||
**> quantum_s circuit=`self` qubit=`比特`**
|
||||
|
||||
## 相位T
|
||||
+ `比特`=0
|
||||
|
||||
**> quantum_t circuit=`self` qubit=`比特`**
|
||||
|
||||
## 单位
|
||||
+ `比特`=0
|
||||
|
||||
**> quantum_i circuit=`self` qubit=`比特`**
|
||||
|
||||
## 绕X
|
||||
+ `比特`=0
|
||||
+ `参数`
|
||||
|
||||
**> quantum_rx circuit=`self` qubit=`比特` theta=`参数`**
|
||||
|
||||
## 绕Y
|
||||
+ `比特`=0
|
||||
+ `参数`
|
||||
|
||||
**> quantum_ry circuit=`self` qubit=`比特` theta=`参数`**
|
||||
|
||||
## 绕Z
|
||||
+ `比特`=0
|
||||
+ `参数`
|
||||
|
||||
**> quantum_rz circuit=`self` qubit=`比特` theta=`参数`**
|
||||
|
||||
## 控非
|
||||
+ `控制`=0
|
||||
+ `目标`=1
|
||||
|
||||
**> quantum_cx circuit=`self` control=`控制` target=`目标`**
|
||||
|
||||
## 控相
|
||||
+ `控制`=0
|
||||
+ `目标`=1
|
||||
|
||||
**> quantum_cz circuit=`self` control=`控制` target=`目标`**
|
||||
|
||||
## 交换
|
||||
+ `比特甲`=0
|
||||
+ `比特乙`=1
|
||||
|
||||
**> quantum_swap circuit=`self` a=`比特甲` b=`比特乙`**
|
||||
|
||||
## 模拟
|
||||
|
||||
**> quantum_simulate circuit=`self`**
|
||||
@@ -56,3 +106,11 @@ description: 官方量子电路模拟(中文)。门 + 电路;ABI 插件。
|
||||
基矢标签 → 概率(qubit 0 为最右侧比特)。
|
||||
|
||||
**> quantum_probabilities circuit=`self`**
|
||||
|
||||
## 运行
|
||||
+ `次数`=1024
|
||||
+ `种子`=1
|
||||
|
||||
计算基采样(`种子=` 可复现)。
|
||||
|
||||
**> quantum_run circuit=`self` shots=`次数` seed=`种子`**
|
||||
@@ -138,6 +138,19 @@ fn arg_u(v: &Value, key: &str) -> Result<usize, String> {
|
||||
}
|
||||
}
|
||||
|
||||
fn arg_f(v: &Value, key: &str) -> Result<f64, String> {
|
||||
match v.get(key) {
|
||||
Some(Value::Number(n)) => n
|
||||
.as_f64()
|
||||
.ok_or_else(|| format!("bad `{key}`")),
|
||||
Some(Value::String(s)) => s
|
||||
.trim()
|
||||
.parse()
|
||||
.map_err(|_| format!("bad `{key}`")),
|
||||
_ => Err(format!("missing `{key}`")),
|
||||
}
|
||||
}
|
||||
|
||||
fn circuit_of(args: &Value) -> Result<&Value, String> {
|
||||
args.get("circuit")
|
||||
.or_else(|| args.get("电路"))
|
||||
@@ -174,12 +187,57 @@ q_ffi!(quantum_x, |args: &Value| {
|
||||
sim::push_op(c, "X", vec![q], None)
|
||||
});
|
||||
|
||||
q_ffi!(quantum_y, |args: &Value| {
|
||||
let c = circuit_of(args)?;
|
||||
let q = arg_u(args, "qubit").or_else(|_| arg_u(args, "比特"))?;
|
||||
sim::push_op(c, "Y", vec![q], None)
|
||||
});
|
||||
|
||||
q_ffi!(quantum_z, |args: &Value| {
|
||||
let c = circuit_of(args)?;
|
||||
let q = arg_u(args, "qubit").or_else(|_| arg_u(args, "比特"))?;
|
||||
sim::push_op(c, "Z", vec![q], None)
|
||||
});
|
||||
|
||||
q_ffi!(quantum_s, |args: &Value| {
|
||||
let c = circuit_of(args)?;
|
||||
let q = arg_u(args, "qubit").or_else(|_| arg_u(args, "比特"))?;
|
||||
sim::push_op(c, "S", vec![q], None)
|
||||
});
|
||||
|
||||
q_ffi!(quantum_t, |args: &Value| {
|
||||
let c = circuit_of(args)?;
|
||||
let q = arg_u(args, "qubit").or_else(|_| arg_u(args, "比特"))?;
|
||||
sim::push_op(c, "T", vec![q], None)
|
||||
});
|
||||
|
||||
q_ffi!(quantum_i, |args: &Value| {
|
||||
let c = circuit_of(args)?;
|
||||
let q = arg_u(args, "qubit").or_else(|_| arg_u(args, "比特"))?;
|
||||
sim::push_op(c, "I", vec![q], None)
|
||||
});
|
||||
|
||||
q_ffi!(quantum_rx, |args: &Value| {
|
||||
let c = circuit_of(args)?;
|
||||
let q = arg_u(args, "qubit").or_else(|_| arg_u(args, "比特"))?;
|
||||
let th = arg_f(args, "theta").or_else(|_| arg_f(args, "参数"))?;
|
||||
sim::push_op(c, "RX", vec![q], Some(th))
|
||||
});
|
||||
|
||||
q_ffi!(quantum_ry, |args: &Value| {
|
||||
let c = circuit_of(args)?;
|
||||
let q = arg_u(args, "qubit").or_else(|_| arg_u(args, "比特"))?;
|
||||
let th = arg_f(args, "theta").or_else(|_| arg_f(args, "参数"))?;
|
||||
sim::push_op(c, "RY", vec![q], Some(th))
|
||||
});
|
||||
|
||||
q_ffi!(quantum_rz, |args: &Value| {
|
||||
let c = circuit_of(args)?;
|
||||
let q = arg_u(args, "qubit").or_else(|_| arg_u(args, "比特"))?;
|
||||
let th = arg_f(args, "theta").or_else(|_| arg_f(args, "参数"))?;
|
||||
sim::push_op(c, "RZ", vec![q], Some(th))
|
||||
});
|
||||
|
||||
q_ffi!(quantum_cx, |args: &Value| {
|
||||
let c = circuit_of(args)?;
|
||||
let control = arg_u(args, "control").or_else(|_| arg_u(args, "控制"))?;
|
||||
@@ -187,6 +245,20 @@ q_ffi!(quantum_cx, |args: &Value| {
|
||||
sim::push_op(c, "CX", vec![control, target], None)
|
||||
});
|
||||
|
||||
q_ffi!(quantum_cz, |args: &Value| {
|
||||
let c = circuit_of(args)?;
|
||||
let control = arg_u(args, "control").or_else(|_| arg_u(args, "控制"))?;
|
||||
let target = arg_u(args, "target").or_else(|_| arg_u(args, "目标"))?;
|
||||
sim::push_op(c, "CZ", vec![control, target], None)
|
||||
});
|
||||
|
||||
q_ffi!(quantum_swap, |args: &Value| {
|
||||
let c = circuit_of(args)?;
|
||||
let a = arg_u(args, "a").or_else(|_| arg_u(args, "比特甲"))?;
|
||||
let b = arg_u(args, "b").or_else(|_| arg_u(args, "比特乙"))?;
|
||||
sim::push_op(c, "SWAP", vec![a, b], None)
|
||||
});
|
||||
|
||||
q_ffi!(quantum_simulate, |args: &Value| {
|
||||
let c = circuit_of(args)?;
|
||||
let (qubits, amps) = sim::simulate_circuit(c)?;
|
||||
@@ -203,6 +275,17 @@ q_ffi!(quantum_probabilities, |args: &Value| {
|
||||
Ok(Value::Object(sim::probabilities(&s, qubits)))
|
||||
});
|
||||
|
||||
q_ffi!(quantum_run, |args: &Value| {
|
||||
let c = circuit_of(args)?;
|
||||
let shots = arg_u(args, "shots")
|
||||
.or_else(|_| arg_u(args, "次数"))
|
||||
.unwrap_or(1024);
|
||||
let seed = arg_u(args, "seed")
|
||||
.or_else(|_| arg_u(args, "种子"))
|
||||
.unwrap_or(1) as u64;
|
||||
sim::run_circuit(c, shots, seed)
|
||||
});
|
||||
|
||||
fn register(host: &MarqdoHostApi, name: &str, params: &str, fn_ptr: PluginFn) -> c_int {
|
||||
let register = match host.register_fn {
|
||||
Some(f) => f,
|
||||
@@ -241,12 +324,25 @@ pub unsafe extern "C" fn marqdo_plugin_init(host: *const MarqdoHostApi) -> c_int
|
||||
),
|
||||
("quantum_h", "circuit,qubit", quantum_h as PluginFn),
|
||||
("quantum_x", "circuit,qubit", quantum_x as PluginFn),
|
||||
("quantum_y", "circuit,qubit", quantum_y as PluginFn),
|
||||
("quantum_z", "circuit,qubit", quantum_z as PluginFn),
|
||||
("quantum_s", "circuit,qubit", quantum_s as PluginFn),
|
||||
("quantum_t", "circuit,qubit", quantum_t as PluginFn),
|
||||
("quantum_i", "circuit,qubit", quantum_i as PluginFn),
|
||||
("quantum_rx", "circuit,qubit,theta", quantum_rx as PluginFn),
|
||||
("quantum_ry", "circuit,qubit,theta", quantum_ry as PluginFn),
|
||||
("quantum_rz", "circuit,qubit,theta", quantum_rz as PluginFn),
|
||||
(
|
||||
"quantum_cx",
|
||||
"circuit,control,target",
|
||||
quantum_cx as PluginFn,
|
||||
),
|
||||
(
|
||||
"quantum_cz",
|
||||
"circuit,control,target",
|
||||
quantum_cz as PluginFn,
|
||||
),
|
||||
("quantum_swap", "circuit,a,b", quantum_swap as PluginFn),
|
||||
(
|
||||
"quantum_simulate",
|
||||
"circuit",
|
||||
@@ -257,6 +353,7 @@ pub unsafe extern "C" fn marqdo_plugin_init(host: *const MarqdoHostApi) -> c_int
|
||||
"circuit",
|
||||
quantum_probabilities as PluginFn,
|
||||
),
|
||||
("quantum_run", "circuit,shots,seed", quantum_run as PluginFn),
|
||||
];
|
||||
for (name, params, f) in regs {
|
||||
if register(host, name, params, f) != 0 {
|
||||
|
||||
+240
-3
@@ -132,6 +132,121 @@ pub fn apply_cx(amps: &mut [C], control: usize, target: usize) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn apply_y(amps: &mut [C], q: usize) {
|
||||
// [[0, -i], [i, 0]]
|
||||
apply_u2(
|
||||
amps,
|
||||
q,
|
||||
C::zero(),
|
||||
C::new(0.0, -1.0),
|
||||
C::new(0.0, 1.0),
|
||||
C::zero(),
|
||||
);
|
||||
}
|
||||
|
||||
pub fn apply_z(amps: &mut [C], q: usize) {
|
||||
apply_u2(
|
||||
amps,
|
||||
q,
|
||||
C::one(),
|
||||
C::zero(),
|
||||
C::zero(),
|
||||
C::new(-1.0, 0.0),
|
||||
);
|
||||
}
|
||||
|
||||
pub fn apply_s(amps: &mut [C], q: usize) {
|
||||
apply_u2(
|
||||
amps,
|
||||
q,
|
||||
C::one(),
|
||||
C::zero(),
|
||||
C::zero(),
|
||||
C::new(0.0, 1.0),
|
||||
);
|
||||
}
|
||||
|
||||
pub fn apply_t(amps: &mut [C], q: usize) {
|
||||
let s = std::f64::consts::FRAC_1_SQRT_2;
|
||||
apply_u2(
|
||||
amps,
|
||||
q,
|
||||
C::one(),
|
||||
C::zero(),
|
||||
C::zero(),
|
||||
C::new(s, s),
|
||||
);
|
||||
}
|
||||
|
||||
pub fn apply_rx(amps: &mut [C], q: usize, theta: f64) {
|
||||
let half = theta / 2.0;
|
||||
let c = half.cos();
|
||||
let s = half.sin();
|
||||
apply_u2(
|
||||
amps,
|
||||
q,
|
||||
C::new(c, 0.0),
|
||||
C::new(0.0, -s),
|
||||
C::new(0.0, -s),
|
||||
C::new(c, 0.0),
|
||||
);
|
||||
}
|
||||
|
||||
pub fn apply_ry(amps: &mut [C], q: usize, theta: f64) {
|
||||
let half = theta / 2.0;
|
||||
let c = half.cos();
|
||||
let s = half.sin();
|
||||
apply_u2(
|
||||
amps,
|
||||
q,
|
||||
C::new(c, 0.0),
|
||||
C::new(-s, 0.0),
|
||||
C::new(s, 0.0),
|
||||
C::new(c, 0.0),
|
||||
);
|
||||
}
|
||||
|
||||
pub fn apply_rz(amps: &mut [C], q: usize, theta: f64) {
|
||||
let half = theta / 2.0;
|
||||
apply_u2(
|
||||
amps,
|
||||
q,
|
||||
C::new((-half).cos(), (-half).sin()),
|
||||
C::zero(),
|
||||
C::zero(),
|
||||
C::new(half.cos(), half.sin()),
|
||||
);
|
||||
}
|
||||
|
||||
pub fn apply_cz(amps: &mut [C], control: usize, target: usize) {
|
||||
if control == target {
|
||||
return;
|
||||
}
|
||||
for (i, a) in amps.iter_mut().enumerate() {
|
||||
if bit(i, control) && bit(i, target) {
|
||||
*a = a.scale(-1.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn apply_swap(amps: &mut [C], a: usize, b: usize) {
|
||||
if a == b {
|
||||
return;
|
||||
}
|
||||
let n = amps.len();
|
||||
for i in 0..n {
|
||||
let j = if bit(i, a) != bit(i, b) {
|
||||
flip(flip(i, a), b)
|
||||
} else {
|
||||
i
|
||||
};
|
||||
if i < j {
|
||||
amps.swap(i, j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn zero_state(qubits: usize) -> Vec<C> {
|
||||
let dim = 1usize << qubits;
|
||||
let mut amps = vec![C::zero(); dim];
|
||||
@@ -174,8 +289,7 @@ pub fn amps_to_json(amps: &[C]) -> Value {
|
||||
pub struct Op {
|
||||
pub gate: String,
|
||||
pub qubits: Vec<usize>,
|
||||
#[allow(dead_code)]
|
||||
pub theta: Option<f64>, // reserved for Rx/Ry/Rz (Q2)
|
||||
pub theta: Option<f64>,
|
||||
}
|
||||
|
||||
pub fn parse_ops(circuit: &Value) -> Result<Vec<Op>, String> {
|
||||
@@ -263,21 +377,144 @@ pub fn apply_op(amps: &mut [C], qubits: usize, op: &Op) -> Result<(), String> {
|
||||
let q = *op.qubits.first().ok_or("X needs qubit")?;
|
||||
apply_x(amps, q);
|
||||
}
|
||||
"Y" => {
|
||||
let q = *op.qubits.first().ok_or("Y needs qubit")?;
|
||||
apply_y(amps, q);
|
||||
}
|
||||
"Z" => {
|
||||
let q = *op.qubits.first().ok_or("Z needs qubit")?;
|
||||
apply_z(amps, q);
|
||||
}
|
||||
"S" => {
|
||||
let q = *op.qubits.first().ok_or("S needs qubit")?;
|
||||
apply_s(amps, q);
|
||||
}
|
||||
"T" => {
|
||||
let q = *op.qubits.first().ok_or("T needs qubit")?;
|
||||
apply_t(amps, q);
|
||||
}
|
||||
"H" | "HADAMARD" => {
|
||||
let q = *op.qubits.first().ok_or("H needs qubit")?;
|
||||
apply_h(amps, q);
|
||||
}
|
||||
"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);
|
||||
}
|
||||
"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);
|
||||
}
|
||||
"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);
|
||||
}
|
||||
"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]);
|
||||
}
|
||||
other => return Err(format!("unsupported gate `{other}` in Q1 (use I/X/H/CX)")),
|
||||
"CZ" => {
|
||||
if op.qubits.len() < 2 {
|
||||
return Err("CZ needs control and target".into());
|
||||
}
|
||||
apply_cz(amps, op.qubits[0], op.qubits[1]);
|
||||
}
|
||||
"SWAP" => {
|
||||
if op.qubits.len() < 2 {
|
||||
return Err("SWAP needs two qubits".into());
|
||||
}
|
||||
apply_swap(amps, op.qubits[0], op.qubits[1]);
|
||||
}
|
||||
other => {
|
||||
return Err(format!(
|
||||
"unsupported gate `{other}` (I/X/Y/Z/H/S/T/Rx/Ry/Rz/CX/CZ/SWAP)"
|
||||
));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
struct Rng {
|
||||
state: u64,
|
||||
}
|
||||
|
||||
impl Rng {
|
||||
fn new(seed: u64) -> Self {
|
||||
Self {
|
||||
state: if seed == 0 { 0x9e37_79b9_7f4a_7c15 } else { seed },
|
||||
}
|
||||
}
|
||||
fn next_u64(&mut self) -> u64 {
|
||||
// SplitMix64
|
||||
self.state = self.state.wrapping_add(0x9e37_79b9_7f4a_7c15);
|
||||
let mut z = self.state;
|
||||
z = (z ^ (z >> 30)).wrapping_mul(0xbf58_476d_1ce4_e5b9);
|
||||
z = (z ^ (z >> 27)).wrapping_mul(0x94d0_49bb_1331_11eb);
|
||||
z ^ (z >> 31)
|
||||
}
|
||||
fn next_f64(&mut self) -> f64 {
|
||||
(self.next_u64() >> 11) as f64 / ((1u64 << 53) as f64)
|
||||
}
|
||||
}
|
||||
|
||||
/// Sample computational-basis shots; returns counts map.
|
||||
pub fn sample_counts(amps: &[C], qubits: usize, shots: usize, seed: u64) -> Map<String, Value> {
|
||||
let mut cdf = Vec::with_capacity(amps.len());
|
||||
let mut acc = 0.0;
|
||||
for a in amps {
|
||||
acc += a.norm_sq();
|
||||
cdf.push(acc);
|
||||
}
|
||||
if acc <= 0.0 {
|
||||
return Map::new();
|
||||
}
|
||||
// normalize cdf tail to 1
|
||||
let inv = 1.0 / acc;
|
||||
for x in &mut cdf {
|
||||
*x *= inv;
|
||||
}
|
||||
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;
|
||||
counts.insert(label, json!(n));
|
||||
}
|
||||
counts
|
||||
}
|
||||
|
||||
pub fn run_circuit(circuit: &Value, shots: usize, seed: u64) -> Result<Value, String> {
|
||||
if shots == 0 {
|
||||
return Err("shots must be >= 1".into());
|
||||
}
|
||||
let (qubits, amps) = simulate_circuit(circuit)?;
|
||||
let counts = sample_counts(&s, qubits, shots, seed);
|
||||
Ok(json!({
|
||||
"qubits": qubits,
|
||||
"shots": shots,
|
||||
"seed": seed,
|
||||
"counts": counts,
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn simulate_circuit(circuit: &Value) -> Result<(usize, Vec<C>), String> {
|
||||
let qubits = circuit
|
||||
.get("qubits")
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
---
|
||||
title: quantum gates + run smoke
|
||||
description: Z sandwich, Ry(pi), seeded Bell run counts.
|
||||
> ext/quantum/quantum.mq.md
|
||||
> lib/math.mq.md
|
||||
> lib/json.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
H-Z-H on |0⟩ → |1⟩.
|
||||
|
||||
*`qc` = > quantum.circuit qubits=1 *
|
||||
*`qc` = > `qc`.h qubit=0 *
|
||||
*`qc` = > `qc`.z qubit=0 *
|
||||
*`qc` = > `qc`.h qubit=0 *
|
||||
*`p` = > `qc`.probabilities *
|
||||
*`p1` = > json.get value=`p` key="1" *
|
||||
*`lo` = > math.div a=9 b=10 *
|
||||
|
||||
1. `p1` > `lo`
|
||||
> print text=z-sandwich-ok
|
||||
2. *
|
||||
> print text=z-sandwich-fail
|
||||
|
||||
Ry(π) on |0⟩ → |1⟩.
|
||||
|
||||
*`pi` = > math.pi *
|
||||
*`ry` = > quantum.circuit qubits=1 *
|
||||
*`ry` = > `ry`.ry qubit=0 theta=`pi` *
|
||||
*`rp` = > `ry`.probabilities *
|
||||
*`rp1` = > json.get value=`rp` key="1" *
|
||||
|
||||
1. `rp1` > `lo`
|
||||
> print text=ry-ok
|
||||
2. *
|
||||
> print text=ry-fail
|
||||
|
||||
Bell run with fixed seed: both 00 and 11 appear; shots sum.
|
||||
|
||||
*`bell` = > quantum.circuit qubits=2 *
|
||||
*`bell` = > `bell`.h qubit=0 *
|
||||
*`bell` = > `bell`.cx control=0 target=1 *
|
||||
*`res` = > `bell`.run shots=200 seed=42 *
|
||||
*`counts` = > json.get value=`res` key=counts *
|
||||
*`c00` = > json.get value=`counts` key="00" *
|
||||
*`c11` = > json.get value=`counts` key="11" *
|
||||
*`shots` = > json.get value=`res` key=shots *
|
||||
|
||||
1. `c00`
|
||||
1. `c11`
|
||||
> print text=run-ok
|
||||
2. *
|
||||
> print text=run-fail
|
||||
2. *
|
||||
> print text=run-fail
|
||||
|
||||
1. `shots` == 200
|
||||
> print text=shots-ok
|
||||
2. *
|
||||
> print text=shots-fail
|
||||
@@ -0,0 +1,22 @@
|
||||
---
|
||||
title: quantum zh API smoke
|
||||
description: Chinese 量子.电路 泡利Z sandwich.
|
||||
> ext/quantum/量子.mq.md
|
||||
> lib/math.mq.md
|
||||
> lib/json.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
*`电路` = > 量子.电路 比特数=1 *
|
||||
*`电路` = > `电路`.哈达玛 比特=0 *
|
||||
*`电路` = > `电路`.泡利Z 比特=0 *
|
||||
*`电路` = > `电路`.哈达玛 比特=0 *
|
||||
*`概率` = > `电路`.概率 *
|
||||
*`p1` = > json.get value=`概率` key="1" *
|
||||
*`lo` = > math.div a=9 b=10 *
|
||||
|
||||
1. `p1` > `lo`
|
||||
> print text=zh-ok
|
||||
2. *
|
||||
> print text=zh-fail
|
||||
@@ -1390,6 +1390,34 @@ ping-ok",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ext_quantum_gates_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-gates-smoke.mq.md",
|
||||
"z-sandwich-ok
|
||||
ry-ok
|
||||
run-ok
|
||||
shots-ok",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ext_quantum_zh_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-zh-smoke.mq.md", "zh-ok");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ext_agent_framework_smoke() {
|
||||
let status = Command::new("cargo")
|
||||
|
||||
Reference in new issue
Block a user