jsqubits icon indicating copy to clipboard operation
jsqubits copied to clipboard

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

Open tobiasBora opened this issue 5 years ago • 2 comments

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.

tobiasBora avatar Oct 02 '20 17:10 tobiasBora

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 🤔

davidbkemp avatar Apr 11 '21 06:04 davidbkemp

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>

tobiasBora avatar Dec 11 '21 12:12 tobiasBora