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

Only run translation stage in optimization loop if necessary #6940

Merged
merged 44 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
195e4e1
Add GatesInBasis analysis pass
mtreinish Jul 29, 2021
904d81e
added main
irajput Aug 23, 2021
b951b71
adding to runningpass and level3
irajput Aug 23, 2021
d9b6132
formatted
irajput Aug 24, 2021
92f9606
rename
irajput Aug 26, 2021
b9abc24
minor format
irajput Aug 26, 2021
a205725
edit
irajput Aug 27, 2021
eeb5b78
fixed make errors with gates_in_basis
irajput Aug 27, 2021
3f2b815
adding
irajput Aug 27, 2021
17eb708
added reno
irajput Aug 27, 2021
593b6d0
changes
irajput Aug 30, 2021
f339588
adding other br
irajput Aug 30, 2021
2dd7321
added suggested changes
irajput Aug 30, 2021
87cb46d
format
irajput Aug 30, 2021
0d7e226
pulled changes
irajput Aug 30, 2021
f8fdb06
Update qiskit/transpiler/passes/utils/gates_basis.py
irajput Aug 30, 2021
b65e9ed
Update qiskit/transpiler/runningpassmanager.py
irajput Aug 30, 2021
e461aa8
fixed raise
irajput Aug 30, 2021
3c596fe
added fix
irajput Aug 30, 2021
97b69cc
updates
irajput Aug 30, 2021
b0c4d7b
added tests
irajput Aug 31, 2021
0446793
added m
irajput Aug 31, 2021
914429f
add main
irajput Aug 31, 2021
5c18599
added tests for nested do_while
irajput Sep 1, 2021
c42ef92
added do_while
irajput Sep 1, 2021
328a3a1
added test
irajput Sep 10, 2021
63d4c71
Merge branch 'main' of https://github.com/Qiskit/qiskit-terra into fi…
jlapeyre Dec 1, 2021
36d23a6
Remove some tests that are added in PR #6962
jlapeyre Dec 1, 2021
a91e23e
Remove newline added accidentally
jlapeyre Dec 1, 2021
b4ded60
Add test for running _unroll if not GatesInBasis in level3
jlapeyre Dec 2, 2021
c791322
Black reformat
jlapeyre Dec 2, 2021
b8ddc0b
Fix pylint complaints
jlapeyre Dec 6, 2021
c4b9d8f
Merge branch 'main' into fix6677-2-try-merge
jlapeyre Dec 6, 2021
422e091
Merge branch 'main' into fix6677-2
jlapeyre Jan 18, 2022
68a6521
Format file with black
jlapeyre Jan 18, 2022
0e5478f
Build one list rather than concatenating the same two lists repeatedly
jlapeyre Jan 19, 2022
1f0104a
Merge remote-tracking branch 'origin/main' into fix6677-2
mtreinish Aug 30, 2022
646ff82
Update optimization level 1 and 2 too
mtreinish Aug 30, 2022
451b399
Update gates in basis to return True if no constraints are present
mtreinish Aug 30, 2022
92bb65e
Merge branch 'main' into fix6677-2
mtreinish Aug 30, 2022
bfc4b3a
Return early to avoid unnecessary nesting
jakelishman Sep 2, 2022
9ce384f
Merge remote-tracking branch 'ibm/main' into fix6677-2
jakelishman Sep 2, 2022
6d61551
Merge branch 'main' into fix6677-2
mergify[bot] Sep 2, 2022
7b52dfd
Dummy commit to retrigger CI
jakelishman Sep 2, 2022
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
2 changes: 2 additions & 0 deletions qiskit/transpiler/passes/utils/gates_basis.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def __init__(self, basis_gates):
basis_gates (list): The list of strings representing the set of basis gates.
"""
super().__init__()
if basis_gates is None:
basis_gates = []
self._basis_gates = set(basis_gates)

def run(self, dag):
Expand Down
21 changes: 18 additions & 3 deletions qiskit/transpiler/preset_passmanagers/level3.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
from qiskit.transpiler.passes import PulseGates
from qiskit.transpiler.passes import Error
from qiskit.transpiler.passes import ContainsInstruction
from qiskit.transpiler.passes import GatesInBasis
from qiskit.transpiler.runningpassmanager import ConditionalController

from qiskit.transpiler import TranspilerError

Expand Down Expand Up @@ -322,6 +324,16 @@ def _contains_delay(property_set):
else:
_alignments = []

# Build nested Flow controllers

# Check if the gates are in the basis to determine whether to run _unroll passes
_unroll_check = [GatesInBasis(basis_gates)]

def _unroll_condition(property_set):
return not property_set["all_gates_in_basis"]

_flow_unroll = [ConditionalController(_unroll, condition=_unroll_condition)]
jlapeyre marked this conversation as resolved.
Show resolved Hide resolved

# Build pass manager
pm3 = PassManager()
pm3.append(_unroll3q)
Expand All @@ -346,12 +358,15 @@ def _contains_delay(property_set):
# inserted by UnitarySynthesis which is direction aware but only via
# the coupling map which with a target doesn't give a full picture
if target is not None:
pm3.append(_depth_check + _opt + _unroll + _direction, do_while=_opt_control)
pm3.append(
_depth_check + _opt + _unroll_check + _flow_unroll + _direction,
do_while=_opt_control,
)
else:
pm3.append(_depth_check + _opt + _unroll, do_while=_opt_control)
pm3.append(_depth_check + _opt + _unroll_check + _flow_unroll, do_while=_opt_control)
else:
pm3.append(_reset)
pm3.append(_depth_check + _opt + _unroll, do_while=_opt_control)
pm3.append(_depth_check + _opt + _unroll_check + _flow_unroll, do_while=_opt_control)
if inst_map and inst_map.has_custom_gate():
pm3.append(PulseGates(inst_map=inst_map))
if scheduling_method:
Expand Down
29 changes: 28 additions & 1 deletion test/python/transpiler/test_preset_passmanagers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
from qiskit.circuit import Qubit
from qiskit.compiler import transpile, assemble
from qiskit.transpiler import CouplingMap, Layout
from qiskit.circuit.library import U2Gate, U3Gate
from qiskit.transpiler.passes import GatesInBasis, Collect2qBlocks
from qiskit.circuit.library import U2Gate, U3Gate, QuantumVolume
from qiskit.test import QiskitTestCase
from qiskit.test.mock import (
FakeBelem,
FakeTenerife,
FakeMelbourne,
FakeJohannesburg,
Expand Down Expand Up @@ -202,6 +204,31 @@ def test_alignment_constraints_called_with_delay_in_circuit(self, level):
transpile(circuit, backend=FakeJohannesburg(), optimization_level=level)
mock.assert_called_once()

def test_unroll_only_if_not_gates_in_basis(self):
"""Test that the list of passes _unroll only runs if a gate is not in the basis."""
qcomp = FakeBelem()
qv_circuit = QuantumVolume(3)
gates_in_basis_true_count = 0
collect_2q_blocks_count = 0

# pylint: disable=unused-argument
def counting_callback_func(pass_, dag, time, property_set, count):
nonlocal gates_in_basis_true_count
nonlocal collect_2q_blocks_count
Comment on lines +221 to +222
Copy link
Member

Choose a reason for hiding this comment

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

It's not everyday there's a ~valid usage for nonlocal.

if isinstance(pass_, GatesInBasis) and property_set["all_gates_in_basis"]:
gates_in_basis_true_count += 1
if isinstance(pass_, Collect2qBlocks):
collect_2q_blocks_count += 1

transpile(
qv_circuit,
backend=qcomp,
optimization_level=3,
callback=counting_callback_func,
translation_method="synthesis",
)
self.assertEqual(gates_in_basis_true_count + 1, collect_2q_blocks_count)


@ddt
class TestTranspileLevels(QiskitTestCase):
Expand Down