def test_normal_order(): a = BosonOp('a') b = BosonOp('b') c = FermionOp('c') d = FermionOp('d') assert normal_order(a * Dagger(a)) == Dagger(a) * a assert normal_order(Dagger(a) * a) == Dagger(a) * a assert normal_order(a * Dagger(a)**2) == Dagger(a)**2 * a assert normal_order(c * Dagger(c)) == -Dagger(c) * c assert normal_order(Dagger(c) * c) == Dagger(c) * c assert normal_order(c * Dagger(c)**2) == Dagger(c)**2 * c
def test_tensorproduct(): a = BosonOp("a") b = BosonOp("b") ket1 = TensorProduct(BosonFockKet(1), BosonFockKet(2)) ket2 = TensorProduct(BosonFockKet(0), BosonFockKet(0)) ket3 = TensorProduct(BosonFockKet(0), BosonFockKet(2)) bra1 = TensorProduct(BosonFockBra(0), BosonFockBra(0)) bra2 = TensorProduct(BosonFockBra(1), BosonFockBra(2)) assert qapply(TensorProduct(a, b**2) * ket1) == sqrt(2) * ket2 assert qapply(TensorProduct(a, Dagger(b) * b) * ket1) == 2 * ket3 assert qapply(bra1 * TensorProduct(a, b * b), dagger=True) == sqrt(2) * bra2 assert qapply(bra2 * ket1).doit() == TensorProduct(1, 1) assert qapply(TensorProduct(a, b * b) * ket1) == sqrt(2) * ket2 assert qapply(Dagger(TensorProduct(a, b * b) * ket1), dagger=True) == sqrt(2) * Dagger(ket2)
def test_normal_ordered_form(): a = BosonOp('a') b = BosonOp('b') c = FermionOp('c') d = FermionOp('d') assert normal_ordered_form(Dagger(a) * a) == Dagger(a) * a assert normal_ordered_form(a * Dagger(a)) == 1 + Dagger(a) * a assert normal_ordered_form(a ** 2 * Dagger(a)) == \ 2 * a + Dagger(a) * a ** 2 assert normal_ordered_form(a ** 3 * Dagger(a)) == \ 3 * a ** 2 + Dagger(a) * a ** 3 assert normal_ordered_form(Dagger(c) * c) == Dagger(c) * c assert normal_ordered_form(c * Dagger(c)) == 1 - Dagger(c) * c assert normal_ordered_form(c**2 * Dagger(c)) == Dagger(c) * c**2 assert normal_ordered_form(c ** 3 * Dagger(c)) == \ c ** 2 - Dagger(c) * c ** 3
def test_normal_order(): a = BosonOp("a") c = FermionOp("c") assert normal_order(a * Dagger(a)) == Dagger(a) * a assert normal_order(Dagger(a) * a) == Dagger(a) * a assert normal_order(a * Dagger(a)**2) == Dagger(a)**2 * a assert normal_order(c * Dagger(c)) == -Dagger(c) * c assert normal_order(Dagger(c) * c) == Dagger(c) * c assert normal_order(c * Dagger(c)**2) == Dagger(c)**2 * c
def test_normal_ordered_form(): a = BosonOp("a") c = FermionOp("c") assert normal_ordered_form(Dagger(a) * a) == Dagger(a) * a assert normal_ordered_form(a * Dagger(a)) == 1 + Dagger(a) * a assert normal_ordered_form(a**2 * Dagger(a)) == 2 * a + Dagger(a) * a**2 assert normal_ordered_form(a**3 * Dagger(a)) == 3 * a**2 + Dagger(a) * a**3 assert normal_ordered_form(Dagger(c) * c) == Dagger(c) * c assert normal_ordered_form(c * Dagger(c)) == 1 - Dagger(c) * c assert normal_ordered_form(c**2 * Dagger(c)) == Dagger(c) * c**2 assert normal_ordered_form(c**3 * Dagger(c)) == c**2 - Dagger(c) * c**3
def test_bosonoperator(): a = BosonOp('a') b = BosonOp('b') assert isinstance(a, BosonOp) assert isinstance(Dagger(a), BosonOp) assert a.is_annihilation assert not Dagger(a).is_annihilation assert BosonOp("a") == BosonOp("a", True) assert BosonOp("a") != BosonOp("c") assert BosonOp("a", True) != BosonOp("a", False) assert Commutator(a, Dagger(a)).doit() == 1 assert Commutator(a, Dagger(b)).doit() == a * Dagger(b) - Dagger(b) * a
def test_boson_states(): a = BosonOp("a") # Fock states n = 3 assert (BosonFockBra(0) * BosonFockKet(1)).doit() == 0 assert (BosonFockBra(1) * BosonFockKet(1)).doit() == 1 assert qapply(BosonFockBra(n) * Dagger(a)**n * BosonFockKet(0)) \ == sqrt(prod(range(1, n+1))) # Coherent states alpha1, alpha2 = 1.2, 4.3 assert (BosonCoherentBra(alpha1) * BosonCoherentKet(alpha1)).doit() == 1 assert (BosonCoherentBra(alpha2) * BosonCoherentKet(alpha2)).doit() == 1 assert abs((BosonCoherentBra(alpha1) * BosonCoherentKet(alpha2)).doit() - exp((alpha1 - alpha2) ** 2 * Rational(-1, 2))) < 1e-12 assert qapply(a * BosonCoherentKet(alpha1)) == \ alpha1 * BosonCoherentKet(alpha1)