Coder Social home page Coder Social logo

kamakiri01 / qulacs-wasm Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 0.0 13.52 MB

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

License: MIT License

C++ 34.22% TypeScript 64.55% JavaScript 1.24%
nisq quantum-circuit quantum-circuit-simulator quantum-computing qulacs typescript webassembly

qulacs-wasm's Introduction

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!

qulacs-wasm's People

Contributors

kamakiri01 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

qulacs-wasm's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.