Repository: PennyLaneAI/pennylane
Pull request #4649 (2023-10-04 20:13:54)

Include results on tracker for default.qubit
DQL included results in the tracker, DQ2 currently does not. Results for each call of `execute` added here.

github-actions[bot]: Hello. You may have forgotten to update the changelog!
Please edit [`doc/releases/changelog-dev.md`](/PennyLaneAI/pennylane/blob/master/doc/releases/changelog-dev.md) with:
* A one-to-two sentence description of the change. You may include a small working example for new features.
* A link back to this PR.
* Your name (or GitHub username) in the contributors section.
lillian542: From the perspective of a user this just makes `default.qubit` keeps doing a thing that it already did already, so I didn't add a changelog entry for this one
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/4649?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) Report
All modified lines are covered by tests :white_check_mark:
> Comparison is base [(`da79370`)](https://app.codecov.io/gh/PennyLaneAI/pennylane/commit/da79370db01834671bcd4d1edce408131f920ab2?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) 99.63% compared to head [(`9237cf0`)](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/4649?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) 99.63%.

<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##           master    #4649   +/-   ##
=======================================
  Coverage   99.63%   99.63%           
=======================================
  Files         376      376           
  Lines       33666    33666           
=======================================
  Hits        33542    33542           
  Misses        124      124           
```


| [Files](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/4649?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) | Coverage Δ | |
|---|---|---|
| [pennylane/devices/default\_qubit.py](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/4649?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI#diff-cGVubnlsYW5lL2RldmljZXMvZGVmYXVsdF9xdWJpdC5weQ==) | `100.00% <100.00%> (ø)` | |


</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/4649?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI).

lillian542: [sc-46914]

Patch:
diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md
index 5e7b4c5c7..a213c0216 100644
--- a/doc/releases/changelog-dev.md
+++ b/doc/releases/changelog-dev.md
@@ -80,8 +80,10 @@
 
 * `default.qubit` now tracks the number of equivalent qpu executions and total shots
   when the device is sampling. Note that `"simulations"` denotes the number of simulation passes, where as
-  `"executions"` denotes how many different computational bases need to be sampled in.
+  `"executions"` denotes how many different computational bases need to be sampled in. Additionally, the
+  new `default.qubit` also tracks the results of `device.execute`.
   [(#4628)](https://github.com/PennyLaneAI/pennylane/pull/4628)
+  [(#4649)](https://github.com/PennyLaneAI/pennylane/pull/4649)
 
 * The `JacobianProductCalculator` abstract base class and implementation `TransformJacobianProducts`
   have been added to `pennylane.interfaces.jacobian_products`.
diff --git a/pennylane/devices/default_qubit.py b/pennylane/devices/default_qubit.py
index 487a920f3..13f1c3d6d 100644
--- a/pennylane/devices/default_qubit.py
+++ b/pennylane/devices/default_qubit.py
@@ -129,6 +129,7 @@ class DefaultQubit(Device):
         * ``resources``: the :class:`~.resource.Resources` for the executed circuit.
         * ``simulations``: the number of simulations performed. One simulation can cover multiple QPU executions, such as for non-commuting measurements and batched parameters.
         * ``batches``: The number of times :meth:`~.execute` is called.
+        * ``results``: The results of each call of :meth:`~.execute`
         * ``derivative_batches``: How many times :meth:`~.compute_derivatives` is called.
         * ``execute_and_derivative_batches``: How many times :meth:`~.execute_and_compute_derivatives` is called
         * ``vjp_batches``: How many times :meth:`~.compute_vjp` is called
@@ -298,24 +299,6 @@ class DefaultQubit(Device):
             is_single_circuit = True
             circuits = [circuits]
 
-        if self.tracker.active:
-            self.tracker.update(batches=1)
-            self.tracker.record()
-            for c in circuits:
-                qpu_executions, shots = get_num_shots_and_executions(c)
-                if c.shots:
-                    self.tracker.update(
-                        simulations=1,
-                        executions=qpu_executions,
-                        shots=shots,
-                        resources=c.specs["resources"],
-                    )
-                else:
-                    self.tracker.update(
-                        simulations=1, executions=qpu_executions, resources=c.specs["resources"]
-                    )
-                self.tracker.record()
-
         max_workers = execution_config.device_options.get("max_workers", self._max_workers)
         interface = (
             execution_config.interface
@@ -349,6 +332,29 @@ class DefaultQubit(Device):
             # reset _rng to mimic serial behavior
             self._rng = np.random.default_rng(self._rng.integers(2**31 - 1))
 
+        if self.tracker.active:
+            self.tracker.update(batches=1)
+            self.tracker.record()
+            for i, c in enumerate(circuits):
+                qpu_executions, shots = get_num_shots_and_executions(c)
+                res = np.array(results[i]) if isinstance(results[i], Number) else results[i]
+                if c.shots:
+                    self.tracker.update(
+                        simulations=1,
+                        executions=qpu_executions,
+                        results=res,
+                        shots=shots,
+                        resources=c.specs["resources"],
+                    )
+                else:
+                    self.tracker.update(
+                        simulations=1,
+                        executions=qpu_executions,
+                        results=res,
+                        resources=c.specs["resources"],
+                    )
+                self.tracker.record()
+
         return results[0] if is_single_circuit else results
 
     def compute_derivatives(


Test patch:
diff --git a/tests/devices/test_default_qubit_tracking.py b/tests/devices/test_default_qubit_tracking.py
index 93c0981f9..16e35f09a 100644
--- a/tests/devices/test_default_qubit_tracking.py
+++ b/tests/devices/test_default_qubit_tracking.py
@@ -55,6 +55,7 @@ class TestTracking:
             "batches": [1, 1],
             "executions": [1, 1, 1],
             "simulations": [1, 1, 1],
+            "results": [1.0, 1.0, 1.0],
             "resources": [Resources(num_wires=1), Resources(num_wires=1), Resources(num_wires=1)],
             "derivative_batches": [1],
             "derivatives": [1],
@@ -69,6 +70,7 @@ class TestTracking:
         assert tracker.latest == {
             "executions": 1,
             "simulations": 1,
+            "results": 1,
             "resources": Resources(num_wires=1),
         }