Ask Your Question

sympy.physics.quantum Bra and Ket: orthonormal basis

asked 2017-03-29 01:52:02 -0500

hiro protagonist gravatar image

updated 2017-03-29 08:35:29 -0500

i would like to be able to tell qapply from sympy.physics.quantum that i have a set (2 in my example) of orthonormal vectors:

from sympy.physics.quantum import Bra, Ket, qapply, Dagger

e0 = Ket('e0')
e1 = Ket('e1')

X = e1*Dagger(e0) + e0*Dagger(e1)
qapply(X * e1)
# ->  <e0|e1>*|e1> + <e1|e1>*|e0>

is there a way to tell sage (or sympy in that case) that $\langle e_i|e_j \rangle = \delta_{ij}$?

the best i could come up with is a substituion rule:

# e_rules = [(Dagger(e0)*e0, 1), (Dagger(e1)*e1, 1), (Dagger(e0)*e1, 0), (Dagger(e1)*e0, 0)]
e_rules = {Dagger(e0)*e0: 1, Dagger(e1)*e1: 1, Dagger(e0)*e1: 0, Dagger(e1)*e0: 0}
qapply(X * e1).subs(e_rules)
# ->  |e0>

isn't there something more elegant?

edit retag flag offensive close merge delete



thanks for pointing out this package ! there are some explanations here

mforets gravatar imagemforets ( 2017-03-29 12:07:43 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2017-03-29 12:09:45 -0500

mforets gravatar image

updated 2017-04-03 00:48:17 -0500

The Qubit constructor can be used to define an orthonormal basis. The scalar product can be actually computed with the doit() method. For example:

sage: from sympy.physics.quantum.qubit import Qubit
sage: from sympy.physics.quantum import Dagger
sage: e0 = Qubit('0'); e1 = Qubit('1')
sage: c = Dagger(e0)*e1; c
sage: c.doit()
sage: (Dagger(e1)*e1).doit()
sage: (Dagger(e1)*e0).doit()

Edit: The functionality actually exists, but it requires a systematic use of qapply.

First, suppose that we want to just rotate the qubit. We can apply the Hadamard coin as follows:

sage: from sympy.physics.quantum import qapply
sage: from sympy.physics.quantum.gate import H

sage: # the argument passed to H is the target qubit (starting at 0, as usual)
sage: [e0r, e1r] = [qapply(H(0)*ei) for ei in [e0, e1]]    
sage: e0r, e1r
(sqrt(2)*|0>/2 + sqrt(2)*|1>/2, sqrt(2)*|0>/2 - sqrt(2)*|1>/2)


sage: qapply(Dagger(e0r)*e0r)
sage: qapply(Dagger(e0r)*e1r) 
sage: qapply(Dagger(e1r)*e0r) 
sage: qapply(Dagger(e1r)*e1r) 

Linear combinations are also valid (but remember to qapply):

sage: from sympy import Symbol, cos, sin, exp, I
sage: theta = Symbol('theta', real=True); phi = Symbol('varphi', real=True)
sage: Q = cos(theta)*e0 + exp(I*phi)*sin(theta)*e1; Q
exp(I*phi)*sin(theta)*|1> + cos(theta)*|0>
sage: simplify(qapply(Dagger(Q)*Q))

Those imports are to avoid the missing conversion (AttributeError) for the Qubit class, for instance if you type qapply(x*e0). Alternatively, you may sympify the Sage expressions.

Since the Pauli matrices X, Y, Z can also be imported, it is possible to perform an arbitrary operation on a qubit. Multiple qubits can be handled similarly; there are some examples in the package documentation.

edit flag offensive delete link more


thanks, i saw that. but what if i want to rotate the basis vectors and create a new set of orthonormal vectors that are not in the computational basis? (something like |+> = n(|0> +|1>) ; |-> = n(|0>-|1> ; $n=1/\sqrt(2) $; how do i tell sage now that $|+\rangle $ and $|-\rangle $ are orthonormal)?

hiro protagonist gravatar imagehiro protagonist ( 2017-03-30 07:45:54 -0500 )edit

i would attempt to define them via superposition over the canonical basis. but it doesn't work as expected, since it produces things like <0|*|0>/2 + <0|*|1>/2 - <1|*|0>/2 - <1|*|1>/2 .

mforets gravatar imagemforets ( 2017-03-30 11:52:40 -0500 )edit

well... i would very much like to demonstrate a basis change. the new basis vectors should have the same simple form as the original ones... in any case: thanks a lot for your effort! +1 anyway. should you find some other way, i'd be really happy!

hiro protagonist gravatar imagehiro protagonist ( 2017-03-30 12:25:00 -0500 )edit

that looks good! thanks for the update!

hiro protagonist gravatar imagehiro protagonist ( 2017-04-03 14:57:16 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2017-03-29 01:52:02 -0500

Seen: 108 times

Last updated: Apr 03