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 sop_exprs(): """Prepare a list of super operator algebra expressions""" hs1 = LocalSpace('q1', dimension=2) hs2 = LocalSpace('q2', dimension=2) A = SuperOperatorSymbol("A", hs=hs1) B = SuperOperatorSymbol("B", hs=hs1) C = SuperOperatorSymbol("C", hs=hs2) L = SuperOperatorSymbol("L", hs=1) M = SuperOperatorSymbol("M", hs=1) A_op = OperatorSymbol("A", hs=1) gamma = symbols('gamma') return [ SuperOperatorSymbol("A", hs=hs1), SuperOperatorSymbol("A_1", hs=hs1 * hs2), SuperOperatorSymbol("A", symbols('alpha'), symbols('beta'), hs=hs1), IdentitySuperOperator, ZeroSuperOperator, A + B, A * B, A * C, 2 * A, (1 + 2j) * A, -(gamma**2) / 2 * A, SuperAdjoint(A + B), 2 * A - sqrt(gamma) * (B + C), SPre(A_op), SPost(A_op), SuperOperatorTimesOperator(L, sqrt(gamma) * A_op), SuperOperatorTimesOperator((L + 2 * M), A_op), ]
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 test_ascii_sop_operations(): """Test the ascii representation of super operator algebra operations""" hs1 = LocalSpace('q_1', dimension=2) hs2 = LocalSpace('q_2', dimension=2) A = SuperOperatorSymbol("A", hs=hs1) B = SuperOperatorSymbol("B", hs=hs1) C = SuperOperatorSymbol("C", hs=hs2) L = SuperOperatorSymbol("L", hs=1) M = SuperOperatorSymbol("M", hs=1) A_op = OperatorSymbol("A", hs=1) 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(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(SuperAdjoint(A)) == 'A^(q_1)H' assert ascii(SuperAdjoint(A + B)) == '(A^(q_1) + B^(q_1))^H' assert ascii(A - B) == 'A^(q_1) - B^(q_1)' assert ascii(A - B + C) == 'A^(q_1) - B^(q_1) + C^(q_2)' assert ( ascii(2 * A - sqrt(gamma) * (B + C)) == '2 * A^(q_1) - sqrt(gamma) * (B^(q_1) + C^(q_2))') assert ascii(SPre(A_op)) == 'SPre(A^(1))' assert ascii(SPost(A_op)) == 'SPost(A^(1))' assert ascii(SuperOperatorTimesOperator(L, A_op)) == 'L^(1)[A^(1)]' assert (ascii(SuperOperatorTimesOperator( L, sqrt(gamma) * A_op)) == 'L^(1)[sqrt(gamma) * A^(1)]') assert (ascii(SuperOperatorTimesOperator( (L + 2 * M), A_op)) == '(L^(1) + 2 * M^(1))[A^(1)]')
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_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_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_indexed_hs_not_disjoint(): i, j = symbols('i, j', cls=IdxSym) hs_i = LocalSpace(StrLabel(i)) hs_j = LocalSpace(StrLabel(j)) assert not hs_i.isdisjoint(hs_i) assert not hs_i.isdisjoint(hs_j) expr = Create(hs=hs_j) * Destroy(hs=hs_i) assert expr.args == (Create(hs=hs_j), Destroy(hs=hs_i))
def bell1_expr(): 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) return (ket_e1 * ket_g2 - I * ket_g1 * ket_e2) / sqrt(2)
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 test_hilbert_tree(): """Test tree representation of a Hilbert space algebra expression""" H1 = LocalSpace(1) H2 = LocalSpace(2) tree = tree_str(H1 * H2) assert (tree == dedent(r''' . ProductSpace(ℌ₁, ℌ₂) ├─ LocalSpace(1) └─ LocalSpace(2) ''').strip())
def test_quantum_symbols_with_indexedhs(): """Test the free_symbols method for objects that have a Hilbert space with a sybmolic label, for the example of an OperatorSymbol""" i, j = symbols('i, j', cls=IdxSym) hs_i = LocalSpace(StrLabel(i)) hs_j = LocalSpace(StrLabel(j)) A = OperatorSymbol("A", hs=hs_i * hs_j) assert A.free_symbols == {i, j} expr = Create(hs=hs_i) * Destroy(hs=hs_i) assert expr.free_symbols == { i, }
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_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_operator_times_order(): A1 = OperatorSymbol("A", hs=1) B1 = OperatorSymbol("B", hs=1) A2 = OperatorSymbol("A", hs=2) A3 = OperatorSymbol("A", hs=3) B4 = OperatorSymbol("B", hs=4) B1_m = OperatorSymbol("B", hs=LocalSpace(1, order_index=2)) B2_m = OperatorSymbol("B", hs=LocalSpace(2, order_index=1)) assert A1 * A2 == A2 * A1 assert A1 * B1 != B1 * A1 assert (A2 * A1).operands == (A1, A2) assert (B2_m * B1_m).operands == (B2_m, B1_m) assert ((B4 + A3) * (A2 + A1)).operands == (A1 + A2, A3 + B4)
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_operator_elements(): """Test the unicode representation of "atomic" operator algebra elements""" hs1 = LocalSpace('q1', dimension=2) hs2 = LocalSpace('q2', dimension=2) alpha, beta = symbols('alpha, beta') assert unicode(OperatorSymbol("A", hs=hs1)) == 'A\u0302^(q\u2081)' # Â^(q₁) assert (unicode(OperatorSymbol('A', hs=1), show_hs_label='subscript') == 'A\u0302\u208d\u2081\u208e' ) # Â₍₁₎ assert (unicode( OperatorSymbol("A", hs=hs1), unicode_op_hats=False, unicode_sub_super=False, ) == 'A^(q_1)') assert (unicode(OperatorSymbol( "A_1", hs=hs1 * hs2)) == 'A\u0302_1^(q\u2081\u2297q\u2082)') # Â_1^(q₁⊗q₂) assert (unicode(OperatorSymbol( "Xi_2", hs=('q1', 'q2'))) == '\u039e\u0302_2^(q\u2081\u2297q\u2082)' ) # Ξ̂_2^(q₁⊗q₂) assert unicode(OperatorSymbol("Xi", alpha, beta, hs=1)) == ('Ξ̂⁽¹⁾(α, β)') assert unicode(IdentityOperator) == "𝟙" assert unicode(ZeroOperator) == "0" assert unicode(Create(hs=1)) == 'a\u0302^(1)\u2020' # â^(1)† assert unicode(Destroy(hs=1)) == 'a\u0302\u207d\xb9\u207e' # â⁽¹⁾ assert unicode(Destroy(hs=1), unicode_sub_super=False) == 'a\u0302^(1)' assert unicode(Destroy(hs=1), unicode_op_hats=False) == 'a\u207d\xb9\u207e' assert (unicode(Destroy(hs=1), unicode_op_hats=False, unicode_sub_super=False) == 'a^(1)') assert (unicode(Squeeze(Rational(1, 2), hs=1)) == 'Squeeze\u207d\xb9\u207e(1/2)') # Squeeze⁽¹⁾(1/2) hs_tls = LocalSpace('1', basis=('g', 'e')) sig_e_g = LocalSigma('e', 'g', hs=hs_tls) assert unicode(sig_e_g) == '|e⟩⟨g|⁽¹⁾' assert unicode(sig_e_g, unicode_sub_super=False) == '|e⟩⟨g|^(1)' assert unicode(sig_e_g, show_hs_label=False) == '|e⟩⟨g|' assert (unicode(sig_e_g, sig_as_ketbra=False) == '\u03c3\u0302_e,g^(1)' ) # σ̂_e,g^(1) sig_e_e = LocalProjector('e', hs=hs_tls) assert unicode(sig_e_e) == '|e⟩⟨e|⁽¹⁾' assert (unicode( sig_e_e, sig_as_ketbra=False) == '\u03a0\u0302\u2091\u207d\xb9\u207e') # Π̂ₑ⁽¹⁾ assert (unicode(BasisKet(0, hs=1) * BasisKet(0, hs=2) * BasisKet(0, hs=3)) == '|0,0,0⟩^(1⊗2⊗3)') assert unicode(BasisKet(0, hs=hs1) * BasisKet(0, hs=hs2)) == '|00⟩^(q₁⊗q₂)'
def test_ascii_sop_elements(): """Test the ascii representation of "atomic" Superoperators""" hs1 = LocalSpace('q1', dimension=2) hs2 = LocalSpace('q2', dimension=2) alpha, beta = symbols('alpha, beta') assert ascii(SuperOperatorSymbol("A", hs=hs1)) == 'A^(q1)' assert ascii(SuperOperatorSymbol("A_1", hs=hs1 * hs2)) == 'A_1^(q1*q2)' assert (ascii(SuperOperatorSymbol("Xi_2", hs=('q1', 'q2'))) == 'Xi_2^(q1*q2)') assert (ascii(SuperOperatorSymbol("Xi", alpha, beta, hs=hs1)) == 'Xi^(q1)(alpha, beta)') assert ascii(SuperOperatorSymbol("Xi_full", hs=1)) == 'Xi_full^(1)' with pytest.raises(ValueError): SuperOperatorSymbol(r'\Xi^2', hs='a') assert ascii(IdentitySuperOperator) == "1" assert ascii(ZeroSuperOperator) == "0"
def test_qubit_state_bra(): """Test sum_i alpha_i <i| for TLS""" i = IdxSym('i') alpha = IndexedBase('alpha') alpha_i = alpha[i] hs_tls = LocalSpace('tls', basis=('g', 'e')) term = alpha_i * BasisKet(FockIndex(i), hs=hs_tls).dag() expr = KetIndexedSum.create(term, ranges=IndexOverFockSpace(i, hs=hs_tls)) assert IndexOverFockSpace(i, hs=hs_tls) in expr.ket.kwargs['ranges'] assert ascii(expr) == "Sum_{i in H_tls} alpha_i * <i|^(tls)" assert expr.ket.term.free_symbols == set([i, symbols('alpha'), alpha_i]) assert expr.free_symbols == set([symbols('alpha'), alpha_i]) assert expr.ket.variables == [i] assert expr.space == hs_tls assert len(expr.ket.args) == 1 assert len(expr.ket.operands) == 1 assert len(expr.ket.kwargs) == 1 assert expr.ket.args[0] == term.ket assert expr.ket.term == term.ket assert len(expr.kwargs) == 0 expr_expand = Bra.create(expr.ket.doit().substitute({ alpha[0]: alpha['g'], alpha[1]: alpha['e'] })) assert expr_expand == (alpha['g'] * BasisKet('g', hs=hs_tls).dag() + alpha['e'] * BasisKet('e', hs=hs_tls).dag()) assert ascii(expr_expand) == 'alpha_e * <e|^(tls) + alpha_g * <g|^(tls)'
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_invalid_spin_basis_ket(): """Test that trying to instantiate invalid :func:`SpinBasisKet` raises the appropriate exceptions""" hs1 = SpinSpace('s', spin='3/2') hs2 = SpinSpace('s', spin=1) with pytest.raises(TypeError) as exc_info: SpinBasisKet(1, 2, hs=LocalSpace(1)) assert "must be a SpinSpace" in str(exc_info.value) with pytest.raises(TypeError) as exc_info: SpinBasisKet(1, hs=hs1) assert "exactly two positional arguments" in str(exc_info.value) with pytest.raises(TypeError) as exc_info: SpinBasisKet(1, 2, hs=hs2) assert "exactly one positional argument" in str(exc_info.value) with pytest.raises(ValueError) as exc_info: SpinBasisKet(1, 3, hs=hs1) assert "must be 2" in str(exc_info.value) with pytest.raises(ValueError) as exc_info: SpinBasisKet(-5, 2, hs=hs1) assert "must be in range" in str(exc_info.value) with pytest.raises(ValueError) as exc_info: SpinBasisKet(5, 2, hs=hs1) assert "must be in range" in str(exc_info.value) with pytest.raises(ValueError) as exc_info: SpinBasisKet(-5, hs=hs2) assert "must be in range" in str(exc_info.value) with pytest.raises(ValueError) as exc_info: SpinBasisKet(5, hs=hs2) assert "must be in range" in str(exc_info.value)
def test_coherent_state(): """Test fock representation of coherent state""" alpha = symbols('alpha') hs0 = LocalSpace(0) hs1 = LocalSpace(1, dimension=3) i = IdxSym('i') n = IdxSym('n') psi = CoherentStateKet(alpha, hs=hs0) psi_focksum_3 = psi.to_fock_representation(max_terms=3) assert len(psi_focksum_3.term) == 3 for n_val in (0, 1, 2): assert n_val in psi_focksum_3.term.ranges[0] psi_focksum_inf = psi.to_fock_representation() with pytest.raises(InfiniteSumError): len(psi_focksum_inf.term) for n_val in (0, 1, 2, 3): assert n_val in psi_focksum_inf.term.ranges[0] assert psi_focksum_inf.term.term.free_symbols == set([n, symbols('alpha')]) assert psi_focksum_inf.free_symbols == set([symbols('alpha')]) assert psi_focksum_inf.term.variables == [n] assert psi_focksum_inf.substitute({n: i}).term.variables == [i] assert (psi_focksum_inf.substitute({hs0: hs1}) == CoherentStateKet( alpha, hs=hs1).to_fock_representation()) assert (psi.to_fock_representation(index_symbol='i').substitute( {i: n}) == psi_focksum_inf) assert (psi.to_fock_representation(index_symbol=i).substitute( {i: n}) == psi_focksum_inf) assert psi_focksum_3.doit([IndexedSum ]) == psi_focksum_inf.doit([IndexedSum], max_terms=3) psi_expanded_3 = psi_focksum_3.doit([IndexedSum]) assert psi_expanded_3 == ( sympy.exp(-alpha * alpha.conjugate() / 2) * KetPlus( BasisKet(0, hs=LocalSpace(0)), ScalarTimesKet(alpha, BasisKet(1, hs=LocalSpace(0))), ScalarTimesKet(alpha**2 / sympy.sqrt(2), BasisKet(2, hs=LocalSpace(0))), )) psi = CoherentStateKet(alpha, hs=hs1) assert psi.to_fock_representation().doit( [IndexedSum]) == psi_expanded_3.substitute({hs0: hs1})
def test_ket_indexed_sum_simplify_scalar(): """Test calling the simplify_scalar method of an KetIndexedSum.""" # This tests originates from some broken behavior when IndexedSum received # `ranges` as a positional argument instead of a keyword argument. a, b, ϕ = symbols('a, b, phi') factor = (a + b) * sympy.exp(I * ϕ) factor_expand = factor.expand() hs = LocalSpace(0) n = symbols('n', cls=IdxSym) psi_n = hs.basis_state(FockIndex(n)) expr = KetIndexedSum(factor * psi_n, ranges=(IndexOverFockSpace(n, hs=hs), )) expr_expand = expr.simplify_scalar(sympy.expand) expected = factor_expand * KetIndexedSum( psi_n, ranges=(IndexOverFockSpace(n, hs=hs), )) assert expr_expand != expected.term # happened when ranges was an argument assert expr_expand == expected
def test_cached_srepr(bell1_expr): """Test that we can get simplified expressions by passing a cache, and that the cache is updated appropriately while printing""" hs1 = LocalSpace('q_1', basis=('g', 'e')) hs2 = LocalSpace('q_2', basis=('g', 'e')) ket_g1 = BasisKet('g', hs=hs1) cache = {hs1: 'hs1', hs2: 'hs2', 1 / sqrt(2): '1/sqrt(2)', -I: '-I'} res = srepr(bell1_expr, cache=cache) expected = ( "ScalarTimesKet(1/sqrt(2), KetPlus(TensorKet(BasisKet('e', hs=hs1), " "BasisKet('g', hs=hs2)), ScalarTimesKet(-I, " "TensorKet(BasisKet('g', hs=hs1), BasisKet('e', hs=hs2)))))") assert res == expected assert ket_g1 in cache assert cache[ket_g1] == "BasisKet('g', hs=hs1)" cache = {hs1: 'hs1', hs2: 'hs2', 1 / sqrt(2): '1/sqrt(2)', -I: '-I'} # note that we *must* use a different cache res = srepr(bell1_expr, cache=cache, indented=True) expected = dedent(r''' ScalarTimesKet( 1/sqrt(2), KetPlus( TensorKet( BasisKet( 'e', hs=hs1), BasisKet( 'g', hs=hs2)), ScalarTimesKet( -I, TensorKet( BasisKet( 'g', hs=hs1), BasisKet( 'e', hs=hs2)))))''').strip() assert res == expected assert ket_g1 in cache assert cache[ket_g1] == "BasisKet(\n 'g',\n hs=hs1)"
def test_tex_sop_elements(): """Test the tex representation of "atomic" Superoperators""" hs1 = LocalSpace('q1', dimension=2) hs2 = LocalSpace('q2', dimension=2) alpha, beta = symbols('alpha, beta') assert latex(SuperOperatorSymbol("A", hs=hs1)) == r'\mathrm{A}^{(q_{1})}' assert (latex(SuperOperatorSymbol( "A_1", hs=hs1 * hs2)) == r'\mathrm{A}_{1}^{(q_{1} \otimes q_{2})}') assert (latex(SuperOperatorSymbol( "Xi", alpha, beta, hs=hs1)) == r'\mathrm{\Xi}^{(q_{1})}\left(\alpha, \beta\right)') assert (latex(SuperOperatorSymbol( "Xi_2", hs=('q1', 'q2'))) == r'\mathrm{\Xi}_{2}^{(q_{1} \otimes q_{2})}') assert (latex(SuperOperatorSymbol( "Xi_full", hs=1)) == r'\mathrm{\Xi}_{\text{full}}^{(1)}') assert latex(IdentitySuperOperator) == r'\mathbb{1}' assert latex(ZeroSuperOperator) == r'\mathbb{0}'
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_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_operator_kronecker_sum(): """Test that Kronecker delta are eliminiated from indexed sums over operators""" i = IdxSym('i') j = IdxSym('j') alpha = symbols('alpha') delta_ij = KroneckerDelta(i, j) delta_0i = KroneckerDelta(0, i) delta_1j = KroneckerDelta(1, j) delta_0j = KroneckerDelta(0, j) delta_1i = KroneckerDelta(1, i) def A(i, j): return OperatorSymbol(StrLabel(IndexedBase('A')[i, j]), hs=0) term = delta_ij * A(i, j) sum = OperatorIndexedSum.create(term, ranges=(IndexOverList(i, (1, 2)), IndexOverList(j, (1, 2)))) assert sum == OperatorIndexedSum.create(A(i, i), ranges=(IndexOverList(i, (1, 2)), )) assert sum.doit() == (OperatorSymbol("A_11", hs=0) + OperatorSymbol("A_22", hs=0)) term = alpha * delta_ij * A(i, j) range_i = IndexOverList(i, (1, 2)) range_j = IndexOverList(j, (1, 2)) sum = OperatorIndexedSum.create(term, ranges=(range_i, range_j)) assert isinstance(sum, ScalarTimesOperator) expected = alpha * OperatorIndexedSum.create( A(i, i), ranges=(IndexOverList(i, (1, 2)), )) assert sum == expected hs = LocalSpace('0', basis=('g', 'e')) i_range = IndexOverFockSpace(i, hs) j_range = IndexOverFockSpace(j, hs) sig_ij = LocalSigma(FockIndex(i), FockIndex(j), hs=hs) sig_0j = LocalSigma('g', FockIndex(j), hs=hs) sig_i1 = LocalSigma(FockIndex(i), 'e', hs=hs) term = delta_0i * delta_1j * sig_ij sum = OperatorIndexedSum.create(term, ranges=(i_range, )) expected = delta_1j * sig_0j assert sum == expected sum = OperatorIndexedSum.create(term, ranges=(j_range, )) expected = delta_0i * sig_i1 assert sum == expected term = (delta_0i * delta_1j + delta_0j * delta_1i) * sig_ij sum = OperatorIndexedSum.create(term, ranges=(i_range, j_range)) expected = LocalSigma('g', 'e', hs=hs) + LocalSigma('e', 'g', hs=hs) assert sum == expected
def test_tex_sop_operations(): """Test the tex representation of super operator algebra operations""" hs1 = LocalSpace('q_1', dimension=2) hs2 = LocalSpace('q_2', dimension=2) A = SuperOperatorSymbol("A", hs=hs1) B = SuperOperatorSymbol("B", hs=hs1) C = SuperOperatorSymbol("C", hs=hs2) L = SuperOperatorSymbol("L", hs=1) M = SuperOperatorSymbol("M", hs=1) A_op = OperatorSymbol("A", hs=1) gamma = symbols('gamma', positive=True) assert latex(A + B) == r'\mathrm{A}^{(q_{1})} + \mathrm{B}^{(q_{1})}' assert latex(A * B) == r'\mathrm{A}^{(q_{1})} \mathrm{B}^{(q_{1})}' assert latex(A * C) == r'\mathrm{A}^{(q_{1})} \mathrm{C}^{(q_{2})}' assert latex(2 * A) == r'2 \mathrm{A}^{(q_{1})}' assert latex(2j * A) == r'2i \mathrm{A}^{(q_{1})}' assert latex((1 + 2j) * A) == r'(1+2i) \mathrm{A}^{(q_{1})}' assert latex(gamma**2 * A) == r'\gamma^{2} \mathrm{A}^{(q_{1})}' assert (latex(-(gamma**2) / 2 * A) == r'- \frac{\gamma^{2}}{2} \mathrm{A}^{(q_{1})}') assert latex(SuperAdjoint(A)) == r'\mathrm{A}^{(q_{1})\dagger}' assert (latex(SuperAdjoint(A + B)) == r'\left(\mathrm{A}^{(q_{1})} + ' r'\mathrm{B}^{(q_{1})}\right)^\dagger') assert latex(A - B) == r'\mathrm{A}^{(q_{1})} - \mathrm{B}^{(q_{1})}' assert (latex(A - B + C) == r'\mathrm{A}^{(q_{1})} - \mathrm{B}^{(q_{1})} + ' r'\mathrm{C}^{(q_{2})}') assert (latex(2 * A - sqrt(gamma) * (B + C)) == r'2 \mathrm{A}^{(q_{1})} - \sqrt{\gamma} ' r'\left(\mathrm{B}^{(q_{1})} + \mathrm{C}^{(q_{2})}\right)') assert latex(SPre(A_op)) == r'\mathrm{SPre}\left(\hat{A}^{(1)}\right)' assert latex(SPost(A_op)) == r'\mathrm{SPost}\left(\hat{A}^{(1)}\right)' assert (latex(SuperOperatorTimesOperator( L, A_op)) == r'\mathrm{L}^{(1)}\left[\hat{A}^{(1)}\right]') assert (latex(SuperOperatorTimesOperator( L, sqrt(gamma) * A_op)) == r'\mathrm{L}^{(1)}\left[\sqrt{\gamma} \hat{A}^{(1)}\right]') assert (latex(SuperOperatorTimesOperator( (L + 2 * M), A_op)) == r'\left(\mathrm{L}^{(1)} + 2 \mathrm{M}^{(1)}\right)' r'\left[\hat{A}^{(1)}\right]')