Skip to content

WIP: Exploratory tests #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Tests

on:
push:
branches: [ master ]
pull_request:

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
python-version: ["3.10"]
os: [ubuntu-latest]

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install qutip-symbolic
run: |
python -m pip install --upgrade pip
pip install -e .[tests]
- name: Test with pytest
run: |
pytest tests --strict-config --strict-markers --verbosity=1
3 changes: 2 additions & 1 deletion src/qutip_symbolic/commutators.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

from .compat.commutator import Commutator
#from .compat.commutator import Commutator
from sympy.physics.quantum.commutator import Commutator


def recursive_commutator(a, b, n=1):
Expand Down
43 changes: 22 additions & 21 deletions src/qutip_symbolic/operator_utilities.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from sympy import Add, Mul, Pow, Symbol
from .compat.operator import Operator
from sympy import Add, Mul, Pow, Symbol, Expr
#from .compat.operator import Operator
from sympy.physics.quantum import Operator
from sympy.physics.quantum.operatorordering import normal_ordered_form

debug = False # TODO: replace with logging


def split_coeff_operator(e):
"""
Expand All @@ -16,42 +15,44 @@ def split_coeff_operator(e):
Commuting factors and noncommuting (operator) factors
"""
if isinstance(e, Symbol):
return e, 1

if isinstance(e, Operator):
if e.is_commutative:
return e, 1
return 1, e

if isinstance(e, Pow):
c, o = split_coeff_operator(e.base)
return (c ** e.exp, o ** e.exp)

if isinstance(e, Mul):
c_args = []
o_args = []

for arg in e.args:
if isinstance(arg, Operator):
o_args.append(arg)
elif isinstance(arg, Pow):
c, o = split_coeff_operator(arg.base)

if c and c != 1:
c_args.append(c ** arg.exp)
if o and o != 1:
o_args.append(o ** arg.exp)
elif isinstance(arg, Add):
if isinstance(arg, Add):
if arg.is_commutative:
c_args.append(arg)
else:
o_args.append(arg)
else:
c_args.append(arg)
c, o = split_coeff_operator(arg)
if c != 1:
c_args.append(c)
if o != 1:
o_args.append(o)

return Mul(*c_args), Mul(*o_args)

if isinstance(e, Add):
return [split_coeff_operator(arg) for arg in e.args]

if debug:
print("Warning: Unrecognized type of e: %s" % type(e))
if isinstance(e, Expr): # also covers the Operator case
# is_commuative may also be None
if e.is_commutative is True:
return e, 1
elif e.is_commutative is False:
return 1, e

return None, None
raise TypeError(f"split_coeff_operator: {e!r} has unsupported type")


def extract_operators(e, independent=False):
Expand Down
16 changes: 16 additions & 0 deletions tests/test_bch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from sympy.core.symbol import symbols
from sympy.physics.quantum.commutator import Commutator as Comm

from qutip_symbolic.bch import _bch_expansion

A, B = symbols('A, B', commutative=False)


def test_simple_expand():
C = _bch_expansion(A, B, N=4)
assert C == (
B
+ Comm(A, B)
+ Comm(A, Comm(A, B)) / 2
+ Comm(A, Comm(A, Comm(A, B))) / 6
)
16 changes: 16 additions & 0 deletions tests/test_commutators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from sympy.core.symbol import symbols
from sympy.physics.quantum.commutator import Commutator as Comm

from qutip_symbolic.commutators import recursive_commutator

A, B = symbols('A, B', commutative=False)


def test_recursive_commutators_n_eq_1():
C = recursive_commutator(A, B, n=1)
assert C == Comm(A, B)


def test_recursive_commutators_n_eq_2():
C = recursive_commutator(A, B, n=2)
assert C == Comm(A, Comm(A, B))
28 changes: 28 additions & 0 deletions tests/test_operator_utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import pytest
from sympy.core.symbol import symbols, Expr
from sympy.physics.quantum import Operator

from qutip_symbolic.operator_utilities import split_coeff_operator

a, b = symbols('a, b')
A, B = symbols('A, B', commutative=False)
Op = Operator()
E = Expr()


def test_split_coeff_operator():
assert split_coeff_operator(A) == (1, A)
assert split_coeff_operator(Op) == (1, Op)
assert split_coeff_operator(a) == (a, 1)
assert split_coeff_operator(A * B) == (1, A * B)
assert split_coeff_operator(a * A) == (a, A)
assert split_coeff_operator(a * A * b * B) == (a * b, A * B)
assert split_coeff_operator(a * A + b * B) == [(a, A), (b, B)]
assert split_coeff_operator(A + B) == [(1, A), (1, B)]
assert split_coeff_operator(a + b) == [(a, 1), (b, 1)]
assert split_coeff_operator(a * (A + B)) == (a, A + B)
with pytest.raises(TypeError) as err:
split_coeff_operator(E)
assert str(err.value) == (
"split_coeff_operator: Expr() has unsupported type"
)