A Quantum Module Question

61 views
Skip to first unread message

Uğur Güney

unread,
Nov 3, 2011, 11:51:51 PM11/3/11
to sy...@googlegroups.com
Dear Sympy users,

I'm a physics PhD student, doing research on differences between classical and quantum correlations.

I was in need of a simple calculator which works with quantum states etc. While searching for quantum simulators in Python I came to know that there is already a quantum module in SymPy. Because I couldn't find an explicit documentation I looked at the source files to understand how basic calculations can be done using the quantum module.

Can you please help me in applying a projection operator on a general state. This is the code that I tried:

from sympy import *
from sympy.physics.quantum import *
from sympy.physics.quantum.qubit import *
[c00,c01,c10,c11]=var('c00,c01,c10,c11')  # coefficients
state = c00*Qubit('00')+c01*Qubit('01')+c10*Qubit('10')+c11*Qubit('11') # most general two-qubit state
qbt=Qubit('01') # a qubit
proj = qbt*qbt.dual # projection operator
# proj*state # operator applied on the state
# (proj*state).expand() operator applied on each term in the expansion
print (proj*state).expand().doit() # try to do the inner-products

In the last step "doit()" did not work as I expected, it did not do the inner products. I suspect that the expression involving TensorProduct is not converted to InnerProduct automatically. But normally "(Qubit('01').dual*Qubit('01')).doit()" and "(Qubit('00').dual*Qubit('01')).doit()" work as expected and give 1 and 0.

I'll be happy if you can guide me how to use this beautiful tool.

Best,
ugur guney

Brian Granger

unread,
Nov 4, 2011, 1:27:10 AM11/4/11
to sy...@googlegroups.com
Ugur,

Welcome!

2011/11/3 Uğur Güney <ugur...@gmail.com>:


> Dear Sympy users,
> I'm a physics PhD student, doing research on differences between classical
> and quantum correlations.
> I was in need of a simple calculator which works with quantum states etc.
> While searching for quantum simulators in Python I came to know that there
> is already a quantum module in SymPy. Because I couldn't find an explicit
> documentation I looked at the source files to understand how basic
> calculations can be done using the quantum module.

Great, currently this is the best way to learn about how it all works.

> Can you please help me in applying a projection operator on a general state.
> This is the code that I tried:
> from sympy import *
> from sympy.physics.quantum import *
> from sympy.physics.quantum.qubit import *
> [c00,c01,c10,c11]=var('c00,c01,c10,c11')  # coefficients
> state = c00*Qubit('00')+c01*Qubit('01')+c10*Qubit('10')+c11*Qubit('11') #
> most general two-qubit state
> qbt=Qubit('01') # a qubit
> proj = qbt*qbt.dual # projection operator
> # proj*state # operator applied on the state

The following will work:

qapply(proj*state)

The need for qapply ("quantum apply") is as follows. Because of how
python/sympy handle multiplication, we are only able to detect inner
products in very simple situations like Qubit('01').dual*Qubit('01').
For more complex situations, we just leave it as a general
"multiplication" operation. The qapply function walks through a
general quantum expression and does a couple of things:

* Applies any operators to states.
* Looks for <bra|*|ket> expressions and turns them into inner products.

This is why you need to call qapply by hand after creating a general
quantum expression.

Hope this helps. Also, we would love to know how you end up using
this stuff. It is pretty new, so there is a ton left to do. Also, if
you are looking at classical/quantum correlations, you may be
interested in the work we have done on density matrices. That has not
yet been merged into sympy's master branch, but we plan on doing
that....always looking for help.

Cheers,

Brian

> # (proj*state).expand() operator applied on each term in the expansion
> print (proj*state).expand().doit() # try to do the inner-products
> In the last step "doit()" did not work as I expected, it did not do the
> inner products. I suspect that the expression involving TensorProduct is not
> converted to InnerProduct automatically. But normally
> "(Qubit('01').dual*Qubit('01')).doit()" and
> "(Qubit('00').dual*Qubit('01')).doit()" work as expected and give 1 and 0.
> I'll be happy if you can guide me how to use this beautiful tool.
> Best,
> ugur guney
>

> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to
> sympy+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/sympy?hl=en.
>

--
Brian E. Granger
Cal Poly State University, San Luis Obispo
bgra...@calpoly.edu and elli...@gmail.com

Uğur Güney

unread,
Nov 4, 2011, 9:13:12 AM11/4/11
to sy...@googlegroups.com
Hi!

Thanks for the clear explanation!

Now I realized that even the simplest calculation that is made with pencil and paper involves these kind of background processes that we do unconsciously. Dirac notation is too intuitive for a computer. :-) 


Hope this helps.  Also, we would love to know how you end up using
this stuff.  It is pretty new, so there is a ton left to do.  Also, if
you are looking at classical/quantum correlations, you may be
interested in the work we have done on density matrices.  That has not
yet been merged into sympy's master branch, but we plan on doing
that....always looking for help.

Thanks! OK, I will inform the group about how and where I used Sympy. I'll definitely look at density matrices because I want to work on both pure states and mixed states.

vug

Uğur Güney

unread,
Nov 4, 2011, 10:07:44 AM11/4/11
to sy...@googlegroups.com
Now, I can ask my second question. :-)

Are the states and operators aware of their Hilbert spaces? For example, say, I'm working on a two particle system. I'm only looking at their spin states, described by qubits. First particle lives in Hilbert space H1, and second one in H2. So the combined state lives in H1xH2. I want to apply projection operators which works on either H1 or H2 to this state.

I tried this:

H1=HilbertSpace() # Hilbert space for first particle
H2=HilbertSpace() # Hilbert space for second particle
qb1 = Qubit('0') # first particle's state
qb2 = Qubit('1') # second particle's state
state = TensorProduct(qb1,qb2) # state of combined system
# state.hilbert_space # gave an error: no such attribute
proj = qb2*qb2.dual # projection operator which works on H2
proj*state
(proj*state).doit()
qapply(proj*state)

The result was: "|1><1|*|0>x|1>" Is it possible to convert this expression into "|0>x(|1><1|1>)" which can be further simplified to "|0>x|1>"?

vug

2011/11/4 Uğur Güney <ugur...@gmail.com>

Brian Granger

unread,
Nov 4, 2011, 12:56:12 PM11/4/11
to sy...@googlegroups.com
2011/11/4 Uğur Güney <ugur...@gmail.com>:

> Now, I can ask my second question. :-)
> Are the states and operators aware of their Hilbert spaces? For example,
> say, I'm working on a two particle system. I'm only looking at their spin
> states, described by qubits. First particle lives in Hilbert space H1, and
> second one in H2. So the combined state lives in H1xH2. I want to apply
> projection operators which works on either H1 or H2 to this state.
> I tried this:

All of the Hilbert space stuff is implicit, but you can still do what you want.

> H1=HilbertSpace() # Hilbert space for first particle
> H2=HilbertSpace() # Hilbert space for second particle
> qb1 = Qubit('0') # first particle's state
> qb2 = Qubit('1') # second particle's state
> state = TensorProduct(qb1,qb2) # state of combined system
> # state.hilbert_space # gave an error: no such attribute
> proj = qb2*qb2.dual # projection operator which works on H2

proj = TensorProduct(1, qb2*qb2.dual)
qapply(tensor_product_simp(proj*state))

The extra tensor_product_simp turns a product of TPs into a TP of products.

But the main point is that you have to be careful about creating
tensor products explicitly. This is another place where we tend to be
very sneaky when writing Dirac notation by hand. We keep track of
which Hilbert space each operator is acting in but don't express it in
our notation. As you can imagine, it was quite difficult to teach
SymPy Dirac notation and we have tried to strike a balance between
Mathematical accuracy and easy of use.

Cheers,

Brian

Uğur Güney

