Skip to content

Commit

Permalink
Merge pull request #35 from rigetticomputing/feature/multi_ops_JW
Browse files Browse the repository at this point in the history
Construction of products of second quantized ops
  • Loading branch information
ncrubin committed Jun 23, 2017
2 parents d437551 + cc8bdbe commit f361683
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
18 changes: 18 additions & 0 deletions grove/fermion_transforms/jwtransform.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ def kill(self, index):
"""
return self._operator_generator(index, 1.0)

def product_ops(self, indices, conjugate):
"""
Convert a list of site indices and coefficients to a Pauli Operators
list with the Jordan-Wigner (JW) transformation
:param List indices: list of ints specifying the site the fermionic operator acts on,
e.g. [0,2,4,6]
:param List conjugate: List of -1, 1 specifying which of the indices are
creation operators (-1) and which are annihilation
operators (1). e.g. [-1,-1,1,1]
"""
pterm = PauliTerm('I', 0, 1.0)
for conj, index in zip(conjugate, indices):
pterm = pterm * self._operator_generator(index, conj)

pterm = pterm.simplify()
return pterm

def _operator_generator(self, index, conj):
"""
Internal method to generate the appropriate operator
Expand Down
18 changes: 18 additions & 0 deletions grove/fermion_transforms/tests/test_jwtransform.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from grove.fermion_transforms.jwtransform import JWTransform
from itertools import product


def test_create():
Expand Down Expand Up @@ -28,3 +29,20 @@ def test_hopping():
jw = JWTransform()
hopping = jw.create(2)*jw.kill(0) + jw.create(0)*jw.kill(2)
assert '(0.5+0j)*X0*Z1*X2 + (0.5+0j)*Y0*Z1*Y2' == hopping.__str__()

def test_multi_ops():
"""
test construction of Paulis for product of second quantized operators
"""
jw = JWTransform()
# test on one particle density matrix
for p, q, in product(range(6), repeat=2):
truth = jw.create(p)*jw.kill(q)
prod_ops_out = jw.product_ops([p, q], [-1, 1])
assert truth.__str__() == prod_ops_out.__str__()

# test on two particle density matrix
for p, q, r, s in product(range(4), repeat=4):
truth = jw.create(p)*jw.create(q)*jw.kill(s)*jw.kill(r)
prod_ops_out = jw.product_ops([p, q, s, r], [-1, -1, 1, 1])
assert truth.__str__() == prod_ops_out.__str__()

0 comments on commit f361683

Please sign in to comment.