Harnessing the Power of the wavefunctions to navigate the quantum realm.🚀🌌
This project presents an optimized approach for calculating the position wave functions of a Fock state of a quantum harmonic oscillator, with applications in Photonic Quantum Computing simulations. Leveraging Numba [1] and Cython [2], this approach outperforms the Mr Mustard package [3, 4] in computing a single wave function value at a single position and at multiple positions.
- 📑 Table of Contents
- ✨ Advantages
- 🛠️ Setup
- 🎨 Examples
- 🌊 The Wavefunction
- 🔁 The Wavefunction Recurrence
- ⚡️The Numba Module - Hybrid Solution
- ⚡️ The Numba Module - Arguments
- ⚙️ The Cython Module
- 📖 References
- 🤝 Contributing
- 📜 License
- 📬 Contact
- Highly Efficient: This package includes two fixed-point modules focused on speed. One is implemented using Numba, an open-source Just-in-Time (JIT) compiler, and the other module is implemented in Cython, a programming language that combines the ease of use of Python with the speed of C.
- Highly Accurate: The functions in this package have precision next to the precision of Wolfram Mathematica and MATLAB. In addition, there is a module for calculating wave functions with arbitrary precision using the mpmath package.
- Past response cache: This package provides a caching module designed to enhance the performance of functions that take multiple positions of a NumPy array as input. This module stores previously computed results by leveraging Python's functools.lru_cache, eliminating the need for redundant calculations. This caching module is inspired by the caching module from Mr. Mustard, a package from the photonic quantum computing company Xanadu.
To use this package, simply run the following command in the command line:
pip install fast-wave
The functions psi_n_multiple_fock_multiple_position
calculate the values of the wavefunction
Inputs:
-
n
: An integer specifying the maximum Fock state ($n$ ). -
X_m
: A 1Dnumpy
array withm
positions, representing the input values where$\psi_{,0\rightarrow n}$ will be evaluated. For example,np.array([1.0, 2.0])
has dimension$(m,)$ , where$m=2$ in this case.
Outputs:
-
numpy
Matrix: The output has dimensions$(n+1) \times m$ , where:-
$n+1$ : Corresponds to the Fock states$[0, 1, ..., n]$ . -
$m$ : Represents the positions given inX_m
.
-
Demonstration:
Using the provided inputs:
>>> import fast_wave.wavefunction_numba as wn
Functionality Test Passed: True
>>> import fast_wave.wavefunction_cython as wc
>>> import numpy as np
>>> wn.psi_n_multiple_fock_multiple_position(1,np.array([1.0 ,2.0]))
array([[0.45558067, 0.10165379],
[0.64428837, 0.28752033]])
>>> wc.psi_n_multiple_fock_multiple_position(1,np.array([1.0 ,2.0]))
array([[0.45558067, 0.10165379],
[0.64428837, 0.28752033]])
Explanation of the Output:
- For
n=1
andX_m = np.array([1.0, 2.0])
:- The output matrix has dimensions
$(n+1) \times m = 2 \times 2$ . - The first row contains
$\psi_{0}(x_1)$ and$\psi_{0}(x_2)$ . - The second row contains
$\psi_{1}(x_1)$ and$\psi_{1}(x_2)$ .
- The output matrix has dimensions
There are other examples in the examples folder: Speed Tests: Numba & Cython; Precision Tests: mpmath. In the first one there is a comparison with the Mr Mustard package.
The wavefunction,
The wavefunction is the solution to the Schrödinger equation, a fundamental equation in quantum mechanics:
where
where
The term
By solving equation (4), we obtain a family of energy eigenfunctions defined as follows [5]:
where
Wavefunctions and energies for different
The energy eigenfunction for an energy state
where
When defining the dimensionless variable
This demonstrates that the wavefunction of a Quantum Harmonic Oscillator can be represented in a more dimensionless form, known as the Hermite function, it is also sometimes referred to as the Gauss-Hermite function. (equation
This type of representation for the wavefunction enables the modeling of wave functions for Fock states, which are energy eigenstates with a well-defined number of particles. When the particles are considered photons,
where
The wavefunction
In essence, Mr Mustard's strategy is to use the Renormalized Hermite Polynomial [3, 4] for the computation of the wavefunction of a quantum harmonic oscillator. Below, we show the recurrence for calculating the Renormalized Hermite Polynomial, as well as the method for calculating it using the traditional Hermite polynomial:
When we use this polynomial in calculating the wavefunction of a Quantum Harmonic Oscillator, the equation is as follows:
In this package, we implemented a recurrence based on the recursive solution to the wavefunction of the Quantum Harmonic Oscillator presented in the work of José Maria Pérez-Jordá [11]. The recurrence we implemented was for
We use a hybrid solution with two algorithms for calculating the wave function for calculating a single Fock wave function's values at multiple positions (Single Fock and Multiple Position) (psi_n_single_fock_multiple_position
). For
where psi_n_single_fock_single_position
) problems, though it offers no computational advantage in these cases. Additionally, there is an argument named CS_matrix for these Single Fock functions, set to True to enable the use of this matrix. In other words, you can use only the recurrence relation for the wave function at any value. The use of this coefficient matrix is limited to values up to 60 (determined empirically), as beyond this point, the function may encounter precision errors, resulting in incoherent outputs [13].
For this algorithm to perform as efficiently as possible, Numba's Just-in-Time compilation is used in conjunction with lru_cache (Least Recently Used - Cache Management). The following arguments were used in the @nb.jit decorator:
- nopython=True: This argument forces the Numba compiler to operate in "nopython" mode, which means that all the code within the function must be compilable to pure machine code without falling back to the Python interpreter. This results in significant performance improvements by eliminating the overhead of the Python interpreter.
- looplift=True: This argument allows Numba to "lift" loops out of "nopython" mode. That is, if there are loops in the code that cannot be compiled in "nopython" mode, Numba will try to move them outside of the compiled part and execute them as normal Python code.
- nogil=True: This argument releases the Python Global Interpreter Lock (GIL) while the function is executing. It is useful for allowing the Numba-compiled code to run in parallel with other Python threads, increasing performance in multi-threaded programs.
- boundscheck=False: Disables array bounds checking. Normally, Numba checks if array indices are within valid bounds. Disabling this check can increase performance but may result in undefined behavior if there are out-of-bounds accesses.
- cache=True: Enables caching of the compiled function. The first time the function is compiled, Numba stores the compiled version in a cache. On subsequent executions, Numba can reuse the compiled version from the cache instead of recompiling the function, reducing the function's startup time.
The Cython module includes compiled files for Linux (.so) and Windows (.pyd), which allows it to be used in Google Colab (Linux). Additionally, this module supports three versions of Python 3: 3.10, 3.11, and 3.12. All these files are placed in the package folder upon installation. The source code of the Cython module is available in the repository in .pyx format. In the functions of the Cython module, some decorators are used to increase speed:
- @cython.nogil: This decorator allows a Cython function to release the Global Interpreter Lock (GIL), making it possible to execute that block of code concurrently in multiple threads.
- @cython.cfunc: This decorator tells Cython to treat the function as a C function, meaning it can be called from other Cython or C code, not just Python. The function will have C-level calling conventions.
- @cython.locals(...): Declares local variable types to optimize performance.
- @cython.boundscheck(False): Disables bounds checking for arrays/lists to boost speed, but at the cost of safety.
Our journey through the quantum realm is inspired by the following:
- Lam, S. K., Pitrou, A., & Seibert, S. (2015). Numba: A LLVM-based Python JIT compiler. In Proceedings of the Second Workshop on the LLVM Compiler Infrastructure in HPC (LLVM '15) (pp. 7-12). Association for Computing Machinery. https://doi.org/10.1145/2833157.2833162
- Behnel, S., Bradshaw, R., Citro, C., Dalcin, L., Seljebotn, D. S., & Smith, K. (2011). Cython: The best of both worlds. Computing in Science & Engineering, 13(2), 31-39. https://doi.org/10.1109/MCSE.2010.118
- Yao, Y., Miatto, F., & Quesada, N. (2024). Riemannian optimization of photonic quantum circuits in phase and Fock space [Preprint]. arXiv:2209.06069. https://doi.org/10.21468/SciPostPhys.17.3.082
- Miatto, F. M., & Quesada, N. (2020). Fast optimization of parametrized quantum optical circuits (Quantum, 4, 366). https://doi.org/10.22331/q-2020-11-30-366
- Bowers, P. L. (2020). Lectures on Quantum Mechanics: A Primer for Mathematicians. Cambridge University Press. ISBN: 1108429769 (9781108429764)
- Aerts, D., Beltran, L. Quantum Structure in Cognition: Human Language as a Boson Gas of Entangled Words. Found Sci 25, 755–802 (2020). https://doi.org/10.1007/s10699-019-09633-4
- Beiser, A. (2003). Concepts of Modern Physics. 6th ed. McGraw Hill. ISBN: 0072448482 (9780072448481)
- Celeghini, E., Gadella, M., & del Olmo, M. A. (2021). Hermite functions and Fourier series. Symmetry, 13(5), Article 853. https://doi.org/10.3390/sym13050853
- Schleich, W. P. (2001). Quantum optics in phase space. Wiley-VCH. ISBN:352729435X (9783527294350)
- Leonhardt, U. (2010). Essential Quantum Optics: From Quantum Measurements to Black Holes. Cambridge: Cambridge University Press. ISBN: 0521869781 (9780521869782)
- Pérez-Jordá, J. M. (2017). On the recursive solution of the quantum harmonic oscillator. European Journal of Physics, 39(1), 015402. https://doi.org/10.1088/1361-6404/aa9584
- Olver, F. W. J., & Maximon, L. C. (2010). NIST Handbook of Mathematical Functions. Cambridge University Press. ISBN: 0521192250 (9780521192255)
- Cordeiro, M., Bezerra, I. P., & Vasconcelos, H. H. M. (2024). Efficient computation of the wave function ψn(x) using Hermite coefficient matrix in Python. In 7º Workshop Escola de Computação e Informação Quântica (7ª WECIQ) (pp. 56-60). CEFET/RJ.
Contributions, whether filing an issue, proposing improvements, or submitting a pull request, are welcome! Please feel free to explore, ask questions, and share your ideas.
This project is available under the BSD 3-Clause License. See the LICENSE file for more details.
If you have any questions or want to reach out to the team, please send me an email at matheusgomescord@gmail.com.