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

FullAncillaAllocation for backends witout a coupling map #10240

Merged
merged 8 commits into from
Jun 9, 2023
12 changes: 8 additions & 4 deletions qiskit/transpiler/passes/layout/full_ancilla_allocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@


class FullAncillaAllocation(AnalysisPass):
"""Allocate all idle nodes from the coupling map as ancilla on the layout.
"""Allocate all idle nodes from the coupling map or target as ancilla on the layout.

A pass for allocating all idle physical qubits (those that exist in coupling
map but not the dag circuit) as ancilla. It will also choose new virtual
qubits to correspond to those physical ancilla.
map or target but not the dag circuit) as ancilla. It will also choose new
virtual qubits to correspond to those physical ancilla.

Note:
This is an analysis pass, and only responsible for choosing physical
Expand Down Expand Up @@ -81,7 +81,11 @@ def run(self, dag):

idle_physical_qubits = [q for q in layout_physical_qubits if q not in physical_bits]

if self.coupling_map:
if self.target:
idle_physical_qubits = [
q for q in range(self.target.num_qubits) if q not in physical_bits
]
elif self.coupling_map:
idle_physical_qubits = [
q for q in self.coupling_map.physical_qubits if q not in physical_bits
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
fixes:
- |
Fixed an issue with :class:`~.FullAncillaAllocation` so it can now function with :class:`~.Target` objects that do not have a
coupling map (typically because there are no 2 qubit gates in the :class:`~.Target`). In this case :class:`~.FullAncillaAllocation` will add
ancilla qubits so that the number of qubits in the :class:`~.DAGCircuit` matches the number
:attr:`.Target.num_qubits`.
22 changes: 22 additions & 0 deletions test/python/transpiler/test_full_ancilla_allocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,28 @@ def test_bad_layout(self):
cm.exception.message,
)

def test_target_without_cmap(self):
"""Test that FullAncillaAllocation works when the target does not have a coupling map.

This situation occurs at the early stages of backend bring-up.
"""
target_data = {"basis_gates": ["h"], "num_qubits": 3}
target = Target.from_configuration(**target_data)

circ = QuantumCircuit(1)
circ.h(0)

pass_ = FullAncillaAllocation(target)
pass_.property_set["layout"] = Layout.from_intlist([0], *circ.qregs)

# Pre pass check
self.assertEqual(len(pass_.property_set["layout"]), 1)

pass_.run(circuit_to_dag(circ))

# Post pass check
self.assertEqual(len(pass_.property_set["layout"]), target.num_qubits)


if __name__ == "__main__":
unittest.main()