Skip to main content

functorial visualization library with support for quantum mechanics

Project description

spheres

Installation

pip install spheres

Usage

spheres provides a seamless mechanism for real-time visualization of python data in the browser, with an emphasis on the numerical data required for simulations of quantum mechanics.

a = View(1)

The basic pattern is to wrap a View around any python object. The result is that, for example:

type(a) => <class 'spheres.view.View(int)>, but a.__class__ => <class 'int'>.

The View object completely emulates its "inner class" and can be used more or less interchangeably.

a + 1 => 2

To assign an "inner value" to a View use:

a << (a + 1)

Make sure to include the parentheses because otherwise python will interpret this as (a << a) + 1. You can retrieve the underlying object of a View with a.get().

Meanwhile, however, when you import spheres a webserver built on flask and socketio starts in a background thread, which provides 3D visualizations of certain datatypes using three.js in the browser. Note that the import won't complete until the server has accepted a connection via the browser. You can specify the port using a commandline argument:

python test.py -p 8080 or python test.py --port 8080.

Then the page will be available at localhost:8080.

The idea is that each View object python-side is linked to a corresponding View object on the javascript side. Every time the python View is changed, the javascript View reflects the change. Note that javascript Views are created on the first "flush," but that repr(view) has been overloaded to flush.

a = View(1)
a

The value of a should print to the console, while a pane displaying that same value should appear in the browser. This pane can be dragged around, resized, and double clicking it will make it dissapear.

You can even access javascript methods in python. The idea is that when you try to call a method of python View a, first the object checks its own namespace, then it checks the namespace of its "inner class," but then it checks the namespace of the corresponding javascript class in the browser. For example, the javascript View has a destroy method, which kills the visualization (although it will return upon another flush). It can be easily called via python as if it were a python method: a.destroy().

Symmetrically, we can call python methods from javascript. To see this, first we note that a.js() will return a useful string of the form,

workspace.views['b5e9f8c4-c43e-41df-9c64-fd195f1da7b2'].

from which it becomes clear that each python/js View is assigned a unique id. In the javascript console, we can then use:

await workspace.views['b5e9f8c4-c43e-41df-9c64-fd195f1da7b2'].call('js')()

In the python console, this prints the same value as before. The results of the method (nothing in this case) are returned as a javascript Promise. We also observe that while python Views are managed by the View class itself, the javascript Views are managed by a "Workspace," available globally as workspace.)

A view can be defined in the following way (in python):

View(obj,\
		to_client=lambda view: <view to viz_data>,\
		from_client=lambda viz_data: <viz_data to inner obj>,\
		js_class="<javascript class name>",\
		requires_flush=["these", "methods", "will", "trigger", "an", "update"])

The default View has a simple to_client function: lambda view: str(view) and its from_client function does nothing. Its js_class is "View," and nothing special requires flushing.

Currently, the only other View which has been implemented is Sphere. It's defined in the following way.

def Sphere(obj):
	return View(obj,\
		 		to_client=lambda view:\
				 	{"stars": [xyz.tolist() for xyz in spin_XYZ(view.get())],\
				 	 "phase": [1,0]},\
		 		from_client=lambda data: qt.Qobj(XYZ_spin(data["stars"])),\
		 		js_class="Sphere")

A Sphere represents the state of a "quantum spin" as a constellation of points on a 2-sphere via an insight often attributed to Ettore Majorana. Note that we use the automatically included qutip library, available as qt, for our quantum calculations, and we assume knowledge of its use.

A quantum spin can be represented as finite dimensional complex vector. Eliding many details, if the components of a spin state in the |j, m> representation are interpreted as the coefficients of a polynomial, then the roots of that polynomial when stereographically projected from the complex plane to the 2-sphere along the axis of quantization correspond to a constellation of points. Each point or "star" contributes an angular momentum of 1/2 in its direction so that the total angular momentum axis is just the sum of the stars.

The provided method spin_XYZ takes a qt.Qobj (or an np.array) representing a spin state and returns a list of the xyz coordinates of its stars. Conversely, the method XYZ_spin takes a list of xyz coordinates and returns its corresponding complex vector (as an np.array). We leave aside for now the "phase," which, to put it briefly, has to do with the fact that the roots of polynomials are only defined up to multiplcation by a complex scalar.

To create a Sphere for a random spin state, we can use:

a = Sphere(qt.rand_ket(3))
a

Updating a in python, of course, updates the constellation. But because the relationship is bidirectional, if we drag the stars around in the visual representation, a automatically reflects the change in python.

We can easily see this by using one View to "listen" to another View.

a = Sphere(qt.rand_ket(3))
b = View("")
b.listen(a, lambda o: str(o))

Here, View("") is, of course, a wrapper around a string object. The effect is that every time a changes, b reflects the change: specifically, the inner object of b is set to the return value of the provided lambda to which a is passed as o.

In the browser, there should appear a sphere for a and a pane for b. It's clear that dragging the stars around the sphere updates the complex vector in real time, which is reflected in b. (If you're done listening, use b.unlisten(a).)

Finally, consider:

dt = 0.008
u = (-1j*dt*qt.rand_herm(3)).expm()
a.loop_for(5000, lambda o: u*o)

The first line generates a random unitary matrix representing quantum time evolution over a short interval dt. We can apply this unitary to our quantum state with u*a. It often happens we want to do this over and over again, animating the constellation. But for efficiency, we don't really want to update the visualization after every iteration, and in fact, doing so often overloads the browser. So we have loop_for. It takes optional parameters rate and sleep: a rate of 1/2 flushes updates to the browser after every other iteration; a rate of 1/3 flushes updates every three iterations, etc; and we can also sleep for a certain amount of time after each iteration. Generally, the default values are fine.

And that's all for now.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spheres-0.1.0.3.tar.gz (3.8 MB view details)

Uploaded Source

Built Distribution

spheres-0.1.0.3-py3-none-any.whl (3.8 MB view details)

Uploaded Python 3

File details

Details for the file spheres-0.1.0.3.tar.gz.

File metadata

  • Download URL: spheres-0.1.0.3.tar.gz
  • Upload date:
  • Size: 3.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1

File hashes

Hashes for spheres-0.1.0.3.tar.gz
Algorithm Hash digest
SHA256 d9c62e07ffc519b381be76c00545b254e9f8dd0d1e21c279543388e1df6f8b5f
MD5 ef633841bd8cf3d97601ea4e2af9322a
BLAKE2b-256 b7a0d7f3a1e7a842227bb98ac0058f28723772af879d9088a2767e4a0cb2af8e

See more details on using hashes here.

File details

Details for the file spheres-0.1.0.3-py3-none-any.whl.

File metadata

  • Download URL: spheres-0.1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 3.8 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1

File hashes

Hashes for spheres-0.1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 9152138e18328a7f0605a35efa692d3f429f41ee0b8873c3da3b10b24e0d7e00
MD5 6d8247aae1a10d52066cbef3607c6281
BLAKE2b-256 8c048006afd5ffc56ee3715efc7f35dcc36a16ed09be8c9fd0e2a8f7ffebd25d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page