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

Error in Operator() addition/subtraction in- and out-of-place #622

Closed
juanjosegarciaripoll opened this issue Jul 24, 2019 · 4 comments
Closed
Labels
type: bug Something isn't working

Comments

@juanjosegarciaripoll
Copy link

Informations

  • Qiskit Aqua version: 0.5.2
  • Python version: 3.6.7
  • Operating system: Windows 10 (Miniconda3)

What is the current behavior?

When adding or subtracting Operator() objects, the output is not the result of the operation, but the original operator.

Steps to reproduce the problem

from qiskit.quantum_info import Pauli
from qiskit.aqua import Operator
import copy
aux = Operator(paulis=[(1.0,Pauli(label='II'))])
aux.to_matrix()
print('op1=',aux.matrix)
aux2 = Operator(paulis=[(1.0,Pauli(label='ZI'))])
aux2.to_matrix()
print('op2=',aux2.matrix)

print('---Out-of-place subtraction')
print(aux.matrix-aux2.matrix)
aux3 = aux - aux2
print(aux3.matrix)

print('---Out-of-place addition')
print(aux.matrix+aux2.matrix)
aux3 = aux + aux2
print(aux3.matrix)

print('---In-place subtraction')
aux3 = copy.deepcopy(aux)
print(aux3.matrix)
print(aux.matrix-aux2.matrix)
aux3 -= aux2
print(aux3.matrix)

print('---In-place addition')
aux3 = copy.deepcopy(aux)
print(aux3.matrix)
print(aux3.matrix+aux2.matrix)
aux3 += aux2
print(aux3.matrix)

Output
op1= [1. 1. 1. 1.]
op2= [ 1. 1. -1. -1.]
---Out-of-place subtraction
[0. 0. 2. 2.]
[1. 1. 1. 1.]
---Out-of-place addition
[2. 2. 0. 0.]
[1. 1. 1. 1.]
---In-place subtraction
[1. 1. 1. 1.]
[0. 0. 2. 2.]
[1. 1. 1. 1.]
---In-place addition
[1. 1. 1. 1.]
[2. 2. 0. 0.]
[1. 1. 1. 1.]

What is the expected behavior?

The expected solution is that the third row of each example should be equal to the second row.

Suggested solutions

Fix the Operator() method auxiliary function _extend_or_combine()

@juanjosegarciaripoll
Copy link
Author

juanjosegarciaripoll commented Jul 24, 2019

The following patch fixed the bug

diff --git a/qiskit/aqua/operator.py b/qiskit/aqua/operator.py
index ba315389..8caf7116 100644
--- a/qiskit/aqua/operator.py
+++ b/qiskit/aqua/operator.py
@@ -106,6 +106,7 @@ class Operator(object):
             lhs._paulis_to_grouped_paulis()
         elif lhs._matrix is not None and rhs._matrix is not None:
             lhs._matrix = operation(lhs._matrix, rhs._matrix)
+            lhs._to_dia_matrix(mode="matrix")
         else:
             raise TypeError("the representations of two Operators should be the same. ({}, {})".format(
                 lhs.representations, rhs.representations))

@chunfuchen
Copy link
Contributor

chunfuchen commented Jul 24, 2019

the above patch only fixes for matrix mode. for the pauli mode, another patch is needed (since the right operand is manipulated accidentally.
addressed in #620 and #619

@chunfuchen
Copy link
Contributor

resolved by #620 and #619.

@chunfuchen chunfuchen added the type: bug Something isn't working label Jul 24, 2019
@chunfuchen
Copy link
Contributor

close it since it has been resolved.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants