def test_tex_symbolic_labels(): """Test tex representation of symbols with symbolic labels""" i = IdxSym('i') j = IdxSym('j') hs0 = LocalSpace(0) hs1 = LocalSpace(1) Psi = IndexedBase('Psi') with configure_printing(tex_use_braket=True): assert latex(BasisKet(FockIndex(2 * i), hs=hs0)) == r'\Ket{2 i}^{(0)}' assert latex(KetSymbol(StrLabel(2 * i), hs=hs0)) == r'\Ket{2 i}^{(0)}' assert (latex(KetSymbol(StrLabel(Psi[i, j]), hs=hs0 * hs1)) == r'\Ket{\Psi_{i j}}^{(0 \otimes 1)}') expr = BasisKet(FockIndex(i), hs=hs0) * BasisKet(FockIndex(j), hs=hs1) assert latex(expr) == r'\Ket{i,j}^{(0 \otimes 1)}' assert (latex(Bra(BasisKet(FockIndex(2 * i), hs=hs0))) == r'\Bra{2 i}^{(0)}') assert (latex(LocalSigma(FockIndex(i), FockIndex(j), hs=hs0)) == r'\Ket{i}\!\Bra{j}^{(0)}') alpha = symbols('alpha') expr = CoherentStateKet(alpha, hs=1).to_fock_representation() assert (latex(expr) == r'e^{- \frac{\alpha \overline{\alpha}}{2}} ' r'\left(\sum_{n \in \mathcal{H}_{1}} ' r'\frac{\alpha^{n}}{\sqrt{n!}} \Ket{n}^{(1)}\right)') assert (latex( expr, conjg_style='star') == r'e^{- \frac{\alpha {\alpha}^*}{2}} ' r'\left(\sum_{n \in \mathcal{H}_{1}} ' r'\frac{\alpha^{n}}{\sqrt{n!}} \Ket{n}^{(1)}\right)') tls = SpinSpace(label='s', spin='1/2', basis=('down', 'up')) Sig = IndexedBase('sigma') n = IdxSym('n') Sig_n = OperatorSymbol(StrLabel(Sig[n]), hs=tls) assert latex(Sig_n, show_hs_label=False) == r'\hat{\sigma}_{n}'
def braket(): """An example symbolic braket""" Psi = KetSymbol("Psi", hs=0) Phi = KetSymbol("Phi", hs=0) res = BraKet.create(Psi, Phi) assert isinstance(res, ScalarExpression) return res
def test_ascii_bra_operations(): """Test the ascii representation of bra operations""" hs1 = LocalSpace('q_1', dimension=2) hs2 = LocalSpace('q_2', dimension=2) 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) bra_psi1 = KetSymbol("Psi_1", hs=hs1).dag() bra_psi2 = KetSymbol("Psi_2", hs=hs1).dag() bra_psi2 = KetSymbol("Psi_2", hs=hs1).dag() bra_psi3 = KetSymbol("Psi_3", hs=hs1).dag() bra_phi = KetSymbol("Phi", hs=hs2).dag() A = OperatorSymbol("A_0", hs=hs1) gamma = symbols('gamma', positive=True) phase = exp(-I * gamma) assert ascii((psi1 + psi2).dag()) == '<Psi_1|^(q_1) + <Psi_2|^(q_1)' assert ascii(bra_psi1 + bra_psi2) == '<Psi_1|^(q_1) + <Psi_2|^(q_1)' assert (ascii( (psi1 - psi2 + psi3).dag()) == '<Psi_1|^(q_1) - <Psi_2|^(q_1) + <Psi_3|^(q_1)') assert (ascii(bra_psi1 - bra_psi2 + bra_psi3) == '<Psi_1|^(q_1) - <Psi_2|^(q_1) + <Psi_3|^(q_1)') assert ascii((psi1 * phi).dag()) == '<Psi_1|^(q_1) * <Phi|^(q_2)' assert ascii(bra_psi1 * bra_phi) == '<Psi_1|^(q_1) * <Phi|^(q_2)' assert ascii(Bra(phase * psi1)) == 'exp(I*gamma) * <Psi_1|^(q_1)' assert ascii((A * psi1).dag()) == '<Psi_1|^(q_1) A_0^(q_1)H'
def test_ascii_symbolic_labels(): """Test ascii representation of symbols with symbolic labels""" i = IdxSym('i') j = IdxSym('j') hs0 = LocalSpace(0) hs1 = LocalSpace(1) Psi = IndexedBase('Psi') assert ascii(BasisKet(FockIndex(2 * i), hs=hs0)) == '|2*i>^(0)' assert ascii(KetSymbol(StrLabel(2 * i), hs=hs0)) == '|2*i>^(0)' assert (ascii(KetSymbol(StrLabel(Psi[i, j]), hs=hs0 * hs1)) == '|Psi_ij>^(0*1)') expr = BasisKet(FockIndex(i), hs=hs0) * BasisKet(FockIndex(j), hs=hs1) assert ascii(expr) == '|i,j>^(0*1)' assert ascii(Bra(BasisKet(FockIndex(2 * i), hs=hs0))) == '<2*i|^(0)' assert (ascii(LocalSigma(FockIndex(i), FockIndex(j), hs=hs0)) == '|i><j|^(0)') expr = CoherentStateKet(symbols('alpha'), hs=1).to_fock_representation() assert (ascii(expr) == 'exp(-alpha*conjugate(alpha)/2) * ' '(Sum_{n in H_1} alpha**n/sqrt(n!) * |n>^(1))') tls = SpinSpace(label='s', spin='1/2', basis=('down', 'up')) Sig = IndexedBase('sigma') n = IdxSym('n') Sig_n = OperatorSymbol(StrLabel(Sig[n]), hs=tls) assert ascii(Sig_n, show_hs_label=False) == 'sigma_n'
def test_indexed_sum_over_scalartimes(): """Test ScalarIndexedSum over a term that is an ScalarTimes instance""" i, j = symbols('i, j', cls=IdxSym) hs = LocalSpace(1, dimension=2) Psi_i = KetSymbol(StrLabel(IndexedBase('Psi')[i]), hs=hs) Psi_j = KetSymbol(StrLabel(IndexedBase('Psi')[j]), hs=hs) term = KroneckerDelta(i, j) * BraKet(Psi_i, Psi_j) assert isinstance(term, ScalarTimes) i_range = IndexOverFockSpace(i, hs) j_range = IndexOverFockSpace(j, hs) sum = ScalarIndexedSum.create(term, ranges=(i_range, j_range)) assert sum == hs.dimension
def test_two_hs_symbol_sum(): """Test sum_{ij} a_{ij} Psi_{ij}""" i = IdxSym('i') j = IdxSym('j') a = IndexedBase('a') hs1 = LocalSpace('1', dimension=3) hs2 = LocalSpace('2', dimension=3) hs = hs1 * hs2 Psi = IndexedBase('Psi') a_ij = a[i, j] Psi_ij = Psi[i, j] KetPsi_ij = KetSymbol(StrLabel(Psi_ij), hs=hs) term = a_ij * KetPsi_ij expr1 = KetIndexedSum( term, ranges=(IndexOverFockSpace(i, hs=hs1), IndexOverFockSpace(j, hs=hs2)), ) expr2 = KetIndexedSum(term, ranges=(IndexOverRange(i, 0, 2), IndexOverRange(j, 0, 2))) assert expr1.term.free_symbols == set( [i, j, symbols('a'), symbols('Psi'), a_ij, Psi_ij]) assert expr1.free_symbols == set( [symbols('a'), symbols('Psi'), a_ij, Psi_ij]) assert expr1.variables == [i, j] assert ( ascii(expr1) == 'Sum_{i in H_1} Sum_{j in H_2} a_ij * |Psi_ij>^(1*2)') assert unicode(expr1) == '∑_{i ∈ ℌ₁} ∑_{j ∈ ℌ₂} a_ij |Ψ_ij⟩^(1⊗2)' assert (latex(expr1) == r'\sum_{i \in \mathcal{H}_{1}} \sum_{j \in \mathcal{H}_{2}} ' r'a_{i j} \left\lvert \Psi_{i j} \right\rangle^{(1 \otimes 2)}') assert ascii(expr2) == 'Sum_{i,j=0}^{2} a_ij * |Psi_ij>^(1*2)' assert unicode(expr2) == '∑_{i,j=0}^{2} a_ij |Ψ_ij⟩^(1⊗2)' assert (latex(expr2) == r'\sum_{i,j=0}^{2} a_{i j} ' r'\left\lvert \Psi_{i j} \right\rangle^{(1 \otimes 2)}') assert expr1.doit() == expr2.doit() assert expr1.doit() == KetPlus( a[0, 0] * KetSymbol('Psi_00', hs=hs), a[0, 1] * KetSymbol('Psi_01', hs=hs), a[0, 2] * KetSymbol('Psi_02', hs=hs), a[1, 0] * KetSymbol('Psi_10', hs=hs), a[1, 1] * KetSymbol('Psi_11', hs=hs), a[1, 2] * KetSymbol('Psi_12', hs=hs), a[2, 0] * KetSymbol('Psi_20', hs=hs), a[2, 1] * KetSymbol('Psi_21', hs=hs), a[2, 2] * KetSymbol('Psi_22', hs=hs), )
def test_unicode_bra_operations(): """Test the unicode representation of bra operations""" hs1 = LocalSpace('q_1', dimension=2) hs2 = LocalSpace('q_2', dimension=2) psi1 = KetSymbol("Psi_1", hs=hs1) psi2 = KetSymbol("Psi_2", hs=hs1) psi2 = KetSymbol("Psi_2", hs=hs1) phi = KetSymbol("Phi", hs=hs2) gamma = symbols('gamma', positive=True) phase = exp(-I * gamma) assert unicode((psi1 + psi2).dag()) == '⟨Ψ₁|^(q₁) + ⟨Ψ₂|^(q₁)' assert unicode((psi1 * phi).dag()) == '⟨Ψ₁|^(q₁) ⊗ ⟨Φ|^(q₂)' assert unicode(Bra(phase * psi1)) == 'exp(ⅈ γ) ⟨Ψ₁|^(q₁)'
def test_ascii_ket_elements(): """Test the ascii representation of "atomic" kets""" hs1 = LocalSpace('q1', basis=('g', 'e')) hs2 = LocalSpace('q2', basis=('g', 'e')) alpha, beta = symbols('alpha, beta') assert ascii(KetSymbol('Psi', hs=hs1)) == '|Psi>^(q1)' psi = KetSymbol('Psi', hs=1) assert ascii(psi) == '|Psi>^(1)' assert ascii(KetSymbol('Psi', alpha, beta, hs=1)) == ('|Psi(alpha, beta)>^(1)') assert ascii(psi, show_hs_label='subscript') == '|Psi>_(1)' assert ascii(psi, show_hs_label=False) == '|Psi>' assert ascii(KetSymbol('Psi', hs=(1, 2))) == '|Psi>^(1*2)' assert ascii(KetSymbol('Psi', hs=hs1 * hs2)) == '|Psi>^(q1*q2)' with pytest.raises(ValueError): KetSymbol(r'\Psi', hs=hs1) assert ascii(KetSymbol('Psi', hs=1)) == '|Psi>^(1)' assert ascii(KetSymbol('Psi', hs=hs1 * hs2)) == '|Psi>^(q1*q2)' assert ascii(ZeroKet) == '0' assert ascii(TrivialKet) == '1' assert ascii(BasisKet('e', hs=hs1)) == '|e>^(q1)' assert ascii(BasisKet(1, hs=1)) == '|1>^(1)' assert ascii(BasisKet(1, hs=hs1)) == '|e>^(q1)' with pytest.raises(ValueError): BasisKet('1', hs=hs1) assert ascii(CoherentStateKet(2.0, hs=1)) == '|alpha=2>^(1)' assert ascii(CoherentStateKet(2.1, hs=1)) == '|alpha=2.1>^(1)'
def test_unicode_bra_elements(): """Test the unicode representation of "atomic" kets""" hs1 = LocalSpace('q1', basis=('g', 'e')) alpha, beta = symbols('alpha, beta') assert unicode(Bra(KetSymbol('Psi', hs=hs1))) == '⟨Ψ|^(q₁)' assert unicode(Bra(KetSymbol('Psi', hs=1))) == '⟨Ψ|⁽¹⁾' assert unicode(Bra(KetSymbol('Psi', alpha, beta, hs=hs1))) == ('⟨Ψ(α, β)|^(q₁)') assert unicode(Bra(KetSymbol('Psi', hs=(1, 2)))) == '⟨Ψ|^(1⊗2)' assert unicode(Bra(ZeroKet)) == '0' assert unicode(Bra(TrivialKet)) == '1' assert unicode(BasisKet('e', hs=hs1).adjoint()) == '⟨e|^(q₁)' assert unicode(BasisKet(1, hs=1).adjoint()) == '⟨1|⁽¹⁾' assert unicode(CoherentStateKet(2.0, hs=1).dag()) == '⟨α=2|⁽¹⁾' assert unicode(CoherentStateKet(0.5j, hs=1).dag()) == '⟨α=0.5j|⁽¹⁾' assert unicode(CoherentStateKet(I / 2, hs=1).dag()) == '⟨α=ⅈ/2|⁽¹⁾'
def test_ketbra_indexed_sum(): """Test ketbra product of sums""" i = IdxSym('i') hs = LocalSpace(1, dimension=5) alpha = IndexedBase('alpha') psi = KetSymbol('Psi', hs=hs) psi1 = KetIndexedSum( alpha[1, i] * BasisKet(FockIndex(i), hs=hs), ranges=IndexOverFockSpace(i, hs), ) psi2 = KetIndexedSum( alpha[2, i] * BasisKet(FockIndex(i), hs=hs), ranges=IndexOverFockSpace(i, hs), ) expr = psi1 * psi2.dag() assert expr.space == hs expected = OperatorIndexedSum( alpha[2, i.prime].conjugate() * alpha[1, i] * KetBra.create(BasisKet(FockIndex(i), hs=hs), BasisKet(FockIndex(i.prime), hs=hs)), ranges=(IndexOverFockSpace(i, hs), IndexOverFockSpace(i.prime, hs)), ) assert expr == expected assert KetBra.create(psi1, psi2) == expr expr = psi * psi2.dag() assert expr.space == hs expected = OperatorIndexedSum( alpha[2, i].conjugate() * KetBra.create(psi, BasisKet(FockIndex(i), hs=hs)), ranges=IndexOverFockSpace(i, hs), ) assert expr == expected assert KetBra.create(psi, psi2) == expr expr = psi1 * psi.dag() assert expr.space == hs expected = OperatorIndexedSum( alpha[1, i] * KetBra.create(BasisKet(FockIndex(i), hs=hs), psi), ranges=IndexOverFockSpace(i, hs), ) assert expr == expected assert KetBra.create(psi1, psi) == expr
def test_unicode_ket_elements(): """Test the unicode representation of "atomic" kets""" hs1 = LocalSpace('q1', basis=('g', 'e')) hs2 = LocalSpace('q2', basis=('g', 'e')) alpha, beta = symbols('alpha, beta') psi_hs1 = KetSymbol('Psi', hs=hs1) assert unicode(psi_hs1) == '|Ψ⟩^(q₁)' assert unicode(psi_hs1, unicode_sub_super=False) == '|Ψ⟩^(q_1)' assert unicode(KetSymbol('Psi', hs=1)) == '|Ψ⟩⁽¹⁾' assert unicode(KetSymbol('Psi', alpha, beta, hs=1)) == '|Ψ(α, β)⟩⁽¹⁾' assert unicode(KetSymbol('Psi', hs=(1, 2))) == '|Ψ⟩^(1⊗2)' assert unicode(KetSymbol('Psi', hs=hs1 * hs2)) == '|Ψ⟩^(q₁⊗q₂)' assert unicode(ZeroKet) == '0' assert unicode(TrivialKet) == '1' assert unicode(BasisKet('e', hs=hs1)) == '|e⟩^(q₁)' assert unicode(BasisKet(1, hs=1)) == '|1⟩⁽¹⁾' assert unicode(CoherentStateKet(2, hs=1)) == '|α=2⟩⁽¹⁾' assert unicode(CoherentStateKet(2.0, hs=1)) == '|α=2⟩⁽¹⁾' unicode.printer.cache = {} assert unicode(CoherentStateKet(2.0, hs=1)) == '|α=2⟩⁽¹⁾' assert unicode(CoherentStateKet(2.1, hs=1)) == '|α=2.1⟩⁽¹⁾'
def test_tex_bra_elements(): """Test the tex representation of "atomic" kets""" hs1 = LocalSpace('q1', basis=('g', 'e')) hs2 = LocalSpace('q2', basis=('g', 'e')) alpha, beta = symbols('alpha, beta') bra = Bra(KetSymbol('Psi', hs=hs1)) assert latex(bra) == r'\left\langle \Psi \right\rvert^{(q_{1})}' assert latex(Bra(KetSymbol('Psi', alpha, beta, hs=hs1))) == ( r'\left\langle \Psi\left(\alpha, \beta\right) \right\rvert^{(q_{1})}') assert latex(bra, tex_use_braket=True) == r'\Bra{\Psi}^{(q_{1})}' assert (latex(bra, tex_use_braket=True, show_hs_label='subscript') == r'\Bra{\Psi}_{(q_{1})}') assert (latex(bra, tex_use_braket=True, show_hs_label=False) == r'\Bra{\Psi}') assert (latex(Bra(KetSymbol( 'Psi', hs=1))) == r'\left\langle \Psi \right\rvert^{(1)}') assert (latex(Bra(KetSymbol( 'Psi', hs=(1, 2)))) == r'\left\langle \Psi \right\rvert^{(1 \otimes 2)}') assert (latex(Bra(KetSymbol( 'Psi', hs=hs1 * hs2))) == r'\left\langle \Psi \right\rvert^{(q_{1} \otimes q_{2})}') assert (latex(KetSymbol( 'Psi', hs=1).dag()) == r'\left\langle \Psi \right\rvert^{(1)}') assert latex(Bra(ZeroKet)) == '0' assert latex(Bra(TrivialKet)) == '1' assert (latex(BasisKet( 'e', hs=hs1).adjoint()) == r'\left\langle e \right\rvert^{(q_{1})}') assert (latex(BasisKet( 1, hs=1).adjoint()) == r'\left\langle 1 \right\rvert^{(1)}') assert (latex(CoherentStateKet( 2.0, hs=1).dag()) == r'\left\langle \alpha=2 \right\rvert^{(1)}')
def test_braket_indexed_sum(): """Test braket product of sums""" i = IdxSym('i') hs = LocalSpace(1, dimension=5) alpha = IndexedBase('alpha') psi = KetSymbol('Psi', hs=hs) psi1 = KetIndexedSum( alpha[1, i] * BasisKet(FockIndex(i), hs=hs), ranges=IndexOverFockSpace(i, hs), ) psi2 = KetIndexedSum( alpha[2, i] * BasisKet(FockIndex(i), hs=hs), ranges=IndexOverFockSpace(i, hs), ) expr = Bra.create(psi1) * psi2 assert expr.space == TrivialSpace assert expr == ScalarIndexedSum.create( alpha[1, i].conjugate() * alpha[2, i], ranges=(IndexOverFockSpace(i, hs), ), ) assert BraKet.create(psi1, psi2) == expr expr = psi.dag() * psi2 assert expr == ScalarIndexedSum( alpha[2, i] * BraKet(psi, BasisKet(FockIndex(i), hs=hs)), ranges=IndexOverFockSpace(i, hs), ) assert BraKet.create(psi, psi2) == expr expr = psi1.dag() * psi assert expr == ScalarIndexedSum( alpha[1, i].conjugate() * BraKet(BasisKet(FockIndex(i), hs=hs), psi), ranges=IndexOverFockSpace(i, hs), ) assert BraKet.create(psi1, psi) == expr
def test_unicode_operator_operations(): """Test the unicode representation of operator algebra operations""" hs1 = LocalSpace('q_1', dimension=2) hs2 = LocalSpace('q_2', dimension=2) A = OperatorSymbol("A", hs=hs1) B = OperatorSymbol("B", hs=hs1) C = OperatorSymbol("C", hs=hs2) psi = KetSymbol('Psi', hs=hs1) gamma = symbols('gamma', positive=True) assert unicode(A + B) == 'A\u0302^(q\u2081) + B\u0302^(q\u2081)' # Â^(q₁) + B̂^(q₁) assert unicode(A * B) == 'A\u0302^(q\u2081) B\u0302^(q\u2081)' # Â^(q₁) B̂^(q₁) assert unicode(A * C) == 'A\u0302^(q\u2081) C\u0302^(q\u2082)' # Â^(q₁) Ĉ^(q₂) assert unicode(2 * A) == '2 A\u0302^(q\u2081)' # 2 Â^(q₁) assert unicode(2j * A) == '2j A\u0302^(q\u2081)' # 2j Â^(q₁) assert unicode((1 + 2j) * A) == '(1+2j) A\u0302^(q\u2081)' # (1+2j) Â^(q₁) assert unicode(gamma**2 * A) == '\u03b3\xb2 A\u0302^(q\u2081)' # γ² Â^(q₁) assert unicode(-(gamma**2) / 2 * A) == '-\u03b3\xb2/2 A\u0302^(q\u2081)' # -γ²/2 Â^(q₁) assert (unicode(tr( A * C, over_space=hs2)) == 'tr_(q\u2082)[C\u0302^(q\u2082)] A\u0302^(q\u2081)' ) # tr_(q₂)[Ĉ^(q₂)] Â^(q₁) assert unicode(Adjoint(A)) == 'A\u0302^(q\u2081)\u2020' # Â^(q₁)† assert unicode(Adjoint(Create(hs=1))) == 'a\u0302\u207d\xb9\u207e' # â⁽¹⁾ assert unicode(PseudoInverse(A)) == '(A\u0302^(q\u2081))^+' # (Â^(q₁))^+ assert unicode(NullSpaceProjector(A)) == 'P\u0302_Ker(A\u0302^(q\u2081))' # P̂_Ker(Â^(q₁)) assert unicode(A - B) == 'A\u0302^(q\u2081) - B\u0302^(q\u2081)' # Â^(q₁) - B̂^(q₁) assert unicode(2 * A - sqrt(gamma) * (B + C)) in [ '2 A\u0302^(q\u2081) - \u221a\u03b3 (B\u0302^(q\u2081) ' '+ C\u0302^(q\u2082))', '2 A\u0302^(q\u2081) - sqrt(\u03b3) (B\u0302^(q\u2081) ' '+ C\u0302^(q\u2082))', ] # 2 Â^(q₁) - √γ (B̂^(q₁) + Ĉ^(q₂)) assert (unicode(Commutator(A, B)) == '[A\u0302^(q\u2081), B\u0302^(q\u2081)]') # [Â^(q₁), B̂^(q₁)] expr = (Commutator(A, B) * psi).dag() assert unicode(expr, show_hs_label=False) == r'⟨Ψ| [Â, B̂]^†'
def test_scalar_conjugate(braket): """Test taking the complex conjugate (adjoint) of a scalar""" Psi = KetSymbol("Psi", hs=0) Phi = KetSymbol("Phi", hs=0) phi = symbols('phi', real=True) alpha = symbols('alpha') expr = ScalarValue(1 + 1j) assert expr.adjoint() == expr.conjugate() == 1 - 1j assert braket.adjoint() == BraKet.create(Phi, Psi) expr = 1j + braket assert expr.adjoint() == expr.conjugate() == braket.adjoint() - 1j expr = (1 + 1j) * braket assert expr.adjoint() == expr.conjugate() == (1 - 1j) * braket.adjoint() expr = braket**(I * phi) assert expr.conjugate() == braket.adjoint()**(-I * phi) expr = braket**alpha assert expr.conjugate() == braket.adjoint()**(alpha.conjugate())
def test_tensor_indexed_sum(): """Test tensor product of sums""" i = IdxSym('i') hs1 = LocalSpace(1) hs2 = LocalSpace(2) alpha = IndexedBase('alpha') psi1 = KetIndexedSum( alpha[1, i] * BasisKet(FockIndex(i), hs=hs1), ranges=IndexOverFockSpace(i, hs1), ) psi2 = KetIndexedSum( alpha[2, i] * BasisKet(FockIndex(i), hs=hs2), ranges=IndexOverFockSpace(i, hs2), ) expr = psi1 * psi2 assert expr.space == hs1 * hs2 rhs = KetIndexedSum( alpha[1, i] * alpha[2, i.prime] * (BasisKet(FockIndex(i), hs=hs1) * BasisKet(FockIndex(i.prime), hs=hs2)), ranges=(IndexOverFockSpace(i, hs1), IndexOverFockSpace(i.prime, hs2)), ) assert expr == rhs psi0 = KetSymbol('Psi', hs=0) psi3 = KetSymbol('Psi', hs=3) expr2 = psi0 * psi1 * psi2 * psi3 rhs = KetIndexedSum( alpha[1, i] * alpha[2, i.prime] * (psi0 * BasisKet(FockIndex(i), hs=hs1) * BasisKet(FockIndex(i.prime), hs=hs2) * psi3), ranges=(IndexOverFockSpace(i, hs1), IndexOverFockSpace(i.prime, hs2)), ) assert expr2 == rhs assert TensorKet.create(psi0, psi1, psi2, psi3) == expr2
def test_tex_operator_operations(): """Test the tex representation of operator algebra operations""" hs1 = LocalSpace('q_1', dimension=2) hs2 = LocalSpace('q_2', dimension=2) A = OperatorSymbol("A", hs=hs1) B = OperatorSymbol("B", hs=hs1) C = OperatorSymbol("C", hs=hs2) psi = KetSymbol('Psi', hs=hs1) gamma = symbols('gamma', positive=True) assert latex(A.dag()) == r'\hat{A}^{(q_{1})\dagger}' assert latex(A + B) == r'\hat{A}^{(q_{1})} + \hat{B}^{(q_{1})}' assert latex(A * B) == r'\hat{A}^{(q_{1})} \hat{B}^{(q_{1})}' assert latex(A * C) == r'\hat{A}^{(q_{1})} \hat{C}^{(q_{2})}' assert latex(2 * A) == r'2 \hat{A}^{(q_{1})}' assert latex(2j * A) == r'2i \hat{A}^{(q_{1})}' assert latex((1 + 2j) * A) == r'(1+2i) \hat{A}^{(q_{1})}' assert latex(gamma**2 * A) == r'\gamma^{2} \hat{A}^{(q_{1})}' assert (latex(-(gamma**2) / 2 * A) == r'- \frac{\gamma^{2}}{2} \hat{A}^{(q_{1})}') assert (latex(tr( A * C, over_space=hs2)) == r'{\rm tr}_{q_{2}}\left[\hat{C}^{(q_{2})}\right] ' r'\hat{A}^{(q_{1})}') assert latex(Adjoint(A)) == r'\hat{A}^{(q_{1})\dagger}' assert (latex(Adjoint( A**2)) == r'\left(\hat{A}^{(q_{1})} \hat{A}^{(q_{1})}\right)^\dagger') assert (latex( Adjoint(A)**2) == r'\hat{A}^{(q_{1})\dagger} \hat{A}^{(q_{1})\dagger}') assert latex(Adjoint(Create(hs=1))) == r'\hat{a}^{(1)}' assert (latex(Adjoint(A + B)) == r'\left(\hat{A}^{(q_{1})} + \hat{B}^{(q_{1})}\right)^\dagger') assert latex(PseudoInverse(A)) == r'\left(\hat{A}^{(q_{1})}\right)^+' assert (latex( PseudoInverse(A)**2 ) == r'\left(\hat{A}^{(q_{1})}\right)^+ \left(\hat{A}^{(q_{1})}\right)^+') assert (latex(NullSpaceProjector(A)) == r'\hat{P}_{Ker}\left(\hat{A}^{(q_{1})}\right)') assert latex(A - B) == r'\hat{A}^{(q_{1})} - \hat{B}^{(q_{1})}' assert (latex(A - B + C) == r'\hat{A}^{(q_{1})} - \hat{B}^{(q_{1})} + \hat{C}^{(q_{2})}') assert (latex(2 * A - sqrt(gamma) * (B + C)) == r'2 \hat{A}^{(q_{1})} - \sqrt{\gamma} \left(\hat{B}^{(q_{1})} + ' r'\hat{C}^{(q_{2})}\right)') assert (latex(Commutator( A, B)) == r'\left[\hat{A}^{(q_{1})}, \hat{B}^{(q_{1})}\right]') expr = (Commutator(A, B) * psi).dag() assert (latex(expr, show_hs_label=False) == r'\left\langle \Psi \right\rvert \left[\hat{A}, ' r'\hat{B}\right]^{\dagger}')
def test_unicode_symbolic_labels(): """Test unicode representation of symbols with symbolic labels""" i = IdxSym('i') j = IdxSym('j') hs0 = LocalSpace(0) hs1 = LocalSpace(1) Psi = IndexedBase('Psi') assert unicode(BasisKet(FockIndex(2 * i), hs=hs0)) == '|2 i⟩⁽⁰⁾' assert unicode(KetSymbol(StrLabel(2 * i), hs=hs0)) == '|2 i⟩⁽⁰⁾' assert (unicode(KetSymbol(StrLabel(Psi[i, j]), hs=hs0 * hs1)) == '|Ψ_ij⟩^(0⊗1)') expr = BasisKet(FockIndex(i), hs=hs0) * BasisKet(FockIndex(j), hs=hs1) assert unicode(expr) == '|i,j⟩^(0⊗1)' assert unicode(Bra(BasisKet(FockIndex(2 * i), hs=hs0))) == '⟨2 i|⁽⁰⁾' assert (unicode(LocalSigma(FockIndex(i), FockIndex(j), hs=hs0)) == '|i⟩⟨j|⁽⁰⁾') expr = CoherentStateKet(symbols('alpha'), hs=1).to_fock_representation() assert unicode(expr) == 'exp(-α α ⃰/2) (∑_{n ∈ ℌ₁} αⁿ/√n! |n⟩⁽¹⁾)' tls = SpinSpace(label='s', spin='1/2', basis=('down', 'up')) Sig = IndexedBase('sigma') n = IdxSym('n') Sig_n = OperatorSymbol(StrLabel(Sig[n]), hs=tls) assert unicode(Sig_n, show_hs_label=False) == 'σ̂ₙ'
def test_tex_bra_operations(): """Test the tex representation of bra operations""" hs1 = LocalSpace('q_1', dimension=2) hs2 = LocalSpace('q_2', dimension=2) psi1 = KetSymbol("Psi_1", hs=hs1) psi2 = KetSymbol("Psi_2", hs=hs1) psi2 = KetSymbol("Psi_2", hs=hs1) bra_psi1 = KetSymbol("Psi_1", hs=hs1).dag() bra_psi2 = KetSymbol("Psi_2", hs=hs1).dag() bra_psi2 = KetSymbol("Psi_2", hs=hs1).dag() bra_psi3 = KetSymbol("Psi_3", hs=hs1).dag() bra_phi = KetSymbol("Phi", hs=hs2).dag() A = OperatorSymbol("A_0", hs=hs1) gamma = symbols('gamma', positive=True) phase = exp(-I * gamma) assert (latex( (psi1 + psi2).dag()) == r'\left\langle \Psi_{1} \right\rvert^{(q_{1})} + ' r'\left\langle \Psi_{2} \right\rvert^{(q_{1})}') assert (latex((psi1 + psi2).dag(), tex_use_braket=True) == r'\Bra{\Psi_{1}}^{(q_{1})} + \Bra{\Psi_{2}}^{(q_{1})}') assert ( latex(bra_psi1 + bra_psi2) == r'\left\langle \Psi_{1} \right\rvert^{(q_{1})} + ' r'\left\langle \Psi_{2} \right\rvert^{(q_{1})}') assert ( latex(bra_psi1 - bra_psi2 + bra_psi3) == r'\left\langle \Psi_{1} \right\rvert^{(q_{1})} - ' r'\left\langle \Psi_{2} \right\rvert^{(q_{1})} + ' r'\left\langle \Psi_{3} \right\rvert^{(q_{1})}') assert (latex( bra_psi1 * bra_phi) == r'\left\langle \Psi_{1} \right\rvert^{(q_{1})} \otimes ' r'\left\langle \Phi \right\rvert^{(q_{2})}') assert (latex(bra_psi1 * bra_phi, tex_use_braket=True) == r'\Bra{\Psi_{1}}^{(q_{1})} \otimes \Bra{\Phi}^{(q_{2})}') assert (latex(Bra( phase * psi1)) == r'e^{i \gamma} \left\langle \Psi_{1} \right\rvert^{(q_{1})}') assert (latex( (A * psi1).dag()) == r'\left\langle \Psi_{1} \right\rvert^{(q_{1})} ' r'\hat{A}_{0}^{(q_{1})\dagger}')
def test_ascii_operator_operations(): """Test the ascii representation of operator algebra operations""" hs1 = LocalSpace('q_1', dimension=2) hs2 = LocalSpace('q_2', dimension=2) A = OperatorSymbol("A", hs=hs1) B = OperatorSymbol("B", hs=hs1) C = OperatorSymbol("C", hs=hs2) D = OperatorSymbol("D", hs=hs1) psi = KetSymbol('Psi', hs=hs1) gamma = symbols('gamma', positive=True) assert ascii(A + B) == 'A^(q_1) + B^(q_1)' assert ascii(A * B) == 'A^(q_1) * B^(q_1)' assert ascii(A * C) == 'A^(q_1) * C^(q_2)' assert ascii(A * (B + D)) == 'A^(q_1) * (B^(q_1) + D^(q_1))' assert ascii(A * (B - D)) == 'A^(q_1) * (B^(q_1) - D^(q_1))' assert (ascii( (A + B) * (-2 * B - D)) == '(A^(q_1) + B^(q_1)) * (-D^(q_1) - 2 * B^(q_1))') assert ascii(OperatorTimes(A, -B)) == 'A^(q_1) * (-B^(q_1))' assert ascii(OperatorTimes(A, -B), show_hs_label=False) == 'A * (-B)' assert ascii(2 * A) == '2 * A^(q_1)' assert ascii(2j * A) == '2j * A^(q_1)' assert ascii((1 + 2j) * A) == '(1+2j) * A^(q_1)' assert ascii(gamma**2 * A) == 'gamma**2 * A^(q_1)' assert ascii(-(gamma**2) / 2 * A) == '-gamma**2/2 * A^(q_1)' assert ascii(tr(A * C, over_space=hs2)) == 'tr_(q_2)[C^(q_2)] * A^(q_1)' expr = A + OperatorPlusMinusCC(B * D) assert ascii(expr, show_hs_label=False) == 'A + (B * D + c.c.)' expr = A + OperatorPlusMinusCC(B + D) assert ascii(expr, show_hs_label=False) == 'A + (B + D + c.c.)' expr = A * OperatorPlusMinusCC(B * D) assert ascii(expr, show_hs_label=False) == 'A * (B * D + c.c.)' assert ascii(Adjoint(A)) == 'A^(q_1)H' assert ascii(Adjoint(Create(hs=1))) == 'a^(1)' assert ascii(Adjoint(A + B)) == '(A^(q_1) + B^(q_1))^H' assert ascii(PseudoInverse(A)) == '(A^(q_1))^+' assert ascii(NullSpaceProjector(A)) == 'P_Ker(A^(q_1))' assert ascii(A - B) == 'A^(q_1) - B^(q_1)' assert ascii(A - B + C) == 'A^(q_1) - B^(q_1) + C^(q_2)' expr = 2 * A - sqrt(gamma) * (B + C) assert ascii(expr) == '2 * A^(q_1) - sqrt(gamma) * (B^(q_1) + C^(q_2))' assert ascii(Commutator(A, B)) == r'[A^(q_1), B^(q_1)]' expr = (Commutator(A, B) * psi).dag() assert ascii(expr, show_hs_label=False) == r'<Psi| [A, B]^H'
def test_partial_expansion(): """Test partially executing the sum (only for a subset of summation indices)""" i = IdxSym('i') j = IdxSym('j') k = IdxSym('k') hs = LocalSpace('0', dimension=2) Psi = IndexedBase('Psi') def r(index_symbol): return IndexOverFockSpace(index_symbol, hs=hs) psi_ijk = KetSymbol(StrLabel(Psi[i, j, k]), hs=hs) def psi(i_val, j_val, k_val): return psi_ijk.substitute({i: i_val, j: j_val, k: k_val}) expr = KetIndexedSum(psi_ijk, ranges=(r(i), r(j), r(k))) expr_expanded = expr.doit(indices=[i]) assert expr_expanded == KetIndexedSum(psi(0, j, k) + psi(1, j, k), ranges=(r(j), r(k))) expr_expanded = expr.doit(indices=[j]) assert expr_expanded == KetIndexedSum(psi(i, 0, k) + psi(i, 1, k), ranges=(r(i), r(k))) assert expr.doit(indices=[j]) == expr.doit(indices=['j']) expr_expanded = expr.doit(indices=[i, j]) assert expr_expanded == KetIndexedSum(psi(0, 0, k) + psi(1, 0, k) + psi(0, 1, k) + psi(1, 1, k), ranges=r(k)) assert expr.doit(indices=[i, j]) == expr.doit(indices=[j, i]) assert expr.doit(indices=[i, j, k]) == expr.doit() with pytest.raises(ValueError): expr.doit(indices=[i], max_terms=10)
def test_tex_ket_elements(): """Test the tex representation of "atomic" kets""" hs1 = LocalSpace('q1', basis=('g', 'e')) hs2 = LocalSpace('q2', basis=('g', 'e')) alpha, beta = symbols('alpha, beta') psi = KetSymbol('Psi', hs=hs1) assert latex(psi) == r'\left\lvert \Psi \right\rangle^{(q_{1})}' assert (latex(KetSymbol('Psi', alpha, beta, hs=1)) == r'\left\lvert \Psi\left(\alpha, \beta\right) \right\rangle^{(1)}') assert latex(psi, tex_use_braket=True) == r'\Ket{\Psi}^{(q_{1})}' assert (latex(psi, tex_use_braket=True, show_hs_label='subscript') == r'\Ket{\Psi}_{(q_{1})}') assert (latex(psi, tex_use_braket=True, show_hs_label=False) == r'\Ket{\Psi}') assert (latex(KetSymbol('Psi', hs=1)) == r'\left\lvert \Psi \right\rangle^{(1)}') assert (latex(KetSymbol( 'Psi', hs=(1, 2))) == r'\left\lvert \Psi \right\rangle^{(1 \otimes 2)}') assert (latex(KetSymbol( 'Psi', hs=hs1 * hs2)) == r'\left\lvert \Psi \right\rangle^{(q_{1} \otimes q_{2})}') assert (latex(KetSymbol('Psi', hs=1)) == r'\left\lvert \Psi \right\rangle^{(1)}') assert latex(ZeroKet) == '0' assert latex(TrivialKet) == '1' assert (latex(BasisKet( 'e', hs=hs1)) == r'\left\lvert e \right\rangle^{(q_{1})}') hs_tls = LocalSpace('1', basis=('excited', 'ground')) assert (latex(BasisKet( 'excited', hs=hs_tls)) == r'\left\lvert \text{excited} \right\rangle^{(1)}') assert latex(BasisKet(1, hs=1)) == r'\left\lvert 1 \right\rangle^{(1)}' spin = SpinSpace('s', spin=(3, 2)) assert (latex(SpinBasisKet( -3, 2, hs=spin)) == r'\left\lvert -3/2 \right\rangle^{(s)}') assert (latex(SpinBasisKet( 1, 2, hs=spin)) == r'\left\lvert +1/2 \right\rangle^{(s)}') assert (latex(SpinBasisKet(-3, 2, hs=spin), tex_frac_for_spin_labels=True) == r'\left\lvert -\frac{3}{2} \right\rangle^{(s)}') assert (latex(SpinBasisKet(1, 2, hs=spin), tex_frac_for_spin_labels=True) == r'\left\lvert +\frac{1}{2} \right\rangle^{(s)}') assert (latex(CoherentStateKet( 2.0, hs=1)) == r'\left\lvert \alpha=2 \right\rangle^{(1)}')
def test_ascii_bra_elements(): """Test the ascii representation of "atomic" kets""" hs1 = LocalSpace('q1', basis=('g', 'e')) hs2 = LocalSpace('q2', basis=('g', 'e')) bra = Bra(KetSymbol('Psi', hs=1)) alpha, beta = symbols('alpha, beta') assert ascii(Bra(KetSymbol('Psi', hs=hs1))) == '<Psi|^(q1)' assert ascii(bra) == '<Psi|^(1)' assert ascii(bra, show_hs_label=False) == '<Psi|' assert ascii(bra, show_hs_label='subscript') == '<Psi|_(1)' assert ascii(Bra(KetSymbol('Psi', alpha, beta, hs=hs1))) == ('<Psi(alpha, beta)|^(q1)') assert ascii(Bra(KetSymbol('Psi', hs=(1, 2)))) == '<Psi|^(1*2)' assert ascii(Bra(KetSymbol('Psi', hs=hs1 * hs2))) == '<Psi|^(q1*q2)' assert ascii(KetSymbol('Psi', hs=1).dag()) == '<Psi|^(1)' assert ascii(Bra(ZeroKet)) == '0' assert ascii(Bra(TrivialKet)) == '1' assert ascii(BasisKet('e', hs=hs1).adjoint()) == '<e|^(q1)' assert ascii(BasisKet(1, hs=1).adjoint()) == '<1|^(1)' assert ascii(CoherentStateKet(2.0, hs=1).dag()) == '<alpha=2|^(1)' assert ascii(CoherentStateKet(2.1, hs=1).dag()) == '<alpha=2.1|^(1)' assert ascii(CoherentStateKet(0.5j, hs=1).dag()) == '<alpha=0.5j|^(1)'
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)'
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_scalar_numeric_methods(braket): """Test all of the numerical magic methods for scalars""" three = ScalarValue(3) two = ScalarValue(2) spOne = sympify(1) spZero = sympify(0) spHalf = spOne / 2 assert three == 3 assert three == three assert three != symbols('alpha') assert three <= 3 assert three <= ScalarValue(4) assert three >= 3 assert three >= ScalarValue(2) assert three < 3.1 assert three < ScalarValue(4) assert three > ScalarValue(2) assert three == sympify(3) assert three <= sympify(3) assert three >= sympify(3) assert three < sympify(3.1) assert three > sympify(2.9) with pytest.raises(TypeError): assert three < symbols('alpha') with pytest.raises(TypeError): assert three <= symbols('alpha') with pytest.raises(TypeError): assert three > symbols('alpha') with pytest.raises(TypeError): assert three >= symbols('alpha') assert hash(three) == hash(3) v = -three assert v == -3 assert isinstance(v, ScalarValue) v = three + 1 assert v == 4 assert isinstance(v, ScalarValue) v = three + two assert v == 5 assert isinstance(v, ScalarValue) v = three + Zero assert v is three assert three + spZero == three v = three + One assert v == 4 assert isinstance(v, ScalarValue) assert three + spOne == 4 v = abs(ScalarValue(-3)) assert v == 3 assert isinstance(v, ScalarValue) v = three - 4 assert v == -1 assert isinstance(v, ScalarValue) v = three - two assert v == 1 assert v is One v = three - Zero assert v is three assert three - spZero == three v = three - One assert v == 2 assert isinstance(v, ScalarValue) assert three - spOne == 2 v = three * 2 assert v == 6 assert isinstance(v, ScalarValue) v = three * two assert v == 6 assert isinstance(v, ScalarValue) v = three * Zero assert v == 0 assert v is Zero assert three * spZero is Zero v = three * One assert v is three assert three * spOne == three v = three // 2 assert v is One assert ScalarValue(3.5) // 1 == 3.0 v = three // two assert v is One v = three // One assert v == three assert three // spOne == three with pytest.raises(ZeroDivisionError): v = three // Zero with pytest.raises(ZeroDivisionError): v = three // spZero with pytest.raises(ZeroDivisionError): v = three // 0 v = three / 2 assert v == 3 / 2 assert isinstance(v, ScalarValue) v = three / two assert v == 3 / 2 assert isinstance(v, ScalarValue) v = three / One assert v is three assert three / spOne == three with pytest.raises(ZeroDivisionError): v = three / Zero with pytest.raises(ZeroDivisionError): v = three / spZero with pytest.raises(ZeroDivisionError): v = three / 0 v = three % 2 assert v is One assert three % 0.2 == 3 % 0.2 v = three % two assert v is One v = three % One assert v is Zero assert three % spOne is Zero with pytest.raises(ZeroDivisionError): v = three % Zero with pytest.raises(ZeroDivisionError): v = three % spZero with pytest.raises(ZeroDivisionError): v = three % 0 v = three**2 assert v == 9 assert isinstance(v, ScalarValue) v = three**two assert v == 9 assert isinstance(v, ScalarValue) v = three**One assert v is three assert three**spOne == three v = three**Zero assert v is One assert three**spZero is One v = 1 + three assert v == 4 assert isinstance(v, ScalarValue) v = two + three assert v == 5 assert isinstance(v, ScalarValue) v = sympify(2) + three assert v == 5 assert isinstance(v, SympyBasic) v = 2.0 + three assert v == 5 assert isinstance(v, ScalarValue) v = Zero + three assert v is three with pytest.raises(TypeError): None + three assert spZero + three == three v = One + three assert v == 4 assert isinstance(v, ScalarValue) assert spOne + three == 4 v = 1 - three assert v == -2 assert isinstance(v, ScalarValue) v = two - three assert v == -1 assert isinstance(v, ScalarValue) v = 2.0 - three assert v == -1 assert isinstance(v, ScalarValue) v = sympify(2) - three assert v == -1 assert isinstance(v, SympyBasic) v = Zero - three assert v == -3 assert isinstance(v, ScalarValue) with pytest.raises(TypeError): None - three assert spZero - three == -3 v = One - three assert v == -2 assert isinstance(v, ScalarValue) assert spOne - three == -2 v = 2 * three assert v == 6 assert isinstance(v, ScalarValue) v = Zero * three assert v == 0 assert v is Zero v = spZero * three assert v == Zero assert isinstance(v, SympyBasic) v = One * three assert v is three assert spOne * three == three with pytest.raises(TypeError): None * three v = 2 // three assert v is Zero v = two // three assert v is Zero v = One // three assert v is Zero v = spOne // three assert v == Zero assert isinstance(v, SympyBasic) v = Zero // three assert v is Zero v = spZero // three assert v == Zero assert isinstance(v, SympyBasic) v = 1 // three assert v is Zero with pytest.raises(TypeError): None // three v = 2 / three assert float(v) == 2 / 3 assert v == Rational(2, 3) assert isinstance(v, ScalarValue) v = two / three assert v == 2 / 3 assert isinstance(v, ScalarValue) v = One / three assert v == 1 / 3 assert isinstance(v, ScalarValue) v = 1 / three assert v == Rational(1, 3) assert isinstance(v, ScalarValue) assert float(spOne / three) == 1 / 3 v = Zero / three assert v is Zero v = spZero / three assert v == Zero assert isinstance(v, SympyBasic) with pytest.raises(TypeError): None / three v = 2**three assert v == 8 assert isinstance(v, ScalarValue) v = 0**three assert v is Zero v = two**three assert v == 8 assert isinstance(v, ScalarValue) v = One**three assert v is One with pytest.raises(TypeError): None**three v = 1**three assert v is One v = One**spHalf assert v is One v = spOne**three assert v == One assert isinstance(v, SympyBasic) v = Zero**three assert v is Zero v = spZero**three assert v == Zero assert isinstance(v, SympyBasic) v = complex(three) assert v == 3 + 0j assert isinstance(v, complex) v = int(ScalarValue(3.45)) assert v == 3 assert isinstance(v, int) v = float(three) assert v == 3.0 assert isinstance(v, float) assert Zero == 0 assert Zero != symbols('alpha') assert Zero <= One assert Zero <= three assert Zero >= Zero assert Zero >= -three assert Zero < One assert Zero < three assert Zero > -One assert Zero > -three assert Zero == spZero assert Zero <= spZero assert Zero >= spZero assert Zero < spOne assert Zero > -spOne with pytest.raises(TypeError): assert Zero < symbols('alpha') with pytest.raises(TypeError): assert Zero <= symbols('alpha') with pytest.raises(TypeError): assert Zero > symbols('alpha') with pytest.raises(TypeError): assert Zero >= symbols('alpha') assert hash(Zero) == hash(0) assert abs(Zero) is Zero assert abs(One) is One assert abs(ScalarValue(-1)) is One assert -Zero is Zero v = -One assert v == -1 assert isinstance(v, ScalarValue) assert Zero + One is One assert One + Zero is One assert Zero + Zero is Zero assert Zero - Zero is Zero assert One + One == 2 assert One - One is Zero v = Zero + 2 assert v == 2 assert isinstance(v, ScalarValue) v = Zero - One assert v == -1 assert isinstance(v, ScalarValue) v = Zero - 5 assert v == -5 assert isinstance(v, ScalarValue) v = 2 + Zero assert v == 2 assert isinstance(v, ScalarValue) v = 2 - Zero assert v == 2 assert isinstance(v, ScalarValue) v = sympify(2) + Zero assert v == 2 assert isinstance(v, SympyBasic) v = sympify(2) - Zero assert v == 2 assert isinstance(v, SympyBasic) v = One + 2 assert v == 3 assert isinstance(v, ScalarValue) v = 2 + One assert v == 3 assert isinstance(v, ScalarValue) v = 2 - One assert v is One v = 3 - One assert v == 2 assert isinstance(v, ScalarValue) v = One - 3 assert v == -2 assert isinstance(v, ScalarValue) v = sympify(2) + One assert v == 3 assert isinstance(v, SympyBasic) v = sympify(2) - One assert v == 1 assert isinstance(v, SympyBasic) v = sympify(3) - One assert v == 2 assert isinstance(v, SympyBasic) with pytest.raises(TypeError): None + Zero with pytest.raises(TypeError): None - Zero with pytest.raises(TypeError): None + One with pytest.raises(TypeError): None - One alpha = symbols('alpha') assert Zero * alpha is Zero v = alpha * Zero assert v == Zero assert isinstance(v, SympyBasic) assert 3 * Zero is Zero with pytest.raises(TypeError): None * Zero assert Zero * alpha is Zero assert Zero // 3 is Zero assert One // 1 is One assert One / 1 is One assert One == 1 assert One != symbols('alpha') assert One <= One assert One <= three assert One >= Zero assert One >= -three assert One < three assert One > -three assert One == spOne assert One <= spOne assert One >= spOne assert One < sympify(3) assert One > -sympify(3) with pytest.raises(TypeError): assert One < symbols('alpha') with pytest.raises(TypeError): assert One <= symbols('alpha') with pytest.raises(TypeError): assert One > symbols('alpha') with pytest.raises(TypeError): assert One >= symbols('alpha') with pytest.raises(ZeroDivisionError): One // 0 with pytest.raises(ZeroDivisionError): One / 0 with pytest.raises(TypeError): One // None with pytest.raises(TypeError): One / None with pytest.raises(ZeroDivisionError): 3 // Zero with pytest.raises(TypeError): Zero // None with pytest.raises(TypeError): None // Zero assert Zero / 3 is Zero with pytest.raises(TypeError): Zero / None assert Zero % 3 is Zero assert Zero % three is Zero with pytest.raises(TypeError): assert Zero % None assert One % 3 is One assert One % three is One assert three % One is Zero assert 3 % One is Zero with pytest.raises(TypeError): None % 3 v = sympify(3) % One assert v == 0 assert isinstance(v, SympyBasic) with pytest.raises(TypeError): assert One % None with pytest.raises(TypeError): assert None % One assert Zero**2 is Zero assert Zero**spHalf is Zero with pytest.raises(TypeError): Zero**None with pytest.raises(ZeroDivisionError): v = Zero**-1 with pytest.raises(ZeroDivisionError): v = 1 / Zero v = spOne / Zero assert v == sympy_infinity with pytest.raises(ZeroDivisionError): v = 1 / Zero assert One - Zero is One assert Zero * One is Zero assert One * Zero is Zero with pytest.raises(ZeroDivisionError): v = 3 / Zero with pytest.raises(ZeroDivisionError): v = 3 % Zero v = 3 / One assert v == 3 assert isinstance(v, ScalarValue) v = 3 % One assert v is Zero v = 1 % three assert v is One v = spOne % three assert v == 1 assert isinstance(v, SympyBasic) v = sympify(2) % three assert v == 2 with pytest.raises(TypeError): None % three assert 3**Zero is One v = 3**One assert v == 3 assert isinstance(v, ScalarValue) v = complex(Zero) assert v == 0j assert isinstance(v, complex) v = int(Zero) assert v == 0 assert isinstance(v, int) v = float(Zero) assert v == 0.0 assert isinstance(v, float) v = complex(One) assert v == 1j assert isinstance(v, complex) v = int(One) assert v == 1 assert isinstance(v, int) v = float(One) assert v == 1.0 assert isinstance(v, float) assert braket**Zero is One assert braket**0 is One assert braket**One is braket assert braket**1 is braket v = 1 / braket assert v == braket**(-1) assert isinstance(v, ScalarPower) assert v.base == braket assert v.exp == -1 v = three * braket assert isinstance(v, ScalarTimes) assert v == braket * 3 assert v == braket * sympify(3) assert v == 3 * braket assert v == sympify(3) * braket assert braket * One is braket assert braket * Zero is Zero assert One * braket is braket assert Zero * braket is Zero assert spOne * braket is braket assert spZero * braket is Zero with pytest.raises(TypeError): braket // 3 with pytest.raises(TypeError): braket % 3 with pytest.raises(TypeError): 1 // braket with pytest.raises(TypeError): 3 % braket with pytest.raises(TypeError): 3**braket assert 0**braket is Zero assert 1**braket is One assert spZero**braket is Zero assert spOne**braket is One assert One**braket is One assert 0 // braket is Zero assert 0 / braket is Zero assert 0 % braket is Zero with pytest.raises(ZeroDivisionError): assert 0 / Zero with pytest.raises(ZeroDivisionError): assert 0 / ScalarValue.create(0) assert 0 / ScalarValue(0) == sympy.nan A = OperatorSymbol('A', hs=0) v = A / braket assert isinstance(v, ScalarTimesOperator) assert v.coeff == braket**-1 assert v.term == A with pytest.raises(TypeError): v = None / braket assert braket / three == (1 / three) * braket == (spOne / 3) * braket assert braket / 3 == (1 / three) * braket v = braket / 0.25 assert v == 4 * braket # 0.25 and 4 are exact floats assert braket / sympify(3) == (1 / three) * braket assert 3 / braket == 3 * braket**-1 assert three / braket == 3 * braket**-1 assert spOne / braket == braket**-1 braket2 = BraKet.create(KetSymbol("Chi", hs=0), KetSymbol("Psi", hs=0)) v = braket / braket2 assert v == braket * braket2**-1 with pytest.raises(ZeroDivisionError): braket / Zero with pytest.raises(ZeroDivisionError): braket / 0 with pytest.raises(ZeroDivisionError): braket / sympify(0) assert braket / braket is One with pytest.raises(TypeError): braket / None v = 1 + braket assert v == braket + 1 assert isinstance(v, Scalar) v = One + braket assert v == braket + One assert isinstance(v, Scalar) assert Zero + braket is braket assert spZero + braket is braket assert braket + Zero is braket assert braket + spZero is braket assert 0 + braket is braket assert braket + 0 is braket assert (-1) * braket == -braket assert Zero - braket == -braket assert spZero - braket == -braket assert braket - Zero is braket assert braket - spZero is braket assert 0 - braket == -braket assert braket - 0 is braket assert sympify(3) - braket == 3 - braket
def test_scalar_indexed_sum(braket): """Test instantiation and behavior of a ScalarIndexedSum""" i = IdxSym('i') ip = i.prime ipp = ip.prime alpha = IndexedBase('alpha') a = symbols('a') hs = LocalSpace(0) ket_sum = KetIndexedSum( alpha[1, i] * BasisKet(FockIndex(i), hs=hs), ranges=(IndexOverRange(i, 1, 2), ), ) bra = KetSymbol('Psi', hs=hs).dag() expr = bra * ket_sum half = sympify(1) / 2 assert isinstance(expr, ScalarIndexedSum) assert isinstance(expr.term, ScalarTimes) assert expr.term == bra * ket_sum.term assert expr.ranges == ket_sum.ranges assert expr.doit() == (alpha[1, 1] * bra * BasisKet(1, hs=hs) + alpha[1, 2] * bra * BasisKet(2, hs=hs)) expr = ScalarIndexedSum.create(i, ranges=(IndexOverRange(i, 1, 2), )) assert expr == ScalarIndexedSum(i, ranges=(IndexOverRange(i, 1, 2), )) assert isinstance(expr.doit(), ScalarValue) assert expr.doit() == 3 assert expr.real == expr assert expr.imag == Zero assert expr.conjugate() == expr assert 3 * expr == expr * 3 == Sum(i, 1, 2)(3 * i) assert a * expr == expr * a == Sum(i, 1, 2)(a * i) assert braket * expr == ScalarTimes(braket, Sum(i, 1, 2)(i)) assert expr * braket == ScalarTimes(braket, Sum(i, 1, 2)(i)) assert (2 * i) * expr == 2 * expr * i assert (2 * i) * expr == Sum(i, 1, 2)(2 * i * i.prime) assert expr * expr == ScalarIndexedSum( ScalarValue(i * ip), ranges=(IndexOverRange(i, 1, 2), IndexOverRange(ip, 1, 2)), ) sum3 = expr**3 assert sum3 == ScalarIndexedSum( ScalarValue(i * ip * ipp), ranges=( IndexOverRange(i, 1, 2), IndexOverRange(ip, 1, 2), IndexOverRange(ipp, 1, 2), ), ) assert expr**0 is One assert expr**1 is expr assert (expr**alpha).exp == alpha assert expr**-1 == 1 / expr assert (1 / expr).exp == -1 assert (expr**-alpha).exp == -alpha sqrt_sum = sqrt(expr) assert sqrt_sum == ScalarPower(expr, ScalarValue(half)) expr = ScalarIndexedSum.create(I * i, ranges=(IndexOverRange(i, 1, 2), )) assert expr.real == Zero assert expr.imag == ScalarIndexedSum.create(i, ranges=(IndexOverRange( i, 1, 2), )) assert expr.conjugate() == -expr
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)"
ZeroOperator, no_instance_caching, ) One = sympy.S.One Half = One / 2 gamma = symbols('gamma') hs0 = LocalSpace(0) OpA = OperatorSymbol('A', hs=hs0) OpB = OperatorSymbol('B', hs=hs0) OpC = OperatorSymbol('C', hs=hs0) i = IdxSym('i') j = IdxSym('j') a = IndexedBase('a') ket_sym_hs01 = KetSymbol('Psi', hs=(LocalSpace(0, dimension=2) * LocalSpace(1, dimension=2))) # The following list defines all the automatic rules we want to test # (see `test_rule`). Every tuple in the list yields an independent test. Each # tuple has five elements: # # * The class (``cls``) for which we want to test a rule # * The name of the rule we want to test, as a string. This name must be a # key in the class' `_rules` or `_binary_rules` class attribute # * The positional arguments for the instantiation of the class # (as a tuple ``args``) # * The keyword arguments for the instantiation of the class # (as a dict ``kwargs``) # * The expression that is the expected result from # ``cls.create(*args, # **kwargs)``
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|Ψ⟩⁽⁰⁾"