Skip to content

Commit

Permalink
Bloch sphere in notebook (#81)
Browse files Browse the repository at this point in the history
* Fix notebook not rendering lines on Blcoh sphere

* Update notebooks with colored bloch trajectories

* Add cbar_kwargs to docstring

* Fix docstring in infidelity
  • Loading branch information
thangleiter committed Feb 22, 2022
1 parent 0b6dd48 commit 169d867
Show file tree
Hide file tree
Showing 9 changed files with 27,406 additions and 44,305 deletions.
3,938 changes: 3,925 additions & 13 deletions doc/source/examples/advanced_concatenation.ipynb

Large diffs are not rendered by default.

2,598 changes: 2,520 additions & 78 deletions doc/source/examples/calculating_quantum_processes.ipynb

Large diffs are not rendered by default.

1,080 changes: 1,063 additions & 17 deletions doc/source/examples/extending_pulses.ipynb

Large diffs are not rendered by default.

3,959 changes: 3,944 additions & 15 deletions doc/source/examples/getting_started.ipynb

Large diffs are not rendered by default.

6,355 changes: 6,315 additions & 40 deletions doc/source/examples/periodic_driving.ipynb

Large diffs are not rendered by default.

52,745 changes: 8,629 additions & 44,116 deletions doc/source/examples/quantum_fourier_transform.ipynb

Large diffs are not rendered by default.

994 changes: 987 additions & 7 deletions doc/source/examples/qutip_integration.ipynb

Large diffs are not rendered by default.

11 changes: 4 additions & 7 deletions filter_functions/numeric.py
Expand Up @@ -1979,9 +1979,11 @@ def infidelity(
See Also
--------
calculate_decay_amplitudes
calculate_decay_amplitudes: Calculate the full matrix of first order terms.
error_transfer_matrix: Calculate the full process matrix.
plotting.plot_infidelity_convergence: Convenience function to plot results.
pulse_sequence.concatenate: Concatenate ``PulseSequence`` objects.
calculate_pulse_correlation_filter_function
calculate_pulse_correlation_filter_function.
References
----------
Expand All @@ -1997,11 +1999,6 @@ def infidelity(
fidelity of a quantum dynamical operation. Physics Letters,
Section A: General, Atomic and Solid State Physics, 303(4),
249–252. https://doi.org/10.1016/S0375-9601(02)01272-0
See Also
--------
error_transfer_matrix: Calculate the full process matrix.
plotting.plot_infidelity_convergence: Convenience function to plot results.
"""
# Noise operator indices
idx = util.get_indices_from_identifiers(pulse.n_oper_identifiers, n_oper_identifiers)
Expand Down
31 changes: 19 additions & 12 deletions filter_functions/plotting.py
Expand Up @@ -205,9 +205,12 @@ def plot_bloch_vector_evolution(
Whether to show the sphere (by calling :code:`b.make_sphere()`).
return_Bloch: bool, optional
Whether to return the :class:`qutip.bloch.Bloch` instance
bloch_kwargs: dict, optional
cbar_kwargs: dict, optional
A dictionary with keyword arguments to be fed into the
qutip.Bloch constructor (if *b* not given).
colorbar constructor (if ``add_cbar == True``).
**bloch_kwargs: optional
Keyword arguments to be fed into the qutip.Bloch constructor
(if *b* not given).
Returns
-------
Expand Down Expand Up @@ -240,6 +243,9 @@ def plot_bloch_vector_evolution(
if b.axes is None:
b.axes = b.fig.add_subplot(projection='3d', azim=view[0], elev=view[1])

if show:
b.make_sphere()

if n_samples is None:
# At least 100, at most 5000 points, default 10 points per smallest
# time interval
Expand All @@ -248,31 +254,32 @@ def plot_bloch_vector_evolution(
times = np.linspace(pulse.t[0], pulse.tau, n_samples)
propagators = pulse.propagator_at_arb_t(times)
points = get_bloch_vector(get_states_from_prop(propagators, psi0))

# Qutip convention: -x at +y, +y at +x
copy = points.copy()
points[0] = copy[1]
points[1] = -copy[0]

# Check the matplotlib version to see if we can draw a color gradient line. If not, draw sphere
# after adding the points using the Bloch method. If yes, we apparently need to draw the sphere
# before manually adding the line collection, otherwise there is some strange thing going on in
# notebooks and the lines are not rendered.
if version.parse(matplotlib.__version__) < version.parse('3.3.0'):
# Colored trajectory not available.
b.add_points(points, meth='l')
b.axes.plot(*points, color='b', alpha=0.75)
else:
points = points.T.reshape(-1, 1, 3)
# Qutip convention: -x at +y, +y at +x
copy = points.copy()
points[:, :, 0] = copy[:, :, 1]
points[:, :, 1] = -copy[:, :, 0]
segments = np.concatenate([points[:-1], points[1:]], axis=1)

cmap = plt.get_cmap(cmap)
segment_colors = cmap(np.linspace(0, 1, n_samples - 1))
lc = collections.LineCollection(segments[:, :, :2], colors=segment_colors)
lc = collections.LineCollection(segments[:, :, :2], colors=segment_colors, alpha=0.75)
b.axes.add_collection3d(lc, zdir='z', zs=segments[:, :, 2])

if add_cbar:
default_cbar_kwargs = dict(shrink=2/3, pad=0.05, label=r'$t$ ($\tau$)', ticks=[0, 1])
cbar_kwargs = {**default_cbar_kwargs, **(cbar_kwargs or {})}
b.fig.colorbar(cm.ScalarMappable(cmap=cmap), **cbar_kwargs)

if show:
b.make_sphere()

if return_Bloch:
return b

Expand Down

0 comments on commit 169d867

Please sign in to comment.