pkozlows / fci

0 stars 0 forks source link

Replacing the nested four loops with np.einsum #17

Closed pkozlows closed 1 year ago

pkozlows commented 1 year ago

I believe that this is independent from my implementation of the anti commutator function, so that why I'm raising a separate issue for this matter.

pkozlows commented 1 year ago

I'm struggling to understand how I could replace the nested for loops for calculating the one or two electron matrix elements with an np.einsum. The anti-commutator for a certain second quantization op list could evaluate to -1, 1, or 0. How could I use the Einstein summation convention to replace the nested for loops if the phase factor that each integral will be weighted by could be -1, 1, or 0?

Walter-Feng commented 1 year ago

Then you will need to find out what phase factor means in terms of a matrix element. Why would we need a summation? Why will there be phase factor?

pkozlows commented 1 year ago

Will evaluating the attached second quantization string help me with learning about this, or do I need something entirely else? https://github.com/pkozlows/fci/blob/d2e5773007e169acbec7f29f82de1d72c5a9cf78/questions/3-4.pdf I have spent the last few days trying to figure out how to do latex with dictation, so if there is something that I would want to figure out classically using handwriting, I know that I can do it now with dictation, so that is nice.

Walter-Feng commented 1 year ago

Good you found your way with Latex! A must for theoretical chemists.

I don't think you will eventually need to solve 6 electron system by your hand. Plus the two questions I asked do not really require that much.

So Slater-Condon rule have summation for 3 cases, 1 for one-body operator, 2 for two-body operator. These cases require the orbitals on the left and on the right in some conditions right? Before answering what these conditions are, Slater-Condon rule indicates there is a summation. Where does this summation come from?

The FCI matrix element, <a|H|b>, is not merely two orbitals or two electrons. You have written, there will be a long string of operators. We will need to reduce the string to some place where we can bridge to one/two-electron integrals. In second quantization, what enables us to reduce the string? Why do we emphasize phase factor?

Answering these two questions should help you understand why an np.einsum.

pkozlows commented 1 year ago

https://github.com/pkozlows/fci/blob/5e5f419fc06731e7c928decbac04ad12e2e66d3b/questions/3-8.pdf

I came up with some quite loaded questions, but I feel that they are all questions that I need to answer prefer I can move forward with my program. if you have something more urgent that you need to get to right now, this can wait.

Walter-Feng commented 1 year ago

Math_drafts.pdf

Walter-Feng commented 1 year ago

You can follow this to derive, why 1e operator cannot have more than 2 electron excitations, why phase factor does not affect numpy einsum, how spin is affecting 2e operators. If you really follow step by step, 2e operators should not be differing much from 1e operator cases.

Walter-Feng commented 1 year ago

