Skip to content

Commit

Permalink
Adjust imports in aqc plugin to avoid discovery overhead (#7327)
Browse files Browse the repository at this point in the history
This commit tweaks the imports for the aqc plugin to make the optimizer
and other aqc method imports occur at run time only. Previously these
were done at the module level, which is normally best practice for many
reasons, but in this specific case doing plugin discovery causes the
module to be loaded (and the plugin class instaniated) even if the
plugin is not specified by the user. The imports for aqc are fairly
heavy as both the algorithms tree and scipy can be slow to load. To
avoid the overhead on discovery these imports are moved to runtime so we
only load them if the aqc plugin is used and these imports are needed.

Co-authored-by: Jake Lishman <[email protected]>
  • Loading branch information
mtreinish and jakelishman authored Dec 1, 2021
1 parent 0196754 commit b807eb4
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions qiskit/transpiler/synthesis/aqc/aqc_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,8 @@
"""
import numpy as np

from qiskit.algorithms.optimizers import L_BFGS_B
from qiskit.converters import circuit_to_dag
from qiskit.transpiler.passes.synthesis.plugin import UnitarySynthesisPlugin
from qiskit.transpiler.synthesis.aqc.aqc import AQC
from qiskit.transpiler.synthesis.aqc.cnot_structures import make_cnot_network
from qiskit.transpiler.synthesis.aqc.cnot_unit_circuit import CNOTUnitCircuit
from qiskit.transpiler.synthesis.aqc.cnot_unit_objective import DefaultCNOTUnitObjective


class AQCSynthesisPlugin(UnitarySynthesisPlugin):
Expand Down Expand Up @@ -96,6 +91,15 @@ def supports_coupling_map(self):
return False

def run(self, unitary, **options):

# Runtime imports to avoid the overhead of these imports for
# plugin discovery and only use them if the plugin is run/used
from qiskit.algorithms.optimizers import L_BFGS_B
from qiskit.transpiler.synthesis.aqc.aqc import AQC
from qiskit.transpiler.synthesis.aqc.cnot_structures import make_cnot_network
from qiskit.transpiler.synthesis.aqc.cnot_unit_circuit import CNOTUnitCircuit
from qiskit.transpiler.synthesis.aqc.cnot_unit_objective import DefaultCNOTUnitObjective

num_qubits = int(round(np.log2(unitary.shape[0])))

config = options.get("config") or {}
Expand Down

0 comments on commit b807eb4

Please sign in to comment.