From 7a463c1490afb2679dc11451c7a6f1286fc70fcd Mon Sep 17 00:00:00 2001 From: Andre Luckow <> Date: Sat, 15 Oct 2022 12:06:12 +0200 Subject: [PATCH 1/3] Updating number execution counter correctly when using AWS Qubit Device Increment counter for num_executions for single and batch executions so that BraketQubitDevice behaves consistent with Pennylane specification in base class: --- src/braket/pennylane_plugin/braket_device.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/braket/pennylane_plugin/braket_device.py b/src/braket/pennylane_plugin/braket_device.py index 02379b1a..477879ee 100644 --- a/src/braket/pennylane_plugin/braket_device.py +++ b/src/braket/pennylane_plugin/braket_device.py @@ -219,6 +219,9 @@ def execute(self, circuit: CircuitGraph, **run_kwargs) -> np.ndarray: self.tracker.update(executions=1, shots=self.shots, **tracking_data) self.tracker.record() + # increment counter for number of executions of device + self._num_executions += 1 + return self._braket_to_pl_result(braket_result, circuit) def apply( @@ -381,6 +384,8 @@ def batch_execute(self, circuits, **run_kwargs): # Update the tracker before raising an exception further if some circuits do not complete. finally: + #super()._num_executions += + self._num_executions += len(task_batch.tasks) if self.tracker.active: for task in task_batch.tasks: tracking_data = self._tracking_data(task) @@ -389,6 +394,7 @@ def batch_execute(self, circuits, **run_kwargs): total_shots = total_executions * batch_shots self.tracker.update(batches=1, executions=total_executions, shots=total_shots) self.tracker.record() + return [ self._braket_to_pl_result(braket_result, circuit) From 0dcd9baaaf6195fef4e0e40e07464111c49b6dad Mon Sep 17 00:00:00 2001 From: Andre Luckow <> Date: Sat, 15 Oct 2022 12:32:40 +0200 Subject: [PATCH 2/3] Added Unit Test for text_number_executions --- src/braket/pennylane_plugin/braket_device.py | 1 - test/unit_tests/test_braket_device.py | 13 +++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/braket/pennylane_plugin/braket_device.py b/src/braket/pennylane_plugin/braket_device.py index 477879ee..0ef1c5c8 100644 --- a/src/braket/pennylane_plugin/braket_device.py +++ b/src/braket/pennylane_plugin/braket_device.py @@ -394,7 +394,6 @@ def batch_execute(self, circuits, **run_kwargs): total_shots = total_executions * batch_shots self.tracker.update(batches=1, executions=total_executions, shots=total_shots) self.tracker.record() - return [ self._braket_to_pl_result(braket_result, circuit) diff --git a/test/unit_tests/test_braket_device.py b/test/unit_tests/test_braket_device.py index 84508981..859de771 100644 --- a/test/unit_tests/test_braket_device.py +++ b/test/unit_tests/test_braket_device.py @@ -232,6 +232,19 @@ def test_execute(mock_run): foo="bar", ) +@patch.object(AwsDevice, "run") +def test_number_executions(mock_run): + """Asserts tracker stores information during execute when active""" + mock_run.side_effect = [TASK, SIM_TASK, SIM_TASK, TASK] + dev = _aws_device(wires=4, foo="bar") + + with QuantumTape() as circuit: + qml.Hadamard(wires=0) + qml.probs(wires=(0,)) + dev.execute(circuit) + dev.execute(circuit) + + assert dev.num_executions == 2 @patch.object(AwsDevice, "run") def test_execute_tracker(mock_run): From d255b60efc43e98d3938509b7aefac9eeb17c849 Mon Sep 17 00:00:00 2001 From: Andre Luckow <> Date: Sat, 15 Oct 2022 18:19:58 +0200 Subject: [PATCH 3/3] fixed code format --- src/braket/pennylane_plugin/braket_device.py | 1 - test/unit_tests/test_braket_device.py | 4 +++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/braket/pennylane_plugin/braket_device.py b/src/braket/pennylane_plugin/braket_device.py index 0ef1c5c8..dbe13b91 100644 --- a/src/braket/pennylane_plugin/braket_device.py +++ b/src/braket/pennylane_plugin/braket_device.py @@ -384,7 +384,6 @@ def batch_execute(self, circuits, **run_kwargs): # Update the tracker before raising an exception further if some circuits do not complete. finally: - #super()._num_executions += self._num_executions += len(task_batch.tasks) if self.tracker.active: for task in task_batch.tasks: diff --git a/test/unit_tests/test_braket_device.py b/test/unit_tests/test_braket_device.py index 859de771..ac2b12f4 100644 --- a/test/unit_tests/test_braket_device.py +++ b/test/unit_tests/test_braket_device.py @@ -232,6 +232,7 @@ def test_execute(mock_run): foo="bar", ) + @patch.object(AwsDevice, "run") def test_number_executions(mock_run): """Asserts tracker stores information during execute when active""" @@ -243,9 +244,10 @@ def test_number_executions(mock_run): qml.probs(wires=(0,)) dev.execute(circuit) dev.execute(circuit) - + assert dev.num_executions == 2 + @patch.object(AwsDevice, "run") def test_execute_tracker(mock_run): """Asserts tracker stores information during execute when active"""