Skip to content

Quantum Circuit Simulator for browser and Node.js, powered by WebAssembly

License

Notifications You must be signed in to change notification settings

kamakiri01/qulacs-wasm

Repository files navigation

Qulacs Wasm

qulacs-wasm lets you use Qulacs in JavaScript via WebAssembly. It provides a convenient syntax similar to Qulacs on Python in JavaScript/TypeScript, and aims an efficient way to manipulate quantum computation on JavaScript runtime environment.

Qulacs and qulacs-wasm is licensed under the MIT license.

Usage

npm install qulacs-wasm
import { initQulacs } from "qulacs-wasm";

initQulacs()
  .then(async () => {
    const { QuantumState, QuantumCircuit } = await import("qulacs-wasm");
    const qubitCount = 2;
    const state = new QuantumState(qubitCount);
    state.set_zero_state();
    const circuit = new QuantumCircuit(qubitCount);
    circuit.add_H_gate(0);
    circuit.add_CNOT_gate(0, 1);
    circuit.update_quantum_state(state);
    console.log("state vector ", state.get_vector());
    console.log("sampling", state.sampling(10)); // sampling may return 0th/3th base state with equal probability
  });
state vector [
  { real: 0.7071067811865475, imag: 0 },
  { real: 0, imag: 0 },
  { real: 0, imag: 0 },
  { real: 0.7071067811865475, imag: 0 }
]
sampling [
  0, 0, 3, 3, 3,
  0, 0, 0, 0, 0
]

Features

  • Almost fully Qulacs on Python compatible interface (except for direct json file access)
  • Very fast quantum circuit simulation in JavaScript environments
  • Provides TypeScript d.ts
  • Support for external loading of .wasm file

Qulacs Python Advanced Guide use case is implemented by TypeScript in advanced-guide.test.ts.

Performance

The time to simulate a random quantum circuit is compared to the original Qulacs(November 2020).

This benchmark test complies with benchmark-qulacs and you can see the detail here.

Single-thread benchmark

single thread benchmark

How to import from external .wasm file

qulacs-wasm automatically loads bundled wasm binary, but can also load it externally. This helps manage js bundle size.

import { initQulacs } from "qulacs-wasm/lib/nobundle";

const module = await WebAssembly.compile(fs.readFileSync("../path/to/module.wasm")); // Node.js
// const module = await WebAssembly.compileStreaming(fetch("module.wasm")); // online
await initQulacs({ module });
const { QuantumState, X } = await import("qulacs-wasm/lib/nobundle");
const state = new QuantumState(1);
(X(0)).update_quantum_state(state);

How to include .wasm file for your project, details in sample.

Build qulacs-wasm from source

Building qulacs-wasm involves building Qulacs and Boost, so those requirements must be satisfied, and Emscripten environment for qulacs-wasm.

Requirements

  • Qulacs requirements (tested on v0.5.6)
  • Emscripten (tested on v3.1.22)
    • need em++ command
  • Node.js/npm (tested on Node.js v18.3.0/npm 8.11.0)
    • some version included in Emscripten environment

tested on the following systems.

  • Ubuntu 20.04.3 LTS (on WSL2)
npm run init
npm run submodule:build
npm install
npm run build

Non-available classes and functions

  • GPU class (ex: QuantumStateGpu)
  • ITYPE(long long int) type (automatically cast to int)
  • file access functions (ex: create_quantum_operator_from_openfermion_file / create_observable_from_openfermion_file / create_split_quantum_operator)
  • Multi-thread (limited by currently WebAssembly Specifications)

Pull Requests Welcome!