Skip to content

Commit

Permalink
@ajavadia was to simplify FixedPoint analysis pass.
Browse files Browse the repository at this point in the history
  • Loading branch information
Luciano Bello committed Sep 24, 2018
1 parent 01f19c0 commit cd30801
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
8 changes: 2 additions & 6 deletions qiskit/transpiler/passes/fixed_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,17 @@

class FixedPoint(AnalysisPass):
""" A dummy analysis pass that checks if a property reached a fixed point. The results is saved
in property_set['fixed_point'][<property>] as a boolean. If a pass instance is set in
pass_that_updates_the_property, it will be added as a requires dependency.
in property_set['fixed_point'][<property>] as a boolean.
"""

def __init__(self, property_to_check, pass_that_updates_the_property=None):
def __init__(self, property_to_check):
"""
Args:
property_to_check (str): The property to check if a fixed point was reached.
pass_that_updates_the_property (BasePass): The pass instance that updates that property.
"""
super().__init__()
self._property = property_to_check
self._previous_value = None
if pass_that_updates_the_property:
self.requires = [pass_that_updates_the_property]

def run(self, dag):
if self.property_set['fixed_point'] is None:
Expand Down
11 changes: 8 additions & 3 deletions test/python/transpiler/_dummy_passes.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,18 @@ def run(self, dag):
return "Something else than DAG"


class PassK_check_fixed_point(DummyAP, FixedPoint):
class PassK_check_fixed_point_property(DummyAP, FixedPoint):
""" A dummy analysis pass that checks if a property reached a fixed point. The results is saved
in property_set['fixed_point'][<property>] as a boolean
AP: Analysis Pass
NR: PassG_calculates_dag_property() # set at __init__ time
R: PassG_calculates_dag_property()
"""

requires = [PassG_calculates_dag_property()]

def __init__(self):
super().__init__('property')

def run(self, dag):
for base in PassK_check_fixed_point.__bases__:
for base in PassK_check_fixed_point_property.__bases__:
base.run(self, dag)
24 changes: 12 additions & 12 deletions test/python/transpiler/test_pass_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from qiskit.transpiler._passmanager import PluginDoWhile
from ._dummy_passes import DummyTP, PassA_TP_NR_NP, PassB_TP_RA_PA, PassC_TP_RA_PA, \
PassD_TP_NR_NP, PassE_AP_NR_NP, PassF_reduce_dag_property, PassG_calculates_dag_property, \
PassH_Bad_TP, PassI_Bad_AP, PassJ_Bad_NoReturn, PassK_check_fixed_point
PassH_Bad_TP, PassI_Bad_AP, PassJ_Bad_NoReturn, PassK_check_fixed_point_property
from ..common import QiskitTestCase

logger = "LocalLogger"
Expand Down Expand Up @@ -258,72 +258,72 @@ def test_pass_no_return_a_dag(self):
def test_fixed_point_pass(self):
""" A pass set with a do_while parameter that checks for a fixed point. """
self.passmanager.add_pass(
[PassK_check_fixed_point('property', PassG_calculates_dag_property()),
[PassK_check_fixed_point_property(),
PassA_TP_NR_NP(),
PassF_reduce_dag_property()],
do_while=lambda property_set: not property_set['fixed_point']['property'])
self.assertScheduler(self.dag, self.passmanager,
['run analysis pass PassG_calculates_dag_property',
'set property as 8 (from dag.property)',
'run analysis pass PassK_check_fixed_point',
'run analysis pass PassK_check_fixed_point_property',
'run transformation pass PassA_TP_NR_NP',
'run transformation pass PassF_reduce_dag_property',
'dag property = 6',
'run analysis pass PassG_calculates_dag_property',
'set property as 6 (from dag.property)',
'run analysis pass PassK_check_fixed_point',
'run analysis pass PassK_check_fixed_point_property',
'run transformation pass PassA_TP_NR_NP',
'run transformation pass PassF_reduce_dag_property',
'dag property = 5',
'run analysis pass PassG_calculates_dag_property',
'set property as 5 (from dag.property)',
'run analysis pass PassK_check_fixed_point',
'run analysis pass PassK_check_fixed_point_property',
'run transformation pass PassA_TP_NR_NP',
'run transformation pass PassF_reduce_dag_property',
'dag property = 4',
'run analysis pass PassG_calculates_dag_property',
'set property as 4 (from dag.property)',
'run analysis pass PassK_check_fixed_point',
'run analysis pass PassK_check_fixed_point_property',
'run transformation pass PassA_TP_NR_NP',
'run transformation pass PassF_reduce_dag_property',
'dag property = 3',
'run analysis pass PassG_calculates_dag_property',
'set property as 3 (from dag.property)',
'run analysis pass PassK_check_fixed_point',
'run analysis pass PassK_check_fixed_point_property',
'run transformation pass PassA_TP_NR_NP',
'run transformation pass PassF_reduce_dag_property',
'dag property = 2',
'run analysis pass PassG_calculates_dag_property',
'set property as 2 (from dag.property)',
'run analysis pass PassK_check_fixed_point',
'run analysis pass PassK_check_fixed_point_property',
'run transformation pass PassA_TP_NR_NP',
'run transformation pass PassF_reduce_dag_property',
'dag property = 2',
'run analysis pass PassG_calculates_dag_property',
'set property as 2 (from dag.property)',
'run analysis pass PassK_check_fixed_point',
'run analysis pass PassK_check_fixed_point_property',
'run transformation pass PassA_TP_NR_NP',
'run transformation pass PassF_reduce_dag_property',
'dag property = 2'])

def test_fixed_point_pass_max_iteration(self):
""" A pass set with a do_while parameter that checks that the max_iteration is raised. """
self.passmanager.add_pass(
[PassK_check_fixed_point('property', PassG_calculates_dag_property()),
[PassK_check_fixed_point_property(),
PassA_TP_NR_NP(),
PassF_reduce_dag_property()],
do_while=lambda property_set: not property_set['fixed_point']['property'],
max_iteration=2)
self.assertSchedulerRaises(self.dag, self.passmanager,
['run analysis pass PassG_calculates_dag_property',
'set property as 8 (from dag.property)',
'run analysis pass PassK_check_fixed_point',
'run analysis pass PassK_check_fixed_point_property',
'run transformation pass PassA_TP_NR_NP',
'run transformation pass PassF_reduce_dag_property',
'dag property = 6',
'run analysis pass PassG_calculates_dag_property',
'set property as 6 (from dag.property)',
'run analysis pass PassK_check_fixed_point',
'run analysis pass PassK_check_fixed_point_property',
'run transformation pass PassA_TP_NR_NP',
'run transformation pass PassF_reduce_dag_property',
'dag property = 5'], TranspilerError)
Expand Down

0 comments on commit cd30801

Please sign in to comment.