Skip to content
This repository was archived by the owner on Dec 7, 2021. It is now read-only.

Multiplying two instances of TPBGroupedWeightedPauliOperator fails #913

Closed
samanthavbarron opened this issue Apr 23, 2020 · 1 comment · Fixed by #915
Closed

Multiplying two instances of TPBGroupedWeightedPauliOperator fails #913

samanthavbarron opened this issue Apr 23, 2020 · 1 comment · Fixed by #915

Comments

@samanthavbarron
Copy link

Information

  • Qiskit Aqua version: 0.6.5
  • Python version: 3.7.5
  • Operating system: Debian
{'qiskit-terra': '0.13.0',
 'qiskit-aer': '0.5.0',
 'qiskit-ignis': '0.3.0',
 'qiskit-ibmq-provider': '0.6.0',
 'qiskit-aqua': '0.6.5',
 'qiskit': '0.18.0'}

What is the current behavior?

Multiplying two instances of TPBGroupedWeightedPauliOperator fails.

Steps to reproduce the problem

from qiskit.aqua.operators import WeightedPauliOperator, TPBGroupedWeightedPauliOperator
from qiskit.aqua.operators.op_converter import to_tpb_grouped_weighted_pauli_operator

pauli_dict={'paulis': [{'label': 'IIII',
'coeff': {'real': -0.8288055048633435, 'imag': 0.0}},
{'label': 'IIIZ', 'coeff': {'real': 0.1625164975007994, 'imag': 0.0}},
{'label': 'IIZI', 'coeff': {'real': 0.1625164975007994, 'imag': 0.0}},
{'label': 'IZII', 'coeff': {'real': -0.19744295364673434, 'imag': 0.0}},
{'label': 'ZIII', 'coeff': {'real': -0.19744295364673434, 'imag': 0.0}},
{'label': 'IZIZ', 'coeff': {'real': 0.1172036494860493, 'imag': 0.0}},
{'label': 'IIZZ', 'coeff': {'real': 0.16583253947938562, 'imag': 0.0}},
{'label': 'XXYY', 'coeff': {'real': -0.046156695636240766, 'imag': 0.0}},
{'label': 'YXXY', 'coeff': {'real': 0.046156695636240766, 'imag': 0.0}},
{'label': 'XYYX', 'coeff': {'real': 0.046156695636240766, 'imag': 0.0}},
{'label': 'YYXX', 'coeff': {'real': -0.046156695636240766, 'imag': 0.0}},
{'label': 'ZIIZ', 'coeff': {'real': 0.16336034512229008, 'imag': 0.0}},
{'label': 'ZIZI', 'coeff': {'real': 0.1172036494860493, 'imag': 0.0}},
{'label': 'IZZI', 'coeff': {'real': 0.16336034512229008, 'imag': 0.0}},
{'label': 'ZZII', 'coeff': {'real': 0.1716978884591493, 'imag': 0.0}}]}
operator = WeightedPauliOperator.from_dict(pauli_dict)

tpb_operator = to_tpb_grouped_weighted_pauli_operator(
    operator,
    TPBGroupedWeightedPauliOperator.unsorted_grouping
)

print(tpb_operator.print_details())

tpb_operator * tpb_operator
AttributeErrorTraceback (most recent call last)
<ipython-input-1-6b6da3604543> in <module>
     27 print(tpb_operator.print_details())
     28 
---> 29 tpb_operator * tpb_operator

/usr/local/lib/python3.7/site-packages/qiskit/aqua/operators/weighted_pauli_operator.py in __mul__(self, other)
    276             return self._scaling_weight(other, copy=True)
    277         else:
--> 278             return self.multiply(other)
    279 
    280     def __neg__(self):

/usr/local/lib/python3.7/site-packages/qiskit/aqua/operators/tpb_grouped_weighted_pauli_operator.py in multiply(self, other)
    224         ret_op = super().multiply(other)
    225         # pylint: disable=no-member
--> 226         ret_op = ret_op._grouping_func(ret_op, **self._kwargs)
    227         return ret_op

AttributeError: 'WeightedPauliOperator' object has no attribute '_grouping_func'

What is the expected behavior?

The multiplication should go through and produce an instance of TPBGroupedWeightedPauliOperator.

Suggested solutions

My guess is that on Line 226, it should read maybe:

ret_op = self._grouping_func(ret_op, **self._kwargs)

so that the resulting TPBGroupedWeightedPauliOperator is produced using one of the available grouping functions (either from self or other).

https://github.com/Qiskit/qiskit-aqua/blob/0.6.5/qiskit/aqua/operators/tpb_grouped_weighted_pauli_operator.py#L226

@woodsp-ibm
Copy link
Member

Yes, you are right! I'll change it to take the grouping from self and note that in the docstring. (The prior method seems to have the same problem too). BTW thanks for providing these samples that show the failure - they are super helpful.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants