Skip to content

Commit

Permalink
clean out imports
Browse files Browse the repository at this point in the history
  • Loading branch information
dwierichs committed Jul 24, 2024
1 parent cf3e621 commit 1967a3c
Showing 1 changed file with 35 additions and 43 deletions.
78 changes: 35 additions & 43 deletions pennylane/transforms/optimization/optimization_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,6 @@
# limitations under the License.
"""Utility functions for circuit optimization."""
# pylint: disable=too-many-return-statements,import-outside-toplevel
from pennylane.math import (
allclose,
arccos,
arctan2,
asarray,
cast_like,
cos,
is_abstract,
moveaxis,
requires_grad,
sin,
sqrt,
stack,
zeros_like,
)
from pennylane.ops.identity import GlobalPhase
from pennylane.wires import Wires

Expand Down Expand Up @@ -60,21 +45,25 @@ def _try_no_fuse(angles1, angles2):
if some angles in the input angles vanish."""
dummy_sum = angles1 + angles2
# moveaxis required for batched inputs
phi1, theta1, omega1 = moveaxis(cast_like(asarray(angles1), dummy_sum), -1, 0)
phi2, theta2, omega2 = moveaxis(cast_like(asarray(angles2), dummy_sum), -1, 0)

if allclose(omega1 + phi2, 0.0):
return stack([phi1, theta1 + theta2, omega2])
zero = zeros_like(phi1) + zeros_like(phi2)
if allclose(theta1, 0.0):
phi1, theta1, omega1 = qml.math.moveaxis(

Check notice on line 48 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L48

Undefined variable 'qml' (undefined-variable)
qml.math.cast_like(qml.math.asarray(angles1), dummy_sum), -1, 0

Check notice on line 49 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L49

Undefined variable 'qml' (undefined-variable)

Check notice on line 49 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L49

Undefined variable 'qml' (undefined-variable)
)
phi2, theta2, omega2 = qml.math.moveaxis(

Check notice on line 51 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L51

Undefined variable 'qml' (undefined-variable)
qml.math.cast_like(qml.math.asarray(angles2), dummy_sum), -1, 0

Check notice on line 52 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L52

Undefined variable 'qml' (undefined-variable)

Check notice on line 52 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L52

Undefined variable 'qml' (undefined-variable)
)

if qml.math.allclose(omega1 + phi2, 0.0):

Check notice on line 55 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L55

Undefined variable 'qml' (undefined-variable)
return qml.math.stack([phi1, theta1 + theta2, omega2])

Check notice on line 56 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L56

Undefined variable 'qml' (undefined-variable)
zero = qml.math.zeros_like(phi1) + qml.math.zeros_like(phi2)

Check notice on line 57 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L57

Undefined variable 'qml' (undefined-variable)

Check notice on line 57 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L57

Undefined variable 'qml' (undefined-variable)
if qml.math.allclose(theta1, 0.0):

Check notice on line 58 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L58

Undefined variable 'qml' (undefined-variable)
# No Y rotation in first Rot
if allclose(theta2, 0.0):
if qml.math.allclose(theta2, 0.0):

Check notice on line 60 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L60

Undefined variable 'qml' (undefined-variable)
# Z rotations only
return stack([phi1 + omega1 + phi2 + omega2, zero, zero])
return stack([phi1 + omega1 + phi2, theta2, omega2])
if allclose(theta2, 0.0):
return qml.math.stack([phi1 + omega1 + phi2 + omega2, zero, zero])

Check notice on line 62 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L62

Undefined variable 'qml' (undefined-variable)
return qml.math.stack([phi1 + omega1 + phi2, theta2, omega2])

Check notice on line 63 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L63

Undefined variable 'qml' (undefined-variable)
if qml.math.allclose(theta2, 0.0):

Check notice on line 64 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L64

Undefined variable 'qml' (undefined-variable)
# No Y rotation in second Rot
return stack([phi1, theta1, omega1 + phi2 + omega2])
return qml.math.stack([phi1, theta1, omega1 + phi2 + omega2])

Check notice on line 66 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L66

Undefined variable 'qml' (undefined-variable)
return None


Expand Down Expand Up @@ -118,35 +107,38 @@ def fuse_rot_angles(angles1, angles2):
"""

if not (
is_abstract(angles1)
or is_abstract(angles2)
or requires_grad(angles1)
or requires_grad(angles2)
qml.math.is_abstract(angles1)

Check notice on line 110 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L110

Undefined variable 'qml' (undefined-variable)
or qml.math.is_abstract(angles2)

Check notice on line 111 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L111

Undefined variable 'qml' (undefined-variable)
or qml.math.requires_grad(angles1)

Check notice on line 112 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L112

Undefined variable 'qml' (undefined-variable)
or qml.math.requires_grad(angles2)

Check notice on line 113 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L113

Undefined variable 'qml' (undefined-variable)
):
fused_angles = _try_no_fuse(angles1, angles2)
if fused_angles is not None:
return fused_angles

