.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/quantum/measurement/plot_born_rule_measurement.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_quantum_measurement_plot_born_rule_measurement.py: The Born rule ================ Draws simulated projective position measurements from a harmonic oscillator Fock state :math:`\lvert n{=}2\rangle` via :func:`~physicskit.quantum.utils.measure.simulate_position_measurement` (inverse-CDF sampling of the Born-rule density), and shows the sample histogram converging to the analytic :math:`\lvert\psi_2(x)\rvert^2` as the sample count grows. .. GENERATED FROM PYTHON SOURCE LINES 12-28 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from physicskit.quantum.chapters.harmonic_spin import HarmonicOscillator from physicskit.quantum.utils.measure import simulate_position_measurement ho = HarmonicOscillator() n = 2 x = np.linspace(-6, 6, 2000) psi_n = ho.eigenfunction(n, x) density = psi_n**2 rng = np.random.default_rng(0) sample_sizes = [200, 5000, 200000] .. GENERATED FROM PYTHON SOURCE LINES 29-31 Histograms of simulated measurements converge to the Born-rule density ---------------------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 31-45 .. code-block:: Python fig, axes = plt.subplots(1, len(sample_sizes), figsize=(15, 4.5)) for ax, N in zip(axes, sample_sizes): samples = simulate_position_measurement(x, psi_n, n_samples=N, rng=rng) ax.hist(samples, bins=60, range=(-6, 6), density=True, alpha=0.6, label=f"{N} simulated measurements") ax.plot(x, density, color="black", lw=1.5, label=r"$|\psi_2(x)|^2$ (Born rule)") ax.set_xlabel("x") ax.set_title(f"N = {N} measurements") ax.legend(fontsize=8) fig.suptitle(r"Simulated projective measurements of $|n{=}2\rangle$ converge to $|\psi(x)|^2$") fig.tight_layout() .. image-sg:: /api/gallery/quantum/measurement/images/sphx_glr_plot_born_rule_measurement_001.png :alt: Simulated projective measurements of $|n{=}2\rangle$ converge to $|\psi(x)|^2$, N = 200 measurements, N = 5000 measurements, N = 200000 measurements :srcset: /api/gallery/quantum/measurement/images/sphx_glr_plot_born_rule_measurement_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 46-47 Quantitative check: the histogram approaches the analytic density. .. GENERATED FROM PYTHON SOURCE LINES 47-55 .. code-block:: Python samples_large = simulate_position_measurement(x, psi_n, n_samples=500000, rng=rng) hist, edges = np.histogram(samples_large, bins=100, range=(-6, 6), density=True) centers = 0.5 * (edges[1:] + edges[:-1]) analytic = np.interp(centers, x, density) mean_abs_err = np.mean(np.abs(hist - analytic)) print(f"mean |histogram - |psi|^2| at N=500000: {mean_abs_err:.5f}") .. rst-class:: sphx-glr-script-out .. code-block:: none mean |histogram - |psi|^2| at N=500000: 0.00084 .. GENERATED FROM PYTHON SOURCE LINES 56-65 Convergence to the Born rule across Fock states and sample sizes -------------------------------------------------------------------- The single-state convergence above is one slice of a broader picture: :func:`~physicskit.quantum.utils.measure.simulate_position_measurement` applied to several Fock states :math:`|n\rangle`, each at several sample counts, shows the same histogram-vs-analytic error shrinking with :math:`N` regardless of the state's shape -- a 2D map instead of a single 1D convergence curve. .. GENERATED FROM PYTHON SOURCE LINES 65-88 .. code-block:: Python n_grid = np.arange(0, 6) N_grid = np.array([50, 200, 1000, 5000, 20000, 100000]) error_map = np.zeros((len(n_grid), len(N_grid))) for i, n_state in enumerate(n_grid): psi_state = ho.eigenfunction(int(n_state), x) density_state = psi_state**2 for j, N in enumerate(N_grid): samples = simulate_position_measurement(x, psi_state, n_samples=int(N), rng=rng) h, e = np.histogram(samples, bins=60, range=(-6, 6), density=True) c = 0.5 * (e[1:] + e[:-1]) a = np.interp(c, x, density_state) error_map[i, j] = np.mean(np.abs(h - a)) fig2, ax2 = plt.subplots(figsize=(7, 4.5)) im = ax2.pcolormesh(np.arange(len(N_grid)), n_grid, np.log10(error_map), shading="auto", cmap="viridis_r") ax2.set_xticks(np.arange(len(N_grid))) ax2.set_xticklabels([str(N) for N in N_grid]) ax2.set_xlabel("N (simulated measurements)") ax2.set_ylabel("Fock state n") ax2.set_title("log10(mean |histogram - |psi_n|^2|) vs. state and sample size") fig2.colorbar(im, ax=ax2, label=r"$\log_{10}$(mean abs. error)") fig2.tight_layout() .. image-sg:: /api/gallery/quantum/measurement/images/sphx_glr_plot_born_rule_measurement_002.png :alt: log10(mean |histogram - |psi_n|^2|) vs. state and sample size :srcset: /api/gallery/quantum/measurement/images/sphx_glr_plot_born_rule_measurement_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.265 seconds) .. _sphx_glr_download_api_gallery_quantum_measurement_plot_born_rule_measurement.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_born_rule_measurement.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_born_rule_measurement.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_born_rule_measurement.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_