Skip to content

Commit

Permalink
Merge dc59255 into 59caf56
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinsung committed May 20, 2019
2 parents 59caf56 + dc59255 commit 4187609
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 13 deletions.
62 changes: 49 additions & 13 deletions src/openfermion/transforms/_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
FermionOperator,
InteractionOperator,
InteractionRDM,
MajoranaOperator,
PolynomialTensor,
QuadraticHamiltonian,
QubitOperator,
Expand Down Expand Up @@ -377,26 +378,61 @@ def get_fermion_operator(operator):
Returns:
fermion_operator: An instance of the FermionOperator class.
"""
n_qubits = count_qubits(operator)
if isinstance(operator, PolynomialTensor):
return _polynomial_tensor_to_fermion_operator(operator)
elif isinstance(operator, DiagonalCoulombHamiltonian):
return _diagonal_coulomb_hamiltonian_to_fermion_operator(operator)
elif isinstance(operator, MajoranaOperator):
return _majorana_operator_to_fermion_operator(operator)
else:
raise TypeError('{} cannot be converted to FermionOperator'.format(
type(operator)))


def _polynomial_tensor_to_fermion_operator(operator):
fermion_operator = FermionOperator()
for term in operator:
fermion_operator += FermionOperator(term, operator[term])
return fermion_operator

if isinstance(operator, PolynomialTensor):
for term in operator:
fermion_operator += FermionOperator(term, operator[term])

elif isinstance(operator, DiagonalCoulombHamiltonian):
fermion_operator += FermionOperator((), operator.constant)
for p, q in itertools.product(range(n_qubits), repeat=2):
fermion_operator += FermionOperator(
((p, 1), (q, 0)),
operator.one_body[p, q])
fermion_operator += FermionOperator(
((p, 1), (p, 0), (q, 1), (q, 0)),
operator.two_body[p, q])
def _diagonal_coulomb_hamiltonian_to_fermion_operator(operator):
fermion_operator = FermionOperator()
n_qubits = count_qubits(operator)
fermion_operator += FermionOperator((), operator.constant)
for p, q in itertools.product(range(n_qubits), repeat=2):
fermion_operator += FermionOperator(
((p, 1), (q, 0)),
operator.one_body[p, q])
fermion_operator += FermionOperator(
((p, 1), (p, 0), (q, 1), (q, 0)),
operator.two_body[p, q])
return fermion_operator


def _majorana_operator_to_fermion_operator(majorana_operator):
fermion_operator = FermionOperator()
for term, coeff in majorana_operator.terms.items():
converted_term = _majorana_term_to_fermion_operator(term)
converted_term *= coeff
fermion_operator += converted_term
return fermion_operator


def _majorana_term_to_fermion_operator(term):
converted_term = FermionOperator(())
for index in term:
j, b = divmod(index, 2)
if b:
converted_op = FermionOperator((j, 0), -1j)
converted_op += FermionOperator((j, 1), 1j)
else:
converted_op = FermionOperator((j, 0))
converted_op += FermionOperator((j, 1))
converted_term *= converted_op
return converted_term


def get_molecular_data(interaction_operator,
geometry=None, basis=None, multiplicity=None,
n_electrons=None, reduce_spin=True,
Expand Down
21 changes: 21 additions & 0 deletions src/openfermion/transforms/_conversion_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
import unittest

import numpy
import pytest

from openfermion.hamiltonians import fermi_hubbard
from openfermion.ops import (BosonOperator,
DiagonalCoulombHamiltonian,
FermionOperator,
MajoranaOperator,
QuadOperator,
QubitOperator)
from openfermion.transforms import jordan_wigner
Expand Down Expand Up @@ -486,3 +488,22 @@ def test_two_term(self):
+ (BosonOperator('0') - BosonOperator('0^'))
* (BosonOperator('0') + BosonOperator('0^')))
self.assertTrue(b == expected)


def test_get_fermion_operator_majorana_operator():
a = MajoranaOperator((0, 3), 2.0) + MajoranaOperator((1, 2, 3))
op = get_fermion_operator(a)
expected_op = (-2j*(FermionOperator(((0, 0), (1, 0)))
- FermionOperator(((0, 0), (1, 1)))
+ FermionOperator(((0, 1), (1, 0)))
- FermionOperator(((0, 1), (1, 1))))
- 2*FermionOperator(((0, 0), (1, 1), (1, 0)))
+ 2*FermionOperator(((0, 1), (1, 1), (1, 0)))
+ FermionOperator((0, 0))
- FermionOperator((0, 1)))
assert normal_ordered(op) == normal_ordered(expected_op)


def test_get_fermion_operator_wrong_type():
with pytest.raises(TypeError):
_ = get_fermion_operator(QubitOperator())

0 comments on commit 4187609

Please sign in to comment.