Assumptions with complex variables (possible BUG)

44 views
Skip to first unread message

Janusz Przewocki

unread,
Mar 17, 2020, 1:27:23 PM3/17/20
to sage-devel
I think I have found a bug.

In the code below I am trying to define a symbolic unitary matrix. In doing so, I define variables a, b, c, d - entries of the matrix. Unitary condition gives us assumptions on a, b, c, d, that I force using assume function. While trying to check if my matrix is unitary I have observed strange results.

In particular, Out[5] does not make sense, since c*conjugate(c) is always nonnegative.


+*In[1]:*+
[source, ipython2]
----
#We define variables
a, b, c, d = var('a, b, c, d')

#U is supposed to be an unitary matrix
U = matrix([[a, b], [c, d]])
M = U.H*U - identity_matrix(2)

#Assumptions on the entries of U
U_asm = [M[i][j] == 0 for i in range(2) for j in range(2)]
assume([U_asm[a] for a in [0, 1, 3]])
----


+*In[2]:*+
[source, ipython2]
----
assumptions()
----


+*Out[2]:*+
----[a*conjugate(a) + c*conjugate(c) - 1 == 0,
 b*conjugate(a) + d*conjugate(c) == 0,
 b*conjugate(b) + d*conjugate(d) - 1 == 0]----


+*In[3]:*+
[source, ipython2]
----
U.H*U == identity_matrix(2)
----


+*Out[3]:*+
----True----


+*In[4]:*+
[source, ipython2]
----
bool(a*conjugate(a) + c*conjugate(c) - 1 == 0)
----


+*Out[4]:*+
----False----


+*In[5]:*+
[source, ipython2]
----
a*conjugate(a) + c*conjugate(c) - 1 == 0
----


+*Out[5]:*+
----c*conjugate(c) + 8 == 0----


+*In[6]:*+
[source, ipython2]
----
version()
----


+*Out[6]:*+
----'SageMath version 9.0, Release Date: 2020-01-01'----
---------------------------------------
My system is: Arch Linux
My architecture is: 64 bit.


Markus Wageringel

unread,
Mar 18, 2020, 5:10:55 AM3/18/20
to sage-devel
You are probably using Sage with Python 2. In Python 2, a list expression involving an index variable `a` like

    assume([U_asm[a] for a in [0, 1, 3]])

overwrites the previous content of the variable `a`. At the end of the loop, `a` will have the value `3`, which leads to the output you obtained. If you rename the loop index to something other than a,b,c,d, the problem goes away.

With Python 3, this problem does not exist either, as loop variables are local as one would expect. Therefore, I suggest to switch to using Sage with Python 3.

Janusz Przewocki

unread,
Mar 18, 2020, 5:51:36 AM3/18/20
to sage-devel
Thank you, I did not notice it in my code.

Regards
  Janusz
Reply all
Reply to author
Forward
0 new messages