# sympy.physics.quantum Bra and Ket: orthonormal basis

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 close merge delete

1

Sort by » oldest newest most voted

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
<0|1>
sage: c.doit()
0
sage: (Dagger(e1)*e1).doit()
1
sage: (Dagger(e1)*e0).doit()
0


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)


and

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


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


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.

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

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 .

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!