Spin x/y/z bases representation #431

Merged
merged 1 commit into from Jun 23, 2011

Projects

None yet

4 participants

@flacjacket
Member

Provides improved functionality in moving between Jx, Jy and Jz spin bases. Implements logic for using represent, rewrite and InnerProducts between arbitrary states in any basis. See the docstrings for usage. Also implements some tests and documentation for things in the spin module. Let me know if you have any questions or comments.

Note that there are still bugs in the Rotation.d function that will effect results from this work, see [1]. Some of the cases that need to be resolved are in an XFAIL test.

[1] http://code.google.com/p/sympy/issues/detail?id=2423

@certik
Member
certik commented Jun 16, 2011

Hi Sean, thanks for the pull request! Can you rebase it on top of the latest master? It can't be merged currently.

@certik
Member
certik commented Jun 16, 2011

Test results html report: http://pastehtml.com/view/axdgv94h2.html

Automatic review by sympy-bot.

@flacjacket
Member

I didn't fully clean up the rebase, I updated the file and it should be fine now

@certik
Member
certik commented Jun 16, 2011

Test results html report: http://pastehtml.com/view/axdktb5eu.html

Automatic review by sympy-bot.

@flacjacket
Member

This should clear up the doctest error, but I'm not sure, since I can't reproduce it.

@plaes plaes commented on an outdated diff Jun 17, 2011
sympy/physics/quantum/spin.py
result = exp(-I*m*alpha)*exp(-I*mp*gamma)
result *= cls.d(j, m, mp, beta)
return result
@classmethod
def d(cls, j, m, mp, beta):
- """Wigner's lowercase d function."""
+ """Wigner small-d function.i
@plaes
plaes Jun 17, 2011 Contributor

What's this 'i', in the end of the sentence?

@plaes plaes commented on an outdated diff Jun 17, 2011
sympy/physics/quantum/spin.py
@@ -425,95 +534,212 @@ class SpinState(State):
def _eval_hilbert_space(cls, label):
return ComplexSpace(2*label[0]+1)
+ def _represent_base(self, **options):
+ j = self.j
+ alpha = options.get('alpha', 0)
+ beta = options.get('beta', 0)
+ gamma = options.get('gamma', 0)
+ size, mvals = m_values(j)
+ result = zeros((size,1))
+ for p in range(size):
+ me = Rotation.D(self.j, mvals[p], self.m, alpha, beta, gamma)
@plaes
plaes Jun 17, 2011 Contributor

We really don't need the 'me' variable, as we can just do:
result[p,0] = Rotation.D(j, mvals[p], self.m, alpha, beta, gamma)

@plaes plaes commented on an outdated diff Jun 17, 2011
sympy/physics/quantum/spin.py
def _represent_default_basis(self, **options):
return self._represent_JzOp(None, **options)
+ def _represent_JxOp(self, basis, **options):
+ return self._represent_base(gamma=-pi/2)
+
+ def _represent_JyOp(self, basis, **optinso):
@plaes
plaes Jun 17, 2011 Contributor

typo here..

@plaes plaes commented on an outdated diff Jun 17, 2011
sympy/physics/quantum/spin.py
-class JyBra(SpinState, Bra):
- """Eigenbra of Jy."""
+class JzBra(SpinState, Bra):
+ """Eigenbra of Jz.
+
+ See the JzKet for the usagn of spin eigenstates.
@plaes
plaes Jun 17, 2011 Contributor

typo

@plaes plaes commented on an outdated diff Jun 17, 2011
sympy/physics/quantum/spin.py
from sympy import exp, cos, sin, diff, factorial
from sympy.printing.pretty.stringpict import prettyForm, stringPict
from sympy.matrices.matrices import zeros
+from sympy.physics.quantum.represent import represent
@plaes
plaes Jun 17, 2011 Contributor

Please arrange these imports alphabetically...