unread,
Nov 4, 2011, 1:15:25 PM11/4/11
to sy...@googlegroups.com
On Fri, Nov 4, 2011 at 12:56 PM, Brian Granger <elli...@gmail.com> wrote:
2011/11/4 Uğur Güney <ugur...@gmail.com>:
> Now, I can ask my second question. :-)
> Are the states and operators aware of their Hilbert spaces? For example,
> say, I'm working on a two particle system. I'm only looking at their spin
> states, described by qubits. First particle lives in Hilbert space H1, and
> second one in H2. So the combined state lives in H1xH2. I want to apply
> projection operators which works on either H1 or H2 to this state.
> I tried this:

All of the Hilbert space stuff is implicit, but you can still do what you want.

> H1=HilbertSpace() # Hilbert space for first particle
> H2=HilbertSpace() # Hilbert space for second particle
> qb1 = Qubit('0') # first particle's state
> qb2 = Qubit('1') # second particle's state
> state = TensorProduct(qb1,qb2) # state of combined system
> # state.hilbert_space # gave an error: no such attribute
> proj = qb2*qb2.dual # projection operator which works on H2

proj = TensorProduct(1, qb2*qb2.dual)
qapply(tensor_product_simp(proj*state))

The extra tensor_product_simp turns a product of TPs into a TP of products.

So, adding these lines to the code above
qb1.hilbert_space = H1
qb2.hilbert_space = H2
can not make the system differentiate between two different Hilbert spaces. But I have to keep the order of the objects in the tensor products to indicate the space they belong, such as "1" operator on so-called "H1" and "proj" operator on "H2". I think this is a simple and robust solution.

But, unforunately, when I tried "proj=TensorProduct(1,qb2*qb2.dual)" I got this error:
AttributeError: 'int' object has no attribute 'is_commutative'
 
I think 1 is an integer an does not behaved as an identity operator. Are there any identity operators in Sympy?

vug

Uğur Güney

unread,
Nov 4, 2011, 1:24:25 PM11/4/11
to sy...@googlegroups.com
2011/11/4 Uğur Güney <ugur...@gmail.com>
Without a general abstract identity object, I tried this, by using the property: "Identity = sum over all projection operators"

id=Qubit(0)*Qubit(0).dual+Qubit(1)*Qubit(1).dual # identity operator on a qubit. I can live with this.
proj2=TensorProduct(id,proj) # projection operator which affect only the hilbert space on the right, "H2"
tensor_product_simp(proj2*state) # this function worked exactly as you described!
qapply(tensor_product_simp(proj2*state))

Unfortunately the last line gave the same error:
AttributeError: 'int' object has no attribute 'is_commutative'

Sorry for bombarding you with emails. I get abit excited.

Best,
vug

Aaron Meurer

unread,
Nov 4, 2011, 1:30:19 PM11/4/11
to sy...@googlegroups.com
> But, unforunately, when I tried "proj=TensorProduct(1,qb2*qb2.dual)" I got
> this error:
> AttributeError: 'int' object has no attribute 'is_commutative'
>
> I think 1 is an integer an does not behaved as an identity operator. Are
> there any identity operators in Sympy?
> vug

This is a bug in the quantum module. It needs to have a SymPy
Integer, not a Python int. The quantum function should do this
conversion automatically, but it seems that this one doesn't. Try
using TensorProduct(Integer(1),qb2*qb2.dual) as a workaround.

Aaron Meurer

Brian Granger

unread,
Nov 4, 2011, 2:10:07 PM11/4/11
to sy...@googlegroups.com
Yep Aaron is right, this is a bug.

Uğur Güney

unread,
Nov 4, 2011, 9:42:44 PM11/4/11
to sy...@googlegroups.com
Thanks! "Integer(1)" worked as expected. But can you please tell me why "Qubit(0)*Dagger(Qubit(0))+Qubit(1)*Dagger(Qubit(1))" does not work as the identity operator?

from sympy import *
from sympy.physics.quantum import *
from sympy.physics.quantum.qubit import *

