# Is there any online Bloch sphere simulator?

While writing this answer I realized it would be really helpful if I could show the OP a video or .gif of how qubit states in Bloch spheres transform under certain unitary operations. I googled up a bit and could find only these two simulators:

Both involve some messy software installations and I don't really want to do that. The second one apparently doesn't even allow the user to input arbitrary 2×2 operators!

P.S: It would be great if Craig Gidney could add a full-fledged Bloch sphere simulator within Quirk at some point (ideally, by making the already existing Bloch sphere views of the qubit states clickable and enlargeable). :)

This doesn't really answer the question as it's not an online simulator. It might still be relevant though as it is a way to produce this sort of gifs if one has access to the software.

It is relatively easy to do this sort of things using Wolfram Mathematica.

As a quick and dirty example, if we just define a couple of relevant helper functions:

pauliX = PauliMatrix[1];
pauliY = PauliMatrix[2];
pauliZ = PauliMatrix[3];
ClearAll@decomposeInPauliBasis;
decomposeInPauliBasis[matrix_?MatrixQ] := {
Tr[matrix.pauliX], Tr[matrix.pauliY], Tr[matrix.pauliZ]
}/2;
decomposeInPauliBasis[vec_?VectorQ] := Re@{
Dot[Conjugate@vec, pauliX, vec], Dot[Conjugate@vec, pauliY, vec],
Dot[Conjugate@vec, pauliZ, vec]
};
ClearAll[simulateStateEvolution, smallestEigenvectors];
smallestEigenvectors[matrix_, howmany_Integer] := With[
{nn = Norm@Flatten@matrix},
Eigenvalues[matrix - nn IdentityMatrix[Dimensions@matrix],
howmany] + nn
];
simulateStateEvolution[H : (_Symbol | _Function | _CompiledFunction),
time_: 1., initialState_: None] := Module[{t},
Module[{\[DiamondSuit]initialState, \[DiamondSuit]H},
If[initialState === None,
\[DiamondSuit]initialState =
First@smallestEigenvectors[H[0], 1],
\[DiamondSuit]initialState = initialState
];
(* protect from symbolic evaluation *)
\[DiamondSuit]H[
t_?NumericQ] := H[t];
NDSolveValue[{
\[Psi][0] == \[DiamondSuit]initialState,
\[Psi]'[t] == -I \[DiamondSuit]H[t].\[Psi][t]
}, \[Psi], {t, 0, time}
][time]
]
];


we can then visualise the evolution in the Bloch sphere with

hamiltonian[t_] := pauliZ + 2 pauliX;
initialState = {1, 0};
With[{points = Table[
decomposeInPauliBasis@
simulateStateEvolution[hamiltonian, t, initialState],
{t, 0, 1, 0.01}
]},
Graphics3D[{
{Orange, Opacity@0.2, Sphere[{0, 0, 0}, 1]},
{Red, PointSize@0.02, Point@points[[1]]},
{Blue, PointSize@0.02, Point@points[[-1]]},
Dashed, Thickness@0.005, Arrow@points
}, Axes -> True, AxesOrigin -> {0, 0, 0}, AxesStyle -> Black,
Ticks -> None, Boxed -> False]
]


which gives

We can also use a time-dependent Hamiltonian, for example:

hamiltonian[t_] := pauliZ + t pauliX;
initialState = {1, 0};
With[{points = Table[
decomposeInPauliBasis@
simulateStateEvolution[hamiltonian, t, initialState],
{t, 0, 4, 0.01}
]},
Graphics3D[{
{Orange, Opacity@0.2, Sphere[{0, 0, 0}, 1]},
{Red, PointSize@0.02, Point@points[[1]]},
{Blue, PointSize@0.02, Point@points[[-1]]},
Dashed, Thickness@0.005, Arrow@points
}, Axes -> True, AxesOrigin -> {0, 0, 0}, AxesStyle -> Black,
Ticks -> None, Boxed -> False]
]


If you want something a bit more fancy, you can take the code I used in this answer to draw a better looking Bloch sphere, which would give something like the following:

Finally, if you want some animation, you can try something like the following (where I'm also adding the green line to denote the instantaneous eigenvector of the Hamiltonian):

hamiltonian[t_] := pauliZ + t pauliX;
initialState = {1, 0};
timesList = Range[0, 4, 0.01];
With[{points = Table[
decomposeInPauliBasis@
simulateStateEvolution[hamiltonian, t, initialState],
{t, timesList}
]},
Animate[
Graphics3D[{
{Orange, Opacity@0.2, Sphere[{0, 0, 0}, 1]},
{Red, PointSize@0.02, Point@points[[1]]},
{Purple, PointSize@0.02, Point@points[[idx]]},
{Darker@Green, Thickness@0.01,
InfiniteLine@{-#, #} &@
decomposeInPauliBasis@
First@Eigenvectors@hamiltonian@timesList[[idx]]},
{Dashed, Thickness@0.005, Tube@points[[;; idx]]}
}, Axes -> True, AxesOrigin -> {0, 0, 0}, AxesStyle -> Black,
Ticks -> None, Boxed -> False],
{idx, 1, Length@points, 1}
]
]


(quality and smoothness can definitely be improved here)

I used this last time I needed to look up something about Bloch sphere. It's not perfect, since it doesn't allow entering the exact values of angles, let alone 2x2 matrices, but it has the benefit of being available online.

This one looks promising in that it allows to enter matrices (and is also online), but I haven't tried it.

• Erm, tried the second one. Not sure how it works. Ticked "active" for $\Psi_1$, entered 0,0 corresponding to $\theta$ and $\phi$ and tried with the pre-defined unitaries. The top display shows an empty Bloch sphere. May 20, 2019 at 16:13
• None of the links worked for me. Up-to-date Firefox (67.0) on Linux. I don't know if Chrome users or Mac/Windows users had more chance. May 23, 2019 at 12:13
• @Nelimee Odd, the first one worked for me (Firefox on Ubuntu). May 23, 2019 at 15:58

Have you tried the Bloch Sphere Playground Application? It just might be what you are looking for.

Bloch Sphere Playground Application

https://javafxpert.github.io/grok-bloch/

Let me plug my pet project: https://attilakun.net/bloch

It allows you to enter arbitrary 2x2 matrices and visualize how the quantum state is affected by them.

In the below example the red arc shows how the H matrix transforms the $$|0\rangle$$ state (yellow arrow) into $$|+\rangle$$:

Also, it's open source if you want to play around with the code: https://github.com/attila-kun/bloch