Repository: Qiskit/qiskit

Issue:
pi_check does not convert param containing ParameterExpression of a float
<!-- ⚠️ 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**: current main
- **Python version**: 3.8
- **Operating system**: Ubuntu

### What is the current behavior?

Running this code with main from June 29th,
```
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, transpile
from qiskit.test.mock import FakeTenerife
circuit = QuantumCircuit(3)
circuit.h(1)
transpiled = transpile(circuit, backend=FakeTenerife(),
                       optimization_level=0, initial_layout=[1, 2, 0],
                       basis_gates=["id", "cx", "rz", "sx", "x"], seed_transpiler=0)

transpiled.draw('mpl')
```
produces
![image](https://user-images.githubusercontent.com/16268251/125964884-f9438238-9d7e-42bb-8147-b982efc38083.png)
and using current main produces
![image](https://user-images.githubusercontent.com/16268251/125965214-1b8fe793-7ae0-4ff1-b668-5ec1ab2fde6c.png)
All other gates with params seem to properly display pi.

### Steps to reproduce the problem



### What is the expected behavior?

Should see pi/2.

### Suggested solutions

Not sure what changed. The param in this case is a ParameterExpression which follows a different path in pi_check. For some reason, either a change in tolerances or a change in sympy perhaps, a trailing zero was being added to the sympy expression. This meant the param would not match the sympy expression and the pi string was not substituted.

Should be fixable by a str(float(sym)), but it might be good to know what changed in case there are hidden issues.



Discussion:

Resolving patch:
diff --git a/qiskit/circuit/tools/pi_check.py b/qiskit/circuit/tools/pi_check.py
--- a/qiskit/circuit/tools/pi_check.py
+++ b/qiskit/circuit/tools/pi_check.py
@@ -56,15 +56,14 @@ def pi_check(inpt, eps=1e-6, output="text", ndigits=5):
         for sym in syms:
             if not sym.is_number:
                 continue
-            pi = pi_check(float(sym), eps=eps, output=output, ndigits=ndigits)
+            pi = pi_check(abs(float(sym)), eps=eps, output=output, ndigits=ndigits)
             try:
                 _ = float(pi)
             except (ValueError, TypeError):
-                # Strip leading '-' from pi since must replace with abs(sym)
-                # in order to preserve spacing around minuses in expression
-                if pi[0] == "-":
-                    pi = pi[1:]
-                param_str = param_str.replace(str(abs(sym)), pi)
+                from sympy import sstr
+
+                sym_str = sstr(abs(sym), full_prec=False)
+                param_str = param_str.replace(sym_str, pi)
         return param_str
     elif isinstance(inpt, str):
         return inpt


Test patch:
diff --git a/test/python/circuit/test_tools.py b/test/python/circuit/test_tools.py
--- a/test/python/circuit/test_tools.py
+++ b/test/python/circuit/test_tools.py
@@ -86,6 +86,14 @@ def test_params(self):
         result = pi_check(input_number)
         self.assertEqual(result, expected_string)
 
+    def test_params_str(self):
+        """Test pi/2 displays properly in ParameterExpression - #6758"""
+        x = Parameter("x")
+        input_number = x + pi / 2
+        expected_string = "x + π/2"
+        result = pi_check(input_number)
+        self.assertEqual(result, expected_string)
+
 
 if __name__ == "__main__":
     unittest.main(verbosity=2)