Skip to content

Commit

Permalink
Allow wires and observable parameter when tracing
Browse files Browse the repository at this point in the history
... qml.sample for Catalyst.
  • Loading branch information
dime10 committed May 8, 2024
1 parent 6021dad commit 1b09353
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
8 changes: 6 additions & 2 deletions pennylane/measurements/measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from typing import Optional, Sequence, Tuple, Union

import pennylane as qml
from pennylane.math.utils import is_abstract
from pennylane.operation import DecompositionUndefinedError, EigvalsUndefinedError, Operator
from pennylane.pytrees import register_pytree
from pennylane.typing import TensorLike
Expand Down Expand Up @@ -162,6 +163,9 @@ def __init__(
# Cast sequence of measurement values to list
self.mv = obs if getattr(obs, "name", None) == "MeasurementValue" else list(obs)
self.obs = None
elif is_abstract(obs): # Catalyst program with qml.sample(m, wires=i)
self.mv = obs
self.obs = None

Check warning on line 168 in pennylane/measurements/measurements.py

View check run for this annotation

Codecov / codecov/patch

pennylane/measurements/measurements.py#L167-L168

Added lines #L167 - L168 were not covered by tests
else:
self.obs = obs
self.mv = None
Expand All @@ -171,7 +175,7 @@ def __init__(
if wires is not None:
if len(wires) == 0:
raise ValueError("Cannot set an empty list of wires.")
if obs is not None:
if obs is not None and not is_abstract(obs):
raise ValueError("Cannot set the wires if an observable is provided.")

# _wires = None indicates broadcasting across all available wires.
Expand Down Expand Up @@ -306,7 +310,7 @@ def wires(self):
This is the union of all the Wires objects of the measurement.
"""
if self.mv is not None:
if self.mv is not None and not is_abstract(self.mv):
if isinstance(self.mv, list):
return qml.wires.Wires.all_wires([m.wires for m in self.mv])
return self.mv.wires
Expand Down
3 changes: 2 additions & 1 deletion pennylane/measurements/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import numpy as np

import pennylane as qml
from pennylane.math.utils import is_abstract
from pennylane.operation import Operator
from pennylane.wires import Wires

Expand Down Expand Up @@ -173,7 +174,7 @@ def __init__(self, obs=None, wires=None, eigvals=None, id=None):
return

if wires is not None:
if obs is not None:
if obs is not None and not is_abstract(obs):
raise ValueError(
"Cannot specify the wires to sample if an observable is provided. The wires "
"to sample will be determined directly from the observable."
Expand Down

0 comments on commit 1b09353

Please sign in to comment.