Skip to content

Commit

Permalink
remove do_queue everywhere (#4317)
Browse files Browse the repository at this point in the history
* remove do_queue everywhere

* do not queue adjoint tape

* override adjoint in QuantumTape so it queues

* fix bad merge conflict
  • Loading branch information
timmysilv committed Jul 5, 2023
1 parent 7bc6255 commit 49d325f
Show file tree
Hide file tree
Showing 90 changed files with 348 additions and 1,005 deletions.
9 changes: 2 additions & 7 deletions doc/development/adding_operators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,7 @@ knows a native implementation for ``FlipAndRotate``). It also defines an adjoint
# we request parameter-shift (or "analytic") differentiation.
grad_method = "A"
def __init__(self, angle, wire_rot, wire_flip=None, do_flip=False,
do_queue=None, id=None):
def __init__(self, angle, wire_rot, wire_flip=None, do_flip=False, id=None):
# checking the inputs --------------
Expand Down Expand Up @@ -177,11 +176,7 @@ knows a native implementation for ``FlipAndRotate``). It also defines an adjoint
# The parent class expects all trainable parameters to be fed as positional
# arguments, and all wires acted on fed as a keyword argument.
# The id keyword argument allows users to give their instance a custom name.
# The do_queue keyword argument specifies whether or not
# the operator is queued when created in a tape context.
# Note that do_queue is deprecated. In the future, please use
# qml.QueuingManager.stop_recording().
super().__init__(angle, wires=all_wires, do_queue=do_queue, id=id)
super().__init__(angle, wires=all_wires, id=id)
@property
def num_params(self):
Expand Down
14 changes: 7 additions & 7 deletions doc/development/deprecations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ Deprecations
Pending deprecations
--------------------

* The ``do_queue`` keyword argument in ``qml.operation.Operator`` is deprecated. This affects
all child classes, such as ``Operation``, ``Observable``, ``SymbolicOp`` and more. Instead of
setting ``do_queue=False``, use the ``qml.QueuingManager.stop_recording()`` context.

- Deprecated in v0.31
- Will be removed in v0.32

* ``qml.math.purity``, ``qml.math.vn_entropy``, ``qml.math.mutual_info``, ``qml.math.fidelity``,
``qml.math.relative_entropy``, and ``qml.math.max_entropy`` no longer support state vectors as
input. Please call ``qml.math.dm_from_state_vector`` on the input before passing to any of these functions.
Expand Down Expand Up @@ -66,6 +59,13 @@ Pending deprecations
Completed deprecation cycles
----------------------------

* The ``do_queue`` keyword argument in ``qml.operation.Operator`` has been removed. This affects
all child classes, such as ``Operation``, ``Observable``, ``SymbolicOp`` and more. Instead of
setting ``do_queue=False``, use the ``qml.QueuingManager.stop_recording()`` context.

- Deprecated in v0.31
- Removed in v0.32

* The ``qml.specs`` dictionary longer supports direct key access to certain keys. Instead
these quantities can be accessed as fields of the new ``Resources`` object saved under
``specs_dict["resources"]``:
Expand Down
4 changes: 4 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

<h3>Breaking changes 💔</h3>

