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

BackendEstimator support BackendV2 without coupling_map #10956

Merged
35 changes: 30 additions & 5 deletions qiskit/primitives/backend_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@
from qiskit.quantum_info import Pauli, PauliList
from qiskit.quantum_info.operators.base_operator import BaseOperator
from qiskit.result import Counts, Result
from qiskit.transpiler import PassManager
from qiskit.transpiler import CouplingMap, PassManager
from qiskit.transpiler.passes import (
ApplyLayout,
EnlargeWithAncilla,
FullAncillaAllocation,
Optimize1qGatesDecomposition,
SetLayout,
)

from .base import BaseEstimator, EstimatorResult
from .primitive_job import PrimitiveJob
Expand Down Expand Up @@ -209,9 +216,27 @@ def _transpile(self):
perm_pattern = list(range(transpiled_circuit.num_qubits))

# 2. transpile diff circuits
transpile_opts = copy.copy(self.transpile_options)
transpile_opts.update_options(initial_layout=perm_pattern)
diff_circuits = transpile(diff_circuits, self.backend, **transpile_opts.__dict__)
passmanager = PassManager([SetLayout(perm_pattern)])
t-imamichi marked this conversation as resolved.
Show resolved Hide resolved
if isinstance(self.backend, BackendV2):
opt1q = Optimize1qGatesDecomposition(target=self.backend.target)
else:
opt1q = Optimize1qGatesDecomposition(basis=self.backend.configuration().basis_gates)
passmanager.append(opt1q)
if isinstance(self.backend, BackendV2) and isinstance(
self.backend.coupling_map, CouplingMap
):
coupling_map = self.backend.coupling_map
passmanager.append(FullAncillaAllocation(coupling_map))
passmanager.append(EnlargeWithAncilla())
elif (
isinstance(self.backend, BackendV1)
and self.backend.configuration().coupling_map is not None
):
coupling_map = CouplingMap(self.backend.configuration().coupling_map)
passmanager.append(FullAncillaAllocation(coupling_map))
passmanager.append(EnlargeWithAncilla())
passmanager.append(ApplyLayout())
diff_circuits = passmanager.run(diff_circuits)
# 3. combine
transpiled_circuits = []
for diff_circuit in diff_circuits:
Expand Down Expand Up @@ -333,7 +358,7 @@ def _preprocessing(self) -> list[tuple[QuantumCircuit, list[QuantumCircuit]]]:
}
diff_circuits.append(meas_circuit)
else:
for basis, obs in zip(observable.paulis, observable): # type: ignore
for basis, obs in zip(observable.paulis, observable):
meas_circuit, indices = self._measurement_circuit(circuit.num_qubits, basis)
paulis = PauliList.from_symplectic(
obs.paulis.z[:, indices],
Expand Down