class TensorSymbol(sympy.Expr):is_commutative=Trueclass SymmetricTensor(TensorSymbol):def __new__(cls,symbol,inds1,inds2):symbol = sympy.sympify(symbol)inds1 = list(sympy.ordered(list(inds1)))inds2 = list(sympy.ordered(list(inds2)))inds1,inds2 = list(sympy.ordered([inds1,inds2]))return TensorSymbol.__new__(cls,symbol,inds1,inds2)def __str__(self):return "%s(%s,%s)" % self.args>>> a,b,c,d = sympy.symbols('a b c d')>>> SymmetricTensor('I2',(a,b),(c,d))I2([a,b],[c,d])>>> SymmetricTensor('I2',(c,d),(a,b))I2([a,b],[c,d])>>> SymmetricTensor('I1',(a,),(b,))I1([a],[b])>>> SymmetricTensor('I1',(b,),(a,))I1([a],[b])
(Apologies for the length...)
Here's my larger plan. It's strictly a night-and-weekends thing, but I'd be interested in working with you (or anyone else here) who thinks this is fun.
I'd like to get better at generating expressions for correlated wave functions. Both to evaluate and optimize their energy using programs like PyQuante, Pyscf, Psi4, or whatever, and to generate quantum circuits (i.e. sequences of Clifford group operators in a quantum circuit) to run on quantum emulators like ProjectQ and others.
Hirata's tensor contraction engine is really impressive, and it's written in Python, although it doesn't make use of a lot of tools like Sympy that I think could substantially simplify it. I'm not as interested in generating optimized, parallel code (yet), mostly just with generating correct code right now.
There are two sympy paths to this: first, using the sympy.physics.secondquant operators to generate expressions. I'm getting up to speed on this as quickly as I can.
My post in this thread is for the second path, which is more of a first quantization approach, where we create operators that are terms of the energy expression. A lot of my thesis work took simple MC-SCF energy expressions:
E = sum_i f(i)I1(i,i) + sum_ij a(i,j)I2(i,i,j,j) + b(i,j)I2(i,j,i,j)
And creating expressions to optimize the corresponding set of equations, essentially using variational calculus (i.e. j <- j + dj). Sympy should be able to do this automatically, provided we can generate I1 and I2 operators that can simplify expressions in the right way.
I think the little toy code I posted will work for the above expressions. But it doesn't use any of the substantial power of the sympy tensor operators, which I'm only just now learning about. I'd be interested in seeing whether using something more powerful than my little sympy objects that simplify in the right way could open up new avenues, again, with the goal of generating something like TCE in python.
Sorry for the long post, trying to give you context. I think there's a real opportunity here for sympy, and I'd be willing to get together for an offline discussion with you or anyone else who would be interested in this.