Repository: Qiskit/qiskit

Issue:
Error in matplotlib RGBA values for ```plot_state_qsphere()``` function
<!-- ⚠️ If you do not respect this template, your issue will be closed -->
<!-- ⚠️ Make sure to browse the opened and closed issues -->

### Information
I was trying out Grover's search algorithm when encountered this problem. The ```plot_state_qsphere()``` function gives an error because the **calculated RGBA values are not between 0-1**. The goal was to display each iteration of Grover's algorithm step-by-step to properly visualize what's happening within the algorithm.

- **Qiskit Terra version**: 0.14.1
- **Python version**: 3.7.7
- **Operating system**: MacOS

### What is the current behavior?
The code gives this error:-

```
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-16-0aa254c5599e> in <module>
      1 backend = Aer.get_backend('statevector_simulator')
      2 result = execute(qc, backend=backend, shots=1).result()
----> 3 plot_state_qsphere(result.get_statevector(qc))

/usr/local/lib/python3.7/site-packages/qiskit/visualization/state_visualization.py in plot_state_qsphere(rho, figsize, ax)
    782                 a = Arrow3D([0, xvalue], [0, yvalue], [0, zvalue],
    783                             mutation_scale=20, alpha=prob, arrowstyle="-",
--> 784                             color=colorstate, lw=2)
    785                 ax.add_artist(a)
    786 

/usr/local/lib/python3.7/site-packages/qiskit/visualization/state_visualization.py in __init__(self, xs, ys, zs, *args, **kwargs)
     48         def __init__(self, xs, ys, zs, *args, **kwargs):
     49             """Create arrow."""
---> 50             FancyArrowPatch.__init__(self, (0, 0), (0, 0), *args, **kwargs)
     51             self._verts3d = xs, ys, zs
     52 

/usr/local/lib/python3.7/site-packages/matplotlib/patches.py in __init__(self, posA, posB, path, arrowstyle, arrow_transmuter, connectionstyle, connector, patchA, patchB, shrinkA, shrinkB, mutation_scale, mutation_aspect, dpi_cor, **kwargs)
   3998         kwargs.setdefault("capstyle", "round")
   3999 
-> 4000         Patch.__init__(self, **kwargs)
   4001 
   4002         if posA is not None and posB is not None and path is None:

/usr/local/lib/python3.7/site-packages/matplotlib/patches.py in __init__(self, edgecolor, facecolor, color, linewidth, linestyle, antialiased, hatch, fill, capstyle, joinstyle, **kwargs)
     94 
     95         if len(kwargs):
---> 96             self.update(kwargs)
     97 
     98     def get_verts(self):

/usr/local/lib/python3.7/site-packages/matplotlib/artist.py in update(self, props)
   1004 
   1005         with cbook._setattr_cm(self, eventson=False):
-> 1006             ret = [_update_property(self, k, v) for k, v in props.items()]
   1007 
   1008         if len(ret):

/usr/local/lib/python3.7/site-packages/matplotlib/artist.py in <listcomp>(.0)
   1004 
   1005         with cbook._setattr_cm(self, eventson=False):
-> 1006             ret = [_update_property(self, k, v) for k, v in props.items()]
   1007 
   1008         if len(ret):

/usr/local/lib/python3.7/site-packages/matplotlib/artist.py in _update_property(self, k, v)
   1001                     raise AttributeError('{!r} object has no property {!r}'
   1002                                          .format(type(self).__name__, k))
-> 1003                 return func(v)
   1004 
   1005         with cbook._setattr_cm(self, eventson=False):

/usr/local/lib/python3.7/site-packages/matplotlib/patches.py in set_alpha(self, alpha)
    376         # docstring inherited
    377         super().set_alpha(alpha)
--> 378         self._set_facecolor(self._original_facecolor)
    379         self._set_edgecolor(self._original_edgecolor)
    380         # stale is already True

/usr/local/lib/python3.7/site-packages/matplotlib/patches.py in _set_facecolor(self, color)
    343             color = mpl.rcParams['patch.facecolor']
    344         alpha = self._alpha if self._fill else 0
--> 345         self._facecolor = colors.to_rgba(color, alpha)
    346         self.stale = True
    347 

/usr/local/lib/python3.7/site-packages/matplotlib/colors.py in to_rgba(c, alpha)
    183         rgba = None
    184     if rgba is None:  # Suppress exception chaining of cache lookup failure.
--> 185         rgba = _to_rgba_no_colorcycle(c, alpha)
    186         try:
    187             _colors_full_map.cache[c, alpha] = rgba

/usr/local/lib/python3.7/site-packages/matplotlib/colors.py in _to_rgba_no_colorcycle(c, alpha)
    276         c = c[:3] + (alpha,)
    277     if any(elem < 0 or elem > 1 for elem in c):
--> 278         raise ValueError("RGBA values should be within 0-1 range")
    279     return c
    280 

ValueError: RGBA values should be within 0-1 range
```

