Repository: Qiskit/qiskit

Issue:
DiagonalGate does not raise when diagonal entries do not have modulus 1
<!-- ⚠️ If you do not respect this template, your issue will be closed -->
<!-- ⚠️ Make sure to browse the opened and closed issues -->

### Information

- **Qiskit Terra version**: 0.12.0
- **Python version**: 3.6.9
- **Operating system**: Linux

### What is the current behavior?

`diag_gate` allows entries whose modulus are not one.

### Steps to reproduce the problem

```
import qiskit
from numpy import exp,pi

backend = qiskit.Aer.get_backend("statevector_simulator")
qc = qiskit.QuantumCircuit(1)
qc.h(0)
qc.diag_gate([exp(1j*pi/8),0],qc.qregs)  # <-- no error here
qiskit.execute(qc,backend).result().to_dict()
```

### What is the expected behavior?

An error should be raised.

### Suggested solutions
Line 59 in https://github.com/Qiskit/qiskit-terra/blob/master/qiskit/extensions/quantum_initializer/diag.py#L59: 
`if not np.abs( np.abs(z) - 1 ) < _EPS:`



Discussion:
Hey guys! I would like to give it a try. Just to be sure I understood it correctly, the second entry of the list which is 0 seems to be the problem right? Maybe the if case in Line 59 only checks the first entry, therefore no error is raised?
hey can I have a shot at this

There's an old PR that should fix this, #4375. It has some merge conflicts, maybe you can check with @ewinston if you can brush it up, or create a new one based on that 🙂 

Resolving patch:
diff --git a/qiskit/extensions/quantum_initializer/diagonal.py b/qiskit/extensions/quantum_initializer/diagonal.py
--- a/qiskit/extensions/quantum_initializer/diagonal.py
+++ b/qiskit/extensions/quantum_initializer/diagonal.py
@@ -55,7 +55,7 @@ def __init__(self, diag):
                 raise QiskitError(
                     "Not all of the diagonal entries can be converted to complex numbers."
                 ) from ex
-            if not np.abs(z) - 1 < _EPS:
+            if not np.abs(np.abs(z) - 1) < _EPS:
                 raise QiskitError("A diagonal entry has not absolute value one.")
         # Create new gate.
         super().__init__("diagonal", int(num_action_qubits), diag)


Test patch:
diff --git a/test/python/circuit/test_diagonal_gate.py b/test/python/circuit/test_diagonal_gate.py
--- a/test/python/circuit/test_diagonal_gate.py
+++ b/test/python/circuit/test_diagonal_gate.py
@@ -18,8 +18,10 @@
 
 from qiskit import QuantumCircuit, QuantumRegister, BasicAer, execute
 
+from qiskit import QiskitError
 from qiskit.test import QiskitTestCase
 from qiskit.compiler import transpile
+from qiskit.extensions.quantum_initializer import DiagonalGate
 from qiskit.quantum_info.operators.predicates import matrix_equal
 
 
@@ -54,6 +56,13 @@ def test_diag_gate(self):
                 unitary_desired = _get_diag_gate_matrix(diag)
                 self.assertTrue(matrix_equal(unitary, unitary_desired, ignore_phase=False))
 
+    def test_mod1_entries(self):
+        """Test that diagonal raises if entries do not have modules of 1."""
+        from qiskit.quantum_info.operators.predicates import ATOL_DEFAULT, RTOL_DEFAULT
+
+        with self.assertRaises(QiskitError):
+            DiagonalGate([1, 1 - 2 * ATOL_DEFAULT - RTOL_DEFAULT])
+
 
 def _get_diag_gate_matrix(diag):
     return np.diagflat(diag)