Skip to content

Commit

Permalink
apply format
Browse files Browse the repository at this point in the history
  • Loading branch information
LuisAlfredoNu committed Sep 24, 2024
1 parent 763d026 commit bdafe00
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions mpitests/test_adjoint_jacobian.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
[True, False],
)


@pytest.fixture(name="dev", params=fixture_params)
def fixture_dev(request):
"""Returns a PennyLane device."""
Expand All @@ -59,6 +60,7 @@ def fixture_dev(request):
batch_obs=request.param[1],
)


class TestAdjointJacobian: # pylint: disable=too-many-public-methods
"""Tests for the adjoint_jacobian method"""

Expand Down Expand Up @@ -219,28 +221,26 @@ def test_Rot_gradient(self, stateprep, theta, batch_obs, dev):
assert np.allclose(calculated_val, numeric_val, atol=tol, rtol=0)

@pytest.mark.parametrize("param", [1, -2, 1.623, -0.051, 0]) # integers, floats, zero
@pytest.mark.parametrize("rotation,obs,expected_func",
[
(qml.RY, qml.PauliX, lambda x : np.cos(x)),
(qml.RX, qml.PauliZ, lambda x : -np.sin(x))
])
@pytest.mark.parametrize(
"rotation,obs,expected_func",
[(qml.RY, qml.PauliX, lambda x: np.cos(x)), (qml.RX, qml.PauliZ, lambda x: -np.sin(x))],
)
@pytest.mark.parametrize("batch_obs", [True, False])
def test_r_gradient(self, tol, param, rotation, obs, expected_func, batch_obs, dev):
"""Test for the gradient of the rotation gate matches the known formula."""

qs = QuantumScript(
[rotation(param, wires=0)],
[qml.expval(obs(0))],
trainable_params=[0],
)

config = ExecutionConfig(gradient_method="adjoint", device_options={"batch_obs": batch_obs})

# circuit jacobians
dev_jacobian = dev.compute_derivatives(qs, config)
expected_jacobian = expected_func(param)
assert np.allclose(dev_jacobian, expected_jacobian, atol=tol, rtol=0)


@staticmethod
def process_and_execute_multiple_rx(dev, params, obs, batch_obs):
Expand Down Expand Up @@ -388,7 +388,7 @@ def test_gradients_pauliz_hermitian(self, op, obs, batch_obs, dev):
qml.Hadamard(wires=0),
qml.RX(0.543, wires=0),
qml.CNOT(wires=[0, 1]),
op,
op,
qml.Rot(1.3, -2.3, 0.5, wires=[0]),
qml.RZ(-0.5, wires=0),
qml.adjoint(qml.RY(0.5, wires=1), lazy=False),
Expand Down Expand Up @@ -461,6 +461,7 @@ def test_gradient_gate_with_multiple_parameters(self, obs, batch_obs, dev):
qml.PauliZ.compute_matrix(),
)


def Rx(theta):
r"""One-qubit rotation about the x axis.
Expand All @@ -471,6 +472,7 @@ def Rx(theta):
"""
return math.cos(theta / 2) * I + 1j * math.sin(-theta / 2) * X


def Ry(theta):
r"""One-qubit rotation about the y axis.
Expand All @@ -481,6 +483,7 @@ def Ry(theta):
"""
return math.cos(theta / 2) * I + 1j * math.sin(-theta / 2) * Y


def Rz(theta):
r"""One-qubit rotation about the z axis.
Expand Down

0 comments on commit bdafe00

Please sign in to comment.