Code Monkey home page Code Monkey logo

jsqubits's Introduction

jsqubits

Quantum computation simulation JavaScript library

Build Status

Website: https://davidbkemp.github.io/jsqubits/

The user manual: https://davidbkemp.github.io/jsqubits/jsqubitsManual.html

Try it out online using the jsqubits runner: https://davidbkemp.github.io/jsqubits/jsqubitsRunner.html

Wiki (with examples): https://github.com/davidbkemp/jsqubits/wiki

GitHub: https://github.com/davidbkemp/jsqubits

Node npm module: https://npmjs.org/package/jsqubits

You can use it to implement quantum algorithms using JavaScript like this:

jsqubits('|01>')
    .hadamard(jsqubits.ALL)
    .cnot(1, 0)
    .hadamard(jsqubits.ALL)
    .measure(1)
    .result

If you are new to quantum programming, then it is highly recommended that you try reading John Watrous' Quantum Information and Computation Lecture Notes. You may also wish to try reading the (work in progress) Introduction to Quantum Programming using jsqubits.

Usage

Online

Try it out online using the jsqubits runner: https://davidbkemp.github.io/jsqubits/jsqubitsRunner.html

Install using npm

You can use jsqubits in a Node application by installing jsqubits using npm.

First, make sure you have at least version 16 of Node installed.

Place the following code in myprogram.mjs. Note the .mjs extension is a way of informing Node that the program uses ES modules.

import {jsqubits} from 'jsqubits'
const result = jsqubits('|0101>').hadamard(jsqubits.ALL);
console.log(result.toString());

Run the following:

$ npm install jsqubits@2
$ node myprogram.mjs

Downloading from github

You could use jsqubits by cloning the github repository, or downloading a release from github.

First, make sure you have at least version 16 of Node installed.

Clone jsqubits

$ git clone https://github.com/davidbkemp/jsqubits.git

Place the following code in myprogram.mjs. Note the .mjs extension is a way of informing Node that the program uses ES modules.

import {jsqubits} from './jsqubits/lib/index.js'
const result = jsqubits('|0101>').hadamard(jsqubits.ALL);
console.log(result.toString());

Run your program using Node:

$ node myprogram.mjs

On your own website

We are using native ES modules. Sadly, this complicates things when using jsqubits from within a web page:

  • The page will need to be served by a web server, not just loaded from your local file system. Note: placing your web page on github pages is an option for this.
  • You will need a copy of the contents of the lib directory on your web server.
  • Your page will need to specify type="module" in the script tag used to load the jsqubits library.
  • The page will not work on old versions of web browsers. It definitely won't work on Internet Explorer. I have successfully had jsqubits running on Chrome 89, Firefox 87, Safari 14, and Edge 89.

e.g. assuming you have placed the contents of lib in a directory called jsqubits that sits next to your webpage, then you could create a simple web page like this:

<html>
<body><p id="result"></p></body>
<script type="module">
  import jsqubits from './jsqubits/index.js';
  const qstate = jsqubits('|0>').hadamard(0).T(0);
  document.getElementById("result").innerHTML = qstate.toString();
</script>
</html>

See other examples in the examples directory.

TypeScript type definitions

TypeScript type definitions for jsqubits are available:

Development

To run the tests, you will need to install version 16 or later of Node.js (https://nodejs.org). Then use npm install to install the testing dependencies and npm test to run the specs. NOTE: The tests include an example of factoring using Shor's faction algorithm. This is non-deterministic and can take a fraction of a second or several seconds to complete.

License

(The MIT License)

Copyright (c) 2012 David Kemp <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

jsqubits's People

Contributors

davidbkemp avatar paneraallday avatar tearsofphoenix avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jsqubits's Issues

Publish new npm version

If anyone is following this and can help me get my head around the implications of the ES6 transpiling with respect to publishing to npm and node modules it would be greatly appreciated.

Currently you can do this as npm is using the pre-ES6 version of the code:

$ npm install jsqubits
$ node
> qubits = require('jsqubits').jsqubits

With the move to ES6, you need to use require('jsqubits').default instead of require('jsqubits').jsqubits

I am thinking I should maintain backward compatibility by exporting Qubits as both the default and as jsqubits. It looks like this could be done by adding a line to lib/index.js

Either way, I might increment the major version in npm and in github to 2.0.0 just in case this has introduced any breaking changes.

Improve the objects returned by each()

StateWithAmplitude is a lame name. QStateComponent would be better. The meaning of the index attribute is not clear (do we need it?). The method names are not obvious and they should probably cache (or precompute) their values.

Examples don't run out of the box.

Exception looking for: dist/commonjs/index.js
So I downloaded the distribution release which also lacks a dist folder.

I'm assuming there is a command that will produce a runnable example but I'm not so inclined to fix a broken distribution. Cost / benefit and all.

"As a new user of this library I would like the demo examples to work so I don't need to invest time in development to find out if this code actually works as advertised."

declaration for TypeScript

Hello,
Since I want to use jsqubits with TypeScript, I wrote d.ts of jsqubits.

https://github.com/qramana/jsqubits.d.ts

This type definition mostly covers the I/F of jsqubits.

I'm already happy, but I think it would be more useful if this was published npm.

May I create PR of this for DefinitelyTyped? (DefinitelyTyped is TypeScript d.ts repository)

This d.ts maybe named @types/jsqubits when publish.
This name is same as your library.
If you do not want this publish, please let me know.

Provide easy to use js file (even better: url to include directly)

Hello,

First, thanks a lot for this great simulator, it will be quite practical for me. I just have a minor issue: would it be possible to provide from time to time a packed .js file for simple in-browser use? Because otherwise one need to install nodejs to write the code, or download the .js file used in the demo that may be outdated.

By the way, the README says:

Use it in your own web application by including the JavaScript files available from GitHub: https://github.com/davidbkemp/jsqubits/tree/master/lib (jsqubits.js is the core library, while jsqubitsmath.js ...)

However, neither jsqubits.js nor jsqubitsmath.js exist in this folder.

dist/commonjs/index.js not found

Looks like jsqubits runner has stable architecture, but this repo doesn't.
It says
Failed to load resource: the server responded with a status of 404 (File not found).
So I can be sure there must be some directory like dist, but there isn't(at least in this github repo). Perhaps there are some differences between runner's version and repo.

Basically, I need some help of this library's author...to make some working quantum program.

Document QState.each()

QState.each() is likely to be useful to anyone wanting to add new operators and should be documented.

Measure and destroy a qubit

Hello,

First, thanks for this nice library. I'd like to perform sometimes some measurements that require auxiliary qubits (a.k.a. ancillae), but after the measurement the qubits are not useful anymore. I can add qubits using the tensor mechanism, but is there a way to remove qubits that have been measured? It could be useful to avoid to have an increasing number of qubits in use, to simplify the states, and also to simplify the code (one solution to avoid the explosion of the number of qubits in simple cases is to keep in mind which qubit is auxiliary and which one is not, but it complicates the code, and if at one point one needs lots of auxiliary for a particular measurement, then the state will stay big for the rest of the computation).

Thanks!

quantum gate conjugate

Would you like to ask how to realize the conjugate calculation of quantum gate, or how to get the conjugate quantum gate

Support Complex.getPhase()

It would be nice if Complex had a function that returned the phase of the complex number (i.e. the angle when expressed in polar co-ordinates).

Decide what to do about bower support

Bower is no longer all that popular. Even the Bower web site recommends using "Yarn and Webpack or Parcel".

So, I think that the bower.json should be deleted, but what should I replace it with?

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.