qiskit_trebugger.model.data_collector
Implements a data collector for the qiskit transpiler with a custom callback function.
1"""Implements a data collector for the qiskit transpiler with a custom callback 2function. 3""" 4 5from copy import deepcopy 6 7 8from .pass_type import PassType 9from .property import Property 10from .transpilation_step import TranspilationStep 11 12 13class TranspilerDataCollector: 14 """Class to implement the data collector and the custom 15 callback function to collect data from the qiskit 16 transpiler 17 """ 18 19 def __init__(self, transpilation_sequence) -> None: 20 self.transpilation_sequence = transpilation_sequence 21 self._properties = {} 22 23 def callback(**kwargs): 24 pass_ = kwargs["pass_"] 25 26 pass_type = ( 27 PassType.ANALYSIS if pass_.is_analysis_pass else PassType.TRANSFORMATION 28 ) 29 30 transpilation_step = TranspilationStep(pass_.name(), pass_type) 31 transpilation_step.docs = pass_.__doc__ 32 transpilation_step.run_method_docs = getattr(pass_, "run").__doc__ 33 34 transpilation_step.duration = round(1000 * kwargs["time"], 2) 35 36 # Properties 37 property_set = kwargs["property_set"] 38 _added_props = [] 39 _updated_props = [] 40 for key in property_set: 41 value = property_set[key] 42 if key not in self._properties.keys(): 43 _added_props.append(key) 44 elif (self._properties[key] is None) and (value is not None): 45 _updated_props.append(key) 46 elif hasattr(value, "__len__") and ( 47 len(value) != len(self._properties[key]) 48 ): 49 _updated_props.append(key) 50 51 if len(_added_props) > 0 or len(_updated_props) > 0: 52 for property_name in property_set: 53 self._properties[property_name] = property_set[property_name] 54 55 property_state = "" 56 if property_name in _added_props: 57 property_state = "new" 58 elif property_name in _updated_props: 59 property_state = "updated" 60 61 transpilation_step.property_set[property_name] = Property( 62 property_name, 63 type(property_set[property_name]), 64 property_set[property_name], 65 property_state, 66 ) 67 68 dag = deepcopy(kwargs["dag"]) 69 70 # circuit stats: 71 if pass_.is_analysis_pass and len(self.transpilation_sequence.steps) > 0: 72 transpilation_step.circuit_stats = self.transpilation_sequence.steps[ 73 -1 74 ].circuit_stats 75 else: 76 transpilation_step.circuit_stats.width = dag.width() 77 transpilation_step.circuit_stats.size = dag.size() 78 transpilation_step.circuit_stats.depth = dag.depth() 79 80 circ_ops = {1: 0, 2: 0, 3: 0} 81 82 for node in dag.op_nodes(include_directives=False): 83 operands_count = len(node.qargs) 84 if operands_count < 4: 85 circ_ops[operands_count] += 1 86 87 transpilation_step.circuit_stats.ops_1q = circ_ops[1] 88 transpilation_step.circuit_stats.ops_2q = circ_ops[2] 89 transpilation_step.circuit_stats.ops_3q = circ_ops[3] 90 91 # Store `dag` to use it for circuit plot generation: 92 if ( 93 transpilation_step.pass_type == PassType.TRANSFORMATION 94 and transpilation_step.circuit_stats.depth <= 300 95 ): 96 transpilation_step.dag = dag 97 98 self.transpilation_sequence.add_step(transpilation_step) 99 100 self._transpiler_callback = callback 101 102 @property 103 def transpiler_callback(self): 104 """Custom callback for the transpiler 105 106 Returns: 107 function object: function handle for the callback 108 """ 109 return self._transpiler_callback 110 111 def show_properties(self): 112 """Displays transpilation sequence properties""" 113 print("Properties of transpilation sequence : ", self._properties)
class
TranspilerDataCollector:
14class TranspilerDataCollector: 15 """Class to implement the data collector and the custom 16 callback function to collect data from the qiskit 17 transpiler 18 """ 19 20 def __init__(self, transpilation_sequence) -> None: 21 self.transpilation_sequence = transpilation_sequence 22 self._properties = {} 23 24 def callback(**kwargs): 25 pass_ = kwargs["pass_"] 26 27 pass_type = ( 28 PassType.ANALYSIS if pass_.is_analysis_pass else PassType.TRANSFORMATION 29 ) 30 31 transpilation_step = TranspilationStep(pass_.name(), pass_type) 32 transpilation_step.docs = pass_.__doc__ 33 transpilation_step.run_method_docs = getattr(pass_, "run").__doc__ 34 35 transpilation_step.duration = round(1000 * kwargs["time"], 2) 36 37 # Properties 38 property_set = kwargs["property_set"] 39 _added_props = [] 40 _updated_props = [] 41 for key in property_set: 42 value = property_set[key] 43 if key not in self._properties.keys(): 44 _added_props.append(key) 45 elif (self._properties[key] is None) and (value is not None): 46 _updated_props.append(key) 47 elif hasattr(value, "__len__") and ( 48 len(value) != len(self._properties[key]) 49 ): 50 _updated_props.append(key) 51 52 if len(_added_props) > 0 or len(_updated_props) > 0: 53 for property_name in property_set: 54 self._properties[property_name] = property_set[property_name] 55 56 property_state = "" 57 if property_name in _added_props: 58 property_state = "new" 59 elif property_name in _updated_props: 60 property_state = "updated" 61 62 transpilation_step.property_set[property_name] = Property( 63 property_name, 64 type(property_set[property_name]), 65 property_set[property_name], 66 property_state, 67 ) 68 69 dag = deepcopy(kwargs["dag"]) 70 71 # circuit stats: 72 if pass_.is_analysis_pass and len(self.transpilation_sequence.steps) > 0: 73 transpilation_step.circuit_stats = self.transpilation_sequence.steps[ 74 -1 75 ].circuit_stats 76 else: 77 transpilation_step.circuit_stats.width = dag.width() 78 transpilation_step.circuit_stats.size = dag.size() 79 transpilation_step.circuit_stats.depth = dag.depth() 80 81 circ_ops = {1: 0, 2: 0, 3: 0} 82 83 for node in dag.op_nodes(include_directives=False): 84 operands_count = len(node.qargs) 85 if operands_count < 4: 86 circ_ops[operands_count] += 1 87 88 transpilation_step.circuit_stats.ops_1q = circ_ops[1] 89 transpilation_step.circuit_stats.ops_2q = circ_ops[2] 90 transpilation_step.circuit_stats.ops_3q = circ_ops[3] 91 92 # Store `dag` to use it for circuit plot generation: 93 if ( 94 transpilation_step.pass_type == PassType.TRANSFORMATION 95 and transpilation_step.circuit_stats.depth <= 300 96 ): 97 transpilation_step.dag = dag 98 99 self.transpilation_sequence.add_step(transpilation_step) 100 101 self._transpiler_callback = callback 102 103 @property 104 def transpiler_callback(self): 105 """Custom callback for the transpiler 106 107 Returns: 108 function object: function handle for the callback 109 """ 110 return self._transpiler_callback 111 112 def show_properties(self): 113 """Displays transpilation sequence properties""" 114 print("Properties of transpilation sequence : ", self._properties)
Class to implement the data collector and the custom callback function to collect data from the qiskit transpiler
TranspilerDataCollector(transpilation_sequence)
20 def __init__(self, transpilation_sequence) -> None: 21 self.transpilation_sequence = transpilation_sequence 22 self._properties = {} 23 24 def callback(**kwargs): 25 pass_ = kwargs["pass_"] 26 27 pass_type = ( 28 PassType.ANALYSIS if pass_.is_analysis_pass else PassType.TRANSFORMATION 29 ) 30 31 transpilation_step = TranspilationStep(pass_.name(), pass_type) 32 transpilation_step.docs = pass_.__doc__ 33 transpilation_step.run_method_docs = getattr(pass_, "run").__doc__ 34 35 transpilation_step.duration = round(1000 * kwargs["time"], 2) 36 37 # Properties 38 property_set = kwargs["property_set"] 39 _added_props = [] 40 _updated_props = [] 41 for key in property_set: 42 value = property_set[key] 43 if key not in self._properties.keys(): 44 _added_props.append(key) 45 elif (self._properties[key] is None) and (value is not None): 46 _updated_props.append(key) 47 elif hasattr(value, "__len__") and ( 48 len(value) != len(self._properties[key]) 49 ): 50 _updated_props.append(key) 51 52 if len(_added_props) > 0 or len(_updated_props) > 0: 53 for property_name in property_set: 54 self._properties[property_name] = property_set[property_name] 55 56 property_state = "" 57 if property_name in _added_props: 58 property_state = "new" 59 elif property_name in _updated_props: 60 property_state = "updated" 61 62 transpilation_step.property_set[property_name] = Property( 63 property_name, 64 type(property_set[property_name]), 65 property_set[property_name], 66 property_state, 67 ) 68 69 dag = deepcopy(kwargs["dag"]) 70 71 # circuit stats: 72 if pass_.is_analysis_pass and len(self.transpilation_sequence.steps) > 0: 73 transpilation_step.circuit_stats = self.transpilation_sequence.steps[ 74 -1 75 ].circuit_stats 76 else: 77 transpilation_step.circuit_stats.width = dag.width() 78 transpilation_step.circuit_stats.size = dag.size() 79 transpilation_step.circuit_stats.depth = dag.depth() 80 81 circ_ops = {1: 0, 2: 0, 3: 0} 82 83 for node in dag.op_nodes(include_directives=False): 84 operands_count = len(node.qargs) 85 if operands_count < 4: 86 circ_ops[operands_count] += 1 87 88 transpilation_step.circuit_stats.ops_1q = circ_ops[1] 89 transpilation_step.circuit_stats.ops_2q = circ_ops[2] 90 transpilation_step.circuit_stats.ops_3q = circ_ops[3] 91 92 # Store `dag` to use it for circuit plot generation: 93 if ( 94 transpilation_step.pass_type == PassType.TRANSFORMATION 95 and transpilation_step.circuit_stats.depth <= 300 96 ): 97 transpilation_step.dag = dag 98 99 self.transpilation_sequence.add_step(transpilation_step) 100 101 self._transpiler_callback = callback