/bloch_sphere

Visualization tools for the qubit Bloch sphere

Primary LanguagePythonMIT LicenseMIT

Qubit Bloch Sphere Visualization

A collection of visualization tools for the qubit Bloch sphere. The Bloch sphere is a useful representation of the state of a single-qubit quantum computer.

Example animated Bloch sphere

Install

bloch_sphere is available on PyPI:

python3 -m pip install bloch_sphere

Prerequisites

Cairo needs to be installed separately to render videos. See platform-specific instructions for Linux, Windows, and macOS from Cairo. Below are some examples for installing Cairo on Linux distributions and macOS.

Ubuntu

sudo apt-get install libcairo2

macOS

Using homebrew:

brew install cairo

Usage

This package provides a command line tool to generate animations. In your shell, run the following (run animate_bloch -h for help).

animate_bloch hadamard x y s s
animate_bloch2 xy_vs_z x,y z

Code Examples

Visualize a single Bloch sphere

from bloch_sphere.animate_bloch import do_or_save_animation, AnimState

@do_or_save_animation('my_animation', save=False, fps=20, preview=True)
# Or
#@do_or_save_animation('my_animation', save='gif', fps=20, preview=True)
#@do_or_save_animation('my_animation', save='mp4', fps=20, preview=False)
def animate(state: AnimState):
    state.x_gate()
    state.y_gate()
    state.s_gate()
    state.s_gate()
    ...
    state.wait()  # Pause at the end

Example output animation

Compare two sequences of gates

from bloch_sphere.animate_bloch_compare import main

main('hzh_x', 'h,z,h'.split(','), 'x'.split(','),
     r'& \gate{H} & \gate{Z} & \gate{H} & \qw & \push{=} & & \gate{X} & \qw',
     r'$HZH\ket{\psi}=X\ket{\psi}$',
     mp4=False,
     fps=20,
     preview=True,
)

Example output animation