Skip to content

Commit 69f47fe

Browse files
author
Christopher J. Wood
committed
Fixed bug with QSimplify and CircleTimes. Moved symbolic nested Com back to QSimplfy rules due to performance issues with QSimplify.
1 parent 0021948 commit 69f47fe

File tree

2 files changed

+30
-26
lines changed

2 files changed

+30
-26
lines changed

src/QuantumSystems.m

+19-10
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
(*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THEIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AREDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLEFOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIALDAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS ORSERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVERCAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USEOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*)
2323

2424

25-
(* ::Subsection:: *)
25+
(* ::Subsection::Closed:: *)
2626
(*Preamble*)
2727

2828

@@ -104,7 +104,7 @@
104104
AssignUsage[RandomHermitian,$Usages];
105105

106106

107-
(* ::Subsection:: *)
107+
(* ::Subsection::Closed:: *)
108108
(*Error Messages*)
109109

110110

@@ -162,7 +162,7 @@
162162
Begin["`Private`"];
163163

164164

165-
(* ::Subsection:: *)
165+
(* ::Subsection::Closed:: *)
166166
(*States and Operators*)
167167

168168

@@ -517,7 +517,7 @@
517517
VecForm[a__,opts:OptionsPattern[VecForm]]:=Map[VecForm[#,opts]&,{a}]
518518

519519

520-
(* ::Subsection:: *)
520+
(* ::Subsection::Closed:: *)
521521
(*Symbolic Evaluation*)
522522

523523

@@ -555,7 +555,7 @@
555555
}
556556

557557

558-
(* ::Subsubsection:: *)
558+
(* ::Subsubsection::Closed:: *)
559559
(*QSimplify*)
560560

561561

@@ -574,6 +574,10 @@
574574
QSimplify[expr_,opts:OptionsPattern[]]:= QSimplifyCached[expr,opts]
575575

576576

577+
QSimplify[Com[a_,b_,n_?Positive],opts:OptionsPattern[]]:=QSimplify[Com[a,QSimplify[Com[a,b],opts],n-1],opts]
578+
QSimplify[ACom[a_,b_,n_?Positive],opts:OptionsPattern[]]:=QSimplify[ACom[a,QSimplify[ACom[a,b],opts],n-1],opts]
579+
580+
577581
(* ::Text:: *)
578582
(*Memoized function for QSimplify*)
579583

@@ -624,7 +628,7 @@
624628
]
625629

626630

627-
(* ::Subsubsection:: *)
631+
(* ::Subsubsection::Closed:: *)
628632
(*Linear Algebra Rules*)
629633

630634

@@ -644,6 +648,7 @@
644648
Dot[QPower[a_,n_],a_]:> QPower[a,n+1],
645649
Dot[a_,QPower[a_,n_]]:> QPower[a,n+1],
646650
Dot[QPower[a_,m_],QPower[a_,n_]]:> QPower[a,n+m],
651+
Dot[CircleTimes[a1_,b1__],CircleTimes[a2_,b2__]]:> CircleTimes@@MapThread[Dot,{{a1,b1},{a2,b2}}],
647652
(* CircleTimes *)
648653
KroneckerProduct[a_,b__]:> CircleTimes[a,b],
649654
CircleTimes[Plus[a_,b__],c_]:> Plus@@Map[CircleTimes[#,c]&,{a,b}],
@@ -683,10 +688,12 @@
683688
Com[QPower[a_,n_],b_]:> Dot[a,Com[QPower[a,n-1],b]]+Dot[Com[QPower[a,n-1],b],a],
684689
Com[a_,QPower[b_,n_]]:> Dot[b,Com[a,QPower[b,n-1]]]+Dot[Com[a,QPower[b,n-1]],b],
685690
Com[CircleTimes[a1_,b1__],CircleTimes[a2_,b2__]]:>
686-
CircleTimes[Com[a1,a2],Dot[b1,b2]]
691+
CircleTimes[Com[a1,a2],Dot[CircleTimes[b1],CircleTimes[b2]]]
687692
+CircleTimes[Dot[a1,a2],Com[CircleTimes[b1],CircleTimes[b2]]],
693+
Com[a_,b_,n_?Positive]:> Com[a,Com[a,b],n-1],
688694
(* ACom *)
689-
ACom[a_,b_]:> a.b+b.a
695+
ACom[a_,b_]:> a.b+b.a,
696+
ACom[a_,b_,n_?Positive]:> ACom[a,ACom[a,b],n-1]
690697
};
691698

692699

@@ -755,11 +762,13 @@
755762
(* X and Y expand to P and M *)
756763
$QSimplifySpinPM={
757764
Spin["X"]:> (Spin["P"]+Spin["M"])/2,
758-
Spin["Y"]:> (-I*Spin["P"]+I*Spin["M"])/2};
765+
Spin["Y"]:> (-I*Spin["P"]+I*Spin["M"])/2
766+
};
759767
(* P and M expand to X and Y *)
760768
$QSimplifySpinXY={
761769
Spin["P"]:> Spin["X"]+I*Spin["Y"],
762-
Spin["M"]:> Spin["X"]-I*Spin["Y"]};
770+
Spin["M"]:> Spin["X"]-I*Spin["Y"]
771+
};
763772

