# My Quantum Circuit Simulator: Quirk

22 May 2016

I've been working on a quantum circuit simulator that runs in your browser. It's called Quirk. Quirk is open source (github repository: Strilanc/Quirk), and there's a live instance you can play with at algorithmicassertions.com/quirk:

(Not familiar with what a quantum circuit is? Watch the Khan-academy-style video series Quantum Computing for the Determined. It's by Michael Nielson, co-author of the de-facto standard textbook.)

Features:

• Drag-and-drop circuit editing.
• Reacts, simulates, and animates in real time.
• Inline state displays.
• Bookmarkable circuits.
• Up to 16 qubits.

Recorded Demo:

Quirk exists because I got interested in quantum computing, and because I read content by Bret Victor.

Bret has a lot of provocative ideas about how we should interact with computers. One of the more obvious ideas he talks about is immediate feedback; that you shouldn't have to jump through hoops to see the effects of changes you're making. It should feel like you're directly manipulating the system, not like you're flipping switches that eventually result in the system being poked with a stick.

(For example, I find that my programming productivity is much higher when using a continuous testing tool that highlights lines covered by failing tests (e.g. NCrunch). When the line you just wrongly-refactored turns red before you had a chance to navigate away, everything feels right with the world. After experiencing that, it's hard to go back to waiting 30 seconds for tests to run.)

I wanted to have that kind of 'direct manipulation' experience with quantum circuits, but existing quantum circuit simulators just didn't have that feel.

Comparison: IBM Quantum Experience

Although IBM's "quantum experience" (hereafter 'IQE') didn't exist when I started Quirk, it's a good example of not-feeling-like-direct-manipulation.

Suppose you've gone through the signup hurdles for IQE (you don't just need to register an account, you need to request to register an account) and want to simulate a dead simple circuit. How does that experience play out? Well, here's a recording of me doing it:

First, it turns out I can't get any results without adding measurement gates. So I fix that. Then I'm prompted to confirm a name for this simulation run. Not sure why that's needed, but whatever. Then the UI throbs for 5 seconds while IBM works out the result of this 2 qubit circuit.

More succinctly: blerghh.

I remember watching IBM's demo video for IQE and just not believing how much time and clicks they managed to put between you and the results. In terms of the timescales of user interaction, they're flirting with the 10 second bucket:

After 1 second, users get impatient and notice that they're waiting for a slow computer to respond. The longer the wait, the more this impatience grows; after about 10 seconds, the average attention span is maxed out. At that point, the user's mind starts wandering and doesn't retain enough information in short-term memory to easily resume the interaction once the computer finally loads the next screen.

To be fair, the design decisions IBM made make more sense in the context of running the circuit on actual quantum hardware (Which IQE can do! That's awesome!). Nevertheless, it's made the experience of using IBM's simulator kind of... really awful.

Comparison: Davy Wybiral's Simulator

Davy Wybiral's Simulator can't run your circuits on actual hardware, but otherwise it's better than IQE in basically every way (including predating IQE by three years).

I like Davy's simulator. It was really helpful to me when I was learning. Here's a recording of me using it:

This recording loops faster than the IBM one, yet manages to get twice as much dicking around done. That's because, although evaluation still has to be triggered, it's fast. You don't need to click through a bunch of dialogs with already-good-enough options pre-selected, or twiddle your thumbs while packets bounce back and forth across America.

I could talk about some of the UX issues with Davy's simulator, like the output being a raw list of numbers, but I want to focus just on the "evaluation still has to be triggered" part.

Simulating small quantum circuits isn't expensive. People make a big deal about the exponential costs of qubits, but that only matters for large circuits. That, and the fact that GPUs are amazing, mean my phone can apply hundreds of gates to a dozen qubits fast enough to animate in real time. There's no need for a "Dear UI, please do the thing you knew I would want to do." button.

Comparison: Quirk

Quirk has no 'evaluate the circuit now' button. Quirk is always evaluating the circuit; it's part of the drawing code.

Originally, I tried using just raw javascript for the simulation. Things got slow around 7 qubits. Later, I read the Quantum Computing Playground's source code, saw that they were using webgl, and started experimenting with that.

Webgl is a pain (a giant pain), but now Quirk starts getting slow around the 14 qubit mark. That's about 100X faster. And I figure it's possible to squeeze out another order of magnitude or two of speed by moving the display-drawing code into the GPU, pipelining, and interpolating.

Still, those optimizations would just be gravy. The difference in experience, compared to the other two circuit simulators I mentioned, is already quite noticeable even in a recording:

On a change-to-result basis, Quirk gives you feedback two orders of magnitude faster than IQE. Instead of being in the 10 second interaction bucket, Quirk is in the 0.1 second bucket:

if you click on an expandable menu and see the expanded version in less than 0.1 seconds, then it feels as if you made the menu open up. If it takes longer than 0.1 seconds for the revised state to appear, then the response doesn't feel "instantaneous" — instead, it feels as if the computer is doing something to make the menu open.

You don't have to think about getting results, it just happens. This lets you experiment freely, and the immediate feedback from that experimentation gives you a feel for how adding/moving a gate will affect the output. Eventually, that feel turns into intuition.

I made Quirk to build my own intuitions about quantum circuits, and I'm happy with the results so far.

# Other Simulators

Davy Wybiral's Simulator, which I already mentioned, is another in-browser drag-and-drop circuit simulator.

The Quantum Computing Playground is a browser-based quantum program simulator.

Microsoft's LIQ𝑈𝑖⏐〉 is a serious useful-for-actual-research tool.

Quantiki has a long list of qc simulators.

# Example Circuits

Not sure what to make in Quirk? Here's a few example circuits.

Grover Search

Grover's search algorithm does unstructured search quadratically faster than classical computers can. It's also got a really straightforward circuit: oracle, hadamard, controlled-Z, hadamard, repeat. A couple years ago, I wrote a pretty good beginner-level blog post on Grover's algorithm.

See also: 6 qubit and 8 qubit variants. (I tried making the 16-qubit variant... Quirk started seriously breaking about 2/3rds of the way through, around the 8K gate mark.)

Quantum Teleportation

Quantum teleportation uses classical communication, and some pre-shared entanglement, to move a qubit. With Quirk's inline Bloch-sphere displays, we can see that the qubit being put in at the top is in fact coming out at the bottom:

See also: superdense coding circuit, which uses the same basic trick in reverse to pack 2 classical bits into a single qubit.

Delayed Erasure

A qubit version of the infamous optical experiment:

Pretty Colors

Sometimes it's fun to just dick around and make things that look visually interesting:

Other Circuits

A symmetry-breaking circuit, where both sides do the same thing yet end up with opposite values.

A non-local measurement circuit that doesn't require coordination until later (see also: the paper).

# Work in Progress

Quirk is still under development. There are still bugs and hitches and missing features. Notable ones include:

• The touch interaction in firefox mobile is awful. Drags keep turning into scrolls.
• The circuit URLs are kind of ridiculously long, and hard to share because of how many special characters they use.
• There's no way to add custom gates (besides manually editing the circuit URL to contain an entry like {"id":"cool_gate","matrix":"{ {1,i}, {i,1} }"}).
• The toolbox has too much stuff in it, but also not enough stuff.
• You can't put measured qubits back into superposition. (Though post-selecting does count as 'unmeasuring' them, and you can use CNOTs instead of measurement gates.)

You should try Quirk out anyways.

# Summary

Quirk is a browser-based drag-and-drop quantum circuit simulator that reacts, simulates, and animates in real-time.

 « Reducing Test Boilerplate with Ascii Diagrams Ultradense Coding would allow FTL Signalling »