Skip to content

Commit

Permalink
Move qml.cond to ops.op_math (#4860)
Browse files Browse the repository at this point in the history
* Moved `condition.py` from the `transforms` folder to `ops/op_math`.
`qml.transforms.Conditional` is now available as `qml.ops.Conditional`.
* Updated docs accordingly.
  • Loading branch information
mudit2812 authored Nov 21, 2023
1 parent 1f47116 commit c049e15
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 39 deletions.
4 changes: 4 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@

<h3>Breaking changes 💔</h3>

* Moved `qml.cond` and the `Conditional` operation from the `transforms` folder to the `ops/op_math` folder.
`qml.transforms.Conditional` will now be available as `qml.ops.Conditional`.
[(#4860)](https://github.com/PennyLaneAI/pennylane/pull/4860)

* The `prep` keyword argument has been removed from `QuantumScript` and `QuantumTape`.
`StatePrepBase` operations should be placed at the beginning of the `ops` list instead.
[(#4756)](https://github.com/PennyLaneAI/pennylane/pull/4756)
Expand Down
3 changes: 1 addition & 2 deletions pennylane/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
shadow_expval,
)
from pennylane.ops import *
from pennylane.ops import adjoint, ctrl, exp, sum, pow, prod, s_prod
from pennylane.ops import adjoint, ctrl, cond, exp, sum, pow, prod, s_prod
from pennylane.templates import broadcast, layer
from pennylane.templates.embeddings import *
from pennylane.templates.layers import *
Expand All @@ -89,7 +89,6 @@
cut_circuit,
cut_circuit_mc,
compile,
cond,
defer_measurements,
metric_tensor,
specs,
Expand Down
3 changes: 3 additions & 0 deletions pennylane/ops/op_math/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
~adjoint
~ctrl
~cond
~exp
~sum
~pow
Expand All @@ -40,6 +41,7 @@
~Adjoint
~CompositeOp
~Conditional
~Controlled
~ControlledOp
~Evolution
Expand Down Expand Up @@ -78,6 +80,7 @@

from .adjoint import Adjoint, adjoint
from .composite import CompositeOp
from .condition import cond, Conditional
from .controlled import Controlled, ControlledOp, ctrl
from .controlled_ops import ControlledQubitUnitary, CY, CZ
from .evolution import Evolution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from functools import wraps
from typing import Type

from pennylane.measurements import MeasurementValue
from pennylane.operation import AnyWires, Operation
from pennylane.tape import make_qscript

Expand Down Expand Up @@ -48,12 +47,7 @@ class Conditional(Operation):

num_wires = AnyWires

def __init__(
self,
expr: MeasurementValue[bool],
then_op: Type[Operation],
id=None,
):
def __init__(self, expr, then_op: Type[Operation], id=None):
self.meas_val = expr
self.then_op = then_op
super().__init__(wires=then_op.wires, id=id)
Expand Down
13 changes: 0 additions & 13 deletions pennylane/transforms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,6 @@
~draw
~draw_mpl
Transforms that act on quantum functions
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
These transforms accept quantum functions (Python functions
containing quantum operations) that are used to construct QNodes.
.. autosummary::
:toctree: api
~transforms.cond
Decorators and utility functions
--------------------------------
Expand Down Expand Up @@ -237,7 +225,6 @@
from .batch_input import batch_input
from .batch_partial import batch_partial
from .classical_jacobian import classical_jacobian
from .condition import cond, Conditional
from .convert_to_numpy_parameters import convert_to_numpy_parameters
from .compile import compile
from .decompositions import (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import pytest

import pennylane as qml
from pennylane.transforms.condition import ConditionalTransformError
from pennylane.ops.op_math.condition import ConditionalTransformError

terminal_meas = [
qml.probs(wires=[1, 0]),
Expand Down Expand Up @@ -64,16 +64,16 @@ def f(x):
assert len(ops) == 4
assert ops[0].return_type == qml.measurements.MidMeasure

assert isinstance(ops[1], qml.transforms.condition.Conditional)
assert isinstance(ops[1], qml.ops.Conditional)
assert isinstance(ops[1].then_op, qml.PauliX)
assert ops[1].then_op.wires == target_wire

assert isinstance(ops[2], qml.transforms.condition.Conditional)
assert isinstance(ops[2], qml.ops.Conditional)
assert isinstance(ops[2].then_op, qml.RY)
assert ops[2].then_op.wires == target_wire
assert ops[2].then_op.data == (r,)

assert isinstance(ops[3], qml.transforms.condition.Conditional)
assert isinstance(ops[3], qml.ops.Conditional)
assert isinstance(ops[3].then_op, qml.PauliZ)
assert ops[3].then_op.wires == target_wire

Expand Down Expand Up @@ -129,20 +129,20 @@ def g(x):

assert ops[0].return_type == qml.measurements.MidMeasure

assert isinstance(ops[1], qml.transforms.condition.Conditional)
assert isinstance(ops[1], qml.ops.Conditional)
assert isinstance(ops[1].then_op, qml.PauliX)
assert ops[1].then_op.wires == target_wire

assert isinstance(ops[2], qml.transforms.condition.Conditional)
assert isinstance(ops[2], qml.ops.Conditional)
assert isinstance(ops[2].then_op, qml.RY)
assert ops[2].then_op.wires == target_wire
assert ops[2].then_op.data == (r,)

assert isinstance(ops[3], qml.transforms.condition.Conditional)
assert isinstance(ops[3], qml.ops.Conditional)
assert isinstance(ops[3].then_op, qml.PauliZ)
assert ops[3].then_op.wires == target_wire

assert isinstance(ops[4], qml.transforms.condition.Conditional)
assert isinstance(ops[4], qml.ops.Conditional)
assert isinstance(ops[4].then_op, qml.PauliY)
assert ops[4].then_op.wires == target_wire

Expand Down Expand Up @@ -224,12 +224,12 @@ def test_cond_operationss_with_adjoint(self, terminal_measurement):
assert len(ops) == 3
assert ops[0].return_type == qml.measurements.MidMeasure

assert isinstance(ops[1], qml.transforms.condition.Conditional)
assert isinstance(ops[1], qml.ops.Conditional)
assert isinstance(ops[1].then_op, qml.ops.op_math.Adjoint)
assert isinstance(ops[1].then_op.base, qml.RX)
assert ops[1].then_op.wires == target_wire

assert isinstance(ops[2], qml.transforms.condition.Conditional)
assert isinstance(ops[2], qml.ops.Conditional)
assert isinstance(ops[2].then_op, qml.RX)
assert ops[2].then_op.data == (r,)
assert ops[2].then_op.wires == target_wire
Expand All @@ -253,11 +253,11 @@ def test_cond_operationss_with_ctrl(self, terminal_measurement):
assert len(ops) == 3
assert ops[0].return_type == qml.measurements.MidMeasure

assert isinstance(ops[1], qml.transforms.condition.Conditional)
assert isinstance(ops[1], qml.ops.Conditional)
assert isinstance(ops[1].then_op, qml.ops.op_math.Controlled)
assert qml.equal(ops[1].then_op.base, qml.RX(r, wires=2))

assert isinstance(ops[2], qml.transforms.condition.Conditional)
assert isinstance(ops[2], qml.ops.Conditional)
assert isinstance(ops[2].then_op, qml.ops.op_math.Controlled)
assert qml.equal(ops[2].then_op.base, qml.RY(r, wires=2))

Expand All @@ -281,11 +281,11 @@ def test_ctrl_operations_with_cond(self, terminal_measurement):
assert ops[0].return_type == qml.measurements.MidMeasure

assert isinstance(ops[1], qml.ops.op_math.Controlled)
assert isinstance(ops[1].base, qml.transforms.condition.Conditional)
assert isinstance(ops[1].base, qml.ops.Conditional)
assert qml.equal(ops[1].base.then_op, qml.RX(1.234, wires=0))

assert isinstance(ops[2], qml.ops.op_math.Controlled)
assert isinstance(ops[2].base, qml.transforms.condition.Conditional)
assert isinstance(ops[2].base, qml.ops.Conditional)
assert qml.equal(ops[2].base.then_op, qml.RY(r, wires=0))

assert len(tape.measurements) == 1
Expand Down
6 changes: 3 additions & 3 deletions tests/tape/test_tape.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,16 +296,16 @@ def f(x):
assert len(tape.circuit) == 5
assert tape.circuit[0].return_type == qml.measurements.MidMeasure

assert isinstance(tape.circuit[1], qml.transforms.condition.Conditional)
assert isinstance(tape.circuit[1], qml.ops.Conditional)
assert isinstance(tape.circuit[1].then_op, qml.PauliX)
assert tape.circuit[1].then_op.wires == target_wire

assert isinstance(tape.circuit[2], qml.transforms.condition.Conditional)
assert isinstance(tape.circuit[2], qml.ops.Conditional)
assert isinstance(tape.circuit[2].then_op, qml.RY)
assert tape.circuit[2].then_op.wires == target_wire
assert tape.circuit[2].then_op.data == (r,)

assert isinstance(tape.circuit[3], qml.transforms.condition.Conditional)
assert isinstance(tape.circuit[3], qml.ops.Conditional)
assert isinstance(tape.circuit[3].then_op, qml.PauliZ)
assert tape.circuit[3].then_op.wires == target_wire

Expand Down

0 comments on commit c049e15

Please sign in to comment.