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
@@ -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")
|
||||
|
||||
Reference in new issue
Block a user