# moveaxis required for batched inputs
phi1, theta1, omega1 = moveaxis(asarray(angles1), -1, 0)
phi2, theta2, omega2 = moveaxis(asarray(angles2), -1, 0)
c1, c2, s1, s2 = cos(theta1 / 2), cos(theta2 / 2), sin(theta1 / 2), sin(theta2 / 2)
phi1, theta1, omega1 = qml.math.moveaxis(qml.math.asarray(angles1), -1, 0)

Check notice on line 120 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L120

Undefined variable 'qml' (undefined-variable)

Check notice on line 120 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L120

Undefined variable 'qml' (undefined-variable)
phi2, theta2, omega2 = qml.math.moveaxis(qml.math.asarray(angles2), -1, 0)

Check notice on line 121 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L121

Undefined variable 'qml' (undefined-variable)

Check notice on line 121 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L121

Undefined variable 'qml' (undefined-variable)
c1, c2 = qml.math.cos(theta1 / 2), qml.math.cos(theta2 / 2)

Check notice on line 122 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L122

Undefined variable 'qml' (undefined-variable)

Check notice on line 122 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L122

Undefined variable 'qml' (undefined-variable)
s1, s2 = qml.math.sin(theta1 / 2), qml.math.sin(theta2 / 2)

Check notice on line 123 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L123

Undefined variable 'qml' (undefined-variable)

Check notice on line 123 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L123

Undefined variable 'qml' (undefined-variable)

mag = sqrt(c1**2 * c2**2 + s1**2 * s2**2 - 2 * c1 * c2 * s1 * s2 * cos(omega1 + phi2))
theta_f = 2 * arccos(mag)
mag = qml.math.sqrt(

Check notice on line 125 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L125

Undefined variable 'qml' (undefined-variable)
c1**2 * c2**2 + s1**2 * s2**2 - 2 * c1 * c2 * s1 * s2 * qml.math.cos(omega1 + phi2)

Check notice on line 126 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L126

Undefined variable 'qml' (undefined-variable)
)
theta_f = 2 * qml.math.arccos(mag)

Check notice on line 128 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L128

Undefined variable 'qml' (undefined-variable)

alpha1, beta1 = (phi1 + omega1) / 2, (phi1 - omega1) / 2
alpha2, beta2 = (phi2 + omega2) / 2, (phi2 - omega2) / 2

alpha_arg1 = -c1 * c2 * sin(alpha1 + alpha2) - s1 * s2 * sin(beta2 - beta1)
alpha_arg2 = c1 * c2 * cos(alpha1 + alpha2) - s1 * s2 * cos(beta2 - beta1)
alpha_f = -1 * arctan2(alpha_arg1, alpha_arg2)
alpha_arg1 = -c1 * c2 * qml.math.sin(alpha1 + alpha2) - s1 * s2 * qml.math.sin(beta2 - beta1)

Check notice on line 133 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L133

Undefined variable 'qml' (undefined-variable)

Check notice on line 133 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L133

Undefined variable 'qml' (undefined-variable)
alpha_arg2 = c1 * c2 * qml.math.cos(alpha1 + alpha2) - s1 * s2 * qml.math.cos(beta2 - beta1)

Check notice on line 134 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L134

Undefined variable 'qml' (undefined-variable)

Check notice on line 134 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L134

Undefined variable 'qml' (undefined-variable)
alpha_f = -1 * qml.math.arctan2(alpha_arg1, alpha_arg2)

Check notice on line 135 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L135

Undefined variable 'qml' (undefined-variable)

beta_arg1 = -c1 * s2 * sin(alpha1 + beta2) + s1 * c2 * sin(alpha2 - beta1)
beta_arg2 = c1 * s2 * cos(alpha1 + beta2) + s1 * c2 * cos(alpha2 - beta1)
beta_f = -1 * arctan2(beta_arg1, beta_arg2)
beta_arg1 = -c1 * s2 * qml.math.sin(alpha1 + beta2) + s1 * c2 * qml.math.sin(alpha2 - beta1)

Check notice on line 137 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L137

Undefined variable 'qml' (undefined-variable)

Check notice on line 137 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L137

Undefined variable 'qml' (undefined-variable)
beta_arg2 = c1 * s2 * qml.math.cos(alpha1 + beta2) + s1 * c2 * qml.math.cos(alpha2 - beta1)

Check notice on line 138 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L138

Undefined variable 'qml' (undefined-variable)

Check notice on line 138 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L138

Undefined variable 'qml' (undefined-variable)
beta_f = -1 * qml.math.arctan2(beta_arg1, beta_arg2)

Check notice on line 139 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L139

Undefined variable 'qml' (undefined-variable)

return stack([alpha_f + beta_f, theta_f, alpha_f - beta_f], axis=-1)
return qml.math.stack([alpha_f + beta_f, theta_f, alpha_f - beta_f], axis=-1)

Check notice on line 141 in pennylane/transforms/optimization/optimization_utils.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/transforms/optimization/optimization_utils.py#L141

Undefined variable 'qml' (undefined-variable)


def _fuse_global_phases(operations):
Expand Down

0 comments on commit 1967a3c

Please sign in to comment.