diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md index 5c8a92760..893c5bd07 100644 --- a/.github/CHANGELOG.md +++ b/.github/CHANGELOG.md @@ -13,6 +13,9 @@ ### Breaking changes +* Removed all instances of `qml.QubitStateVector`. + [(#985)](https://github.com/PennyLaneAI/pennylane-lightning/pull/985) + * Handling for the legacy operator arithmetic (the `Hamiltonian` and `Tensor` classes in PennyLane) is removed. [(#994)](https://github.com/PennyLaneAI/pennylane-lightning/pull/994) @@ -44,7 +47,7 @@ This release contains contributions from (in alphabetical order): -Ali Asadi, Joseph Lee, Luis Alfredo Nuñez Meneses, Shuli Shu, Raul Torres, Haochen Paul Wang +Ali Asadi, Joseph Lee, Luis Alfredo Nuñez Meneses, Andrija Paurevic, Shuli Shu, Raul Torres, Haochen Paul Wang --- diff --git a/doc/lightning_gpu/device.rst b/doc/lightning_gpu/device.rst index 405ea9764..8d8d74a75 100644 --- a/doc/lightning_gpu/device.rst +++ b/doc/lightning_gpu/device.rst @@ -68,7 +68,6 @@ Supported operations and observables ~pennylane.PSWAP ~pennylane.QFT ~pennylane.QubitCarry - ~pennylane.QubitStateVector ~pennylane.QubitSum ~pennylane.QubitUnitary ~pennylane.Rot diff --git a/doc/lightning_kokkos/device.rst b/doc/lightning_kokkos/device.rst index ce6e34303..60b09c9bf 100644 --- a/doc/lightning_kokkos/device.rst +++ b/doc/lightning_kokkos/device.rst @@ -66,7 +66,6 @@ Supported operations and observables ~pennylane.PSWAP ~pennylane.QFT ~pennylane.QubitCarry - ~pennylane.QubitStateVector ~pennylane.QubitSum ~pennylane.QubitUnitary ~pennylane.Rot diff --git a/doc/lightning_qubit/device.rst b/doc/lightning_qubit/device.rst index cea995827..43140350f 100644 --- a/doc/lightning_qubit/device.rst +++ b/doc/lightning_qubit/device.rst @@ -58,7 +58,6 @@ Supported operations and observables ~pennylane.PSWAP ~pennylane.QFT ~pennylane.QubitCarry - ~pennylane.QubitStateVector ~pennylane.QubitSum ~pennylane.QubitUnitary ~pennylane.Rot diff --git a/doc/lightning_tensor/device.rst b/doc/lightning_tensor/device.rst index 641a7f762..9fe0f225c 100644 --- a/doc/lightning_tensor/device.rst +++ b/doc/lightning_tensor/device.rst @@ -103,7 +103,6 @@ The "lightning.tensor" supports all gate operations supported by PennyLane. ~pennylane.PSWAP ~pennylane.QFT ~pennylane.QubitCarry - ~pennylane.QubitStateVector ~pennylane.QubitSum ~pennylane.QubitUnitary ~pennylane.Rot diff --git a/mpitests/test_adjoint_jacobian.py b/mpitests/test_adjoint_jacobian.py index 9d56dfdb1..b1c768aba 100644 --- a/mpitests/test_adjoint_jacobian.py +++ b/mpitests/test_adjoint_jacobian.py @@ -165,10 +165,9 @@ def tol_for_allclose(c_dtype): @pytest.mark.parametrize("theta", np.linspace(-2 * np.pi, 2 * np.pi, 7)) @pytest.mark.parametrize("G", [qml.RX, qml.RY, qml.RZ]) - @pytest.mark.parametrize("stateprep", [qml.QubitStateVector, qml.StatePrep]) @pytest.mark.parametrize("batch_obs", [True, False]) def test_pauli_rotation_gradient( - self, stateprep, G, theta, batch_obs, dev + self, G, theta, batch_obs, dev ): # pylint: disable=too-many-arguments """Tests that the automatic gradients of Pauli rotations are correct.""" random_state = np.array( @@ -176,7 +175,7 @@ def test_pauli_rotation_gradient( ) qs = QuantumScript( - [stateprep(random_state, 0), G(theta, 0)], + [qml.StatePrep(random_state, 0), G(theta, 0)], [qml.expval(qml.PauliZ(0))], trainable_params=[1], ) @@ -192,16 +191,15 @@ def test_pauli_rotation_gradient( assert np.allclose(calculated_val, numeric_val, atol=tol, rtol=0) @pytest.mark.parametrize("theta", np.linspace(-2 * np.pi, 2 * np.pi, 7)) - @pytest.mark.parametrize("stateprep", [qml.QubitStateVector, qml.StatePrep]) @pytest.mark.parametrize("batch_obs", [True, False]) - def test_Rot_gradient(self, stateprep, theta, batch_obs, dev): + def test_Rot_gradient(self, theta, batch_obs, dev): """Tests that the device gradient of an arbitrary Euler-angle-parameterized gate is correct.""" params = np.array([theta, theta**3, np.sqrt(2) * theta]) qs = QuantumScript( [ - stateprep(np.array([1.0, -1.0], requires_grad=False) / np.sqrt(2), wires=0), + qml.StatePrep(np.array([1.0, -1.0], requires_grad=False) / np.sqrt(2), wires=0), qml.Rot(*params, wires=[0]), ], [qml.expval(qml.PauliZ(0))], @@ -761,7 +759,7 @@ def f(params1, params2): def circuit_ansatz(params, wires): """Circuit ansatz containing all the parametrized gates""" - qml.QubitStateVector(unitary_group.rvs(2**8, random_state=0)[0], wires=wires) + qml.StatePrep(unitary_group.rvs(2**8, random_state=0)[0], wires=wires) qml.RX(params[0], wires=wires[0]) qml.RY(params[1], wires=wires[1]) qml.adjoint(qml.RX(params[2], wires=wires[2])) diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/AdjointJacobianGPU.hpp b/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/AdjointJacobianGPU.hpp index d1e982216..0e84a76f3 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/AdjointJacobianGPU.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/AdjointJacobianGPU.hpp @@ -312,8 +312,7 @@ class AdjointJacobian final PL_ABORT_IF(ops.getOpsParams()[op_idx].size() > 1, "The operation is not supported using the adjoint " "differentiation method"); - if ((ops_name[op_idx] == "QubitStateVector") || - (ops_name[op_idx] == "StatePrep") || + if ((ops_name[op_idx] == "StatePrep") || (ops_name[op_idx] == "BasisState")) { continue; } diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/AdjointJacobianGPUMPI.hpp b/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/AdjointJacobianGPUMPI.hpp index 77b3a8656..8d28827f3 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/AdjointJacobianGPUMPI.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/AdjointJacobianGPUMPI.hpp @@ -171,8 +171,7 @@ class AdjointJacobianMPI final PL_ABORT_IF(ops.getOpsParams()[op_idx].size() > 1, "The operation is not supported using the adjoint " "differentiation method"); - if ((ops_name[op_idx] == "QubitStateVector") || - (ops_name[op_idx] == "StatePrep") || + if ((ops_name[op_idx] == "StatePrep") || (ops_name[op_idx] == "BasisState")) { continue; } @@ -296,8 +295,7 @@ class AdjointJacobianMPI final PL_ABORT_IF(ops.getOpsParams()[op_idx].size() > 1, "The operation is not supported using the adjoint " "differentiation method"); - if ((ops_name[op_idx] == "QubitStateVector") || - (ops_name[op_idx] == "StatePrep") || + if ((ops_name[op_idx] == "StatePrep") || (ops_name[op_idx] == "BasisState")) { continue; } diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/tests/Test_AdjointJacobianGPU.cpp b/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/tests/Test_AdjointJacobianGPU.cpp index 23a873f0b..29dad3318 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/tests/Test_AdjointJacobianGPU.cpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/tests/Test_AdjointJacobianGPU.cpp @@ -91,11 +91,10 @@ TEST_CASE("AdjointJacobianGPU::adjointJacobian Op=RY, Obs=X", } } -TEST_CASE("AdjointJacobianGPU::adjointJacobian Op=[QubitStateVector, " +TEST_CASE("AdjointJacobianGPU::adjointJacobian Op=[" "StatePrep, BasisState], Obs=[Z,Z]", "[AdjointJacobianGPU]") { - const std::string test_ops = - GENERATE("QubitStateVector", "StatePrep", "BasisState"); + const std::string test_ops = GENERATE("StatePrep", "BasisState"); using StateVectorT = StateVectorCudaManaged; using ComplexT = StateVectorT::ComplexT; AdjointJacobian adj; @@ -124,7 +123,7 @@ TEST_CASE("AdjointJacobianGPU::adjointJacobian Op=[QubitStateVector, " ops, tp}; // apply_operations should be set as false to cover if statement in - // adjointJacobian when ops is "QubitStateVector" "StatePrep" or + // adjointJacobian when ops is "StatePrep" or // "BasisState". If apply_operations is set as true, errors will be // thrown out since ops mentioned above is not supported in // apply_operation method of sv. diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/tests/mpi/Test_AdjointJacobianGPUMPI.cpp b/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/tests/mpi/Test_AdjointJacobianGPUMPI.cpp index 3d0e6cab7..125a4505c 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/tests/mpi/Test_AdjointJacobianGPUMPI.cpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/algorithms/tests/mpi/Test_AdjointJacobianGPUMPI.cpp @@ -107,11 +107,10 @@ TEST_CASE("AdjointJacobianGPUMPI::adjointJacobianMPI Op=RX, Obs=[Z,Z]", } } -TEST_CASE("AdjointJacobianGPUMPI::adjointJacobianMPI Op=[QubitStateVector, " +TEST_CASE("AdjointJacobianGPUMPI::adjointJacobianMPI Op=[" "StatePrep, BasisState], Obs=[Z,Z]", "[AdjointJacobianGPUMPI]") { - const std::string test_ops = - GENERATE("QubitStateVector", "StatePrep", "BasisState"); + const std::string test_ops = GENERATE("StatePrep", "BasisState"); using StateVectorT = StateVectorCudaMPI; MPIManager mpi_manager(MPI_COMM_WORLD); diff --git a/pennylane_lightning/core/src/simulators/lightning_kokkos/algorithms/AdjointJacobianKokkos.hpp b/pennylane_lightning/core/src/simulators/lightning_kokkos/algorithms/AdjointJacobianKokkos.hpp index 60f0ff260..429a27d23 100644 --- a/pennylane_lightning/core/src/simulators/lightning_kokkos/algorithms/AdjointJacobianKokkos.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_kokkos/algorithms/AdjointJacobianKokkos.hpp @@ -137,8 +137,7 @@ class AdjointJacobian final PL_ABORT_IF(ops.getOpsParams()[op_idx].size() > 1, "The operation is not supported using the adjoint " "differentiation method"); - if ((ops_name[op_idx] == "QubitStateVector") || - (ops_name[op_idx] == "StatePrep") || + if ((ops_name[op_idx] == "StatePrep") || (ops_name[op_idx] == "BasisState")) { continue; } diff --git a/pennylane_lightning/core/src/simulators/lightning_qubit/algorithms/AdjointJacobianLQubit.hpp b/pennylane_lightning/core/src/simulators/lightning_qubit/algorithms/AdjointJacobianLQubit.hpp index 87c1b8069..a8b850a0c 100644 --- a/pennylane_lightning/core/src/simulators/lightning_qubit/algorithms/AdjointJacobianLQubit.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_qubit/algorithms/AdjointJacobianLQubit.hpp @@ -293,8 +293,7 @@ class AdjointJacobian final PL_ABORT_IF(ops.getOpsParams()[op_idx].size() > 1, "The operation is not supported using the adjoint " "differentiation method"); - if ((ops_name[op_idx] == "QubitStateVector") || - (ops_name[op_idx] == "StatePrep") || + if ((ops_name[op_idx] == "StatePrep") || (ops_name[op_idx] == "BasisState")) { continue; // Ignore them } diff --git a/pennylane_lightning/core/src/simulators/lightning_qubit/algorithms/VectorJacobianProduct.hpp b/pennylane_lightning/core/src/simulators/lightning_qubit/algorithms/VectorJacobianProduct.hpp index 9bbb9831c..e07174088 100644 --- a/pennylane_lightning/core/src/simulators/lightning_qubit/algorithms/VectorJacobianProduct.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_qubit/algorithms/VectorJacobianProduct.hpp @@ -120,8 +120,7 @@ class VectorJacobianProduct final PL_ABORT_IF(ops.getOpsParams()[op_idx].size() > 1, "The operation is not supported using the adjoint " "differentiation method"); - if ((ops_name[op_idx] == "QubitStateVector") || - (ops_name[op_idx] == "StatePrep") || + if ((ops_name[op_idx] == "StatePrep") || (ops_name[op_idx] == "BasisState")) { continue; // ignore them } diff --git a/pennylane_lightning/lightning_gpu/lightning_gpu.py b/pennylane_lightning/lightning_gpu/lightning_gpu.py index 2fd61aa67..47efd2bf6 100644 --- a/pennylane_lightning/lightning_gpu/lightning_gpu.py +++ b/pennylane_lightning/lightning_gpu/lightning_gpu.py @@ -78,7 +78,6 @@ _operations = frozenset( { "Identity", - "QubitStateVector", "QubitUnitary", "ControlledQubitUnitary", "MultiControlledX", diff --git a/pennylane_lightning/lightning_gpu/lightning_gpu.toml b/pennylane_lightning/lightning_gpu/lightning_gpu.toml index 1cef542ca..56da85aaf 100644 --- a/pennylane_lightning/lightning_gpu/lightning_gpu.toml +++ b/pennylane_lightning/lightning_gpu/lightning_gpu.toml @@ -49,7 +49,6 @@ GlobalPhase = { properties = [ "invertible", "controllable", "differe [operators.gates.decomp] BasisState = {} -QubitStateVector = {} StatePrep = {} MultiControlledX = {} diff --git a/pennylane_lightning/lightning_kokkos/lightning_kokkos.py b/pennylane_lightning/lightning_kokkos/lightning_kokkos.py index f9e4aa630..ee733fbbc 100644 --- a/pennylane_lightning/lightning_kokkos/lightning_kokkos.py +++ b/pennylane_lightning/lightning_kokkos/lightning_kokkos.py @@ -67,7 +67,6 @@ _operations = frozenset( { "Identity", - "QubitStateVector", "QubitUnitary", "ControlledQubitUnitary", "MultiControlledX", diff --git a/pennylane_lightning/lightning_kokkos/lightning_kokkos.toml b/pennylane_lightning/lightning_kokkos/lightning_kokkos.toml index 3676feb99..62f1a14ee 100644 --- a/pennylane_lightning/lightning_kokkos/lightning_kokkos.toml +++ b/pennylane_lightning/lightning_kokkos/lightning_kokkos.toml @@ -50,7 +50,6 @@ T = { properties = [ "invertible", "differe BasisState = {} MultiControlledX = {} -QubitStateVector = {} StatePrep = {} ControlledQubitUnitary = {} diff --git a/tests/lightning_qubit/test_adjoint_jacobian_class.py b/tests/lightning_qubit/test_adjoint_jacobian_class.py index f4d9f877d..e1cc77e79 100644 --- a/tests/lightning_qubit/test_adjoint_jacobian_class.py +++ b/tests/lightning_qubit/test_adjoint_jacobian_class.py @@ -324,7 +324,7 @@ def test_multiple_rx_gradient_expval_hamiltonian(self, tol, lightning_sv): def simple_circuit_ansatz(params, wires): """Circuit ansatz containing a large circuit""" return [ - qml.QubitStateVector(unitary_group.rvs(2**4, random_state=0)[0], wires=wires), + qml.StatePrep(unitary_group.rvs(2**4, random_state=0)[0], wires=wires), qml.RX(params[0], wires=wires[0]), qml.RY(params[1], wires=wires[1]), qml.RZ(params[2], wires=wires[3]), diff --git a/tests/lightning_qubit/test_measurements_class.py b/tests/lightning_qubit/test_measurements_class.py index f24090903..b1532c545 100644 --- a/tests/lightning_qubit/test_measurements_class.py +++ b/tests/lightning_qubit/test_measurements_class.py @@ -957,11 +957,11 @@ def test_sum(self, phi, theta, lightning_sv, tol): @pytest.mark.parametrize( "op,par,wires,expected", [ - (qml.QubitStateVector, [0, 1], [1], [1, -1]), - (qml.QubitStateVector, [0, 1], [0], [-1, 1]), - (qml.QubitStateVector, [1.0 / np.sqrt(2), 1.0 / np.sqrt(2)], [1], [1, 0]), - (qml.QubitStateVector, [1j / 2.0, np.sqrt(3) / 2.0], [1], [1, -0.5]), - (qml.QubitStateVector, [(2 - 1j) / 3.0, 2j / 3.0], [0], [1 / 9.0, 1]), + (qml.StatePrep, [0, 1], [1], [1, -1]), + (qml.StatePrep, [0, 1], [0], [-1, 1]), + (qml.StatePrep, [1.0 / np.sqrt(2), 1.0 / np.sqrt(2)], [1], [1, 0]), + (qml.StatePrep, [1j / 2.0, np.sqrt(3) / 2.0], [1], [1, -0.5]), + (qml.StatePrep, [(2 - 1j) / 3.0, 2j / 3.0], [0], [1 / 9.0, 1]), ], ) def test_state_vector_2_qubit_subset(tol, op, par, wires, expected, lightning_sv): diff --git a/tests/lightning_qubit/test_state_vector_class.py b/tests/lightning_qubit/test_state_vector_class.py index b3baaa3ea..28927a0b5 100644 --- a/tests/lightning_qubit/test_state_vector_class.py +++ b/tests/lightning_qubit/test_state_vector_class.py @@ -109,8 +109,6 @@ def test_apply_state_vector_with_lightning_handle(tol): [ (qml.BasisState, [0, 0, 1, 0], [1, 0]), (qml.BasisState, [0, 0, 0, 1], [1, 1]), - (qml.QubitStateVector, [0, 0, 1, 0], [0, 0, 1, 0]), - (qml.QubitStateVector, [0, 0, 0, 1], [0, 0, 0, 1]), (qml.StatePrep, [0, 0, 1, 0], [0, 0, 1, 0]), (qml.StatePrep, [0, 0, 0, 1], [0, 0, 0, 1]), ( @@ -140,7 +138,6 @@ def test_apply_operation_state_preparation(tol, operation, expected_output, par) "operation,par", [ (qml.BasisState, [1, 0]), - (qml.QubitStateVector, [0, 0, 1, 0]), ( qml.StatePrep, [1 / math.sqrt(3), 0, 1 / math.sqrt(3), 1 / math.sqrt(3)], diff --git a/tests/test_adjoint_jacobian.py b/tests/test_adjoint_jacobian.py index f37ea7a57..fcb066ef3 100644 --- a/tests/test_adjoint_jacobian.py +++ b/tests/test_adjoint_jacobian.py @@ -219,15 +219,14 @@ def test_proj_unsupported(self, dev): @pytest.mark.parametrize("theta", np.linspace(-2 * np.pi, 2 * np.pi, 7)) @pytest.mark.parametrize("G", [qml.RX, qml.RY, qml.RZ]) - @pytest.mark.parametrize("stateprep", [qml.QubitStateVector, qml.StatePrep]) - def test_pauli_rotation_gradient(self, stateprep, G, theta, dev): + def test_pauli_rotation_gradient(self, G, theta, dev): """Tests that the automatic gradients of Pauli rotations are correct.""" random_state = np.array( [0.43593284 - 0.02945156j, 0.40812291 + 0.80158023j], requires_grad=False ) tape = qml.tape.QuantumScript( - [stateprep(random_state, 0), G(theta, 0)], [qml.expval(qml.PauliZ(0))] + [qml.StatePrep(random_state, 0), G(theta, 0)], [qml.expval(qml.PauliZ(0))] ) tape.trainable_params = {1} @@ -243,14 +242,13 @@ def test_pauli_rotation_gradient(self, stateprep, G, theta, dev): assert np.allclose(calculated_val, numeric_val, atol=tol, rtol=0) @pytest.mark.parametrize("theta", np.linspace(-2 * np.pi, 2 * np.pi, 7)) - @pytest.mark.parametrize("stateprep", [qml.QubitStateVector, qml.StatePrep]) - def test_Rot_gradient(self, stateprep, theta, dev): + def test_Rot_gradient(self, theta, dev): """Tests that the device gradient of an arbitrary Euler-angle-parameterized gate is correct.""" params = np.array([theta, theta**3, np.sqrt(2) * theta]) with qml.tape.QuantumTape() as tape: - stateprep(np.array([1.0, -1.0], requires_grad=False) / np.sqrt(2), wires=0) + qml.StatePrep(np.array([1.0, -1.0], requires_grad=False) / np.sqrt(2), wires=0) qml.Rot(*params, wires=[0]) qml.expval(qml.PauliZ(0)) @@ -1142,7 +1140,7 @@ def f(params1, params2): def circuit_ansatz(params, wires): """Circuit ansatz containing all the parametrized gates""" - qml.QubitStateVector(unitary_group.rvs(2**4, random_state=0)[0], wires=wires) + qml.StatePrep(unitary_group.rvs(2**4, random_state=0)[0], wires=wires) qml.RX(params[0], wires=wires[0]) qml.RY(params[1], wires=wires[1]) qml.adjoint(qml.RX(params[2], wires=wires[2])) diff --git a/tests/test_apply.py b/tests/test_apply.py index b5e7c48bd..927058d01 100644 --- a/tests/test_apply.py +++ b/tests/test_apply.py @@ -187,8 +187,6 @@ def test_apply_operation_preserve_pointer_three_wires_no_parameters( [ (qml.BasisState, [0, 0, 1, 0], [1, 0]), (qml.BasisState, [0, 0, 0, 1], [1, 1]), - (qml.QubitStateVector, [0, 0, 1, 0], [0, 0, 1, 0]), - (qml.QubitStateVector, [0, 0, 0, 1], [0, 0, 0, 1]), (qml.StatePrep, [0, 0, 1, 0], [0, 0, 1, 0]), (qml.StatePrep, [0, 0, 0, 1], [0, 0, 0, 1]), ( @@ -479,19 +477,18 @@ def test_apply_operation_preserve_pointer_two_wires_with_parameters( assert pointer_before == pointer_after - @pytest.mark.parametrize("stateprep", [qml.QubitStateVector, qml.StatePrep]) - def test_apply_errors_qubit_state_vector(self, stateprep, qubit_device): + def test_apply_errors_qubit_state_vector(self, qubit_device): """Test that apply fails for incorrect state preparation, and > 2 qubit gates""" dev = qubit_device(wires=2) with pytest.raises(ValueError, match="The state must be a vector of norm 1.0;"): - dev.apply([stateprep(np.array([1, -1]), wires=[0])]) + dev.apply([qml.StatePrep(np.array([1, -1]), wires=[0])]) with pytest.raises( DeviceError, - match=f"Operation {stateprep(np.array([1, 0]), wires=[0]).name} cannot be used after other Operations have already been applied ", + match=f"Operation {qml.StatePrep(np.array([1, 0]), wires=[0]).name} cannot be used after other Operations have already been applied ", ): dev.reset() - dev.apply([qml.RZ(0.5, wires=[0]), stateprep(np.array([0, 1, 0, 0]), wires=[0, 1])]) + dev.apply([qml.RZ(0.5, wires=[0]), qml.StatePrep(np.array([0, 1, 0, 0]), wires=[0, 1])]) def test_apply_errors_basis_state(self, qubit_device): dev = qubit_device(wires=2) @@ -549,14 +546,13 @@ class TestExpval: (qml.Identity, [1 / math.sqrt(2), -1 / math.sqrt(2)], 1), ], ) - @pytest.mark.parametrize("stateprep", [qml.QubitStateVector, qml.StatePrep]) def test_expval_single_wire_no_parameters( - self, qubit_device, tol, stateprep, operation, input, expected_output + self, qubit_device, tol, operation, input, expected_output ): """Tests that expectation values are properly calculated for single-wire observables without parameters.""" dev = qubit_device(wires=1) obs = operation(wires=[0]) - ops = [stateprep(np.array(input), wires=[0])] + ops = [qml.StatePrep(np.array(input), wires=[0])] tape = qml.tape.QuantumScript(ops, [qml.expval(op=obs)]) res = dev.execute(tape) @@ -608,14 +604,13 @@ class TestVar: (qml.Identity, [1 / math.sqrt(2), -1 / math.sqrt(2)], 0), ], ) - @pytest.mark.parametrize("stateprep", [qml.QubitStateVector, qml.StatePrep]) def test_var_single_wire_no_parameters( - self, qubit_device, tol, stateprep, operation, input, expected_output + self, qubit_device, tol, operation, input, expected_output ): """Tests that variances are properly calculated for single-wire observables without parameters.""" dev = qubit_device(wires=1) obs = operation(wires=[0]) - ops = [stateprep(np.array(input), wires=[0])] + ops = [qml.StatePrep(np.array(input), wires=[0])] tape = qml.tape.QuantumScript(ops, [qml.var(op=obs)]) res = dev.execute(tape) @@ -842,10 +837,7 @@ def circuit(): ("CZ", [-1 / 2, -1 / 2]), ], ) - @pytest.mark.parametrize("stateprep", [qml.QubitStateVector, qml.StatePrep]) - def test_supported_gate_two_wires_no_parameters( - self, qubit_device, tol, stateprep, name, expected_output - ): + def test_supported_gate_two_wires_no_parameters(self, qubit_device, tol, name, expected_output): """Tests supported gates that act on two wires that are not parameterized""" dev = qubit_device(wires=2) op = getattr(qml.ops, name) @@ -855,7 +847,7 @@ def test_supported_gate_two_wires_no_parameters( @qml.qnode(dev) def circuit(): - stateprep(np.array([1 / 2, 0, 0, math.sqrt(3) / 2]), wires=[0, 1]) + qml.StatePrep(np.array([1 / 2, 0, 0, math.sqrt(3) / 2]), wires=[0, 1]) op(wires=[0, 1]) return qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliZ(1)) @@ -892,9 +884,9 @@ def circuit(): ("BasisState", [0, 0], [1, 1]), ("BasisState", [1, 0], [-1, 1]), ("BasisState", [0, 1], [1, -1]), - ("QubitStateVector", [1, 0, 0, 0], [1, 1]), - ("QubitStateVector", [0, 0, 1, 0], [-1, 1]), - ("QubitStateVector", [0, 1, 0, 0], [1, -1]), + ("StatePrep", [1, 0, 0, 0], [1, 1]), + ("StatePrep", [0, 0, 1, 0], [-1, 1]), + ("StatePrep", [0, 1, 0, 0], [1, -1]), ], ) def test_supported_state_preparation(self, qubit_device, tol, name, par, expected_output): @@ -955,11 +947,11 @@ def circuit(): @pytest.mark.parametrize( "name,par,wires,expected_output", [ - ("QubitStateVector", [0, 1], [1], [1, -1]), - ("QubitStateVector", [0, 1], [0], [-1, 1]), - ("QubitStateVector", [1.0 / np.sqrt(2), 1.0 / np.sqrt(2)], [1], [1, 0]), - ("QubitStateVector", [1j / 2.0, np.sqrt(3) / 2.0], [1], [1, -0.5]), - ("QubitStateVector", [(2 - 1j) / 3.0, 2j / 3.0], [0], [1 / 9.0, 1]), + ("StatePrep", [0, 1], [1], [1, -1]), + ("StatePrep", [0, 1], [0], [-1, 1]), + ("StatePrep", [1.0 / np.sqrt(2), 1.0 / np.sqrt(2)], [1], [1, 0]), + ("StatePrep", [1j / 2.0, np.sqrt(3) / 2.0], [1], [1, -0.5]), + ("StatePrep", [(2 - 1j) / 3.0, 2j / 3.0], [0], [1 / 9.0, 1]), ], ) def test_state_vector_2_qubit_subset( @@ -983,17 +975,17 @@ def circuit(): "name,par,wires,expected_output", [ ( - "QubitStateVector", + "StatePrep", [1j / np.sqrt(10), (1 - 2j) / np.sqrt(10), 0, 0, 0, 2 / np.sqrt(10), 0, 0], [0, 1, 2], [1 / 5.0, 1.0, -4 / 5.0], ), - ("QubitStateVector", [1 / np.sqrt(2), 0, 0, 1 / np.sqrt(2)], [0, 2], [0.0, 1.0, 0.0]), - ("QubitStateVector", [1 / np.sqrt(2), 0, 0, 1 / np.sqrt(2)], [0, 1], [0.0, 0.0, 1.0]), - ("QubitStateVector", [0, 1, 0, 0, 0, 0, 0, 0], [2, 1, 0], [-1.0, 1.0, 1.0]), - ("QubitStateVector", [0, 1j, 0, 0, 0, 0, 0, 0], [0, 2, 1], [1.0, -1.0, 1.0]), - ("QubitStateVector", [0, 1 / np.sqrt(2), 0, 1 / np.sqrt(2)], [1, 0], [-1.0, 0.0, 1.0]), - ("QubitStateVector", [0, 1 / np.sqrt(2), 0, 1 / np.sqrt(2)], [0, 1], [0.0, -1.0, 1.0]), + ("StatePrep", [1 / np.sqrt(2), 0, 0, 1 / np.sqrt(2)], [0, 2], [0.0, 1.0, 0.0]), + ("StatePrep", [1 / np.sqrt(2), 0, 0, 1 / np.sqrt(2)], [0, 1], [0.0, 0.0, 1.0]), + ("StatePrep", [0, 1, 0, 0, 0, 0, 0, 0], [2, 1, 0], [-1.0, 1.0, 1.0]), + ("StatePrep", [0, 1j, 0, 0, 0, 0, 0, 0], [0, 2, 1], [1.0, -1.0, 1.0]), + ("StatePrep", [0, 1 / np.sqrt(2), 0, 1 / np.sqrt(2)], [1, 0], [-1.0, 0.0, 1.0]), + ("StatePrep", [0, 1 / np.sqrt(2), 0, 1 / np.sqrt(2)], [0, 1], [0.0, -1.0, 1.0]), ], ) def test_state_vector_3_qubit_subset( @@ -1078,9 +1070,8 @@ def circuit(): ("ControlledPhaseShift", [math.pi], [-1 / 2, -1 / 2]), ], ) - @pytest.mark.parametrize("stateprep", [qml.QubitStateVector, qml.StatePrep]) def test_supported_gate_two_wires_with_parameters( - self, qubit_device, tol, stateprep, name, par, expected_output + self, qubit_device, tol, name, par, expected_output ): """Tests supported gates that act on two wires that are parameterized""" dev = qubit_device(wires=2) @@ -1091,7 +1082,7 @@ def test_supported_gate_two_wires_with_parameters( @qml.qnode(dev) def circuit(): - stateprep(np.array([1 / 2, 0, 0, math.sqrt(3) / 2]), wires=[0, 1]) + qml.StatePrep(np.array([1 / 2, 0, 0, math.sqrt(3) / 2]), wires=[0, 1]) op(*par, wires=[0, 1]) return qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliZ(1)) @@ -1118,9 +1109,8 @@ def circuit(): ("Hadamard", [1 / math.sqrt(2), 1 / math.sqrt(2)], 1 / math.sqrt(2)), ], ) - @pytest.mark.parametrize("stateprep", [qml.QubitStateVector, qml.StatePrep]) def test_supported_observable_single_wire_no_parameters( - self, qubit_device, tol, stateprep, name, state, expected_output + self, qubit_device, tol, name, state, expected_output ): """Tests supported observables on single wires without parameters.""" dev = qubit_device(wires=1) @@ -1131,7 +1121,7 @@ def test_supported_observable_single_wire_no_parameters( @qml.qnode(dev) def circuit(): - stateprep(np.array(state), wires=[0]) + qml.StatePrep(np.array(state), wires=[0]) return qml.expval(obs(wires=[0])) assert np.isclose(circuit(), expected_output, atol=tol, rtol=0) @@ -1148,9 +1138,8 @@ def circuit(): ("Identity", [1 / math.sqrt(2), -1 / math.sqrt(2)], 1, []), ], ) - @pytest.mark.parametrize("stateprep", [qml.QubitStateVector, qml.StatePrep]) def test_supported_observable_single_wire_with_parameters( - self, qubit_device, tol, stateprep, name, state, expected_output, par + self, qubit_device, tol, name, state, expected_output, par ): """Tests supported observables on single wires with parameters.""" dev = qubit_device(wires=1) @@ -1161,7 +1150,7 @@ def test_supported_observable_single_wire_with_parameters( @qml.qnode(dev) def circuit(): - stateprep(np.array(state), wires=[0]) + qml.StatePrep(np.array(state), wires=[0]) return qml.expval(obs(*par, wires=[0])) assert np.isclose(circuit(), expected_output, atol=tol, rtol=0) @@ -1395,7 +1384,6 @@ def circuit(): "op", [ qml.BasisState([0, 0], wires=[0, 1]), - qml.QubitStateVector([0, 1, 0, 0], wires=[0, 1]), qml.StatePrep([0, 1, 0, 0], wires=[0, 1]), ], ) diff --git a/tests/test_comparison.py b/tests/test_comparison.py index 5fbf3a037..47575e5f1 100644 --- a/tests/test_comparison.py +++ b/tests/test_comparison.py @@ -279,10 +279,7 @@ def circuit(measurement): ) @pytest.mark.parametrize("wires", range(1, 17)) @pytest.mark.parametrize("num_threads", [1, 2]) - @pytest.mark.parametrize("stateprep", [qml.QubitStateVector, qml.StatePrep]) - def test_n_qubit_circuit( - self, monkeypatch, stateprep, wires, lightning_dev_version, num_threads - ): + def test_n_qubit_circuit(self, monkeypatch, wires, lightning_dev_version, num_threads): """Test an n-qubit circuit""" monkeypatch.setenv("OMP_NUM_THREADS", str(num_threads)) @@ -294,7 +291,7 @@ def test_n_qubit_circuit( def circuit(measurement): """Prepares the equal superposition state and then applies StronglyEntanglingLayers and concludes with a simple PauliZ measurement""" - stateprep(vec, wires=range(wires)) + qml.StatePrep(vec, wires=range(wires)) qml.StronglyEntanglingLayers(w, wires=range(wires)) return measurement() if callable(measurement) else measurement diff --git a/tests/test_gates.py b/tests/test_gates.py index bc586cb9d..3433f2cee 100644 --- a/tests/test_gates.py +++ b/tests/test_gates.py @@ -92,7 +92,7 @@ def test_gate_unitary_correct(op, op_name): """Test if lightning device correctly applies gates by reconstructing the unitary matrix and comparing to the expected version""" - if op_name in ("BasisState", "QubitStateVector", "StatePrep"): + if op_name in ("BasisState", "StatePrep"): pytest.skip("Skipping operation because it is a state preparation") if op == None: pytest.skip("Skipping operation.") @@ -158,7 +158,7 @@ def test_gate_unitary_correct_lt(op, op_name): """Test if lightning device correctly applies gates by reconstructing the unitary matrix and comparing to the expected version""" - if op_name in ("BasisState", "QubitStateVector", "StatePrep"): + if op_name in ("BasisState", "StatePrep"): pytest.skip("Skipping operation because it is a state preparation") if op == None: pytest.skip("Skipping operation.") @@ -192,7 +192,7 @@ def test_inverse_unitary_correct(op, op_name): """Test if lightning device correctly applies inverse gates by reconstructing the unitary matrix and comparing to the expected version""" - if op_name in ("BasisState", "QubitStateVector", "StatePrep"): + if op_name in ("BasisState", "StatePrep"): pytest.skip("Skipping operation because it is a state preparation") if op == None: pytest.skip("Skipping operation.") diff --git a/tests/test_serialize.py b/tests/test_serialize.py index 76bb3b865..b6d192951 100644 --- a/tests/test_serialize.py +++ b/tests/test_serialize.py @@ -814,12 +814,11 @@ def test_multicontrolledx(self, wires_map): assert s == s_expected @pytest.mark.parametrize("wires_map", [wires_dict, None]) - @pytest.mark.parametrize("stateprep", [qml.QubitStateVector, qml.StatePrep]) - def test_skips_prep_circuit(self, stateprep, wires_map): + def test_skips_prep_circuit(self, wires_map): """Test expected serialization for a simple circuit with state preparation, such that the state preparation is skipped""" with qml.tape.QuantumTape() as tape: - stateprep([1, 0], wires=0) + qml.StatePrep([1, 0], wires=0) qml.BasisState([1], wires=1) qml.RX(0.4, wires=0) qml.RY(0.6, wires=1)