def position(n=default_n): r"""Position operator. Returns the n-dimensional approximation of the dimensionless position operator Q in the number basis. .. math:: Q &= \sqrt{\frac{m \omega}{\hbar}} q = (a+a^\dagger) / \sqrt{2},\\ P &= \sqrt{\frac{1}{m \hbar \omega}} p = -i (a-a^\dagger) / \sqrt{2}. (Equivalently, :math:`a = (Q + iP) / \sqrt{2}`). These operators fulfill :math:`[q, p] = i \hbar, \quad [Q, P] = i`. The Hamiltonian of the harmonic oscillator is .. math:: H = \frac{p^2}{2m} +\frac{1}{2} m \omega^2 q^2 = \frac{1}{2} \hbar \omega (P^2 +Q^2) = \hbar \omega (a^\dagger a +\frac{1}{2}). """ # Ville Bergholm 2010 a = mat(boson_ladder(n)) return array(a + a.H) / sqrt(2)
def test(): """Testing script for the harmonic oscillator module.""" from numpy.random import randn from utils import assert_o def randc(): """Random complex number.""" return randn() + 1j*randn() a = mat(boson_ladder(default_n)) alpha = randc() s = coherent_state(alpha) s0 = state(0, default_n) D = displace(alpha) assert_o((s - s0.u_propagate(D)).norm(), 0, tol) # displacement z = randc() S = squeeze(z) Q = position(); P = momentum() q = randn(); p = randn() sq = position_state(q) sp = momentum_state(p) temp = 1e-1 # the truncation accuracy is not amazing here assert_o(sq.ev(Q), q, temp) # Q, P eigenstates assert_o(sp.ev(P), p, temp) temp = ones(default_n); temp[-1] = -default_n+1 # truncation... assert_o(norm(comm(Q,P) - 1j * diag(temp)), 0, tol) # [Q, P] = i assert_o(norm(mat(P)**2 +mat(Q)**2 - 2 * a.H * a -diag(temp)), 0, tol) # P^2 +Q^2 = 2a^\dagger * a + 1
def displace(alpha, n=default_n): r"""Bosonic displacement operator. Returns the n-dimensional approximation for the bosonic displacement operator .. math:: D(\alpha) := \exp\left(\alpha a^\dagger - \alpha^* a\right) = \exp\left( i \sqrt{2} \left(Q \mathrm{Im}(\alpha) -P \mathrm{Re}(\alpha)\right)\right) in the number basis. This yields .. math:: D(\alpha) Q D^\dagger(\alpha) &= Q -\sqrt{2} \textrm{Re}(\alpha) \mathbb{I},\\ D(\alpha) P D^\dagger(\alpha) &= P -\sqrt{2} \textrm{Im}(\alpha) \mathbb{I}, and thus the displacement operator displaces the state of a harmonic oscillator in phase space. """ # Ville Bergholm 2010 if not isscalar(alpha): raise TypeError('alpha must be a scalar.') a = mat(boson_ladder(n)) return array(expm(alpha * a.H -alpha.conjugate() * a))
def momentum(n=default_n): """Momentum operator. Returns the n-dimensional approximation of the dimensionless momentum operator P in the number basis. See :func:`position`. """ # Ville Bergholm 2010 a = mat(boson_ladder(n)) return -1j*array(a - a.H) / sqrt(2)
def squeeze(z, n=default_n): r"""Bosonic squeezing operator. Returns the n-dimensional approximation for the bosonic squeezing operator .. math:: S(z) := \exp\left(\frac{1}{2} (z^* a^2 - z a^{\dagger 2})\right) = \exp\left(\frac{i}{2} \left((QP+PQ)\mathrm{Re}(z) +(P^2-Q^2)\mathrm{Im}(z)\right)\right) in the number basis. """ # Ville Bergholm 2010 if not isscalar(z): raise TypeError('z must be a scalar.') a = mat(boson_ladder(n)) return array(expm(0.5 * (z.conjugate() * (a ** 2) - z * (a.H ** 2))))