@plaes plaes commented on an outdated diff Jun 17, 2011
sympy/physics/quantum/spin.py
+ Note: that the resulting eigenstates are JxKet's
+
+ >>> JzKet(1,1).rewrite("Jx")
+ |1,-1>/2 + 2**(1/2)*|1,0>/2 + |1,1>/2
+
+ Get the vector representation of a state in terms of the basis elements
+ of the Jx operator:
+
+ >>> from sympy.physics.quantum.represent import represent
+ >>> from sympy.physics.quantum.spin import Jx
+ >>> represent(JzKet(1,-1), basis=Jx)
+ [ 1/2]
+ [-2**(1/2)/2]
+ [ 1/2]
+
+ Apply innerproducts between states
@plaes
plaes Jun 17, 2011 Contributor

Missing ':' at the end of sentence.

@ellisonbg ellisonbg commented on an outdated diff Jun 17, 2011
sympy/physics/quantum/spin.py
@@ -300,7 +303,51 @@ class J2Op(SpinOpBase, HermitianOperator):
class Rotation(UnitaryOperator):
- """Wigner D operator in terms of Euler angles."""
+ """Wigner D operator in terms of Euler angles.
+
+ Defines the rotation operator in terms of the Euler angles defined by
+ the z-y-z convention for a passive transformation. That is the coordinate
+ axes are rotated first about the z-axis, then the y-axis, then again about
@ellisonbg
ellisonbg Jun 17, 2011 Member

When you are giving the conventions for rotating about each axis, please specify if the rotation is about the old or new y and z axes.

@ellisonbg ellisonbg and 1 other commented on an outdated diff Jun 17, 2011
sympy/physics/quantum/tests/test_spin.py
+ #assert JxKet(1,1).rewrite('Jz') == JzKet(1,1)/2+JzKet(1,0)/sqrt(2)+JzKet(1,-1)/2
+ assert JyKet(S(1)/2,-S(1)/2).rewrite('Jz') == \
+ -I*2**(S(1)/2)*JzKet(S(1)/2,S(1)/2)/2+2**(S(1)/2)*JzKet(S(1)/2,-S(1)/2)/2
+ #assert qapply(JzKet(1,1)*JzKet(1,1).rewrite('Jz')) == 1
+
+def test_innerproduct():
+ j,m = symbols("j m")
+ assert InnerProduct(JzBra(1,1), JzKet(1,1)).doit() == 1
+ assert InnerProduct(JzBra(S(1)/2,S(1)/2), JzKet(S(1)/2,-S(1)/2)).doit() == 0
+ assert InnerProduct(JzBra(j,m), JzKet(j,m)).doit() == 1
+ assert InnerProduct(JzBra(1,0), JyKet(1,1)).doit() == -I/sqrt(2)
+ assert InnerProduct(JxBra(S(1)/2,S(1)/2), JzKet(S(1)/2,S(1)/2)).doit() == sqrt(2)/2
+ assert InnerProduct(JyBra(1,1), JzKet(1,1)).doit() == -S(1)/2
+ assert InnerProduct(JxBra(1,-1), JyKet(1,1)).doit() == 0
+
+def test_rotation_d():
@ellisonbg
ellisonbg Jun 17, 2011 Member

We need to have tests of the d matrix that are for symbolic arguments. This is important as we can compare with the tables in Varshalovich.

@certik
certik Jun 17, 2011 Member

+1 However let's do that as another pull request, so that we can get this in, as a starting point.

@ellisonbg
ellisonbg Jun 17, 2011 Member

OK great.

On Thu, Jun 16, 2011 at 10:05 PM, certik
reply@reply.github.com
wrote:

  •    #assert JxKet(1,1).rewrite('Jz') == JzKet(1,1)/2+JzKet(1,0)/sqrt(2)+JzKet(1,-1)/2
  •    assert JyKet(S(1)/2,-S(1)/2).rewrite('Jz') == \
  •            -I_2__(S(1)/2)JzKet(S(1)/2,S(1)/2)/2+2*(S(1)/2)_JzKet(S(1)/2,-S(1)/2)/2
  •    #assert qapply(JzKet(1,1)*JzKet(1,1).rewrite('Jz')) == 1
    +
    +def test_innerproduct():
  •    j,m = symbols("j m")
  •    assert InnerProduct(JzBra(1,1), JzKet(1,1)).doit() == 1
  •    assert InnerProduct(JzBra(S(1)/2,S(1)/2), JzKet(S(1)/2,-S(1)/2)).doit() == 0
  •    assert InnerProduct(JzBra(j,m), JzKet(j,m)).doit() == 1
  •    assert InnerProduct(JzBra(1,0), JyKet(1,1)).doit() == -I/sqrt(2)
  •    assert InnerProduct(JxBra(S(1)/2,S(1)/2), JzKet(S(1)/2,S(1)/2)).doit() == sqrt(2)/2
  •    assert InnerProduct(JyBra(1,1), JzKet(1,1)).doit() == -S(1)/2
  •    assert InnerProduct(JxBra(1,-1), JyKet(1,1)).doit() == 0
    +
    +def test_rotation_d():

+1 However let's do that as another pull request, so that we can get this in, as a starting point.

Reply to this email directly or view it on GitHub:
https://github.com/sympy/sympy/pull/431/files#r46956

Brian E. Granger
Cal Poly State University, San Luis Obispo
bgranger@calpoly.edu and ellisonbg@gmail.com

@ellisonbg ellisonbg and 1 other commented on an outdated diff Jun 17, 2011
sympy/physics/quantum/tests/test_spin.py
def test_represent():
assert represent(Jz) == hbar*Matrix([[1,0],[0,-1]])/2
assert represent(Jz, j=1) == hbar*Matrix([[1,0,0],[0,0,0],[0,0,-1]])
+@XFAIL
+def test_rotation_bug():
+ assert Rotation.d(1,1,0,-pi/2) == sqrt(2)/2
+ assert Rotation.d(1,0,1,-pi/2) == -sqrt(2)/2
+ assert Rotation.d(1,0,-1,-pi/2) == sqrt(2)/2
+ assert Rotation.d(1,-1,0,-pi/2) == -sqrt(2)/2
+
@ellisonbg
ellisonbg Jun 17, 2011 Member

Because the rewrite logic is i) so important and ii) so subtle we need more non-trivial tests that explore the different possibilities. There should be tests that go between all of the different combinations (JzKet+JzBra in Jx, Jy, JxKet+JxBra in Jz, Jx, etc.). I like the idea of doing rewrite on inner products that have known values of 0 or 1.

@certik
certik Jun 17, 2011 Member

Yes. I suggest to finish this pull request without it, and then create a new one just with more thorough tests.

@flacjacket
Member

That last commit should address all the points that have been addressed so far, with the exception of the testing of D/d/rewrite, which will come in a later pull request.

@ellisonbg
Member

One more thing. In the docstrings you are using uppercase J, M, etc., but in the code we use the more common lowercase j, m. Can you update the docstrings to use lowercase letters for j, m?

@flacjacket flacjacket Improve changing between xyz bases for spin states
Use the Wigner D-function to define the represent function for spin
states which allows for statesof any j and m values to be represented as
vectors in the new basis.

Implementation of rewrite, which is used to write states in terms of
different basis is improved to work for all j and m values and for
changing to any basis, not just to z. This utilizes the represent
function.

Changed the InnerProduct evaluation logic to use the represent functions
allowing for innerproducts to be evaluated between any spin states.
7bc3613
@certik
Member
certik commented Jun 22, 2011

Test results html report: http://pastehtml.com/view/ay1bpv44i.html

Automatic review by sympy-bot.

@certik certik merged commit 3d7f6d0 into sympy:master Jun 23, 2011
@certik
Member
certik commented Jun 23, 2011

This looks good to me (doctests are there, all tests pass), I merged it in. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment