def test_sum_instantiator(): """Test use of Sum instantiator.""" i = IdxSym('i') j = IdxSym('j') ket_i = BasisKet(FockIndex(i), hs=0) ket_j = BasisKet(FockIndex(j), hs=0) A_i = OperatorSymbol(StrLabel(IndexedBase('A')[i]), hs=0) hs0 = LocalSpace('0') sum = Sum(i)(ket_i) ful = KetIndexedSum(ket_i, ranges=IndexOverFockSpace(i, hs=hs0)) assert sum == ful assert sum == Sum(i, hs0)(ket_i) assert sum == Sum(i, hs=hs0)(ket_i) sum = Sum(i, 1, 10)(ket_i) ful = KetIndexedSum(ket_i, ranges=IndexOverRange(i, 1, 10)) assert sum == ful assert sum == Sum(i, 1, 10, 1)(ket_i) assert sum == Sum(i, 1, to=10, step=1)(ket_i) assert sum == Sum(i, 1, 10, step=1)(ket_i) sum = Sum(i, (1, 2, 3))(ket_i) ful = KetIndexedSum(ket_i, ranges=IndexOverList(i, (1, 2, 3))) assert sum == KetIndexedSum(ket_i, ranges=IndexOverList(i, (1, 2, 3))) assert sum == Sum(i, [1, 2, 3])(ket_i) sum = Sum(i)(Sum(j)(ket_i * ket_j.dag())) ful = OperatorIndexedSum( ket_i * ket_j.dag(), ranges=(IndexOverFockSpace(i, hs0), IndexOverFockSpace(j, hs0)), ) assert sum == ful
def test_unicode_ket_operations(): """Test the unicode representation of ket operations""" hs1 = LocalSpace('q_1', basis=('g', 'e')) hs2 = LocalSpace('q_2', basis=('g', 'e')) ket_g1 = BasisKet('g', hs=hs1) ket_e1 = BasisKet('e', hs=hs1) ket_g2 = BasisKet('g', hs=hs2) ket_e2 = BasisKet('e', hs=hs2) psi1 = KetSymbol("Psi_1", hs=hs1) psi2 = KetSymbol("Psi_2", hs=hs1) phi = KetSymbol("Phi", hs=hs2) A = OperatorSymbol("A_0", hs=hs1) gamma = symbols('gamma', positive=True) alpha = symbols('alpha') beta = symbols('beta') phase = exp(-I * gamma) i = IdxSym('i') assert unicode(psi1 + psi2) == '|Ψ₁⟩^(q₁) + |Ψ₂⟩^(q₁)' assert unicode(psi1 * phi) == '|Ψ₁⟩^(q₁) ⊗ |Φ⟩^(q₂)' assert unicode(phase * psi1) == 'exp(-ⅈ γ) |Ψ₁⟩^(q₁)' assert unicode((alpha + 1) * KetSymbol('Psi', hs=0)) == '(α + 1) |Ψ⟩⁽⁰⁾' assert (unicode( A * psi1) == 'A\u0302_0^(q\u2081) |\u03a8\u2081\u27e9^(q\u2081)') # Â_0^(q₁) |Ψ₁⟩^(q₁) assert unicode(BraKet(psi1, psi2)) == '⟨Ψ₁|Ψ₂⟩^(q₁)' expr = BraKet(KetSymbol('Psi_1', alpha, hs=hs1), KetSymbol('Psi_2', beta, hs=hs1)) assert unicode(expr) == '⟨Ψ₁(α)|Ψ₂(β)⟩^(q₁)' assert unicode(ket_e1.dag() * ket_e1) == '1' assert unicode(ket_g1.dag() * ket_e1) == '0' assert unicode(KetBra(psi1, psi2)) == '|Ψ₁⟩⟨Ψ₂|^(q₁)' expr = KetBra(KetSymbol('Psi_1', alpha, hs=hs1), KetSymbol('Psi_2', beta, hs=hs1)) assert unicode(expr) == '|Ψ₁(α)⟩⟨Ψ₂(β)|^(q₁)' bell1 = (ket_e1 * ket_g2 - I * ket_g1 * ket_e2) / sqrt(2) bell2 = (ket_e1 * ket_e2 - ket_g1 * ket_g2) / sqrt(2) assert unicode(bell1) == '1/√2 (|eg⟩^(q₁⊗q₂) - ⅈ |ge⟩^(q₁⊗q₂))' assert (unicode(BraKet.create( bell1, bell2)) == r'1/2 (⟨eg|^(q₁⊗q₂) + ⅈ ⟨ge|^(q₁⊗q₂)) (|ee⟩^(q₁⊗q₂) - ' r'|gg⟩^(q₁⊗q₂))') assert (unicode(KetBra.create( bell1, bell2)) == r'1/2 (|eg⟩^(q₁⊗q₂) - ⅈ |ge⟩^(q₁⊗q₂))(⟨ee|^(q₁⊗q₂) - ' r'⟨gg|^(q₁⊗q₂))') assert (unicode( KetBra.create(bell1, bell2), show_hs_label=False) == r'1/2 (|eg⟩ - ⅈ |ge⟩)(⟨ee| - ⟨gg|)') expr = KetBra(KetSymbol('Psi', hs=0), BasisKet(FockIndex(i), hs=0)) assert unicode(expr) == "|Ψ⟩⟨i|⁽⁰⁾" expr = KetBra(BasisKet(FockIndex(i), hs=0), KetSymbol('Psi', hs=0)) assert unicode(expr) == "|i⟩⟨Ψ|⁽⁰⁾" expr = BraKet(KetSymbol('Psi', hs=0), BasisKet(FockIndex(i), hs=0)) assert unicode(expr) == "⟨Ψ|i⟩⁽⁰⁾" expr = BraKet(BasisKet(FockIndex(i), hs=0), KetSymbol('Psi', hs=0)) assert unicode(expr) == "⟨i|Ψ⟩⁽⁰⁾"
def state_exprs(): """Prepare a list of state algebra expressions""" hs1 = LocalSpace('q1', basis=('g', 'e')) hs2 = LocalSpace('q2', basis=('g', 'e')) ket_g1 = BasisKet('g', hs=hs1) ket_e1 = BasisKet('e', hs=hs1) ket_g2 = BasisKet('g', hs=hs2) ket_e2 = BasisKet('e', hs=hs2) psi1 = KetSymbol("Psi_1", hs=hs1) psi1_l = KetSymbol("Psi_1", hs=hs1) psi2 = KetSymbol("Psi_2", hs=hs1) psi2 = KetSymbol("Psi_2", hs=hs1) psi3 = KetSymbol("Psi_3", hs=hs1) phi = KetSymbol("Phi", hs=hs2) phi_l = KetSymbol("Phi", hs=hs2) A = OperatorSymbol("A_0", hs=hs1) gamma = symbols('gamma') phase = exp(-I * gamma) bell1 = (ket_e1 * ket_g2 - I * ket_g1 * ket_e2) / sqrt(2) bell2 = (ket_e1 * ket_e2 - ket_g1 * ket_g2) / sqrt(2) bra_psi1 = KetSymbol("Psi_1", hs=hs1).dag() bra_psi1_l = KetSymbol("Psi_1", hs=hs1).dag() bra_psi2 = KetSymbol("Psi_2", hs=hs1).dag() bra_psi2 = KetSymbol("Psi_2", hs=hs1).dag() bra_phi_l = KetSymbol("Phi", hs=hs2).dag() return [ KetSymbol('Psi', hs=hs1), KetSymbol('Psi', hs=1), KetSymbol('Psi', hs=(1, 2)), KetSymbol('Psi', symbols('alpha'), symbols('beta'), hs=(1, 2)), KetSymbol('Psi', hs=1), ZeroKet, TrivialKet, BasisKet('e', hs=hs1), BasisKet('excited', hs=LocalSpace(1, basis=('ground', 'excited'))), BasisKet(1, hs=1), CoherentStateKet(2.0, hs=1), CoherentStateKet(2.0, hs=1).to_fock_representation(), Bra(KetSymbol('Psi', hs=hs1)), Bra(KetSymbol('Psi', hs=1)), Bra(KetSymbol('Psi', hs=(1, 2))), Bra(KetSymbol('Psi', hs=hs1 * hs2)), KetSymbol('Psi', hs=1).dag(), Bra(ZeroKet), Bra(TrivialKet), BasisKet('e', hs=hs1).adjoint(), BasisKet(1, hs=1).adjoint(), CoherentStateKet(2.0, hs=1).dag(), psi1 + psi2, psi1 - psi2 + psi3, psi1 * phi, psi1_l * phi_l, phase * psi1, A * psi1, BraKet(psi1, psi2), ket_e1.dag() * ket_e1, ket_g1.dag() * ket_e1, KetBra(psi1, psi2), bell1, BraKet.create(bell1, bell2), KetBra.create(bell1, bell2), (psi1 + psi2).dag(), bra_psi1 + bra_psi2, bra_psi1_l * bra_phi_l, Bra(phase * psi1), (A * psi1).dag(), ]
def test_ascii_ket_operations(): """Test the ascii representation of ket operations""" hs1 = LocalSpace('q_1', basis=('g', 'e')) hs2 = LocalSpace('q_2', basis=('g', 'e')) ket_g1 = BasisKet('g', hs=hs1) ket_e1 = BasisKet('e', hs=hs1) ket_g2 = BasisKet('g', hs=hs2) ket_e2 = BasisKet('e', hs=hs2) psi1 = KetSymbol("Psi_1", hs=hs1) psi2 = KetSymbol("Psi_2", hs=hs1) psi2 = KetSymbol("Psi_2", hs=hs1) psi3 = KetSymbol("Psi_3", hs=hs1) phi = KetSymbol("Phi", hs=hs2) A = OperatorSymbol("A_0", hs=hs1) gamma = symbols('gamma', positive=True) alpha = symbols('alpha') beta = symbols('beta') phase = exp(-I * gamma) i = IdxSym('i') assert ascii(psi1 + psi2) == '|Psi_1>^(q_1) + |Psi_2>^(q_1)' assert (ascii(psi1 - psi2 + psi3) == '|Psi_1>^(q_1) - |Psi_2>^(q_1) + |Psi_3>^(q_1)') with pytest.raises(UnequalSpaces): psi1 + phi with pytest.raises(AttributeError): (psi1 * phi).label assert ascii(psi1 * phi) == '|Psi_1>^(q_1) * |Phi>^(q_2)' with pytest.raises(OverlappingSpaces): psi1 * psi2 assert ascii(phase * psi1) == 'exp(-I*gamma) * |Psi_1>^(q_1)' assert (ascii( (alpha + 1) * KetSymbol('Psi', hs=0)) == '(alpha + 1) * |Psi>^(0)') assert ascii(A * psi1) == 'A_0^(q_1) |Psi_1>^(q_1)' with pytest.raises(SpaceTooLargeError): A * phi assert ascii(BraKet(psi1, psi2)) == '<Psi_1|Psi_2>^(q_1)' expr = BraKet(KetSymbol('Psi_1', alpha, hs=hs1), KetSymbol('Psi_2', beta, hs=hs1)) assert ascii(expr) == '<Psi_1(alpha)|Psi_2(beta)>^(q_1)' assert ascii(psi1.dag() * psi2) == '<Psi_1|Psi_2>^(q_1)' assert ascii(ket_e1.dag() * ket_e1) == '1' assert ascii(ket_g1.dag() * ket_e1) == '0' assert ascii(KetBra(psi1, psi2)) == '|Psi_1><Psi_2|^(q_1)' expr = KetBra(KetSymbol('Psi_1', alpha, hs=hs1), KetSymbol('Psi_2', beta, hs=hs1)) assert ascii(expr) == '|Psi_1(alpha)><Psi_2(beta)|^(q_1)' bell1 = (ket_e1 * ket_g2 - I * ket_g1 * ket_e2) / sqrt(2) bell2 = (ket_e1 * ket_e2 - ket_g1 * ket_g2) / sqrt(2) assert ascii(bell1) == '1/sqrt(2) * (|eg>^(q_1*q_2) - I * |ge>^(q_1*q_2))' assert ascii(bell2) == '1/sqrt(2) * (|ee>^(q_1*q_2) - |gg>^(q_1*q_2))' expr = BraKet.create(bell1, bell2) expected = ( r'1/2 * (<eg|^(q_1*q_2) + I * <ge|^(q_1*q_2)) * (|ee>^(q_1*q_2) ' r'- |gg>^(q_1*q_2))') assert ascii(expr) == expected assert (ascii(KetBra.create(bell1, bell2)) == '1/2 * (|eg>^(q_1*q_2) - I * |ge>^(q_1*q_2))(<ee|^(q_1*q_2) ' '- <gg|^(q_1*q_2))') expr = KetBra(KetSymbol('Psi', hs=0), BasisKet(FockIndex(i), hs=0)) assert ascii(expr) == "|Psi><i|^(0)" expr = KetBra(BasisKet(FockIndex(i), hs=0), KetSymbol('Psi', hs=0)) assert ascii(expr) == "|i><Psi|^(0)" expr = BraKet(KetSymbol('Psi', hs=0), BasisKet(FockIndex(i), hs=0)) assert ascii(expr) == "<Psi|i>^(0)" expr = BraKet(BasisKet(FockIndex(i), hs=0), KetSymbol('Psi', hs=0)) assert ascii(expr) == "<i|Psi>^(0)"
def test_tex_ket_operations(): """Test the tex representation of ket operations""" hs1 = LocalSpace('q_1', basis=('g', 'e')) hs2 = LocalSpace('q_2', basis=('g', 'e')) ket_g1 = BasisKet('g', hs=hs1) ket_e1 = BasisKet('e', hs=hs1) ket_g2 = BasisKet('g', hs=hs2) ket_e2 = BasisKet('e', hs=hs2) psi1 = KetSymbol("Psi_1", hs=hs1) psi2 = KetSymbol("Psi_2", hs=hs1) psi2 = KetSymbol("Psi_2", hs=hs1) psi3 = KetSymbol("Psi_3", hs=hs1) phi = KetSymbol("Phi", hs=hs2) A = OperatorSymbol("A_0", hs=hs1) gamma = symbols('gamma', positive=True) alpha = symbols('alpha') beta = symbols('beta') phase = exp(-I * gamma) i = IdxSym('i') assert (latex(psi1 + psi2) == r'\left\lvert \Psi_{1} \right\rangle^{(q_{1})} + ' r'\left\lvert \Psi_{2} \right\rangle^{(q_{1})}') assert (latex(psi1 - psi2 + psi3) == r'\left\lvert \Psi_{1} \right\rangle^{(q_{1})} - ' r'\left\lvert \Psi_{2} \right\rangle^{(q_{1})} + ' r'\left\lvert \Psi_{3} \right\rangle^{(q_{1})}') assert (latex( psi1 * phi) == r'\left\lvert \Psi_{1} \right\rangle^{(q_{1})} \otimes ' r'\left\lvert \Phi \right\rangle^{(q_{2})}') assert (latex(phase * psi1) == r'e^{- i \gamma} \left\lvert \Psi_{1} \right\rangle^{(q_{1})}') assert (latex((alpha + 1) * KetSymbol('Psi', hs=0)) == r'\left(\alpha + 1\right) \left\lvert \Psi \right\rangle^{(0)}') assert (latex( A * psi1 ) == r'\hat{A}_{0}^{(q_{1})} \left\lvert \Psi_{1} \right\rangle^{(q_{1})}') braket = BraKet(psi1, psi2) assert ( latex(braket, show_hs_label='subscript') == r'\left\langle \Psi_{1} \middle\vert \Psi_{2} \right\rangle_{(q_{1})}') assert (latex(braket, show_hs_label=False) == r'\left\langle \Psi_{1} \middle\vert \Psi_{2} \right\rangle') expr = BraKet(KetSymbol('Psi_1', alpha, hs=hs1), KetSymbol('Psi_2', beta, hs=hs1)) assert (latex(expr) == r'\left\langle \Psi_{1}\left(\alpha\right) \middle\vert ' r'\Psi_{2}\left(\beta\right) \right\rangle^{(q_{1})}') assert (latex( ket_e1 * ket_e2) == r'\left\lvert ee \right\rangle^{(q_{1} \otimes q_{2})}') assert latex(ket_e1.dag() * ket_e1) == r'1' assert latex(ket_g1.dag() * ket_e1) == r'0' ketbra = KetBra(psi1, psi2) assert (latex(ketbra) == r'\left\lvert \Psi_{1} \middle\rangle\!' r'\middle\langle \Psi_{2} \right\rvert^{(q_{1})}') assert (latex( ketbra, show_hs_label='subscript') == r'\left\lvert \Psi_{1} \middle\rangle\!' r'\middle\langle \Psi_{2} \right\rvert_{(q_{1})}') assert (latex( ketbra, show_hs_label=False) == r'\left\lvert \Psi_{1} \middle\rangle\!' r'\middle\langle \Psi_{2} \right\rvert') expr = KetBra(KetSymbol('Psi_1', alpha, hs=hs1), KetSymbol('Psi_2', beta, hs=hs1)) assert ( latex(expr) == r'\left\lvert \Psi_{1}\left(\alpha\right) \middle\rangle\!' r'\middle\langle \Psi_{2}\left(\beta\right) \right\rvert^{(q_{1})}') bell1 = (ket_e1 * ket_g2 - I * ket_g1 * ket_e2) / sqrt(2) bell2 = (ket_e1 * ket_e2 - ket_g1 * ket_g2) / sqrt(2) assert (latex(bell1) == r'\frac{1}{\sqrt{2}} \left(\left\lvert eg \right\rangle^{(q_{1} ' r'\otimes q_{2})} - i \left\lvert ge \right\rangle' r'^{(q_{1} \otimes q_{2})}\right)') assert (latex(bell2) == r'\frac{1}{\sqrt{2}} \left(\left\lvert ee \right\rangle^{(q_{1} ' r'\otimes q_{2})} - \left\lvert gg \right\rangle' r'^{(q_{1} \otimes q_{2})}\right)') assert (latex(bell2, show_hs_label=False) == r'\frac{1}{\sqrt{2}} \left(\left\lvert ee \right\rangle - ' r'\left\lvert gg \right\rangle\right)') assert BraKet.create(bell1, bell2).expand() == 0 assert (latex(BraKet.create( bell1, bell2)) == r'\frac{1}{2} \left(\left\langle eg \right\rvert' r'^{(q_{1} \otimes q_{2})} + i \left\langle ge \right\rvert' r'^{(q_{1} \otimes q_{2})}\right) ' r'\left(\left\lvert ee \right\rangle^{(q_{1} \otimes q_{2})} ' r'- \left\lvert gg \right\rangle^{(q_{1} \otimes q_{2})}\right)') assert ( latex(KetBra.create( bell1, bell2)) == r'\frac{1}{2} \left(\left\lvert eg \right\rangle' r'^{(q_{1} \otimes q_{2})} - i \left\lvert ge \right\rangle' r'^{(q_{1} \otimes q_{2})}\right)\left(\left\langle ee \right\rvert' r'^{(q_{1} \otimes q_{2})} - \left\langle gg \right\rvert' r'^{(q_{1} \otimes q_{2})}\right)') with configure_printing(tex_use_braket=True): expr = KetBra(KetSymbol('Psi', hs=0), BasisKet(FockIndex(i), hs=0)) assert latex(expr) == r'\Ket{\Psi}\!\Bra{i}^{(0)}' expr = KetBra(BasisKet(FockIndex(i), hs=0), KetSymbol('Psi', hs=0)) assert latex(expr) == r'\Ket{i}\!\Bra{\Psi}^{(0)}' expr = BraKet(KetSymbol('Psi', hs=0), BasisKet(FockIndex(i), hs=0)) assert latex(expr) == r'\Braket{\Psi | i}^(0)' expr = BraKet(BasisKet(FockIndex(i), hs=0), KetSymbol('Psi', hs=0)) assert latex(expr) == r'\Braket{i | \Psi}^(0)'