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

Fix bugs with VF2Layout pass and Qiskit Aer 0.13 (backport #11585) #12537

Merged
merged 1 commit into from
Jun 10, 2024
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
14 changes: 10 additions & 4 deletions qiskit/transpiler/passes/layout/vf2_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,21 @@ def __init__(
limit on the number of trials will be set.
target (Target): A target representing the backend device to run ``VF2Layout`` on.
If specified it will supersede a set value for ``properties`` and
``coupling_map``.
``coupling_map`` if the :class:`.Target` contains connectivity constraints. If the value
of ``target`` models an ideal backend without any constraints then the value of
``coupling_map``
will be used.

Raises:
TypeError: At runtime, if neither ``coupling_map`` or ``target`` are provided.
"""
super().__init__()
self.target = target
if target is not None:
self.coupling_map = self.target.build_coupling_map()
if (
target is not None
and (target_coupling_map := self.target.build_coupling_map()) is not None
):
self.coupling_map = target_coupling_map
else:
self.coupling_map = coupling_map
self.properties = properties
Expand Down Expand Up @@ -145,7 +151,7 @@ def run(self, dag):
)
# Filter qubits without any supported operations. If they don't support any operations
# They're not valid for layout selection
if self.target is not None:
if self.target is not None and self.target.qargs is not None:
has_operations = set(itertools.chain.from_iterable(self.target.qargs))
to_remove = set(range(len(cm_nodes))).difference(has_operations)
if to_remove:
Expand Down
4 changes: 2 additions & 2 deletions qiskit/transpiler/passes/layout/vf2_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def score_layout(
def build_average_error_map(target, properties, coupling_map):
"""Build an average error map used for scoring layouts pre-basis translation."""
num_qubits = 0
if target is not None:
if target is not None and target.qargs is not None:
num_qubits = target.num_qubits
avg_map = ErrorMap(len(target.qargs))
elif coupling_map is not None:
Expand All @@ -157,7 +157,7 @@ def build_average_error_map(target, properties, coupling_map):
# object
avg_map = ErrorMap(0)
built = False
if target is not None:
if target is not None and target.qargs is not None:
for qargs in target.qargs:
if qargs is None:
continue
Expand Down
4 changes: 4 additions & 0 deletions releasenotes/notes/fix-vf2-aer-a7306ce07ea81700.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fixes:
- |
The :class:`.VF2Layout` pass would raise an exception when provided with a :class:`.Target` instance without connectivity constraints.
This would be the case with targets from Aer 0.13. The issue is now fixed.
20 changes: 20 additions & 0 deletions test/python/transpiler/test_vf2_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,26 @@ def test_3_q_gate(self):
pass_1.property_set["VF2Layout_stop_reason"], VF2LayoutStopReason.MORE_THAN_2Q
)

def test_target_without_coupling_map(self):
"""When a target has no coupling_map but it is provided as argument.
See: https://github.com/Qiskit/qiskit/pull/11585"""

circuit = QuantumCircuit(3)
circuit.cx(0, 1)
dag = circuit_to_dag(circuit)

target = Target(num_qubits=3)
target.add_instruction(CXGate())

vf2_pass = VF2Layout(
coupling_map=CouplingMap([[0, 2], [1, 2]]), target=target, seed=42, max_trials=1
)
vf2_pass.run(dag)

self.assertEqual(
vf2_pass.property_set["VF2Layout_stop_reason"], VF2LayoutStopReason.SOLUTION_FOUND
)


class TestMultipleTrials(QiskitTestCase):
"""Test the passes behavior with >1 trial."""
Expand Down
Loading