Skip to content

Commit 169d867

Browse files
authored
Bloch sphere in notebook (#81)
* Fix notebook not rendering lines on Blcoh sphere * Update notebooks with colored bloch trajectories * Add cbar_kwargs to docstring * Fix docstring in infidelity
1 parent 0b6dd48 commit 169d867

9 files changed

+27406
-44305
lines changed

doc/source/examples/advanced_concatenation.ipynb

Lines changed: 3925 additions & 13 deletions
Large diffs are not rendered by default.

doc/source/examples/calculating_quantum_processes.ipynb

Lines changed: 2520 additions & 78 deletions
Large diffs are not rendered by default.

doc/source/examples/extending_pulses.ipynb

Lines changed: 1063 additions & 17 deletions
Large diffs are not rendered by default.

doc/source/examples/getting_started.ipynb

Lines changed: 3944 additions & 15 deletions
Large diffs are not rendered by default.

doc/source/examples/periodic_driving.ipynb

Lines changed: 6315 additions & 40 deletions
Large diffs are not rendered by default.

doc/source/examples/quantum_fourier_transform.ipynb

Lines changed: 8629 additions & 44116 deletions
Large diffs are not rendered by default.

doc/source/examples/qutip_integration.ipynb

Lines changed: 987 additions & 7 deletions
Large diffs are not rendered by default.

filter_functions/numeric.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,9 +1979,11 @@ def infidelity(
19791979
19801980
See Also
19811981
--------
1982-
calculate_decay_amplitudes
1982+
calculate_decay_amplitudes: Calculate the full matrix of first order terms.
1983+
error_transfer_matrix: Calculate the full process matrix.
1984+
plotting.plot_infidelity_convergence: Convenience function to plot results.
19831985
pulse_sequence.concatenate: Concatenate ``PulseSequence`` objects.
1984-
calculate_pulse_correlation_filter_function
1986+
calculate_pulse_correlation_filter_function.
19851987
19861988
References
19871989
----------
@@ -1997,11 +1999,6 @@ def infidelity(
19971999
fidelity of a quantum dynamical operation. Physics Letters,
19982000
Section A: General, Atomic and Solid State Physics, 303(4),
19992001
249–252. https://doi.org/10.1016/S0375-9601(02)01272-0
2000-
2001-
See Also
2002-
--------
2003-
error_transfer_matrix: Calculate the full process matrix.
2004-
plotting.plot_infidelity_convergence: Convenience function to plot results.
20052002
"""
20062003
# Noise operator indices
20072004
idx = util.get_indices_from_identifiers(pulse.n_oper_identifiers, n_oper_identifiers)

filter_functions/plotting.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,12 @@ def plot_bloch_vector_evolution(
205205
Whether to show the sphere (by calling :code:`b.make_sphere()`).
206206
return_Bloch: bool, optional
207207
Whether to return the :class:`qutip.bloch.Bloch` instance
208-
bloch_kwargs: dict, optional
208+
cbar_kwargs: dict, optional
209209
A dictionary with keyword arguments to be fed into the
210-
qutip.Bloch constructor (if *b* not given).
210+
colorbar constructor (if ``add_cbar == True``).
211+
**bloch_kwargs: optional
212+
Keyword arguments to be fed into the qutip.Bloch constructor
213+
(if *b* not given).
211214
212215
Returns
213216
-------
@@ -240,6 +243,9 @@ def plot_bloch_vector_evolution(
240243
if b.axes is None:
241244
b.axes = b.fig.add_subplot(projection='3d', azim=view[0], elev=view[1])
242245

246+
if show:
247+
b.make_sphere()
248+
243249
if n_samples is None:
244250
# At least 100, at most 5000 points, default 10 points per smallest
245251
# time interval
@@ -248,31 +254,32 @@ def plot_bloch_vector_evolution(
248254
times = np.linspace(pulse.t[0], pulse.tau, n_samples)
249255
propagators = pulse.propagator_at_arb_t(times)
250256
points = get_bloch_vector(get_states_from_prop(propagators, psi0))
251-
257+
# Qutip convention: -x at +y, +y at +x
258+
copy = points.copy()
259+
points[0] = copy[1]
260+
points[1] = -copy[0]
261+
262+
# Check the matplotlib version to see if we can draw a color gradient line. If not, draw sphere
263+
# after adding the points using the Bloch method. If yes, we apparently need to draw the sphere
264+
# before manually adding the line collection, otherwise there is some strange thing going on in
265+
# notebooks and the lines are not rendered.
252266
if version.parse(matplotlib.__version__) < version.parse('3.3.0'):
253267
# Colored trajectory not available.
254-
b.add_points(points, meth='l')
268+
b.axes.plot(*points, color='b', alpha=0.75)
255269
else:
256270
points = points.T.reshape(-1, 1, 3)
257-
# Qutip convention: -x at +y, +y at +x
258-
copy = points.copy()
259-
points[:, :, 0] = copy[:, :, 1]
260-
points[:, :, 1] = -copy[:, :, 0]
261271
segments = np.concatenate([points[:-1], points[1:]], axis=1)
262272

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

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

273-
if show:
274-
b.make_sphere()
275-
276283
if return_Bloch:
277284
return b
278285

0 commit comments

Comments
 (0)