spin 1/2 in physics.quantum and pretty printing

14 views
Skip to first unread message

krastano...@gmail.com

unread,
Mar 30, 2011, 10:49:37 AM3/30/11
to sy...@googlegroups.com
Hi,

I am trying to use the spin classes from physics.quantum. For the moment I am working with a two-state system (spin 1/2). Is there a class dedicated to 1/2 spins (as opposed to J?Ket where I must write zplus=JzKet(1/2,1/2))?

My main concern for the moment is the printing. I would like to use both JzKet and JxKet and they are both printed as |1/2,+-1/2>.

I suppose that there is a nice solution where I can change only the label (I have no knowledge of how the printing system works). If this is the case I would like to find it, but still I prefer a cleaner solution where the class I use has done the change of label for me.

Regards
Stefan Krastanov

Vinzent Steinberg

unread,
Mar 30, 2011, 11:00:57 AM3/30/11
to sympy
Hi,

On Mar 30, 4:49 pm, "krastanov.ste...@gmail.com"
<krastanov.ste...@gmail.com> wrote:
> My main concern for the moment is the printing. I would like to use both
> JzKet and JxKet and they are both printed as |1/2,+-1/2>.

How would you like it to be printed?

Vinzent

krastano...@gmail.com

unread,
Mar 30, 2011, 11:14:00 AM3/30/11
to sy...@googlegroups.com, Vinzent Steinberg
For example:
|z+> for JzKet(1/2,+1/2) and |z-> for JzKet(1/2,-1/2) seem reasonable to me. The same for x and y.

But there is also a more general question - what is the standard way to chose a label for |u> = a * |z+> + b * |z->? I suppose that with some experimentation and looking at the code I will find a working solution, but I prefer to have some style guidelines.

Stefan


--
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.


Vinzent Steinberg

unread,
Mar 30, 2011, 12:18:07 PM3/30/11
to sympy
On Mar 30, 5:14 pm, "krastanov.ste...@gmail.com"
<krastanov.ste...@gmail.com> wrote:
> For example:
> |z+> for JzKet(1/2,+1/2) and |z-> for JzKet(1/2,-1/2) seem reasonable to me.
> The same for x and y.
>
> But there is also a more general question - what is the standard way to
> chose a label for |u> = a * |z+> + b * |z->? I suppose that with some
> experimentation and looking at the code I will find a working solution, but
> I prefer to have some style guidelines.

I'm neither familiar with the quantum code base, but it seems the
printing is implemented in StateBase. If you wan JzKet to be printed
differently, you have to look at the standard implementation and
overload it in JzKet. In this case there are three methods to
overload: _print_contents, _print_contents_pretty and
_print_contents_latex.

If you plan less radical changes, the brackets are defined in KetBase,
so you could also just overload these.

Please note that you can look at the implementation interactively
using source(obj) or obj?? when using isympy.

Vinzent

krastano...@gmail.com

unread,
Mar 30, 2011, 12:56:31 PM3/30/11
to sy...@googlegroups.com, Vinzent Steinberg
So in a more general case if I want to have the variable(instance) foo_bar of some RandomClass be printed in latex(or pretty print or whatever) as \foo_{bar} (or whatever in the other printers) I should first subclass RandomClass with overwritten printer and define foo_bar as an instance of the subclass?

Is not there a more elegant way? Am I save from side effects if I just change the instance method print without subclassing as opposed to changing the class method print in a subclass?

Do my questions make any sense or am I doing all this the wrong way?

Stefan


Vinzent

Brian Granger

unread,
Mar 30, 2011, 4:55:57 PM3/30/11
to sy...@googlegroups.com, krastano...@gmail.com
Hi,

> I am trying to use the spin classes from physics.quantum. For the moment I
> am working with a two-state system (spin 1/2). Is there a class dedicated to
> 1/2 spins (as opposed to J?Ket where I must write zplus=JzKet(1/2,1/2))?

We don't currently have any shorthands for the spin 1/2 case although
they would probably make sense to have.

> My main concern for the moment is the printing. I would like to use both
> JzKet and JxKet and they are both printed as |1/2,+-1/2>.

Yes, especially in the latex rendering it would make sense to add a
subscript "x" or "z" to this notation.

> I suppose that there is a nice solution where I can change only the label (I
> have no knowledge of how the printing system works). If this is the case I
> would like to find it, but still I prefer a cleaner solution where the class
> I use has done the change of label for me.

The print methods for these things are in the spin.py module and are
pretty self explanatory. You may also have to dig a bit deeper in the
qexpr.py and state.py modules to see how the base classes print.

Cheers,

Brian

> Regards
> Stefan Krastanov


>
> --
> 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

krastano...@gmail.com

unread,
Mar 30, 2011, 5:15:51 PM3/30/11
to elli...@gmail.com, sy...@googlegroups.com
Thank you for the answer. I filed an issue and I will try to make an useful patch some time in the near future.

Stefan
Reply all
Reply to author
Forward
0 new messages