Skip to content

Commit

Permalink
GenericBackendV2 should fail when the backend cannot allocate the bas…
Browse files Browse the repository at this point in the history
…is gate because its size (#12653)

* GenericBackendV2 should fail when the backend cannot allocate the basis gate because its size

Co-authored-by: Elena Peña Tapia <[email protected]>

* reno

* Update releasenotes/notes/fixes_GenericBackendV2-668e40596e1f070d.yaml

Co-authored-by: Elena Peña Tapia <[email protected]>

* another single qubit backend

---------

Co-authored-by: Elena Peña Tapia <[email protected]>
(cherry picked from commit e36027c)

# Conflicts:
#	qiskit/providers/fake_provider/generic_backend_v2.py
#	test/visual/mpl/graph/test_graph_matplotlib_drawer.py
  • Loading branch information
1ucian0 authored and mergify[bot] committed Jun 26, 2024
1 parent 1f1b89f commit 58d5154
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions qiskit/providers/fake_provider/generic_backend_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,16 @@ def _build_generic_target(self):
f"in the standard qiskit circuit library."
)
gate = self._supported_gates[name]
<<<<<<< HEAD
noise_params = self._get_noise_defaults(name)
=======
if self.num_qubits < gate.num_qubits:
raise QiskitError(
f"Provided basis gate {name} needs more qubits than {self.num_qubits}, "
f"which is the size of the backend."
)
noise_params = self._get_noise_defaults(name, gate.num_qubits)
>>>>>>> e36027c01 (GenericBackendV2 should fail when the backend cannot allocate the basis gate because its size (#12653))
self._add_noisy_instruction_to_target(gate, noise_params, calibration_inst_map)

if self._control_flow:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
The constructor :class:`.GenericBackendV2` was allowing to create malformed backends because it accepted basis gates that couldn't be allocated in the backend size . That is, a backend with a single qubit should not accept a basis with two-qubit gates.
10 changes: 10 additions & 0 deletions test/python/providers/fake_provider/test_generic_backend_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ def test_supported_basis_gates(self):
with self.assertRaises(QiskitError):
GenericBackendV2(num_qubits=8, basis_gates=["cx", "id", "rz", "sx", "zz"])

def test_cx_1Q(self):
"""Test failing with a backend with single qubit but with a two-qubit basis gate"""
with self.assertRaises(QiskitError):
GenericBackendV2(num_qubits=1, basis_gates=["cx", "id"])

def test_ccx_2Q(self):
"""Test failing with a backend with two qubits but with a three-qubit basis gate"""
with self.assertRaises(QiskitError):
GenericBackendV2(num_qubits=2, basis_gates=["ccx", "id"])

def test_operation_names(self):
"""Test that target basis gates include "delay", "measure" and "reset" even
if not provided by user."""
Expand Down
4 changes: 4 additions & 0 deletions test/visual/mpl/graph/test_graph_matplotlib_drawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,11 @@ def test_plot_1_qubit_gate_map(self):
"""Test plot_gate_map using 1 qubit backend"""
# getting the mock backend from FakeProvider

<<<<<<< HEAD
backend = FakeArmonk()
=======
backend = GenericBackendV2(num_qubits=1, basis_gates=["id", "rz", "sx", "x"])
>>>>>>> e36027c01 (GenericBackendV2 should fail when the backend cannot allocate the basis gate because its size (#12653))

fname = "1_qubit_gate_map.png"
self.graph_plot_gate_map(backend=backend, filename=fname)
Expand Down

0 comments on commit 58d5154

Please sign in to comment.