state = TensorProduct(Qubit(0),Qubit(1)) # example state
id1 = Integer(1) # identity op. type 1
id2 = Qubit(0)*Dagger(Qubit(0))+Qubit(1)*Dagger(Qubit(1)) # identity op. type 2
proj1 = TensorProduct(id1, Qubit(1)*Dagger(Qubit(1))) # projection op which affect only the second Hilbert space
proj2 = TensorProduct(id2, Qubit(1)*Dagger(Qubit(1)))
# apply operator on the state
print "1)"
print qapply(tensor_product_simp(proj1*state)) # this works
print "2)"
print qapply(tensor_product_simp(proj2*state)) # this gives an error
# AttributeError: 'int' object has no attribute 'is_commutative'

Regards,
vug

Aaron Meurer

unread,
Nov 5, 2011, 3:11:32 AM11/5/11
to sy...@googlegroups.com
This is probably the same bug. Try using Integer(1) everywhere where
you have 1 (a shorter version that also should work is S(1)).

I opened http://code.google.com/p/sympy/issues/detail?id=2824 for this.

Aaron Meurer

2011/11/4 Uğur Güney <ugur...@gmail.com>:

Uğur Güney

unread,
Nov 5, 2011, 10:56:08 AM11/5/11
to sy...@googlegroups.com
On Sat, Nov 5, 2011 at 3:11 AM, Aaron Meurer <asme...@gmail.com> wrote:
This is probably the same bug.  Try using Integer(1) everywhere where
you have 1 (a shorter version that also should work is S(1)).

I opened http://code.google.com/p/sympy/issues/detail?id=2824 for this.

Aaron Meurer

Thanks! I changed every number with S(0) and S(1), but get the same error:

from sympy import *
from sympy.physics.quantum import *
from sympy.physics.quantum.qubit import *

q0 = Qubit(S(0))
q1 = Qubit(S(1))
state = TensorProduct(q0,q1) # a 2-qubit state
id = q0*Dagger(q0)+q1*Dagger(q1) # identity
proj = TensorProduct(id, q1*Dagger(q1)) # a projection op. IxP on H1xH2
print qapply(tensor_product_simp(proj*state)) # error

To make it clear I wrote down the intended calculation here: http://mathurl.com/3pe3v7c It fails at qapply step. 

I hope this helps. Have a good day!
vug

smichr

unread,
Nov 5, 2011, 3:39:46 PM11/5/11
to sympy
>
> from sympy import *
> from sympy.physics.quantum import *
> from sympy.physics.quantum.qubit import *
>
> q0 = Qubit(S(0))
> q1 = Qubit(S(1))
> state = TensorProduct(q0,q1) # a 2-qubit state
> id = q0*Dagger(q0)+q1*Dagger(q1) # identity
> proj = TensorProduct(id, q1*Dagger(q1)) # a projection op. IxP on H1xH2
> print qapply(tensor_product_simp(proj*state)) # error
>
> To make it clear I wrote down the intended calculation here:http://mathurl.com/3pe3v7cIt fails at qapply step.

I did some editing of the quantum modules and I now obtain (for the
above):

|0>x|1>

If this is right, perhaps you or Brian can review this at
https://github.com/sympy/sympy/pull/699

Uğur Güney

unread,
Nov 5, 2011, 4:14:11 PM11/5/11
to sy...@googlegroups.com
Thanks! I can not say anything about the SymPy source code but the result you obtained is now correct.

Brian Granger

unread,
Nov 8, 2011, 7:09:15 PM11/8/11
to sy...@googlegroups.com
The pull request linked to above has been merged into master, so this
issue should be fixed.

Cheers,

Brian

2011/11/5 Uğur Güney <ugur...@gmail.com>:

--

Uğur Güney

unread,
Nov 8, 2011, 11:01:21 PM11/8/11
to sy...@googlegroups.com
Thanks! I really appreciate your work.

Regards,
vug
Reply all
Reply to author
Forward
0 new messages