From 28cc99e257eb077ec42aaac141bd722a458930b3 Mon Sep 17 00:00:00 2001 From: Sebastian Brandhofer <148463728+sbrandhsn@users.noreply.github.com> Date: Fri, 12 Jul 2024 14:38:12 +0200 Subject: [PATCH 1/4] up --- .../fake_provider/generic_backend_v2.py | 4 +- .../fake_provider/test_generic_backend_v2.py | 47 ++++++++++++++++++- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/qiskit/providers/fake_provider/generic_backend_v2.py b/qiskit/providers/fake_provider/generic_backend_v2.py index 0da1df7eab65..5a7e1a29aa4f 100644 --- a/qiskit/providers/fake_provider/generic_backend_v2.py +++ b/qiskit/providers/fake_provider/generic_backend_v2.py @@ -406,7 +406,9 @@ def _build_generic_target(self): noise_params = self._get_noise_defaults(name, gate.num_qubits) self._add_noisy_instruction_to_target(gate, noise_params, calibration_inst_map) else: - self._target.add_instruction(gate, properties=None, name=name) + qarg_set = self._coupling_map if gate.num_qubits > 1 else range(self.num_qubits) + props = {(qarg,) if isinstance(qarg, int) else qarg: None for qarg in qarg_set} + self._target.add_instruction(gate, properties=props, name=name) if self._control_flow: self._target.add_instruction(IfElseOp, name="if_else") diff --git a/test/python/providers/fake_provider/test_generic_backend_v2.py b/test/python/providers/fake_provider/test_generic_backend_v2.py index 33bf57cf3903..98cc0e338935 100644 --- a/test/python/providers/fake_provider/test_generic_backend_v2.py +++ b/test/python/providers/fake_provider/test_generic_backend_v2.py @@ -16,8 +16,10 @@ from qiskit import ClassicalRegister, QuantumCircuit, QuantumRegister, transpile from qiskit.providers.fake_provider import GenericBackendV2 +from qiskit.quantum_info import Operator from qiskit.transpiler import CouplingMap from qiskit.exceptions import QiskitError +from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager from test import QiskitTestCase # pylint: disable=wrong-import-order @@ -57,12 +59,53 @@ def test_calibration_no_noise_info(self): def test_no_noise(self): """Test no noise info when parameter is false""" - backend = GenericBackendV2(num_qubits=2, noise_info=False) + backend = GenericBackendV2( + num_qubits=5, coupling_map=CouplingMap.from_line(5), noise_info=False + ) + qc = QuantumCircuit(5) + qc.h(0) + qc.cx(0, 1) + qc.cx(0, 2) + qc.cx(1, 4) + qc.cx(3, 0) + qc.cx(2, 4) + qc_res = generate_preset_pass_manager(optimization_level=2, backend=backend).run(qc) + self.assertTrue(Operator.from_circuit(qc_res).equiv(qc)) + self.assertEqual(backend.target.qubit_properties, None) + + def test_no_info(self): + """Test no noise info when parameter is false""" + backend = GenericBackendV2( + num_qubits=5, + coupling_map=CouplingMap.from_line(5), + noise_info=False, + pulse_channels=False, + ) + qc = QuantumCircuit(5) + qc.h(0) + qc.cx(0, 1) + qc.cx(0, 2) + qc.cx(1, 4) + qc.cx(3, 0) + qc.cx(2, 4) + qc_res = generate_preset_pass_manager(optimization_level=2, backend=backend).run(qc) + self.assertTrue(Operator.from_circuit(qc_res).equiv(qc)) self.assertEqual(backend.target.qubit_properties, None) def test_no_pulse_channels(self): """Test no/empty pulse channels when parameter is false""" - backend = GenericBackendV2(num_qubits=2, pulse_channels=False) + backend = GenericBackendV2( + num_qubits=5, coupling_map=CouplingMap.from_line(5), pulse_channels=False + ) + qc = QuantumCircuit(5) + qc.h(0) + qc.cx(0, 1) + qc.cx(0, 2) + qc.cx(1, 4) + qc.cx(3, 0) + qc.cx(2, 4) + qc_res = generate_preset_pass_manager(optimization_level=2, backend=backend).run(qc) + self.assertTrue(Operator.from_circuit(qc_res).equiv(qc)) self.assertTrue(len(backend.channels_map) == 0) def test_operation_names(self): From d7354fd7230ddc3bc133884d6210fdf1ad45738b Mon Sep 17 00:00:00 2001 From: Sebastian Brandhofer <148463728+sbrandhsn@users.noreply.github.com> Date: Mon, 15 Jul 2024 17:54:39 +0200 Subject: [PATCH 2/4] fix --- .../providers/fake_provider/generic_backend_v2.py | 14 +++++++++++--- .../fake_provider/test_generic_backend_v2.py | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/qiskit/providers/fake_provider/generic_backend_v2.py b/qiskit/providers/fake_provider/generic_backend_v2.py index 5a7e1a29aa4f..867f1057cff9 100644 --- a/qiskit/providers/fake_provider/generic_backend_v2.py +++ b/qiskit/providers/fake_provider/generic_backend_v2.py @@ -388,6 +388,11 @@ def _build_generic_target(self): defaults = self._generate_calibration_defaults() calibration_inst_map = defaults.instruction_schedule_map + is_fully_connected = ( + self._coupling_map.graph.num_edges() + == self._coupling_map.graph.num_nodes() * (self._coupling_map.graph.num_nodes() - 1) + ) + # Iterate over gates, generate noise params from defaults, # and add instructions, noise and calibrations to target. for name in self._basis_gates: @@ -406,9 +411,12 @@ def _build_generic_target(self): noise_params = self._get_noise_defaults(name, gate.num_qubits) self._add_noisy_instruction_to_target(gate, noise_params, calibration_inst_map) else: - qarg_set = self._coupling_map if gate.num_qubits > 1 else range(self.num_qubits) - props = {(qarg,) if isinstance(qarg, int) else qarg: None for qarg in qarg_set} - self._target.add_instruction(gate, properties=props, name=name) + if is_fully_connected: + self._target.add_instruction(gate, properties=None, name=name) + else: + qarg_set = self._coupling_map if gate.num_qubits > 1 else range(self.num_qubits) + props = {(qarg,) if isinstance(qarg, int) else qarg: None for qarg in qarg_set} + self._target.add_instruction(gate, properties=props, name=name) if self._control_flow: self._target.add_instruction(IfElseOp, name="if_else") diff --git a/test/python/providers/fake_provider/test_generic_backend_v2.py b/test/python/providers/fake_provider/test_generic_backend_v2.py index 98cc0e338935..70ac50f659c9 100644 --- a/test/python/providers/fake_provider/test_generic_backend_v2.py +++ b/test/python/providers/fake_provider/test_generic_backend_v2.py @@ -73,6 +73,20 @@ def test_no_noise(self): self.assertTrue(Operator.from_circuit(qc_res).equiv(qc)) self.assertEqual(backend.target.qubit_properties, None) + def test_no_noise_fully_connected(self): + """Test no noise info when parameter is false""" + backend = GenericBackendV2(num_qubits=5, noise_info=False) + qc = QuantumCircuit(5) + qc.h(0) + qc.cx(0, 1) + qc.cx(0, 2) + qc.cx(1, 4) + qc.cx(3, 0) + qc.cx(2, 4) + qc_res = generate_preset_pass_manager(optimization_level=2, backend=backend).run(qc) + self.assertTrue(Operator.from_circuit(qc_res).equiv(qc)) + self.assertEqual(backend.target.qubit_properties, None) + def test_no_info(self): """Test no noise info when parameter is false""" backend = GenericBackendV2( From 0bb08aa371c9de8c07a4751a0fdd8be299afd94b Mon Sep 17 00:00:00 2001 From: Sebastian Brandhofer <148463728+sbrandhsn@users.noreply.github.com> Date: Fri, 26 Jul 2024 12:34:07 +0200 Subject: [PATCH 3/4] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Elena Peña Tapia <57907331+ElePT@users.noreply.github.com> --- .../providers/fake_provider/generic_backend_v2.py | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/qiskit/providers/fake_provider/generic_backend_v2.py b/qiskit/providers/fake_provider/generic_backend_v2.py index 867f1057cff9..9d2dd575f541 100644 --- a/qiskit/providers/fake_provider/generic_backend_v2.py +++ b/qiskit/providers/fake_provider/generic_backend_v2.py @@ -388,11 +388,6 @@ def _build_generic_target(self): defaults = self._generate_calibration_defaults() calibration_inst_map = defaults.instruction_schedule_map - is_fully_connected = ( - self._coupling_map.graph.num_edges() - == self._coupling_map.graph.num_nodes() * (self._coupling_map.graph.num_nodes() - 1) - ) - # Iterate over gates, generate noise params from defaults, # and add instructions, noise and calibrations to target. for name in self._basis_gates: @@ -411,12 +406,9 @@ def _build_generic_target(self): noise_params = self._get_noise_defaults(name, gate.num_qubits) self._add_noisy_instruction_to_target(gate, noise_params, calibration_inst_map) else: - if is_fully_connected: - self._target.add_instruction(gate, properties=None, name=name) - else: - qarg_set = self._coupling_map if gate.num_qubits > 1 else range(self.num_qubits) - props = {(qarg,) if isinstance(qarg, int) else qarg: None for qarg in qarg_set} - self._target.add_instruction(gate, properties=props, name=name) + qarg_set = self._coupling_map if gate.num_qubits > 1 else range(self.num_qubits) + props = {(qarg,) if isinstance(qarg, int) else qarg: None for qarg in qarg_set} + self._target.add_instruction(gate, properties=props, name=name) if self._control_flow: self._target.add_instruction(IfElseOp, name="if_else") From 88d126458ececbfb1bd2b280f3fc83e3714fef66 Mon Sep 17 00:00:00 2001 From: Sebastian Brandhofer <148463728+sbrandhsn@users.noreply.github.com> Date: Fri, 26 Jul 2024 12:36:37 +0200 Subject: [PATCH 4/4] lint --- qiskit/providers/fake_provider/generic_backend_v2.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/qiskit/providers/fake_provider/generic_backend_v2.py b/qiskit/providers/fake_provider/generic_backend_v2.py index 9d2dd575f541..5a7e1a29aa4f 100644 --- a/qiskit/providers/fake_provider/generic_backend_v2.py +++ b/qiskit/providers/fake_provider/generic_backend_v2.py @@ -406,9 +406,9 @@ def _build_generic_target(self): noise_params = self._get_noise_defaults(name, gate.num_qubits) self._add_noisy_instruction_to_target(gate, noise_params, calibration_inst_map) else: - qarg_set = self._coupling_map if gate.num_qubits > 1 else range(self.num_qubits) - props = {(qarg,) if isinstance(qarg, int) else qarg: None for qarg in qarg_set} - self._target.add_instruction(gate, properties=props, name=name) + qarg_set = self._coupling_map if gate.num_qubits > 1 else range(self.num_qubits) + props = {(qarg,) if isinstance(qarg, int) else qarg: None for qarg in qarg_set} + self._target.add_instruction(gate, properties=props, name=name) if self._control_flow: self._target.add_instruction(IfElseOp, name="if_else")