Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run logical optimization passes in init stage #11354

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions qiskit/dagcircuit/dagcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -1900,10 +1900,10 @@ def filter_fn(node):
isinstance(node, DAGOpNode)
and len(node.qargs) == 1
and len(node.cargs) == 0
and getattr(node.op, "condition", None) is None
and not node.op.is_parameterized()
and isinstance(node.op, Gate)
and hasattr(node.op, "__array__")
and getattr(node.op, "condition", None) is None
and not node.op.is_parameterized()
)

return rx.collect_runs(self._multi_graph, filter_fn)
Expand Down
60 changes: 59 additions & 1 deletion qiskit/transpiler/preset_passmanagers/builtin_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class DefaultInitPassManager(PassManagerStagePlugin):
"""Plugin class for default init stage."""

def pass_manager(self, pass_manager_config, optimization_level=None) -> PassManager:
if optimization_level in {1, 2, 0}:
if optimization_level == 0:
init = None
if (
pass_manager_config.initial_layout
Expand All @@ -88,6 +88,44 @@ def pass_manager(self, pass_manager_config, optimization_level=None) -> PassMana
pass_manager_config.unitary_synthesis_plugin_config,
pass_manager_config.hls_config,
)
elif optimization_level in {1, 2}:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incredibly minor: there's no reason to use a set here - optimization_level in (1, 2) would technically be more efficient. Not important at all, though.

init = PassManager()
if (
pass_manager_config.initial_layout
or pass_manager_config.coupling_map
or (
pass_manager_config.target is not None
and pass_manager_config.target.build_coupling_map() is not None
)
):
init += common.generate_unroll_3q(
pass_manager_config.target,
pass_manager_config.basis_gates,
pass_manager_config.approximation_degree,
pass_manager_config.unitary_synthesis_method,
pass_manager_config.unitary_synthesis_plugin_config,
pass_manager_config.hls_config,
)
init.append(Optimize1qGatesDecomposition())
init.append(
InverseCancellation(
[
CXGate(),
ECRGate(),
CZGate(),
CYGate(),
XGate(),
YGate(),
ZGate(),
HGate(),
SwapGate(),
(TGate(), TdgGate()),
(SGate(), SdgGate()),
(SXGate(), SXdgGate()),
]
)
)

elif optimization_level == 3:
init = common.generate_unroll_3q(
pass_manager_config.target,
Expand All @@ -99,6 +137,26 @@ def pass_manager(self, pass_manager_config, optimization_level=None) -> PassMana
)
init.append(OptimizeSwapBeforeMeasure())
init.append(RemoveDiagonalGatesBeforeMeasure())
init.append(Optimize1qGatesDecomposition())
init.append(
InverseCancellation(
[
CXGate(),
ECRGate(),
CZGate(),
CYGate(),
XGate(),
YGate(),
ZGate(),
HGate(),
SwapGate(),
(TGate(), TdgGate()),
(SGate(), SdgGate()),
(SXGate(), SXdgGate()),
]
)
)

else:
return TranspilerError(f"Invalid optimization level {optimization_level}")
Comment on lines 160 to 161
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Completely unrelated, but imo this should actually be a ValueError.

return init
Expand Down
6 changes: 4 additions & 2 deletions test/python/transpiler/test_sabre_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,13 +423,15 @@ def test_starting_layout(self):
def test_integration_with_pass_manager(self):
"""Tests SabrePreLayoutIntegration with the rest of PassManager pipeline."""
backend = FakeAlmadenV2()
pm = generate_preset_pass_manager(1, backend, seed_transpiler=0)
pm = generate_preset_pass_manager(
0, backend, layout_method="sabre", routing_method="sabre", seed_transpiler=0
)
pm.pre_layout = PassManager([SabrePreLayout(backend.target)])
qct = pm.run(self.circuit)
qct_initial_layout = qct.layout.initial_layout
self.assertEqual(
[qct_initial_layout[q] for q in self.circuit.qubits],
[1, 6, 5, 10, 11, 12, 16, 17, 18, 13, 14, 9, 8, 3, 2, 0],
[3, 8, 7, 12, 13, 14, 18, 17, 16, 11, 10, 5, 6, 1, 2, 4],
)


Expand Down