Resultant qsphere in the output:-

<img width="787" alt="Screenshot 2020-07-08 at 2 31 42 PM" src="https://user-images.githubusercontent.com/30632765/86899521-c0c14b80-c127-11ea-9e8d-a9ece852a9eb.png">


### Steps to reproduce the problem
This is my code and circuit:-

<img width="1143" alt="Screenshot 2020-07-08 at 2 24 31 PM" src="https://user-images.githubusercontent.com/30632765/86898772-c10d1700-c126-11ea-9067-7956cdbadc45.png">

The implementation of ```GroversSearch()```function is not important for the purpose of this issue. Please follow the circuit provided in the above image.


### What is the expected behavior?
It should display a **qsphere** with the proper state representations and phase angles.


### Suggested solutions
None.




Discussion:
Here is the code to reproduce this error:
```python
from qiskit import QuantumRegister, QuantumCircuit, ClassicalRegister, Aer, execute, visualization
from numpy import pi

qc = QuantumCircuit(4)
qc.h(range(4))
qc.u1(pi/2, 0)
qc.x(1)
qc.u1(pi/2, 1)
qc.u1(pi/2, 2)
qc.x(3)
qc.cx(0,3)
qc.cx(1,3)
qc.cx(2,3)
qc.u1(-pi/2, 3)
qc.cx(0,3)
qc.cx(1,3)
qc.cx(2,3)
qc.h(0)
qc.x(0)
qc.u1(pi/2, 0)
qc.x(1)
qc.h(1)
qc.x(1)
qc.u1(pi/2, 1)
qc.h(2)
qc.x(2)
qc.u1(pi/2, 2)
qc.u1(pi/2, 3)
qc.x(3)
qc.h(3)
qc.x(3)
qc.cx(0,3)
qc.cx(1,3)
qc.cx(2,3)
qc.u1(-pi/2, 3)
qc.cx(0,3)
qc.cx(1,3)
qc.cx(2,3)
qc.u1(pi/2, 3)
qc.x(range(4))
qc.h(range(4))
qc.draw('mpl')

backend = Aer.get_backend('statevector_simulator')
result = execute(qc, backend=backend, shots=1).result()
visualization.plot_state_qsphere(result.get_statevector(qc))
```

Resolving patch:
diff --git a/qiskit/visualization/state_visualization.py b/qiskit/visualization/state_visualization.py
--- a/qiskit/visualization/state_visualization.py
+++ b/qiskit/visualization/state_visualization.py
@@ -768,13 +768,15 @@ def plot_state_qsphere(state, figsize=None, ax=None, show_state_labels=True,
 
                 # get prob and angle - prob will be shade and angle color
                 prob = np.real(np.dot(state[i], state[i].conj()))
+                if prob > 1:  # See https://github.com/Qiskit/qiskit-terra/issues/4666
+                    prob = 1
                 colorstate = phase_to_rgb(state[i])
 
                 alfa = 1
                 if yvalue >= 0.1:
                     alfa = 1.0 - yvalue
 
-                if prob > 0 and show_state_labels:
+                if not np.isclose(prob, 0) and show_state_labels:
                     rprime = 1.3
                     angle_theta = np.arctan2(np.sqrt(1 - zvalue ** 2), zvalue)
                     xvalue_text = rprime * np.sin(angle_theta) * np.cos(angle)


Test patch: