Repository: Qiskit/qiskit

Issue:
Mapper error when I try to execute QuantumCircuit as an object of a user defined class
<!-- ⚠️ If you do not respect this template, your issue will be closed -->
<!-- ⚠️ Make sure to browse the opened and closed issues -->

### Informations

- **Qiskit version**: 0.7.0
- **Python version**: 3.6.7
- **Operating system**: Ubuntu 18.04

### What is the current behavior?
/home/varun/.local/lib/python3.6/site-packages/marshmallow/schema.py:364: ChangedInMarshmallow3Warning: strict=False is not recommended. In marshmallow 3.0, schemas will always be strict. See https://marshmallow.readthedocs.io/en/latest/upgrading.html#schemas-are-always-strict
  ChangedInMarshmallow3Warning
Please provide the cooeficeints for the intital state in the format a+bj
Coefficient of state zero1+0j
Coefficient of state one0+0j
Provide the square of normalisation denominator1
[1.+0.j 0.+0.j]
The best backend is ibmq_16_melbourne
Traceback (most recent call last):
  File "class_counts.py", line 183, in <module>
    main()
  File "class_counts.py", line 163, in main
    sim = obj.simulate(p,b)
  File "class_counts.py", line 111, in simulate
    dec_state = self.ibmq(b)
  File "class_counts.py", line 97, in ibmq
    job_exp = execute(self.qc, backend=backend, shots=shots, max_credits=max_credits)
  File "/home/varun/.local/lib/python3.6/site-packages/qiskit/tools/compiler.py", line 108, in execute
    skip_transpiler, seed_mapper, pass_manager, memory)
  File "/home/varun/.local/lib/python3.6/site-packages/qiskit/tools/compiler.py", line 61, in compile
    seed_mapper, pass_manager)
  File "/home/varun/.local/lib/python3.6/site-packages/qiskit/transpiler/_transpiler.py", line 67, in transpile
    'pass_manager': pass_manager})
  File "/home/varun/.local/lib/python3.6/site-packages/qiskit/tools/parallel.py", line 93, in parallel_map
    return [task(values[0], *task_args, **task_kwargs)]
  File "/home/varun/.local/lib/python3.6/site-packages/qiskit/transpiler/_transpiler.py", line 109, in _transpilation
    pass_manager=pass_manager)
  File "/home/varun/.local/lib/python3.6/site-packages/qiskit/transpiler/_transpiler.py", line 199, in transpile_dag
    dag = Optimize1qGates().run(dag)
  File "/home/varun/.local/lib/python3.6/site-packages/qiskit/transpiler/passes/optimize_1q_gates.py", line 53, in run
    raise MapperError("internal error")
qiskit.mapper._mappererror.MapperError: 'internal error'



### Steps to reproduce the problem
The QuantumRegister, ClassicalRegister and QuantumCircuit are intialised when a object is created for the user-defined class I have made. The code ran without errors on the local machine and on the hpc. But when I tried to run it on the ibm Qx_16 it showed this error.


### What is the expected behavior?
I expect to get values of get_counts() for 20 different values for a given parameter. For each value a new object is created and the entire circuit is simulated.

### Suggested solutions
The problem could be because I create a new object every iteration of the loop. I made sure to delete the object after each iteration though,


