Q-CTRL JavaScript Visualizer
Animated visualizations for 1-qubit and 2-qubit controls
This is the base library/module for the Q-CTRL JavaScript Visualizer. It is framework/library agnostic and can be implemented in a vanilla JavaScript app/website
Table of Contents
Installation
Via NPM and ES6 import
- Login to npm from the command line:
npm login
your npm account must be linked and have access to the qtrl org (This will be unnecessary if the package becomes publicly available) - Install the Visualizer module into your project with
yarn add @qctrl/visualizer
or npm i @qctrl/visualizer
- Since the Visualizer is an npm module you will need to use the ES6 modules
import
feature and will also need a transpiler such as Babel and/or a build pipeline such as Parcel (easy) or Webpack (Advanced) - Import the Visualizer component into your App like so:
import Visualizer from '@qctrl/visualizer'
Via direct download of pre-built bundle
-
Clone or download the repo and find the file Visualizer-Module/build/visualizer.js
(or download/copy the built code directly from here)
-
Copy Visualizer.js
to your project and include it in your main .html
file, above your main/index .js
file using a <script>
tag like so:
...
<script src="path/to/visualizer.js"></script>
<script src="path/to/index.js"></script>
</body>
Usage
Quick start
-
In your project's JavaScript, get a reference to a <div>
element/DOM-Node that will be used as a wrapper for the visualizer (or create a new <div>
element). The Element should have width
and height
CSS properties set as the visualizer will use the wrapping <div>
width and height parameters to set it's own size parameters internally. Note: The wrapper element will have its style property position
set to relative
by the visualizer
<body>
...
<div id="visualizer-wrapper" style="width: 500px; height: 500px;"></div>
...
</body>
const visualizerWrapper = document.getElementById("visualizer-wrapper");
-
Next create a new Visualizer instance, passing in the wrapper reference, then initialize it like so:
const myVisualizer = new Visualizer({ wrapper: visualizerWrapper }).init();
-
This will create and initialize a new Visualizer object and store a reference to it in the myVisualizer
variable. Also, a canvas/DOM element that the visualizer will be rendered to will be attached to the passed in wrapper element
-
If you now preview the HTML file you should be able to see a single Bloch sphere on the page
-
A Visualizer instance has an update method that can be used to pass in new "props" or settings, for example you can set whether the animation is playing or not with the isPlaying
prop. To try that now, below from where you have created your Visualizer instance, use this code snippet to have the visualizer start playing when the page loads:
myVisualizer.update({ isPlaying: true });
-
Now when your HTML preview loads the Visualizer will start playing immediately. Since no data was provided to the Visualizer instance when you created it, it will have generated some default data: A single 180 degree rotation around the x axis. (If you have predefined data you can pass it in to the constructor when creating a new Visualizer instance and this will be used for the visualization. See the DOCS section on Data for more info
-
If you do not have access to data you can instead pass in an array or a comma separated string of basic gate types. To try this, where you have initialized the visualizer instance update the code like so:
const myVisualizer = new Visualizer({
wrapper: visualizerWrapper,
gates: ["X", "H", "Z", "T"]
})
.init();
The Visualizer will generate the correct data to animate from the gate sequence you passed in. Now when you preview your HTML you will see an animated visualization of the above gate sequence. See the DOCS section on Gates for more info and available gate types
Contributing
Developing and using the module locally
- Clone the repo
cd
into the repo folder- run
yarn
to install the necessary packages
When you run yarn build:dev
first of all a symlink to the project directory will be created and registered with yarn using yarn link. The code will then be built and a watch process for any changes will be started. You can then use the symlinked version of the visualizer module you are working on by running yarn link @qctrl/visualizer
in your dev app project
Building documentation
Documentation generation relies on Sphinx. Automated builds are done by Read The Docs.
Note: If you have made changes to the code base and updated or added JSDoc strings, you will need to first rebuild and convert the JSDoc strings to .md
format using the script in the packages.json
file: From the root of the project run yarn build:jsdoc
. This command will ony work from a Unix based OS. If using windows you will need to use a bash emulator or Linux subsystem for windows
To build the docs locally:
-
nodeJS, python3 and the following modules must be installed on your system for the build to succeed: Sphinx, recommonmark and sphinx-js (Also requires the jsdoc NPM package installed globally for node as stated in the sphinx-js documentation)
-
Execute the make file from the docs directory:
Mac/Linux:
cd docs
make html
Windows:
cd docs
make.bat html
The generated HTML will appear in the docs/_build/html
directory.
Publishing to NPM
- To publish a new version of the
javascript-visualizer
module to the npm registry, you first need to update the version number in package.json
. The best way to do this is to run npm version <update_type>
update_type should be patch
, minor
or major
, see here for semantic versioning explanation - Next run
npm run build:publish
Note: yarn currently will not work for this publishing script. This will first build/compile using babel then publish the compiled lib to NPM. Note: You need to be logged in to NPM and your account needs to be linked and have access to the QCTRL Org. - Once the build and publish processes have completed, a standalone single file build will also be created and output into the
/build
directory
Coding style guide
Here is the hierarchy of conventions to follow for coding style. If in doubt
about a certain style, follow this from the top down until you find an answer
everyone can agree to.
When performing a code review and making a suggestion on coding style, try to
find the corresponding rule from the list below and link to it in the pull
request.
- Any conventions agreed by the team and posted here
- AirBnb's Javascript style guide
- A discussion with the team
- The frontend lead makes a final call if there is no obvious answer we can all
agree to.
Any new decisions which can be applied as a convention should be posted in this
section.
- Any code created or modified should be documented with JSDoc tags at the
minimum
License
See LICENSE.