:

## Procedure for expanding tensor product of quantum operators in Dirac notations - version 2

Maple

Here is a major upgrade of the procedure I submitted in february.

https://www.mapleprimes.com/posts/209030-Procedure-For-Computing-The-Tensor-Product

There is a line after the procedure to save it in the file "ExpandQop.m"
In future post I will use it in order to minimize the size of the examples.

Louis Lamarche

 > ###################################################################### # NOTICE                                                             # # Author: Louis Lamarche                                             # #         Institute of Research of Hydro-Quebec (IREQ)               # #         Science des données et haute performance                   # #         2018, March 7                                              # #                                                                    # # Function name: ExpandQop (x)                                       # #       Purpose: Compute the tensor product of two quantum           # #                operators in Dirac notations                        # #      Argument: x: a quantum operator                               # #  Improvements: Manage all +, -, *, /, ^, mod  operations           # #                in the argument                                     # #       Version: 2                                                   # #                                                                    # #  Copyrigth(c) Hydro-Quebec.                                        # #        Note 1: Permission to use this softwate is granted if you   # #                acknowledge its author and copyright                # #        Note 2: Permission to copy this softwate is granted if you  # #                leave this 21 lines notice intact. Thank you.       # ###################################################################### restart;
 > with(Physics): interface(imaginaryunit=i): Setup(mathematicalnotation=true);
 (1)
 > Setup(quantumoperators={A,B,C,Cn}); Setup(noncommutativeprefix={a,b,q});
 (2)
 > opexp:= op(0,10^x):            # exponentiation id opnp := op(0,10*x):            # normal product id optp := op(0,Ket(q0)*Ket(q1)): # tensor product id opdiv:= `Fraction`:            # fraction       id           opsym:= op(0,x):               # symbol         id opint:= op(0,10):              # integer        id opflt:= op(0,10.0):            # float          id opcpx:= op(0,i):               # complex        id opbra:= op(0,Bra(q)):          # bra            id opket:= op(0,Ket(q)):          # ket            id opmod:= op(0, a mod b):        # mod            id ExpandQop:=proc(x)     local ret,j,lkb,cbk,rkb,no,lop;     lkb:=0; cbk:=0; rkb:=0;     lop:=op(0,x);     no:=nops(x);     if lop = opsym or lop = opint or lop = opflt or        lop = opbra or lop = opket or lop = opcpx then          ret:=x;     else     if lop = opexp then         ret:=x;     else            if lop = opnp then         ret:=1;         for j from 1 to no do             ret:=ret*ExpandQop(op(j,x));         end do;             else     if lop = `+` then         ret:=0;         for j from 1 to no do             ret:=ret+ExpandQop(op(j,x));         end do;     else     if lop = `-` then         ret:=0;         for j from 1 to no do             ret:=ret-ExpandQop(op(j,x));         end do;     else     if lop = opdiv then        ret:=1;        for j from 1 to no do            ret:=ret/ExpandQop(op(j,x));        end do;     else     if lop = opmod then        ret:=x;     else     if lop = optp then         ret:=1;        if (no > 3 ) then            for j from 1 to no do                if (j>1) then                     if(lkb=0) then                         if( type(op(j-1,x),Ket) and                             type(op(j,x),Bra) ) then lkb:=j-1; fi;                     else                         if( type(op(j-1,x),Ket) and                             type(op(j,x),Bra) ) then rkb:=j;   fi;                     fi;                     if( type(op(j-1,x),Bra) and type(op(j,x),Ket) )                                                 then cbk:=j;   fi;                fi;            end do;            if ( (lkb < cbk) and (cbk
 > # Let A be an operator in a first Hilbert space of dimension n #  using the associated orthonormal ket and bra vectors # # kets1:=Ket(a1)*Ket(a2)*Ket(a3)*Ket(a4)*Ket(a5): A:=kets1*Dagger(kets1); # Let B be an operator in a second Hilbert (Ketspace of dimension m #  using the associated orthonormal ket and bra vectors # # kets2:=Ket(b1)*Ket(b2)*Ket(b3): B:=kets2*Dagger(kets2); # The tensor product of the two operators acts on a n+m third # Hilbert space   unsing the appropriately ordered ket # and bra  vectors of the two preceding spaces. The rule for # building this operator in Dirac notation is as follows, # # print("Maple do not compute the tensor product of operators,"); print("C=A*B gives:"); C:=A*B; print("ExpandQop(C) gives the expected result:"); Cn:=ExpandQop(C);
 (3)
 > kets3:=kets1*kets2; bras3:=Dagger(kets3); print("Matrix elements computed with C appears curious"); 'bras3.C. kets3'="..."; bras3.C.kets3; print("Matrix elements computed with Cn as expected"); 'bras3.Cn.kets3'=bras3.Cn.kets3;
 (4)
 > print("Example"); En:=ExpandQop(10*(1-x+y+z)*i*(1/sqrt(2))*A*B);
 (5)
 > print("Another example"); 'F'='A*B/sqrt(2)+B*A/sqrt(2)'; F:=A*B/sqrt(2)+B*A/sqrt(2): 'op(1,F)'=op(1,F); 'op(2,F)'=op(2,F); 'Fn'='ExpandQop(F)'; Fn:=ExpandQop(F): 'op(1,Fn)'=op(1,Fn); 'op(2,Fn)'=op(2,Fn);
 (6)
 >