Repository: Qiskit/qiskit

Issue:
optimize_1q_gates unit test failures
Two unit test failures related to the `optimize_1q_gates` pass were uncovered while working on #1815. These were marked as `expectedFailure` since they weren't caused by the DAGNode PR, but they should be fixed.


```
======================================================================
FAIL: test_ignores_conditional_rotations (python.transpiler.test_optimize_1q_gates.TestOptimize1qGates)
Conditional rotations should not be considered in the chain.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/kevin.krsulichibm.com/q/qiskit-terra/test/python/transpiler/test_optimize_1q_gates.py", line 145, in test_ignores_conditional_rotations
    self.assertEqual(circuit_to_dag(expected), after)
AssertionError: <qiskit.dagcircuit._dagcircuit.DAGCircuit object at 0x12d028fd0> != <qiskit.dagcircuit._dagcircuit.DAGCircuit object at 0x12d00f9e8>

Expected:
         ┌─────────┐┌─────────┐┌─────────┐
qr_0: |0>┤ U1(0.1) ├┤ U1(0.2) ├┤ U1(0.7) ├
         ├────┴────┤├────┴────┤└─────────┘
 cr_0: 0 ╡         ╞╡         ╞═══════════
         │   = 1   ││   = 3   │
 cr_1: 0 ╡         ╞╡         ╞═══════════
         └─────────┘└─────────┘
Actual:
         ┌─────────┐┌─────────┐┌─────────┐
qr_0: |0>┤ U1(0.1) ├┤ U1(0.2) ├┤ U1(0.3) ├
         ├────┴────┤├────┴────┤└─────────┘
 cr_0: 0 ╡         ╞╡         ╞═══════════
         │   = 1   ││   = 3   │
 cr_1: 0 ╡         ╞╡         ╞═══════════
         └─────────┘└─────────┘

======================================================================
FAIL: test_optimize_h_gates_pass_manager (python.transpiler.test_optimize_1q_gates.TestOptimize1qGates)
Transpile: qr:--[H]-[H]-[H]-- == qr:--[u2]--
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/kevin.krsulichibm.com/q/qiskit-terra/test/python/transpiler/test_optimize_1q_gates.py", line 54, in test_optimize_h_gates_pass_manager
    self.assertEqual(expected, result)
AssertionError: <qiskit.circuit.quantumcircuit.QuantumCircuit object at 0x1335f5550> != <qiskit.circuit.quantumcircuit.QuantumCircuit object at 0x12d495c18>

Expected:
         ┌──────────────┐
qr_0: |0>┤ U2(0,3.1416) ├
         └──────────────┘
Actual:
         ┌──────────────┐
qr_0: |0>┤ U2(0,3.1416) ├
         └──────────────┘
```



Discussion:
I think the issue with the second test is somewhere in the pass the constant `pi` is being converted into `3.141592` so when the comparison checks if the two nodes are the same, the parameters are not considered to be equal.

Resolving patch:
diff --git a/qiskit/circuit/instruction.py b/qiskit/circuit/instruction.py
--- a/qiskit/circuit/instruction.py
+++ b/qiskit/circuit/instruction.py
@@ -90,8 +90,9 @@ def __eq__(self, other):
         """
         res = False
         if type(self) is type(other) and \
-                self.name == other.name and \
-                self.params == other.params:
+                self.name == other.name and (self.params == other.params or
+                                             [float(param) for param in other.params] == [
+                                                 float(param) for param in self.params]):
             res = True
         return res
 
diff --git a/qiskit/transpiler/passes/optimize_1q_gates.py b/qiskit/transpiler/passes/optimize_1q_gates.py
--- a/qiskit/transpiler/passes/optimize_1q_gates.py
+++ b/qiskit/transpiler/passes/optimize_1q_gates.py
@@ -22,7 +22,6 @@
 from qiskit.quantum_info.operators.quaternion import quaternion_from_euler
 from qiskit.transpiler.passes.unroller import Unroller
 
-
 _CHOP_THRESHOLD = 1e-15
 
 
@@ -48,7 +47,6 @@ def run(self, dag):
                         or len(current_node.qargs) != 1
                         or current_node.qargs[0] != run_qarg
                         or left_name not in ["u1", "u2", "u3", "id"]):
-
                     raise MapperError("internal error")
                 if left_name == "u1":
                     left_parameters = (0, 0, current_node.op.params[0])
@@ -171,15 +169,16 @@ def run(self, dag):
             if right_name == "u2":
                 new_op = U2Gate(right_parameters[1], right_parameters[2], run_qarg)
             if right_name == "u3":
-                new_op = U3Gate(*right_parameters, run_qarg)
-            dag.node(run[0])['name'] = right_name
-            dag.node(run[0])['op'] = new_op
+                new_op = U3Gate(right_parameters[0], right_parameters[1], right_parameters[2],
+                                run_qarg)
+
+            if right_name != 'nop':
+                dag.apply_operation_back(new_op)
 
             # Delete the other nodes in the run
-            for current_node in run[1:]:
+            for current_node in run:
                 dag._remove_op_node(current_node)
-            if right_name == "nop":
-                dag._remove_op_node(run[0])
+
         return dag
 
     @staticmethod


Test patch:
diff --git a/test/python/transpiler/test_optimize_1q_gates.py b/test/python/transpiler/test_optimize_1q_gates.py
--- a/test/python/transpiler/test_optimize_1q_gates.py
+++ b/test/python/transpiler/test_optimize_1q_gates.py
@@ -36,7 +36,6 @@ def test_optimize_id(self):
 
         self.assertEqual(circuit_to_dag(expected), after)
 
-    @unittest.expectedFailure
     def test_optimize_h_gates_pass_manager(self):
         """Transpile: qr:--[H]-[H]-[H]-- == qr:--[u2]-- """
         qr = QuantumRegister(1, 'qr')
@@ -106,9 +105,8 @@ def test_optimize_1q_gates_symbolic(self):
         simplified_dag = Optimize1qGates().run(dag)
 
         params = set()
-        for node_id in simplified_dag.named_nodes('u1'):
-            node = simplified_dag.node(node_id)
-            params.add(node['op'].params[0])
+        for node in simplified_dag.named_nodes('u1'):
+            params.add(node.op.params[0])
 
         expected_params = {sympy.Number(-3 * np.pi / 2),
                            sympy.Number(1.0 + 0.55 * np.pi),
@@ -117,7 +115,6 @@ def test_optimize_1q_gates_symbolic(self):
 
         self.assertEqual(params, expected_params)
 
-    @unittest.expectedFailure
     def test_ignores_conditional_rotations(self):
         """Conditional rotations should not be considered in the chain.