ruvector/crates/ruqu-wasm
ruvnet dd59745ed8 fix(rvagent-cli, ruqu-wasm): unblock 2 PR #388 test failures
PR #388's matrix-split CI exposed two pre-existing failures hidden
by the previous 30-minute Tests-job timeout. Both have surprising
root causes worth recording.

## Failure 1 — `rvagent-cli::a2a_cli::a2a_serve_discover_and_send_task`

Symptom: `unrecognized subcommand 'a2a'` from the spawned `rvagent`
binary; test panicked at the `expect(server closed before emitting
listening line)` site.

Root cause: **PR #380's `main.rs` and `Cargo.toml` changes were
silently lost during merge.** The new `crates/rvAgent/rvagent-cli/src/a2a.rs`
file landed, but:
  - `mod a2a;` was never added to `main.rs`
  - The `A2a(A2aCommand)` variant was never added to the `Commands`
    enum
  - The dispatch arm was never wired in
  - `Cargo.toml` was never updated with the new deps
    (`rvagent-a2a` path dep, `ed25519-dalek`, `rand_core`, `axum`,
    `reqwest`, `hex`, plus tokio's `signal`/`process`/`time`/`io-*`
    /`fs`/`net` features)

So `rvagent` shipped with `a2a.rs` orphaned: the file compiled into
the lib via `lib.rs` but the binary's `main.rs` never knew about it.

Fix:
  - `main.rs`: add `mod a2a;`, add `A2a(a2a::A2aCommand)` variant,
    add `is_tui_mode` arm, add dispatch arm using
    `cli.command.take()` to own the variant (avoids needing to
    derive Clone on every clap struct in `a2a.rs`).
  - `Cargo.toml`: restore the deps and tokio features PR #380
    intended.

Diagnostic improvement: also extended the test to drain the
server's stderr in the background and dump it on every panic
path. Without that I'd never have seen `unrecognized subcommand
'a2a'` — the future-me debugging this would have spent hours.

Verified locally: `cargo test -p rvagent-cli --test a2a_cli` →
`1 passed; 0 failed`.

## Failure 2 — `ruqu-wasm::tests::test_circuit_rejects_too_many_qubits`

Symptom: panic inside `wasm-bindgen-0.2.117/src/lib.rs:1280`
("function not implemented on non-wasm32 targets").

Root cause: the test module was `#[cfg(test)]` (runs on every
`cargo test`) but called into wasm-bindgen-wrapped types
(`WasmQuantumCircuit::new`), which since wasm-bindgen 0.2.117
panic when called from a non-wasm runtime.

Fix: gate the tests module on `#[cfg(all(test, target_arch =
"wasm32"))]`. WASM-binding tests run via `wasm-pack test`; the
underlying `ruqu-core` numeric logic is already covered by its
own native test suite.

This is the same pattern PR #390 (RaBitQ WASM) used proactively.

## Verification

  cargo build -p rvagent-cli                                 → clean
  cargo test  -p rvagent-cli --test a2a_cli                  → 1/1 pass
  cargo build -p ruqu-wasm                                   → clean
  cargo test  -p ruqu-wasm                                   → 0 native tests
                                                                (wasm-only path)
  cargo clippy -p rvagent-cli -p ruqu-wasm --all-targets
       --no-deps -- -D warnings                              → exit 0
  cargo fmt --all --check                                    → exit 0

After this lands, PR #388's Tests (rvagent) and Tests (ruqu-quantum)
shards should go green.

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-04-26 00:14:39 -04:00
..
src fix(rvagent-cli, ruqu-wasm): unblock 2 PR #388 test failures 2026-04-26 00:14:39 -04:00
Cargo.toml chore(workspace): clippy-clean every crate under -D warnings + fmt + repair pre-existing broken benches 2026-04-25 17:00:20 -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