Repository: Qiskit/qiskit

Issue:
Creating a circuit from matrix representation of Pauli X gate gives incorrect matrix
<!-- ⚠️ If you do not respect this template, your issue will be closed -->
<!-- ⚠️ Make sure to browse the opened and closed issues -->

The exposition below is for the matrix representing the Pauli X gate, but the bug probably occurs for most matrices representing  1Q gates.

```python

# Converting the matrix for X to a circuit results in an erroneous
# phase factor, i, in the unitary.

from qiskit.quantum_info import Pauli, Operator
import qiskit

# If we create a circuit directly from the Pauli X, not its matrix,
# then the circuit is correct
xp = Pauli.from_label('X')
xcirc = xp.to_instruction().definition.decompose()
# The gate in xcirc is an instance of U3Gate
assert type(xcirc.data[0][0]) == qiskit.circuit.library.standard_gates.u3.U3Gate  # true
# The parameters are correct for the X gate
xcirc.data[0][0].params # [3.141592653589793, 0, 3.141592653589793]

# If we create the circuit from the matrix representing the Pauli X gate,
# then the gate in the circuit is wrong (by a global phase)
xop = Operator(Pauli.from_label('X').to_matrix())
xopcirc = xop.to_instruction().definition
# Also an instance of U3Gate
assert type(xopcirc.data[0][0]) == qiskit.circuit.library.standard_gates.u3.U3Gate  # true
# The gate differs by a global phase of i
xopcirc.data[0][0].params # [3.141592653589793, 1.5707963267948966, -1.5707963267948966]

# There is no global phase set to account for this
assert xopcirc.global_phase == 0

# The phase may be computed correctly
from qiskit.quantum_info.synthesis.one_qubit_decompose import OneQubitEulerDecomposer
decomposer = OneQubitEulerDecomposer('U3')
params = decomposer.angles_and_phase(xop.to_instruction().to_matrix())
# params = (3.141592653589793, 1.5707963267948966, -1.5707963267948966, -1.5707963267948966)

# But the property 'Unitary.definition' (computed by '_define') only asks for the angles,
# not the phase
```
### Information

- **Qiskit Terra version**:
  7a24e3a6  Aug 27, 2020
- **Python version**:
 3.8

### Suggested solutions

The parameters for the unitary are computed here:

https://github.com/Qiskit/qiskit-terra/blob/ef57f7275212e0c4aa728c17a34903b80e2df122/qiskit/extensions/unitary.py#L109-L110

I we instead do this:
```python
            theta, phi, lam, global_phase = _DECOMPOSER1Q.angles_and_phase(self.to_matrix())
            qc._append(U3Gate(theta, phi, lam), [q[0]], [])
            qc.global_phase = global_phase
```
then the matrix is computed correctly.

EDIT: Is there is a fairly efficient algorithm to find the more economical/simpler decomposition of the 1Q unitary as global_phase * U3, in this case, the one with global_phase = 1 ? My guess is that there would be little advantage.


Discussion:

Resolving patch:
diff --git a/qiskit/extensions/unitary.py b/qiskit/extensions/unitary.py
--- a/qiskit/extensions/unitary.py
+++ b/qiskit/extensions/unitary.py
@@ -106,8 +106,9 @@ def _define(self):
         if self.num_qubits == 1:
             q = QuantumRegister(1, "q")
             qc = QuantumCircuit(q, name=self.name)
-            theta, phi, lam = _DECOMPOSER1Q.angles(self.to_matrix())
+            theta, phi, lam, global_phase = _DECOMPOSER1Q.angles_and_phase(self.to_matrix())
             qc._append(U3Gate(theta, phi, lam), [q[0]], [])
+            qc.global_phase = global_phase
             self.definition = qc
         elif self.num_qubits == 2:
             self.definition = two_qubit_cnot_decompose(self.to_matrix())


Test patch:
diff --git a/test/python/circuit/test_unitary.py b/test/python/circuit/test_unitary.py
--- a/test/python/circuit/test_unitary.py
+++ b/test/python/circuit/test_unitary.py
@@ -273,3 +273,8 @@ def test_unitary_decomposition(self):
         qc = QuantumCircuit(3)
         qc.unitary(random_unitary(8, seed=42), [0, 1, 2])
         self.assertTrue(Operator(qc).equiv(Operator(qc.decompose())))
+
+    def test_unitary_decomposition_via_definition(self):
+        """Test decomposition for 1Q unitary via definition."""
+        mat = numpy.array([[0, 1], [1, 0]])
+        numpy.allclose(Operator(UnitaryGate(mat).definition).data, mat)