ruvector/crates/ruqu-wasm
rUv eafba64fa5
fix(security): RUSTSEC advisories + clippy hardening in RuVector (#504)
* fix(security): RUSTSEC advisories + clippy hardening in RuVector

- Replace all bare `partial_cmp().unwrap()` calls on f32/f64 with
  `.unwrap_or(Ordering::Equal)` to prevent panics on NaN values in
  sorting/max-by operations across ruvllm, ruvector-dag, prime-radiant,
  and rvagent-wasm (12 sites in production code).
- Add input validation guards to the HTTP search endpoint: reject k=0,
  k > 10_000, empty vectors, and vectors exceeding 65_536 dimensions,
  preventing memory exhaustion via unbounded allocations.
- Harden LocalFsBackend::execute in rvagent-cli with env_clear() +
  safe-env allowlist (SEC-005), deadline-based timeout enforcement, and
  1 MB output truncation, matching the security posture of LocalShellBackend.
- Remove 129 occurrences of the deprecated `unused_unit = "allow"` lint
  and 3 occurrences of the removed `clippy::match_on_vec_items` lint from
  Cargo.toml files workspace-wide; both are no-ops in current Rust/Clippy.
- All 653+ tests across ruvector-core, ruvector-server, ruvector-dag,
  rvagent-cli, and prime-radiant pass with zero failures.

Note: `bytes` is already at 1.11.1 (>= 1.10.0); `paste` 1.0.15 is a
transitive dependency with no semver fix available upstream; `cargo audit`
returns clean.

Co-Authored-By: claude-flow <ruv@ruv.net>

* fix(ci): cargo fmt + restore workspace unused_unit lint allow

- Run cargo fmt --all across all 9 files that drifted from rustfmt style
  (prime-radiant/energy.rs, ruvector-dag/bottleneck.rs+reasoning_bank.rs,
   ruvector-server/points.rs, ruvllm/pretrain_pipeline.rs+report.rs+registry.rs,
   rvagent-cli/app.rs, rvagent-wasm/gallery.rs)
- Add [workspace.lints.clippy] unused_unit = "allow" to root Cargo.toml;
  the per-crate entries removed in the security commit were still needed —
  moving to workspace-level is cleaner and restores -D warnings CI pass

Co-Authored-By: claude-flow <ruv@ruv.net>

* fix(ci): remove unneeded unit return type in ruvix bench

Removes `-> ()` from the Fn bound in run_benchmark_with_kernel
(crates/ruvix/benches/src/ruvix.rs:50) — triggers clippy::unused_unit
under -D warnings. Clippy prefers `Fn(&mut Kernel)` without explicit
unit return.

Co-Authored-By: claude-flow <ruv@ruv.net>

* fix(ci): resolve rustfmt and clippy unused_unit failures

- Run cargo fmt --all to fix long closure formatting in 9 files
  (energy.rs, bottleneck.rs, reasoning_bank.rs, points.rs,
  pretrain_pipeline.rs, report.rs, registry.rs, app.rs, gallery.rs)
- Add unused_unit = "allow" to [lints.clippy] in ruvix-bench and
  ruvector-mincut Cargo.toml files to suppress the unused_unit lint
  that was previously suppressed globally and now fires on two
  Fn(&mut T) -> () and FnMut() -> () function bounds

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-05-23 05:40:24 -04:00
..
src fix(rvagent-cli, ruqu-wasm): unblock 2 PR #388 test failures 2026-04-26 00:14:39 -04:00
Cargo.toml fix(security): RUSTSEC advisories + clippy hardening in RuVector (#504) 2026-05-23 05:40:24 -04:00
README.md chore(ruqu): bump to v2.0.5 with updated READMEs 2026-02-12 18:44:22 +00:00

ruqu-wasm

Crates.io npm License

Run quantum simulations in the browser — WebAssembly bindings for ruqu-core and ruqu-algorithms with 25-qubit support.

Features

  • Browser-Native — Run quantum circuits directly in JavaScript/TypeScript
  • 5 Simulation Backends — StateVector, Stabilizer, Clifford+T, TensorNetwork, Hardware
  • 25-Qubit Limit — Optimized for browser memory constraints (~1GB for 25 qubits)
  • Full Algorithm Suite — VQE, Grover, QAOA, Surface Code available
  • OpenQASM 3.0 — Export circuits to standard quantum assembly format
  • Zero Dependencies — Pure WASM, no server required
  • TypeScript Types — Full type definitions included

Installation

npm

npm install @ruvector/ruqu-wasm

Rust (for building)

cargo add ruqu-wasm
wasm-pack build --target web

Quick Start (JavaScript)

import init, { Circuit, Simulator } from '@ruvector/ruqu-wasm';

await init();

// Create a Bell state
const circuit = new Circuit(2);
circuit.h(0);        // Hadamard on qubit 0
circuit.cnot(0, 1);  // CNOT: entangle qubits

// Run simulation
const sim = new Simulator();
const state = sim.run(circuit);

// Measure
const result = state.measureAll();
console.log(`Measured: ${result.toString(2).padStart(2, '0')}`);
// Output: "00" or "11" with 50% probability each

React Example

import { useEffect, useState } from 'react';
import init, { Circuit, Simulator } from '@ruvector/ruqu-wasm';

function QuantumDemo() {
  const [result, setResult] = useState<string | null>(null);

  useEffect(() => {
    async function runQuantum() {
      await init();

      const circuit = new Circuit(3);
      circuit.h(0);
      circuit.cnot(0, 1);
      circuit.cnot(1, 2);  // GHZ state

      const sim = new Simulator();
      const state = sim.run(circuit);
      setResult(state.measureAll().toString(2).padStart(3, '0'));
    }
    runQuantum();
  }, []);

  return <div>Quantum result: {result ?? 'Computing...'}</div>;
}

API Reference

Circuit

class Circuit {
  constructor(nQubits: number);

  // Single-qubit gates
  h(qubit: number): void;      // Hadamard
  x(qubit: number): void;      // Pauli-X (NOT)
  y(qubit: number): void;      // Pauli-Y
  z(qubit: number): void;      // Pauli-Z
  rx(qubit: number, theta: number): void;  // X-rotation
  ry(qubit: number, theta: number): void;  // Y-rotation
  rz(qubit: number, theta: number): void;  // Z-rotation

  // Two-qubit gates
  cnot(control: number, target: number): void;
  cz(control: number, target: number): void;
  swap(q1: number, q2: number): void;

  // Three-qubit gates
  toffoli(c1: number, c2: number, target: number): void;
}

Simulator

class Simulator {
  constructor();
  run(circuit: Circuit): QuantumState;
}

QuantumState

class QuantumState {
  measureAll(): number;
  measure(qubit: number): number;
  probability(bitstring: number): number;
  amplitudes(): Float64Array;  // Complex interleaved [re, im, re, im, ...]
}

Algorithms

import { Grover } from '@ruvector/ruqu-wasm';

const grover = new Grover(4);  // 4 qubits = search space of 16
grover.setTarget(0b1010);       // Search for |1010⟩

const result = grover.search();
console.log(`Found: ${result.toString(2).padStart(4, '0')}`);

VQE

import { VQE, Hamiltonian } from '@ruvector/ruqu-wasm';

const h = new Hamiltonian();
h.addTerm("ZZ", 0.5);
h.addTerm("XX", 0.3);

const vqe = new VQE(h, nQubits: 4);
const energy = vqe.optimize({ maxIter: 100 });
console.log(`Ground state energy: ${energy}`);

Performance

Qubits Memory Init Time Gate Time
10 16 KB 1ms 0.01ms
15 512 KB 5ms 0.1ms
20 16 MB 50ms 5ms
25 512 MB 500ms 150ms

Note: 25 qubits requires ~1GB browser memory. Use Web Workers for heavy simulations.

Web Worker Example

// worker.js
import init, { Circuit, Simulator } from '@ruvector/ruqu-wasm';

self.onmessage = async (e) => {
  await init();
  const { gates, nQubits } = e.data;

  const circuit = new Circuit(nQubits);
  gates.forEach(g => circuit[g.name](...g.args));

  const sim = new Simulator();
  const state = sim.run(circuit);

  self.postMessage({ result: state.measureAll() });
};

Bundle Size

Build Size (gzip)
Core only 45 KB
With algorithms 120 KB
Full bundle 180 KB

Browser Support

  • Chrome 89+
  • Firefox 89+
  • Safari 15+
  • Edge 89+

Requires WebAssembly SIMD for optimal performance (available in all modern browsers).

Documentation

License

MIT OR Apache-2.0