Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Simone-Bordoni committed Mar 19, 2024
1 parent 44f5b83 commit 1d8ba4a
Showing 1 changed file with 27 additions and 38 deletions.
65 changes: 27 additions & 38 deletions tests/test_derivative.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,45 +57,34 @@ def test_standard_parameter_shift(backend, nshots, atol, scale_factor, grads):
circuit=c, hamiltonian=c, parameter_index=0, nshots=nshots
)

if isinstance(backend, PyTorchBackend):
with pytest.raises(NotImplementedError) as excinfo:
grad = parameter_shift(
circuit=c, hamiltonian=test_hamiltonian, parameter_index=0
)
assert (
str(excinfo.value)
== "PyTorchBackend for the parameter shift rule is not supported."
)

else:
# executing all the procedure
grad_0 = parameter_shift(
circuit=c,
hamiltonian=test_hamiltonian,
parameter_index=0,
scale_factor=scale_factor,
nshots=nshots,
)
grad_1 = parameter_shift(
circuit=c,
hamiltonian=test_hamiltonian,
parameter_index=1,
scale_factor=scale_factor,
nshots=nshots,
)
grad_2 = parameter_shift(
circuit=c,
hamiltonian=test_hamiltonian,
parameter_index=2,
scale_factor=scale_factor,
nshots=nshots,
)
# executing all the procedure
grad_0 = parameter_shift(
circuit=c,
hamiltonian=test_hamiltonian,
parameter_index=0,
scale_factor=scale_factor,
nshots=nshots,
)
grad_1 = parameter_shift(
circuit=c,
hamiltonian=test_hamiltonian,
parameter_index=1,
scale_factor=scale_factor,
nshots=nshots,
)
grad_2 = parameter_shift(
circuit=c,
hamiltonian=test_hamiltonian,
parameter_index=2,
scale_factor=scale_factor,
nshots=nshots,
)

# check of known values
# calculated using tf.GradientTape
backend.assert_allclose(grad_0, grads[0], atol=atol)
backend.assert_allclose(grad_1, grads[1], atol=atol)
backend.assert_allclose(grad_2, grads[2], atol=atol)
# check of known values
# calculated using tf.GradientTape
backend.assert_allclose(grad_0, grads[0], atol=atol)
backend.assert_allclose(grad_1, grads[1], atol=atol)
backend.assert_allclose(grad_2, grads[2], atol=atol)


@pytest.mark.parametrize("step_size", [10**-i for i in range(5, 10, 1)])
Expand Down

0 comments on commit 1d8ba4a

Please sign in to comment.