* The `do_queue` keyword argument in `qml.operation.Operator` has been removed. Instead of
setting `do_queue=False`, use the `qml.QueuingManager.stop_recording()` context.
[(#4317)](https://github.com/PennyLaneAI/pennylane/pull/4317)

* The `grouping_type` and `grouping_method` keyword arguments are removed from `qchem.molecular_hamiltonian`.

* `zyz_decomposition` and `xyx_decomposition` are removed. Use `one_qubit_decomposition` instead.
Expand Down
43 changes: 6 additions & 37 deletions pennylane/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,9 +420,6 @@ class Operator(abc.ABC):
*params (tuple[tensor_like]): trainable parameters
wires (Iterable[Any] or Any): Wire label(s) that the operator acts on.
If not given, args[-1] is interpreted as wires.
do_queue (bool): indicates whether the operator should be recorded when created in
a tape context. This argument is deprecated, instead of setting it to ``False``
use :meth:`~.queuing.QueuingManager.stop_recording`.
id (str): custom label given to an operator instance,
can be useful for some applications where the instance has to be identified
Expand Down Expand Up @@ -451,8 +448,7 @@ class FlipAndRotate(qml.operation.Operation):
# we request parameter-shift (or "analytic") differentiation.
grad_method = "A"
def __init__(self, angle, wire_rot, wire_flip=None, do_flip=False,
do_queue=None, id=None):
def __init__(self, angle, wire_rot, wire_flip=None, do_flip=False, id=None):
# checking the inputs --------------
Expand All @@ -474,11 +470,7 @@ def __init__(self, angle, wire_rot, wire_flip=None, do_flip=False,
# The parent class expects all trainable parameters to be fed as positional
# arguments, and all wires acted on fed as a keyword argument.
# The id keyword argument allows users to give their instance a custom name.
# The do_queue keyword argument specifies whether or not
# the operator is queued when created in a tape context.
# Note that do_queue is deprecated. In the future, please use
# qml.QueuingManager.stop_recording().
super().__init__(angle, wires=all_wires, do_queue=do_queue, id=id)
super().__init__(angle, wires=all_wires, id=id)
@property
def num_params(self):
Expand Down Expand Up @@ -973,7 +965,7 @@ def _format(x):
param_string = ",\n".join(_format(p) for p in params)
return f"{op_label}\n({param_string})"

def __init__(self, *params, wires=None, do_queue=None, id=None):
def __init__(self, *params, wires=None, id=None):
# pylint: disable=too-many-branches
self._name = self.__class__.__name__ #: str: name of the operator
self._id = id
Expand Down Expand Up @@ -1026,15 +1018,7 @@ def __init__(self, *params, wires=None, do_queue=None, id=None):

self.data = tuple(np.array(p) if isinstance(p, (list, tuple)) else p for p in params)

if do_queue is not None:
do_queue_deprecation_warning = (
"The do_queue keyword argument is deprecated. Instead of setting "
"it to False, use qml.queuing.QueuingManager.stop_recording()"
)
warnings.warn(do_queue_deprecation_warning, UserWarning)

if do_queue or do_queue is None:
self.queue()
self.queue()

def _check_batching(self, params):
"""Check if the expected numbers of dimensions of parameters coincides with the
Expand Down Expand Up @@ -1526,9 +1510,6 @@ class Operation(Operator):
params (tuple[tensor_like]): trainable parameters
wires (Iterable[Any] or Any): Wire label(s) that the operator acts on.
If not given, args[-1] is interpreted as wires.
do_queue (bool): indicates whether the operator should be
recorded when created in a tape context. This argument is deprecated,
instead of setting it to ``False`` use :meth:`~.queuing.QueuingManager.stop_recording`.
id (str): custom label given to an operator instance,
can be useful for some applications where the instance has to be identified
"""
Expand Down Expand Up @@ -1659,8 +1640,8 @@ def parameter_frequencies(self):
"and parameter frequencies can not be computed as no generator is defined."
)

def __init__(self, *params, wires=None, do_queue=None, id=None):
super().__init__(*params, wires=wires, do_queue=do_queue, id=id)
def __init__(self, *params, wires=None, id=None):
super().__init__(*params, wires=wires, id=id)

# check the grad_recipe validity
if self.grad_recipe is None:
Expand All @@ -1678,9 +1659,6 @@ class Channel(Operation, abc.ABC):
params (tuple[tensor_like]): trainable parameters
wires (Iterable[Any] or Any): Wire label(s) that the operator acts on.
If not given, args[-1] is interpreted as wires.
do_queue (bool): indicates whether the operator should be
recorded when created in a tape context. This argument is deprecated,
instead of setting it to ``False`` use :meth:`~.queuing.QueuingManager.stop_recording`.
id (str): custom label given to an operator instance,
can be useful for some applications where the instance has to be identified
"""
Expand Down Expand Up @@ -1750,9 +1728,6 @@ class Observable(Operator):
params (tuple[tensor_like]): trainable parameters
wires (Iterable[Any] or Any): Wire label(s) that the operator acts on.
If not given, args[-1] is interpreted as wires.
do_queue (bool): indicates whether the operator should be
recorded when created in a tape context. This argument is deprecated,
instead of setting it to ``False`` use :meth:`~.queuing.QueuingManager.stop_recording`.
id (str): custom label given to an operator instance,
can be useful for some applications where the instance has to be identified
"""
Expand Down Expand Up @@ -2560,9 +2535,6 @@ class CVOperation(CV, Operation):
params (tuple[tensor_like]): trainable parameters
wires (Iterable[Any] or Any): Wire label(s) that the operator acts on.
If not given, args[-1] is interpreted as wires.
do_queue (bool): indicates whether the operator should be
recorded when created in a tape context. This argument is deprecated,
instead of setting it to ``False`` use :meth:`~.queuing.QueuingManager.stop_recording`.
id (str): custom label given to an operator instance,
can be useful for some applications where the instance has to be identified
"""
Expand Down Expand Up @@ -2686,9 +2658,6 @@ class CVObservable(CV, Observable):
params (tuple[tensor_like]): trainable parameters
wires (Iterable[Any] or Any): Wire label(s) that the operator acts on.
If not given, args[-1] is interpreted as wires.
do_queue (bool): indicates whether the operator should be
recorded when created in a tape context. This argument is deprecated,
instead of setting it to ``False`` use :meth:`~.queuing.QueuingManager.stop_recording`.
id (str): custom label given to an operator instance,
can be useful for some applications where the instance has to be identified
"""
Expand Down
74 changes: 20 additions & 54 deletions pennylane/ops/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,14 @@ class AmplitudeDamping(Channel):
Args:
gamma (float): amplitude damping probability
wires (Sequence[int] or int): the wire the channel acts on
do_queue (bool): Indicates whether the operator should be
immediately pushed into the Operator queue (optional)
id (str or None): String representing the operation (optional)
"""
num_params = 1
num_wires = 1
grad_method = "F"

def __init__(self, gamma, wires, do_queue=None, id=None):
super().__init__(gamma, wires=wires, do_queue=do_queue, id=id)
def __init__(self, gamma, wires, id=None):
super().__init__(gamma, wires=wires, id=id)

@staticmethod
def compute_kraus_matrices(gamma): # pylint:disable=arguments-differ
Expand Down Expand Up @@ -131,18 +129,14 @@ class GeneralizedAmplitudeDamping(Channel):
gamma (float): amplitude damping probability
p (float): excitation probability
wires (Sequence[int] or int): the wire the channel acts on
do_queue (bool): Indicates whether the operator should be
immediately pushed into the Operator queue (optional).
This argument is deprecated, instead of setting it to ``False``
use :meth:`~.queuing.QueuingManager.stop_recording`.
id (str or None): String representing the operation (optional)
"""
num_params = 2
num_wires = 1
grad_method = "F"

def __init__(self, gamma, p, wires, do_queue=None, id=None):
super().__init__(gamma, p, wires=wires, do_queue=do_queue, id=id)
def __init__(self, gamma, p, wires, id=None):
super().__init__(gamma, p, wires=wires, id=id)

@staticmethod
def compute_kraus_matrices(gamma, p): # pylint:disable=arguments-differ
Expand Down Expand Up @@ -219,8 +213,8 @@ class PhaseDamping(Channel):
num_wires = 1
grad_method = "F"

def __init__(self, gamma, wires, do_queue=None, id=None):
super().__init__(gamma, wires=wires, do_queue=do_queue, id=id)
def __init__(self, gamma, wires, id=None):
super().__init__(gamma, wires=wires, id=id)

@staticmethod
def compute_kraus_matrices(gamma): # pylint:disable=arguments-differ
Expand Down Expand Up @@ -298,19 +292,15 @@ class DepolarizingChannel(Channel):
Args:
p (float): Each Pauli gate is applied with probability :math:`\frac{p}{3}`
wires (Sequence[int] or int): the wire the channel acts on
do_queue (bool): Indicates whether the operator should be
immediately pushed into the Operator queue (optional).
This argument is deprecated, instead of setting it to ``False``
use :meth:`~.queuing.QueuingManager.stop_recording`.
id (str or None): String representing the operation (optional)
"""
num_params = 1
num_wires = 1
grad_method = "A"
grad_recipe = ([[1, 0, 1], [-1, 0, 0]],)

def __init__(self, p, wires, do_queue=None, id=None):
super().__init__(p, wires=wires, do_queue=do_queue, id=id)
def __init__(self, p, wires, id=None):
super().__init__(p, wires=wires, id=id)

@staticmethod
def compute_kraus_matrices(p): # pylint:disable=arguments-differ
Expand Down Expand Up @@ -375,19 +365,15 @@ class BitFlip(Channel):
Args:
p (float): The probability that a bit flip error occurs.
wires (Sequence[int] or int): the wire the channel acts on
do_queue (bool): Indicates whether the operator should be
immediately pushed into the Operator queue (optional).
This argument is deprecated, instead of setting it to ``False``
use :meth:`~.queuing.QueuingManager.stop_recording`.
id (str or None): String representing the operation (optional)
"""
num_params = 1
num_wires = 1
grad_method = "A"
grad_recipe = ([[1, 0, 1], [-1, 0, 0]],)

def __init__(self, p, wires, do_queue=None, id=None):
super().__init__(p, wires=wires, do_queue=do_queue, id=id)
def __init__(self, p, wires, id=None):
super().__init__(p, wires=wires, id=id)

@staticmethod
def compute_kraus_matrices(p): # pylint:disable=arguments-differ
Expand Down Expand Up @@ -461,18 +447,14 @@ class ResetError(Channel):
p_0 (float): The probability that a reset to 0 error occurs.
p_1 (float): The probability that a reset to 1 error occurs.
wires (Sequence[int] or int): the wire the channel acts on
do_queue (bool): Indicates whether the operator should be
immediately pushed into the Operator queue (optional).
This argument is deprecated, instead of setting it to ``False``
use :meth:`~.queuing.QueuingManager.stop_recording`.
id (str or None): String representing the operation (optional)
"""
num_params = 2
num_wires = 1
grad_method = "F"

def __init__(self, p0, p1, wires, do_queue=None, id=None):
super().__init__(p0, p1, wires=wires, do_queue=do_queue, id=id)
def __init__(self, p0, p1, wires, id=None):
super().__init__(p0, p1, wires=wires, id=id)

@staticmethod
def compute_kraus_matrices(p_0, p_1): # pylint:disable=arguments-differ
Expand Down Expand Up @@ -555,10 +537,6 @@ class PauliError(Channel):
operators (str): The Pauli operators acting on the specified (groups of) wires
p (float): The probability of the operator being applied
wires (Sequence[int] or int): The wires the channel acts on
do_queue (bool): Indicates whether the operator should be
immediately pushed into the Operator queue (optional).
This argument is deprecated, instead of setting it to ``False``
use :meth:`~.queuing.QueuingManager.stop_recording`.
id (str or None): String representing the operation (optional)
**Example:**
Expand All @@ -578,8 +556,8 @@ class PauliError(Channel):
num_params = 2
"""int: Number of trainable parameters that the operator depends on."""

def __init__(self, operators, p, wires=None, do_queue=None, id=None):
super().__init__(operators, p, wires=wires, do_queue=do_queue, id=id)
def __init__(self, operators, p, wires=None, id=None):
super().__init__(operators, p, wires=wires, id=id)

# check if the specified operators are legal
if not set(operators).issubset({"X", "Y", "Z"}):
Expand Down Expand Up @@ -671,19 +649,15 @@ class PhaseFlip(Channel):
Args:
p (float): The probability that a phase flip error occurs.
wires (Sequence[int] or int): the wire the channel acts on
do_queue (bool): Indicates whether the operator should be
immediately pushed into the Operator queue (optional).
This argument is deprecated, instead of setting it to ``False``
use :meth:`~.queuing.QueuingManager.stop_recording`.
id (str or None): String representing the operation (optional)
"""
num_params = 1
num_wires = 1
grad_method = "A"
grad_recipe = ([[1, 0, 1], [-1, 0, 0]],)

def __init__(self, p, wires, do_queue=None, id=None):
super().__init__(p, wires=wires, do_queue=do_queue, id=id)
def __init__(self, p, wires, id=None):
super().__init__(p, wires=wires, id=id)

@staticmethod
def compute_kraus_matrices(p): # pylint:disable=arguments-differ
Expand Down Expand Up @@ -725,17 +699,13 @@ class QubitChannel(Channel):
Args:
K_list (list[array[complex]]): list of Kraus matrices
wires (Union[Wires, Sequence[int], or int]): the wire(s) the operation acts on
do_queue (bool): Indicates whether the operator should be
immediately pushed into the Operator queue (optional).
This argument is deprecated, instead of setting it to ``False``
use :meth:`~.queuing.QueuingManager.stop_recording`.
id (str or None): String representing the operation (optional)
"""
num_wires = AnyWires
grad_method = None

def __init__(self, K_list, wires=None, do_queue=None, id=None):
super().__init__(*K_list, wires=wires, do_queue=do_queue, id=id)
def __init__(self, K_list, wires=None, id=None):
super().__init__(*K_list, wires=wires, id=id)

# check all Kraus matrices are square matrices
if not all(K.shape[0] == K.shape[1] for K in K_list):
Expand Down Expand Up @@ -855,18 +825,14 @@ class ThermalRelaxationError(Channel):
t2 (float): the :math:`T_2` dephasing constant. Must be less than :math:`2 T_1`
tg (float): the gate time for relaxation error
wires (Sequence[int] or int): the wire the channel acts on
do_queue (bool): Indicates whether the operator should be
immediately pushed into the Operator queue (optional).
This argument is deprecated, instead of setting it to ``False``
use :meth:`~.queuing.QueuingManager.stop_recording`.
id (str or None): String representing the operation (optional)
"""
num_params = 4
num_wires = 1
grad_method = "F"

def __init__(self, pe, t1, t2, tq, wires, do_queue=None, id=None):
super().__init__(pe, t1, t2, tq, wires=wires, do_queue=do_queue, id=id)
def __init__(self, pe, t1, t2, tq, wires, id=None):
super().__init__(pe, t1, t2, tq, wires=wires, id=id)

@staticmethod
def compute_kraus_matrices(pe, t1, t2, tg): # pylint:disable=arguments-differ
Expand Down
Loading

0 comments on commit 49d325f

Please sign in to comment.