Sorry in the last case I dropped the phase factor in the last two equality :(

pkozlows commented 1 year ago

https://github.com/pkozlows/fci/blob/8dcf6e7d57545cb6bc8b13f8e6aeea6ccb1d4860/questions/3-21.pdf

Walter-Feng commented 1 year ago

I haven't exactly examined each of your step, but yes, it will produce several intermediate terms (as you need at least 2 x 4 = 8 terms to reduce the 2e operator to numbers) that look super lengthy and tedious. Eventually you should converge to Slater-Condon rules, but with good care of the spins.

Walter-Feng commented 1 year ago

Generally your steps are right, first handling the right two creation operators, then do the same step for the left. The summation will come when you notice the pattern where the pair of creation operators go over one electron from the ket state.

pkozlows commented 1 year ago

I am having some issues where I stop my derivation. the questions that I have are embedded in file. As you will probably notice, I am using an actual .tex editor, so in addition to being able to typeset a lot of things in a short time, I also have a lot more flexibility with the latex forms that I am able to use. I will attach both the output pdf and the .tex here. lmk if having de .tex is hopeful at all, since you can use it to copy and pace code into your editor which is probably overleaf? https://github.com/pkozlows/fci/blob/9ac9db99f5a49968de6aa048da1c6c6f44e2255f/questions/3-31.pdf https://github.com/pkozlows/fci/blob/9ac9db99f5a49968de6aa048da1c6c6f44e2255f/questions/3-31.tex

pkozlows commented 1 year ago

If you haven't looked at my deprivation yet, no need to look at it just yet. I have made some more progress on it and once I write it down. I will just send you the updated version.

Walter-Feng commented 1 year ago

Sorry for not being prompt on the update - and congrats on your progress! That's the usual way of doing researches - get stuck for some days / weeks, an inspiration, and then a solution. I'll then rest it for a while.

pkozlows commented 1 year ago

https://github.com/pkozlows/fci/blob/f13d2240857704c83982b8087735fb2cf21b42cf/latex/no_difference/no_difference.pdf

Do you have any tips about where I should go from here in my derivation? Particularly, I need to figure out how to translated into a np.einsum form, but I'm still not sure how.

pkozlows commented 1 year ago

Also, telling me to fuck the convention was a very important thing for me to hear. Previously, I was spending too much time figuring out how to run my derivations so they would be pretty, but that is not the point; the important thing is that they are in a form that I can understand and that you can understand as well. I didn't mention this earlier, but I am doing voice coding in a heavily snippet-based fashion, so having longer forms in my derivations is no problem at all; what is important is that they are forms that I can understand well and I think you can understand well too, and that is all that really matters.

Walter-Feng commented 1 year ago

To einsum will be super easy - you see v^{ijji} or this sort of thing, so you can write as

np.einsum("ijji", two_electron_integral)

with correct slice of two electron integrals. It's also important that the two electron integrals provided are based on sperical orbitals but not spin orbitals.

pkozlows commented 1 year ago

I feel that I'm getting close to the correct energy for the HF ground state, but not quite there yet. I am getting something for the 2e energy like 3.81, when it should be like 4.68. I am basing my einsum off of what I got in my derivation, which looks like (0.5)(3np.einsum('iijj'->,h2e)-2*np.einsum('ijij'->,h2e)). I feel like I should be getting the correct answer, but I'm not. Is there something missing in my derivation?

Also, I'm still very confused as to why the answer given in the book was incorrect? I understand that they didn't take into account spin, but my form doesn't look like the one in the book? I will attach a picture of the form from the book that I am talking about, so that you can see what I mean.

Also, I'm sorry if my comments on Fri were offensive. It was tone deaf to make light of a stressful situation with your advisor.

https://github.com/pkozlows/fci/blob/75222d483cf92249762e81cdc6459246780fbde2/condon.py#L142

https://github.com/pkozlows/fci/blob/75222d483cf92249762e81cdc6459246780fbde2/latex/no_difference/no_difference.pdf

pkozlows commented 1 year ago

PXL_20230217_042732852.jpg

pkozlows commented 1 year ago

Getting in answer in latex that is considerably different than book has also for the a case of one difference between two determinants. should I be worried atoll about this?

Walter-Feng commented 1 year ago

No worries, I enjoyed your reply - PhD student life is bound to be tough.

Generally when there's conflict with reference, usually it's our fault - well I encountered misprinting in an article, but it's a rare case.

It may be obvious if you tried Hartree Fock implementation, but generally a restricted hartree fock has the form of ( 2 J - K), where J is the coulomb matrix, and K the exchange matrix.

I was following notation from Scholarpedia when writing the draft, so v_{ijkl} \rho_{lk} is the coulomb matrix, v_{ijkl} \rho_{lj} is the coulomb matrix, v_{ijlk} \rho_{lj} is the exchange matrix.

Here I need to sincerely apologize for a serious mistake here, as I was assuming this v_{ijkl} is the same as quantum chemistry's two electron integral. No, they are actually different. They have different assignment of the center, so in general

< i j | k l > = [ i k | j l ]

where i and k share the same center of r1 in 1 / (r1 - r2). [ ij | kl] is the quantum chemistry notation of two electron integrals, so it follows

[ ij | kl ] = [ ji | kl ] = [ ij | lk ] = [ kl | ij ]

but < ij | kl > is slightly different. Once you handled with second quantization, you may need to translate to [ij | kl] <-- this form.

You can check my documentation for a clearer definition.

Back to RHF, we should be having (2 J - K) in spatial orbital representation. pyscf or my code has similar behavior.

A naive thought is, spin orbitals have double the number of spatial orbitals, so there should be (4J - 2K), 6 terms in total - 4 terms for coulomb, 2 terms for exchange. There are indeed 5 terms that get implemented, but it's because two of them actually have symmetry.

Note that (4 J - 2 K) can be simplifed as ( 2 J - K ) because it's RESTRICTED, so generally 6 terms ( or 5 terms if following symmetry) need to be implemented.

pkozlows commented 1 year ago

I feel like I'm being very stupid, but I can't seem to figure out what's wrong. Do you have any tips?

https://github.com/pkozlows/fci/blob/608fa3ad828017f9ab58d3d78d8d3c5a269f5854/latex/no_difference/no_difference.pdf

https://github.com/pkozlows/fci/blob/608fa3ad828017f9ab58d3d78d8d3c5a269f5854/condon.py#L142

Walter-Feng commented 1 year ago

Notabilityメモ.pdf

I obtained same result as Slater-Condon rules.

Here the key point is, k1 / k2 in the first expression, or i / j in the last expression, can have different spins. So the total number of terms is 2 ( J / K ) X 2 (spin for i) X 2 (spin for j) = 8, among which 2 terms are eliminated due to spin restriction in 2e integrals, 1 term is the same to one another.

Walter-Feng commented 1 year ago

Notation is, prime for creation operators, round bracket for delta function.

The corresponding matrix element is v_{alpha beta delta gamma} (Check scholarpedia), so after replacing greek letters with i / j, you get Slater-Condon rules in Wikipedia.

pkozlows commented 1 year ago

https://github.com/pkozlows/fci/blob/b705703702f5dd2e8796617425f228015cd79e98/latex/no_difference/no_difference.pdf Sorry for my rambling, even though I hope this kind of explains what I was thinking about.

Also,btw the derivation that you put together a few days ago, which I imagine took quite a bit of time from you, was actually very helpful for me. It taught me to understand that before I involved the K2 creation operator, I wanted to first involve the K1 annihilation operator, which makes second quantization much simpler. What I was doing previously was probably correct, but it was more complicated and unnecessarily so.

pkozlows commented 1 year ago

I feel like in might be starting to get an idea of where spin is coming in to the Condon roles for the two electron integrals. However, I am still running into some issues. I hope that these issues are concisely summarized in the attached document. https://github.com/pkozlows/fci/blob/19d860f0445f43e1a4a3133f17f5da8eedfa66ae/latex/no_difference/4-24.pdf

Walter-Feng commented 1 year ago

Then let me make it explicit here,

I’m more used to chemical notation, so [ij | kl] requires i <-> j to be same spin, k <-> l to be same spin, as indicated by the integral. You can’t stuff [ (alpha) (alpha) | (alpha) (beta) ] as a non-trivial integral.

pkozlows commented 1 year ago

victorious over np.einsum...for now