764773

765774
(* ::Text:: *)

src/Tensor.m

+11-16
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@
170170
Begin["`Private`"];
171171

172172

173-
(* ::Subsection::Closed:: *)
173+
(* ::Subsection:: *)
174174
(*Matrices and Operations*)
175175

176176

@@ -212,40 +212,35 @@
212212

213213

214214
(* ::Text:: *)
215-
(*Symbolic nested commutator*)
215+
(*Commutator for matrices *)
216216

217217

218218
Com[A_,B_,0]:=B;
219219
Com[A_,B_,1]:=Com[A,B]
220-
Com[A_,B_,n_Integer]:=Com[A,Com[A,B,n-1]]
220+
Com[A_?MatrixQ,B_?MatrixQ]:=A.B-B.A
221+
Com[A_?MatrixQ,B_?MatrixQ,n_?IntegerQ]:=Com[A,Com[A,B],n-1]
221222

222223

223224
(* ::Text:: *)
224-
(*Commutator for matrices and numbers*)
225+
(*Zero Identities*)
225226

226227

227228
Com[A_,A_]:=0
228-
Com[A_?MatrixQ,B_?MatrixQ]:=A.B-B.A
229+
Com[A_,A_,n_?Positive]:=0
229230
Com[A_,B_?NumericQ]:=0
231+
Com[A_,B_?NumericQ,n_?Positive]:=0
230232
Com[A_?NumericQ,B_]:=0
233+
Com[A_?NumericQ,B_,n_?Positive]:=0
231234

232235

233236
(* ::Text:: *)
234-
(*Symbolic nested anti-commutator*)
237+
(*Anti-commutator*)
235238

236239

237240
ACom[A_,B_,0]:=B;
238241
ACom[A_,B_,1]:=ACom[A,B]
239-
ACom[A_,B_,n_Integer]:=ACom[A,ACom[A,B,n-1]]
240-
241-
242-
(* ::Text:: *)
243-
(*Anti-commutator for matrices and numbers*)
244-
245-
246242
ACom[A_?MatrixQ,B_?MatrixQ]:=A.B+B.A
247-
ACom[A_,B_?NumericQ]:=2*B*A
248-
ACom[A_?NumericQ,B_]:=2*A*B
243+
ACom[A_?MatrixQ,B_?MatrixQ,n_?Positive]:=ACom[A,ACom[A,B],n-1]
249244

250245

251246
OuterProduct[u_,v_]:=KroneckerProduct[Flatten[u],Conjugate[Flatten[v]]];
@@ -273,7 +268,7 @@
273268
SwapMatrix[d_Integer,perm_List]:= Normal@SwapMatrix[d,perm,SparseArray]
274269

275270

276-
(* ::Subsection:: *)
271+
(* ::Subsection::Closed:: *)
277272
(*Matrix-Tensor Manipulations*)
278273

279274

0 commit comments

Comments
 (0)