Repository: Qiskit/qiskit

Issue:
The "get_statevector" and "get_unitary" methods of the result objects of statevector and unitary simulators seem to ignore the "decimals" parameter
### Informations

- **Qiskit version**: 0.20.1
- **Python version**: The one on the Jupyter server of the IBM Q Experience
- **Operating system**: The one on the Jupyter server of the IBM Q Experience

### What is the current behavior?

The "get_statevector" and "get_unitary" methods of the result objects of statevector and unitary simulators seem to ignore the "decimals" parameter

### Steps to reproduce the problem

%matplotlib inline
# Importing standard Qiskit libraries and configuring account
from qiskit import QuantumCircuit, execute, Aer, IBMQ
from qiskit.compiler import transpile, assemble
from qiskit.tools.jupyter import *
from qiskit.visualization import *
# Loading your IBM Q account(s)
provider = IBMQ.load_account()

circ = QuantumCircuit(1,1)

circ.h(0)

backend = Aer.get_backend('statevector_simulator') 

job = execute(circ, backend) 
result = job.result()  
outputstate = result.get_statevector(circ, decimals=3)
print(outputstate)
outputstate = result.get_statevector(circ, decimals=1)
print(outputstate)
outputstate = result.get_statevector(circ)
print(outputstate)

*****

Something similar happens with the get_unitary method when the unitary simulator is used


### What is the expected behavior?

The number of decimals of the outputstate should be the one used as parameter


### Suggested solutions



Discussion:
Can I have a permission to work on this.

Resolving patch:
diff --git a/qiskit/result/postprocess.py b/qiskit/result/postprocess.py
--- a/qiskit/result/postprocess.py
+++ b/qiskit/result/postprocess.py
@@ -183,6 +183,8 @@ def format_statevector(vec, decimals=None):
         list[complex]: a list of python complex numbers.
     """
     if isinstance(vec, np.ndarray):
+        if decimals:
+            return np.around(vec, decimals=decimals)
         return vec
     num_basis = len(vec)
     if vec and isinstance(vec[0], complex):
@@ -191,6 +193,7 @@ def format_statevector(vec, decimals=None):
         vec_complex = np.zeros(num_basis, dtype=complex)
         for i in range(num_basis):
             vec_complex[i] = vec[i][0] + 1j * vec[i][1]
+
     if decimals:
         vec_complex = np.around(vec_complex, decimals=decimals)
     return vec_complex
@@ -208,6 +211,8 @@ def format_unitary(mat, decimals=None):
         list[list[complex]]: a matrix of complex numbers
     """
     if isinstance(mat, np.ndarray):
+        if decimals:
+            return np.around(mat, decimals=decimals)
         return mat
     num_basis = len(mat)
     mat_complex = np.zeros((num_basis, num_basis), dtype=complex)


Test patch:
diff --git a/test/python/result/test_result.py b/test/python/result/test_result.py
--- a/test/python/result/test_result.py
+++ b/test/python/result/test_result.py
@@ -335,6 +335,73 @@ def test_meas_level_0_single(self):
         self.assertEqual(memory.dtype, np.complex_)
         np.testing.assert_almost_equal(memory, processed_memory)
 
