davidbkemp / jsqubits

Quantum computation simulation JavaScript library
MIT License
126 stars 24 forks source link

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

Open tobiasBora opened 4 years ago

tobiasBora commented 4 years ago

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.

davidbkemp commented 3 years ago

Moving to ES modules has made this even harder. It seems that browsers won't import ES modules from anywhere other than the the host of the web page doing the import.

I think Rollup https://rollupjs.org/ might make it fairly easy to generate a simple javascript file that could be referenced from within a browser. It goes against my current objective of reducing 3rd party dependencies, but it looks pretty simple to use.

Perhaps we could start hosting rollup'd files on jsqubits github pages 🤔

tobiasBora commented 2 years ago

Rollup worked great in my case, thanks for pointing me to that. With that it even works locally and older browers, no issues with CORS anymore. And it's super simple to use: first install rollup (if you are using nix, it's just nix-shell -p nodePackages.rollup) and run:

$ rollup lib/index.js --file bundleJsqubit.js --format iife --name bundleJsqubit

Then to use it in a project:

<script src="./bundleJsqubit.js"></script>
<script type="text/javascript">
  jsqubits = bundleJsqubit.jsqubits; // 
  var bell_pair = jsqubits('|00>').hadamard(0).controlledX(0,1);
</script>