Skip to content

Commit

Permalink
Removes QubitStateVector from tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PietropaoloFrisoni committed Sep 20, 2024
1 parent 7e82d32 commit abc5729
Showing 1 changed file with 37 additions and 21 deletions.
58 changes: 37 additions & 21 deletions tests/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,22 @@
class TestAnalyticApply:
"""Test application of PennyLane operations with analytic calculation."""

@pytest.mark.parametrize("op", [qml.QubitStateVector, qml.StatePrep])
def test_qubit_state_vector(self, op, init_state, device, tol):
"""Test that the QubitStateVector and StatePrep operations produce the expected
results with the apply method."""
def test_qubit_state_vector(self, init_state, device, tol):
"""Test that the StatePrep operation produces the expected
result with the apply method."""
dev = device(1)
state = init_state(1)

dev.apply([op(state, wires=[0])])
dev.apply([qml.StatePrep(state, wires=[0])])

res = np.abs(dev.state) ** 2
expected = np.abs(state) ** 2
assert np.allclose(res, expected, **tol)

@pytest.mark.parametrize("operation", single_qubit_operations)
def test_single_qubit_operations_no_parameters(self, init_state, device, operation, tol):
def test_single_qubit_operations_no_parameters(
self, init_state, device, operation, tol
):
"""Test that single qubit operations that take no parameters work fine
with the apply method."""
dev = device(1)
Expand All @@ -139,7 +140,9 @@ def test_single_qubit_operations_no_parameters(self, init_state, device, operati

@pytest.mark.parametrize("theta", [0.5432, -0.232])
@pytest.mark.parametrize("operation", single_qubit_operations_param)
def test_single_qubit_operations_parameters(self, init_state, device, operation, theta, tol):
def test_single_qubit_operations_parameters(
self, init_state, device, operation, theta, tol
):
"""Test that single qubit parametrized operations work fine with the
apply method."""
dev = device(1)
Expand All @@ -153,7 +156,9 @@ def test_single_qubit_operations_parameters(self, init_state, device, operation,
assert np.allclose(res, expected, **tol)

@pytest.mark.parametrize("operation", two_qubit)
def test_two_qubit_operations_no_parameters(self, init_state, device, operation, tol):
def test_two_qubit_operations_no_parameters(
self, init_state, device, operation, tol
):
"""Test that two qubit operations that take no parameters work fine
with the apply method."""
dev = device(2)
Expand All @@ -170,7 +175,9 @@ def test_two_qubit_operations_no_parameters(self, init_state, device, operation,

@pytest.mark.parametrize("theta", [0.5432, -0.232])
@pytest.mark.parametrize("operation", two_qubit_param)
def test_two_qubit_operations_parameters(self, init_state, device, operation, theta, tol):
def test_two_qubit_operations_parameters(
self, init_state, device, operation, theta, tol
):
"""Test that two qubit parametrized operations work fine with the
apply method."""
dev = device(2)
Expand All @@ -185,7 +192,9 @@ def test_two_qubit_operations_parameters(self, init_state, device, operation, th
assert np.allclose(res, expected, **tol)

@pytest.mark.parametrize("operation", three_qubit)
def test_three_qubit_operations_no_parameters(self, init_state, device, operation, tol):
def test_three_qubit_operations_no_parameters(
self, init_state, device, operation, tol
):
"""Test that three qubit operations that take no parameters work fine
with the apply method."""
dev = device(3)
Expand Down Expand Up @@ -222,31 +231,30 @@ def test_invalid_qubit(self, init_state, device):
class TestNonAnalyticApply:
"""Test application of PennyLane operations with non-analytic calculation."""

@pytest.mark.parametrize("op", [qml.QubitStateVector, qml.StatePrep])
def test_qubit_state_vector(self, op, init_state, device, tol):
"""Test that the QubitStateVector and StatePrep operations produces the expected
def test_qubit_state_vector(self, init_state, device, tol):
"""Test that the StatePrep operation produces the expected
result with the apply method."""

dev = device(1)
state = init_state(1)
wires = [0]

dev.apply([op(state, wires=wires)])
dev.apply([qml.StatePrep(state, wires=wires)])
dev._samples = dev.generate_samples()

res = np.fromiter(dev.probability(), dtype=np.float64)
expected = np.abs(state) ** 2
assert np.allclose(res, expected, **tol)

@pytest.mark.parametrize("op", [qml.QubitStateVector, qml.StatePrep])
def test_invalid_qubit_state_vector(self, op, device):
def test_invalid_qubit_state_vector(self, device):
"""Test that an exception is raised if the state
vector is the wrong size"""
dev = device(2)
state = np.array([0, 123.432])
wires = [0, 1]

with pytest.raises(ValueError, match=r"State must be of length 4"):
dev.apply([op(state, wires=wires)])
dev.apply([qml.StatePrep(state, wires=wires)])

@pytest.mark.parametrize("mat", [U, U2])
def test_qubit_unitary(self, init_state, device, mat, tol):
Expand All @@ -257,7 +265,9 @@ def test_qubit_unitary(self, init_state, device, mat, tol):
state = init_state(N)
wires = list(range(N))

dev.apply([qml.StatePrep(state, wires=wires), qml.QubitUnitary(mat, wires=wires)])
dev.apply(
[qml.StatePrep(state, wires=wires), qml.QubitUnitary(mat, wires=wires)]
)
dev._samples = dev.generate_samples()

res = np.fromiter(dev.probability(), dtype=np.float64)
Expand All @@ -274,7 +284,9 @@ def test_invalid_qubit_unitary(self, device):
dev.apply([qml.QubitUnitary(state, wires=[0, 1])])

@pytest.mark.parametrize("operation", single_qubit_operations)
def test_single_qubit_operations_no_parameters(self, init_state, device, operation, tol):
def test_single_qubit_operations_no_parameters(
self, init_state, device, operation, tol
):
"""Test that single qubit operations that take no parameters work fine
with the apply method."""
dev = device(1)
Expand All @@ -291,7 +303,9 @@ def test_single_qubit_operations_no_parameters(self, init_state, device, operati

@pytest.mark.parametrize("theta", [0.5432, -0.232])
@pytest.mark.parametrize("operation", single_qubit_operations_param)
def test_single_qubit_operations_parameters(self, init_state, device, operation, theta, tol):
def test_single_qubit_operations_parameters(
self, init_state, device, operation, theta, tol
):
"""Test that single qubit parametrized operations work fine with the
apply method."""
dev = device(1)
Expand Down Expand Up @@ -325,7 +339,9 @@ def test_two_qubit_no_parameters(self, init_state, device, operation, tol):

@pytest.mark.parametrize("theta", [0.5432, -0.232])
@pytest.mark.parametrize("operation", two_qubit_param)
def test_two_qubit_operations_parameters(self, init_state, device, operation, theta, tol):
def test_two_qubit_operations_parameters(
self, init_state, device, operation, theta, tol
):
"""Test that two qubit parametrized operations work fine with the
apply method."""
dev = device(2)
Expand Down

0 comments on commit abc5729

Please sign in to comment.