15
$\begingroup$

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). :)

$\endgroup$

6 Answers 6

10
$\begingroup$

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, [email protected], Sphere[{0, 0, 0}, 1]},
   {Red, [email protected], Point@points[[1]]},
   {Blue, [email protected], Point@points[[-1]]},
   Dashed, [email protected], Arrow@points
   }, Axes -> True, AxesOrigin -> {0, 0, 0}, AxesStyle -> Black, 
  Ticks -> None, Boxed -> False]
 ]

which gives

enter image description here

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, [email protected], Sphere[{0, 0, 0}, 1]},
   {Red, [email protected], Point@points[[1]]},
   {Blue, [email protected], Point@points[[-1]]},
   Dashed, [email protected], Arrow@points
   }, Axes -> True, AxesOrigin -> {0, 0, 0}, AxesStyle -> Black, 
  Ticks -> None, Boxed -> False]
 ]

enter image description here

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:

enter image description here

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, [email protected], Sphere[{0, 0, 0}, 1]},
    {Red, [email protected], Point@points[[1]]},
    {Purple, [email protected], Point@points[[idx]]},
    {Darker@Green, [email protected], 
     InfiniteLine@{-#, #} &@
      decomposeInPauliBasis@
       First@Eigenvectors@hamiltonian@timesList[[idx]]},
    {Dashed, [email protected], Tube@points[[;; idx]]}
    }, Axes -> True, AxesOrigin -> {0, 0, 0}, AxesStyle -> Black, 
   Ticks -> None, Boxed -> False],
  {idx, 1, Length@points, 1}
  ]
 ]

enter image description here

(quality and smoothness can definitely be improved here)

$\endgroup$
6
$\begingroup$

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/

$\endgroup$
1
  • $\begingroup$ This link, unlike the other Block sphere simulator's google was giving me, finally helped me to understand what I missing about phase. (Well there are tonnes and tonnes of other things I'm still missing, but at least I'm over one hump thanks to you. $\endgroup$ Commented Jun 10 at 0:58
5
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$ Commented May 20, 2019 at 16:13
  • $\begingroup$ 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. $\endgroup$ Commented May 23, 2019 at 12:13
  • $\begingroup$ @Nelimee Odd, the first one worked for me (Firefox on Ubuntu). $\endgroup$ Commented May 23, 2019 at 15:58
5
$\begingroup$

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$:

enter image description here

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

$\endgroup$
4
$\begingroup$

I see this thread is a little old but if folks are still looking, here's my pet project showing 2 Bloch spheres. In addition to the full array of standard gates (1 and 2 qubit), the simulation also generates Q# code (and output) on the fly which you can paste and run directly into a Q# program. Given there are 2 qubits, you can also see when they get entangled.

It's all written in Javascript and a link to the code is provided. Enjoy!

https://renniedatascience.com/Bloch

$\endgroup$
1
$\begingroup$

I use very regularly this site:

https://bloch.kherb.io

It is useful to understand and engineer quantum control sequences as well as to generate plots for talks.

$\endgroup$
1
  • $\begingroup$ Very good tool, +1. $\endgroup$ Commented Aug 19, 2023 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.