Stacasso README, from GitHub.
Best viewed at jonhub.github.io/Stacasso.
Stacasso is a Python library for displaying and visualizing quantum computing circuits.
Stacasso works on top of Cirq, the open-source, widely supported quantum computing framework. Cirq is used to create and simulate circuits. Stacasso adds:
- Syntax highlighting for quantum computing circuits (color and formatting). Output can be displayed directly to the screen ("pretty-print"), or returned as
.html
strings for display elsewhere. - Visualization of the circuit execution, by drawing wave function amplitudes (probabilities) in state space. Circuits simulations can be "illustrated" as they execute, from start to finish, with a single function call.
Please note Stacasso is currently in beta release. Features are being added and improved. Currently, syntax highlighting should work on any circuit. Illustrations work on circuits up to four qubits.
These tools can be used to understanding and creating quantum circuits, in the same way that syntax highlighting and debugging tools have become indispensable tools in other computing languages.
To get started using Stacasso, see installing, below.
The examples (below) walk through creating and illustrating 1, 2, 3, and then 4-qubit circuits, using Cirq and Stacasso. These four circuits build on each other conceptually, and introduce many of the key concepts in quantum computation (superposition, entanglement, probability/amplitudes, phase).
Many of these concepts in quantum computing have a direct visual counterpart.
More information can be found in the Stacasso Guide (Notebook). The notebook is part of Stacasso, and can be executed locally, as a starting point for exploring quantum computation. Note that the guide (as well as the Stacasso project) is currently usable, but a work in progress
For more information on quantum computing using Cirq, see
Stacasso is opensource and free, licensed under Apache 2.0. Stacasso also uses code from Cirq, used here under the terms of their license (also Apace 2.0). Stacasso also draws circuits from the Cirq Tutorials, and are incorporated into Stacasso under terms of their license.
Stacasso is a python package, and can be installed using pip.
Download (or clone) a local copy from GitHub. Change to that directory, and install with
pip install -e .
Uninstall with:
pip uninstall stacasso
You should now be able to execute the stacasso_guide
notebook. Note that the notebook will install google Cirq (using pip), if it is not already installed.
A few examples of common quantum computing circuits, illustrated with Stacasso, will make the usage more clear.
Note, these images do not render correctly on GitHub. Make sure you are viewing the file at jonhub.github.io/Stacasso.
We can make and illustrate a simple 1 qubit quantum circuit with:
import cirq
import stacasso as so
# Quantum Random Number Generator
# first, make the circuit, using cirq
qrng_circuit = cirq.Circuit()
q0 = cirq.NamedQubit('qubit 0')
qrng_circuit.append(cirq.H(q0))
qrng_circuit.append(cirq.measure(q0))
# labeling the states is optional
labels = ['$\psi_0$', '', '$\psi_M$']
# print and illustrate with Stacasso
so.pprint( qrng_circuit, '"Quantum Random Number Generator (QRNG)"' )
so.illustrate( qrng_circuit, labels )
"Quantum Random Number Generator (QRNG)"
qubit 0: ──────H──────M──────
Notice that the wave function (the state of the algorithm) is drawn as a grid below the circuit, like a game board. A single qubit has two states, so there are two squares, drawn on top of each other.
Probabilities are visualized as colored disks, with the area proportional to the probability of being measured in that state. The (complex) amplitude is the radius of the disk, with the phase encoded by the disk orientation (the "dial"), as well as color.
The above circuit can be extended, by entangling the first qubit with a second. This creates the classic Bell State, where the result of operations on the first qubit, such as measurement, are inextricably linked to the second qubit.
import cirq
import stacasso as so
# Bell Circuit
bell_circuit = so.make_bell_circuit()
# make labels (optional)
labels = ['$\psi_0$',
'',
'$\psi_{Bell}$',
'$\psi_M$']
so.pprint(bell_circuit,'"Bell State Circuit"')
so.illustrate(bell_circuit, labels)
"Bell State"
q0: ──────H──────@──────M──────
│ │
q1: ─────────────X──────M──────
For this circuit, and any circuit with two or more qubit circuits, Stacasso
draws the state space matrix rotated by 45 degrees, for visual clarity.
(Circuit from Google Cirq Tutorial.)
The teleportation circuit uses the Bell State to send the a message. Entangled qubits (the Bell State) are shared by Alice and Bob, who may be located physically very distant from each other. Alice entangles the message (encoded as a quantum state) into her qubit, and Bob is then able to read the meassage from his (entangled) qubit.
"Quantum Teleportation"
0 (Ali): ──────H──────@─────────────────X─────────────M──────@─────────────
│ │ │ │
1 (Bob): ─────────────X─────────────────┼─────────────┼──────X──────@──────
│ │ │
2 (Msg): ────────────────────X^0.3──────@──────H──────M─────────────@──────
Since the original message disappears (the "no cloning" rule in quantum mechanics), the value contain in that qubit is said to have been "teleported." Notice that since this circuit contains a measurement, the results are stochastic; sometimes, the message is teleported, sometimes it is not.
In a typical quantum computing application, an algorithm is run many times. Running so.illustrate(circuit)
multiple times is a good way to gain understanding of the stochastic nature of measurement and collapse in quantum computing.
(Circuit from Google Cirq Tutorial. See "Quantum advantage with shallow circuits" for the original paper.)
The "Hidden Linear Function (constrained to 2-dimensions)" problem was the first "shallow code," introduced in 2018. Shallow refers to the number of steps that the circuit takes; the idea is to perform a useful computation quickly, before the quantum state of the computer has time to collapse.
"HLF 2D"
0: ──────H──────@──────@─────────────S──────H──────M──────
│ │
1: ──────H──────@──────┼──────@──────S──────H──────M──────
│ │
2: ──────H─────────────┼──────┼─────────────H──────M──────
│ │
3: ──────H─────────────@──────@──────S──────H──────M──────
The HLF 2D problem is useful, in that it can scale to essentially any size (any number of qubits). Note that the correct solutions to the problem are contain in the final wave function, before the measurement. In this case there are two solutions (zero is always a solution), though only one solution is apparent after measurement.
Quite a few interesting quantum circuits can be built with two or three qubits, and many building blocks in quantum computing can be broken down into these smaller circuit snippets.
The tiling scheme can also be extended to five or more qubits, and in principle, Stacasso should be able to visualize higher dimensional circuits, such as quantum error correction. (Planned for future relases).
The wave function which describe a quantum system can be thought of living inside of a "state space" (or "Hilbert space), which is shaped like a high-dimensional cube. Stacasso attempts to draw this space, in a logical and useful manner, to provide insight.
Since drawing all sides of a cube was the artistic goal of the "cubist" painters, the name Stacasso is a tribute to the most famous cubist: (State Space) Picasso.
Stacasso is licened under the Apache 2.0 License.
The code uses parts of the Google Cirq project (also under Apache 2.0 License), used here under terms of that license.
The repository is new and contains beta code, and currently not accepting contributions at this time. Please check back, as we would like to be able to accept contributions at some point in the future.
(Internally, the code could to be cleaned up and refactored.)
-
The goal is to get
Stacasso
usable, and then get it integrated into Google's cirqcontributing
folder, see Contribute to Google Cirq ... -
Larger qubits would be interesting to visualize. In this case, the game board would have to be stepped (or animated). For instance, ten qubits needs 1024 states, but could be used to show error correction, such as the Shore code (9-qubits).
-
Syntax highlighting can be expanded to include numbers, and qubit names (right justify), when they are different lengths.