Repository: Qiskit/qiskit

Issue:
qc.count_ops() should return the operations sorted from high to low by number of occurances
<!-- ⚠️ If you do not respect this template, your issue will be closed -->
<!-- ⚠️ Make sure to browse the opened and closed issues to confirm this idea does not exist. -->

### What is the expected enhancement?
`count_ops` should use a ordered dict with the order set by the number of times the gate is used in the circuit.




Discussion:

Resolving patch:
diff --git a/qiskit/circuit/quantumcircuit.py b/qiskit/circuit/quantumcircuit.py
--- a/qiskit/circuit/quantumcircuit.py
+++ b/qiskit/circuit/quantumcircuit.py
@@ -19,7 +19,7 @@
 import sys
 import multiprocessing as mp
 from warnings import warn
-
+from collections import OrderedDict
 from qiskit.circuit.instruction import Instruction
 from qiskit.qasm.qasm import Qasm
 from qiskit.exceptions import QiskitError
@@ -672,7 +672,7 @@ def count_ops(self):
         """Count each operation kind in the circuit.
 
         Returns:
-            dict: a breakdown of how many operations of each kind.
+            OrderedDict: a breakdown of how many operations of each kind, sorted by amount.
         """
         count_ops = {}
         for instr, _, _ in self.data:
@@ -680,7 +680,7 @@ def count_ops(self):
                 count_ops[instr.name] += 1
             else:
                 count_ops[instr.name] = 1
-        return count_ops
+        return OrderedDict(sorted(count_ops.items(), key=lambda kv: kv[1], reverse=True))
 
     def num_connected_components(self, unitary_only=False):
         """How many non-entangled subcircuits can the circuit be factored to.


Test patch:
diff --git a/test/python/circuit/test_circuit_properties.py b/test/python/circuit/test_circuit_properties.py
--- a/test/python/circuit/test_circuit_properties.py
+++ b/test/python/circuit/test_circuit_properties.py
@@ -16,6 +16,7 @@
 
 """Test Qiskit's inverse gate operation."""
 
+from collections import OrderedDict
 import unittest
 import numpy as np
 from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
@@ -370,39 +371,18 @@ def test_circuit_size_ignores_barriers_snapshots(self):
     def test_circuit_count_ops(self):
         """Tet circuit count ops
         """
-        size = 6
-        q = QuantumRegister(size, 'q')
+        q = QuantumRegister(6, 'q')
         qc = QuantumCircuit(q)
+        qc.h(q)
+        qc.x(q[1])
+        qc.y(q[2:4])
+        qc.z(q[3:])
+        result = qc.count_ops()
+
+        expected = OrderedDict([('h', 6), ('z', 3), ('y', 2), ('x', 1)])
 
-        ans = {}
-        num_gates = np.random.randint(50)
-        # h = 0, x = 1, y = 2, z = 3, cx = 4, ccx = 5
-        lookup = {0: 'h', 1: 'x', 2: 'y', 3: 'z', 4: 'cx', 5: 'ccx'}
-
-        for _ in range(num_gates):
-            item = np.random.randint(6)
-            if item in [0, 1, 2, 3]:
-                idx = np.random.randint(size)
-                if item == 0:
-                    qc.h(q[idx])
-                elif item == 1:
-                    qc.x(q[idx])
-                elif item == 2:
-                    qc.y(q[idx])
-                elif item == 3:
-                    qc.z(q[idx])
-            else:
-                idx = np.random.permutation(size)
-                if item == 4:
-                    qc.cx(q[int(idx[0])], q[int(idx[1])])
-                elif item == 5:
-                    qc.ccx(q[int(idx[0])], q[int(idx[1])], q[int(idx[2])])
-            if lookup[item] not in ans.keys():
-                ans[lookup[item]] = 1
-            else:
-                ans[lookup[item]] += 1
-
-            self.assertEqual(ans, qc.count_ops())
+        self.assertIsInstance(result, OrderedDict)
+        self.assertEqual(expected, result)
 
     def test_circuit_connected_components_empty(self):
         """Verify num_connected_components is width for empty
@@ -500,10 +480,9 @@ def test_circuit_unitary_factors1(self):
     def test_circuit_unitary_factors2(self):
         """Test unitary factors multi qregs
         """
-        size = 4
-        q1 = QuantumRegister(size//2, 'q1')
-        q2 = QuantumRegister(size//2, 'q2')
-        c = ClassicalRegister(size, 'c')
+        q1 = QuantumRegister(2, 'q1')
+        q2 = QuantumRegister(2, 'q2')
+        c = ClassicalRegister(4, 'c')
         qc = QuantumCircuit(q1, q2, c)
         self.assertEqual(qc.num_unitary_factors(), 4)