+    def test_circuit_statevector_repr_without_decimal(self):
+        """Test postprocessing of statevector without giving any decimals arg."""
+        raw_statevector = np.array(
+            [0.35355339 + 0.j, 0.35355339 + 0.j, 0.35355339 + 0.j, 0.35355339 + 0.j,
+             0.35355339 + 0.j, 0.35355339 + 0.j, 0.35355339 + 0.j, 0.35355339 + 0.j],
+            dtype=np.complex_)
+        processed_sv = np.array(
+            [0.35355339 + 0.j, 0.35355339 + 0.j, 0.35355339 + 0.j, 0.35355339 + 0.j,
+             0.35355339 + 0.j, 0.35355339 + 0.j, 0.35355339 + 0.j, 0.35355339 + 0.j],
+            dtype=np.complex_)
+        data = models.ExperimentResultData(statevector=raw_statevector)
+        exp_result = models.ExperimentResult(shots=1, success=True, data=data)
+        result = Result(results=[exp_result], **self.base_result_args)
+        statevector = result.get_statevector()
+        self.assertEqual(statevector.shape, (8,))
+        self.assertEqual(statevector.dtype, np.complex_)
+        np.testing.assert_almost_equal(statevector, processed_sv)
+
+    def test_circuit_statevector_repr_decimal(self):
+        """Test postprocessing of statevector giving decimals arg."""
+        raw_statevector = np.array(
+            [0.35355339 + 0.j, 0.35355339 + 0.j, 0.35355339 + 0.j, 0.35355339 + 0.j,
+             0.35355339 + 0.j, 0.35355339 + 0.j, 0.35355339 + 0.j, 0.35355339 + 0.j],
+            dtype=np.complex_)
+        processed_sv = np.array(
+            [0.354 + 0.j, 0.354 + 0.j, 0.354 + 0.j, 0.354 + 0.j, 0.354 + 0.j, 0.354 + 0.j,
+             0.354 + 0.j, 0.354 + 0.j], dtype=np.complex_)
+        data = models.ExperimentResultData(statevector=raw_statevector)
+        exp_result = models.ExperimentResult(shots=1, success=True, data=data)
+        result = Result(results=[exp_result], **self.base_result_args)
+        statevector = result.get_statevector(decimals=3)
+        self.assertEqual(statevector.shape, (8,))
+        self.assertEqual(statevector.dtype, np.complex_)
+        np.testing.assert_almost_equal(statevector, processed_sv)
+
+    def test_circuit_unitary_repr_without_decimal(self):
+        """Test postprocessing of unitary without giving any decimals arg."""
+        raw_unitary = np.array([[0.70710678 + 0.00000000e+00j, 0.70710678 - 8.65956056e-17j],
+                                [0.70710678 + 0.00000000e+00j, -0.70710678 + 8.65956056e-17j]],
+                               dtype=np.complex_)
+        processed_unitary = np.array([[0.70710678 + 0.00000000e+00j, 0.70710678 - 8.65956056e-17j],
+                                      [0.70710678 + 0.00000000e+00j,
+                                       -0.70710678 + 8.65956056e-17j]],
+                                     dtype=np.complex_)
+        data = models.ExperimentResultData(unitary=raw_unitary)
+        exp_result = models.ExperimentResult(shots=1, success=True, data=data)
+        result = Result(results=[exp_result], **self.base_result_args)
+        unitary = result.get_unitary()
+        self.assertEqual(unitary.shape, (2, 2))
+        self.assertEqual(unitary.dtype, np.complex_)
+        np.testing.assert_almost_equal(unitary, processed_unitary)
+
+    def test_circuit_unitary_repr_decimal(self):
+        """Test postprocessing of unitary giving decimals arg."""
+        raw_unitary = np.array([[0.70710678 + 0.00000000e+00j, 0.70710678 - 8.65956056e-17j],
+                                [0.70710678 + 0.00000000e+00j, -0.70710678 + 8.65956056e-17j]],
+                               dtype=np.complex_)
+        processed_unitary = np.array([[0.707 + 0.j, 0.707 - 0.j],
+                                      [0.707 + 0.j, -0.707 + 0.j]], dtype=np.complex_)
+        data = models.ExperimentResultData(unitary=raw_unitary)
+        exp_result = models.ExperimentResult(shots=1, success=True, data=data)
+        result = Result(results=[exp_result], **self.base_result_args)
+        unitary = result.get_unitary(decimals=3)
+        self.assertEqual(unitary.shape, (2, 2))
+        self.assertEqual(unitary.dtype, np.complex_)
+        np.testing.assert_almost_equal(unitary, processed_unitary)
+
 
 class TestResultOperationsFailed(QiskitTestCase):
     """Result operations methods."""