Repository: Qiskit/qiskit-terra (python)

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:`


Pull request description:
Checks if DiagonalGate has modulus 1 
Fixes #3842

I took the suggested solution from https://github.com/Qiskit/qiskit-terra/issues/3842 (thanks @hhyap!)  and the test from https://github.com/Qiskit/qiskit-terra/pull/4375 (thanks @ewinston!)

This issue was simple enough if you do not consider the full extend of #4375

Patch:
diff --git a/qiskit/extensions/quantum_initializer/diagonal.py b/qiskit/extensions/quantum_initializer/diagonal.py
index 137dce074..b4c24fc41 100644
--- a/qiskit/extensions/quantum_initializer/diagonal.py
+++ b/qiskit/extensions/quantum_initializer/diagonal.py
@@ -55,7 +55,7 @@ class DiagonalGate(Gate):
                 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)
diff --git a/releasenotes/notes/3842-14af3f8d922a7253.yaml b/releasenotes/notes/3842-14af3f8d922a7253.yaml
new file mode 100644
index 000000000..08c8c8555
--- /dev/null
+++ b/releasenotes/notes/3842-14af3f8d922a7253.yaml
@@ -0,0 +1,4 @@
+---
+fixes:
+  - |
+    Extra validation was added to :class:`.DiagonalGate` to check the argument has modulus one.


Test patch:
diff --git a/test/python/circuit/test_diagonal_gate.py b/test/python/circuit/test_diagonal_gate.py
index 4f6bc5eb1..f12d4ab92 100644
--- a/test/python/circuit/test_diagonal_gate.py
+++ b/test/python/circuit/test_diagonal_gate.py
@@ -18,8 +18,10 @@ import numpy as np
 
 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 @@ class TestDiagonalGate(QiskitTestCase):
                 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)