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

Include results on tracker for default.qubit #4649

Merged
merged 7 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
42 changes: 24 additions & 18 deletions pennylane/devices/default_qubit.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ def f(x):
* ``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
Expand Down Expand Up @@ -298,24 +299,6 @@ def execute(
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
Expand Down Expand Up @@ -349,6 +332,29 @@ def execute(
# 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(
Expand Down
2 changes: 2 additions & 0 deletions tests/devices/test_default_qubit_tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def test_tracking_batch(self):
"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],
Expand All @@ -69,6 +70,7 @@ def test_tracking_batch(self):
assert tracker.latest == {
"executions": 1,
"simulations": 1,
"results": 1,
"resources": Resources(num_wires=1),
}

Expand Down
Loading