Discussion:
Hi @isolatedinformation could you provide some more information about the circuit you were trying to execute when this error occurred?
My circuit consisits of 9 qubits and 9 cbits to record thier outcomes. Basically, I was trying to simulate a noisy channel and perform error correction to retireve the initial message. The noisiness of the channel was tuned by a parameter \gamma. For diiferent values of \gamma, I wanted the statistics of the measuremnt outcome and plotted them. For instance, this attached image was the statistics I got for 20 different values of gamma when run on the HPC.
![zero](https://user-images.githubusercontent.com/27089492/52951977-3731a200-33a9-11e9-9c80-dc164d88c2b0.png)

I created a class to make sure the entire circuit was re initilased for every iteration of \gamma since each iteration is independent of the other. The objects of the class helped achieve this objective.

If it's still not clear, I can email my code to you.

I think this is the same issue as #1871 

Resolving patch:
diff --git a/qiskit/dagcircuit/_dagcircuit.py b/qiskit/dagcircuit/_dagcircuit.py
--- a/qiskit/dagcircuit/_dagcircuit.py
+++ b/qiskit/dagcircuit/_dagcircuit.py
@@ -1336,7 +1336,7 @@ def multigraph_layers(self):
             next_layer = []
 
     def collect_runs(self, namelist):
-        """Return a set of runs of "op" nodes with the given names.
+        """Return a set of non-conditional runs of "op" nodes with the given names.
 
         For example, "... h q[0]; cx q[0],q[1]; cx q[0],q[1]; h q[1]; .."
         would produce the tuple of cx nodes as an element of the set returned
@@ -1357,7 +1357,7 @@ def collect_runs(self, namelist):
         for node in tops_node:
             nd = self.multi_graph.node[node]
             if nd["type"] == "op" and nd["name"] in namelist \
-                    and not nodes_seen[node]:
+                    and nd["condition"] is None and not nodes_seen[node]:
                 group = [node]
                 nodes_seen[node] = True
                 s = list(self.multi_graph.successors(node))


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
@@ -20,7 +20,7 @@
 
 
 class TestOptimize1qGates(QiskitTestCase):
-    """ Test for 1q gate optimizations. """
+    """Test for 1q gate optimizations. """
 
     def test_optimize_id(self):
         """ qr0:--[id]-- == qr0:------ """
@@ -36,12 +36,8 @@ def test_optimize_id(self):
 
         self.assertEqual(circuit_to_dag(expected), after)
 
-
-class TestOptimize1qGatesTranspiler(QiskitTestCase):
-    """ Test for 1q gate optimizations as part of the transpiler, with a PassManager """
-
-    def test_optimize_h_gates(self):
-        """ Transpile: qr:--[H]-[H]-[H]-- == qr:--[u2]-- """
+    def test_optimize_h_gates_pass_manager(self):
+        """Transpile: qr:--[H]-[H]-[H]-- == qr:--[u2]-- """
         qr = QuantumRegister(1, 'qr')
         circuit = QuantumCircuit(qr)
         circuit.h(qr[0])
@@ -56,10 +52,6 @@ def test_optimize_h_gates(self):
         result = transpile(circuit, FakeRueschlikon(), pass_manager=passmanager)
         self.assertEqual(expected, result)
 
-
-class TestMovedFromMapper(QiskitTestCase):
-    """ This tests are moved from test_mapper.py"""
-
     def test_optimize_1q_gates_collapse_identity(self):
         """test optimize_1q_gates removes u1(2*pi) rotations.
 
@@ -122,6 +114,34 @@ def test_optimize_1q_gates_symbolic(self):
 
         self.assertEqual(params, expected_params)
 
+    def test_ignores_conditional_rotations(self):
+        """Conditional rotations should not be considered in the chain.
+
+        qr0:--[U1]-[U1]-[U1]-[U1]-    qr0:--[U1]-[U1]-
+               ||   ||                       ||   ||
+        cr0:===.================== == cr0:===.====.===
+                    ||                            ||
+        cr1:========.=============    cr1:========.===
+        """
+        qr = QuantumRegister(1, 'qr')
+        cr = ClassicalRegister(2, 'cr')
+        circuit = QuantumCircuit(qr, cr)
+        circuit.u1(0.1, qr).c_if(cr, 1)
+        circuit.u1(0.2, qr).c_if(cr, 3)
+        circuit.u1(0.3, qr)
+        circuit.u1(0.4, qr)
+        dag = circuit_to_dag(circuit)
+
+        expected = QuantumCircuit(qr, cr)
+        expected.u1(0.1, qr).c_if(cr, 1)
+        expected.u1(0.2, qr).c_if(cr, 3)
+        expected.u1(0.7, qr)
+
+        pass_ = Optimize1qGates()
+        after = pass_.run(dag)
+
+        self.assertEqual(circuit_to_dag(expected), after)
+
 
 if __name__ == '__main__':
     unittest.main()