From f7e456e6e7e48a709a37fbfd270be1350592e674 Mon Sep 17 00:00:00 2001 From: andrijapau Date: Wed, 6 Nov 2024 16:56:19 -0500 Subject: [PATCH 01/10] initial removals --- doc/lightning_gpu/device.rst | 1 - doc/lightning_kokkos/device.rst | 1 - doc/lightning_qubit/device.rst | 1 - doc/lightning_tensor/device.rst | 1 - mpitests/test_adjoint_jacobian.py | 5 +- .../algorithms/AdjointJacobianGPU.hpp | 3 +- .../algorithms/AdjointJacobianGPUMPI.hpp | 6 +- .../tests/Test_AdjointJacobianGPU.cpp | 6 +- .../tests/mpi/Test_AdjointJacobianGPUMPI.cpp | 4 +- .../algorithms/AdjointJacobianKokkos.hpp | 3 +- .../algorithms/AdjointJacobianLQubit.hpp | 3 +- .../algorithms/VectorJacobianProduct.hpp | 3 +- .../lightning_gpu/lightning_gpu.py | 1 - .../lightning_gpu/lightning_gpu.toml | 1 - .../lightning_kokkos/lightning_kokkos.py | 1 - .../lightning_kokkos/lightning_kokkos.toml | 1 - .../test_adjoint_jacobian_class.py | 1 - .../test_measurements_class.py | 26 ------ .../test_state_vector_class.py | 3 - tests/test_adjoint_jacobian.py | 5 +- tests/test_apply.py | 81 ++----------------- tests/test_comparison.py | 2 +- tests/test_gates.py | 6 +- tests/test_serialize.py | 2 +- 24 files changed, 27 insertions(+), 140 deletions(-) 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..ece8f4bf8 100644 --- a/mpitests/test_adjoint_jacobian.py +++ b/mpitests/test_adjoint_jacobian.py @@ -165,7 +165,7 @@ 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("stateprep", qml.StatePrep) @pytest.mark.parametrize("batch_obs", [True, False]) def test_pauli_rotation_gradient( self, stateprep, G, theta, batch_obs, dev @@ -192,7 +192,7 @@ 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("stateprep", qml.StatePrep) @pytest.mark.parametrize("batch_obs", [True, False]) def test_Rot_gradient(self, stateprep, theta, batch_obs, dev): """Tests that the device gradient of an arbitrary Euler-angle-parameterized gate is @@ -761,7 +761,6 @@ 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.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 976bdc71b..5e4e1861f 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 ccdcedaea..09bab6672 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,11 @@ 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"); + GENERATE("StatePrep", "BasisState"); using StateVectorT = StateVectorCudaManaged; using ComplexT = StateVectorT::ComplexT; AdjointJacobian adj; @@ -124,7 +124,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..b79b44ee3 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,11 @@ 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"); + 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 7dbf02ce2..b22675bbd 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 d4a437375..27bfdaee1 100644 --- a/pennylane_lightning/lightning_gpu/lightning_gpu.toml +++ b/pennylane_lightning/lightning_gpu/lightning_gpu.toml @@ -49,7 +49,6 @@ GlobalPhase = { properties = [ "invertible", "differentiable" ] } [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 42a27e969..115aa9caf 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 7fbaa19b1..746c846e2 100644 --- a/tests/lightning_qubit/test_adjoint_jacobian_class.py +++ b/tests/lightning_qubit/test_adjoint_jacobian_class.py @@ -325,7 +325,6 @@ 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.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 9a606e66a..761bc53fd 100644 --- a/tests/lightning_qubit/test_measurements_class.py +++ b/tests/lightning_qubit/test_measurements_class.py @@ -952,29 +952,3 @@ def test_sum(self, phi, theta, lightning_sv, tol): expected = np.cos(phi) + np.sin(theta) assert np.allclose(result, expected, 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]), - ], -) -def test_state_vector_2_qubit_subset(tol, op, par, wires, expected, lightning_sv): - """Tests qubit state vector preparation and measure on subsets of 2 qubits""" - - tape = qml.tape.QuantumScript( - [op(par, wires=wires)], [qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliZ(1))] - ) - - statevector = lightning_sv(2) - statevector = get_final_state(statevector, tape) - - m = LightningMeasurements(statevector) - result = measure_final_state(m, tape) - - assert np.allclose(result, expected, tol) 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 83713b1c6..1ef68f98a 100644 --- a/tests/test_adjoint_jacobian.py +++ b/tests/test_adjoint_jacobian.py @@ -220,7 +220,7 @@ 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]) + @pytest.mark.parametrize("stateprep", qml.StatePrep) def test_pauli_rotation_gradient(self, stateprep, G, theta, dev): """Tests that the automatic gradients of Pauli rotations are correct.""" random_state = np.array( @@ -244,7 +244,7 @@ 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]) + @pytest.mark.parametrize("stateprep", qml.StatePrep) def test_Rot_gradient(self, stateprep, theta, dev): """Tests that the device gradient of an arbitrary Euler-angle-parameterized gate is correct.""" @@ -1081,7 +1081,6 @@ 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.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..836e2016d 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,7 +477,7 @@ def test_apply_operation_preserve_pointer_two_wires_with_parameters( assert pointer_before == pointer_after - @pytest.mark.parametrize("stateprep", [qml.QubitStateVector, qml.StatePrep]) + @pytest.mark.parametrize("stateprep", qml.StatePrep) def test_apply_errors_qubit_state_vector(self, stateprep, qubit_device): """Test that apply fails for incorrect state preparation, and > 2 qubit gates""" dev = qubit_device(wires=2) @@ -549,7 +547,7 @@ class TestExpval: (qml.Identity, [1 / math.sqrt(2), -1 / math.sqrt(2)], 1), ], ) - @pytest.mark.parametrize("stateprep", [qml.QubitStateVector, qml.StatePrep]) + @pytest.mark.parametrize("stateprep", qml.StatePrep) def test_expval_single_wire_no_parameters( self, qubit_device, tol, stateprep, operation, input, expected_output ): @@ -608,7 +606,7 @@ class TestVar: (qml.Identity, [1 / math.sqrt(2), -1 / math.sqrt(2)], 0), ], ) - @pytest.mark.parametrize("stateprep", [qml.QubitStateVector, qml.StatePrep]) + @pytest.mark.parametrize("stateprep", qml.StatePrep) def test_var_single_wire_no_parameters( self, qubit_device, tol, stateprep, operation, input, expected_output ): @@ -842,7 +840,7 @@ def circuit(): ("CZ", [-1 / 2, -1 / 2]), ], ) - @pytest.mark.parametrize("stateprep", [qml.QubitStateVector, qml.StatePrep]) + @pytest.mark.parametrize("stateprep", qml.StatePrep) def test_supported_gate_two_wires_no_parameters( self, qubit_device, tol, stateprep, name, expected_output ): @@ -892,9 +890,6 @@ 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]), ], ) def test_supported_state_preparation(self, qubit_device, tol, name, par, expected_output): @@ -951,67 +946,6 @@ def circuit(): assert np.allclose(circuit(), expected_output, atol=tol, rtol=0) - # This test is run with two expvals - @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]), - ], - ) - def test_state_vector_2_qubit_subset( - self, qubit_device, tol, name, par, wires, expected_output - ): - """Tests qubit state vector preparation on subsets of 2 qubits""" - dev = qubit_device(wires=2) - op = getattr(qml.ops, name) - - par = np.array(par) - - @qml.qnode(dev) - def circuit(): - op(par, wires=wires) - return qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliZ(1)) - - assert np.allclose(circuit(), expected_output, atol=tol, rtol=0) - - # This test is run with three expvals - @pytest.mark.parametrize( - "name,par,wires,expected_output", - [ - ( - "QubitStateVector", - [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]), - ], - ) - def test_state_vector_3_qubit_subset( - self, qubit_device, tol, name, par, wires, expected_output - ): - """Tests qubit state vector preparation on subsets of 3 qubits""" - dev = qubit_device(wires=3) - op = getattr(qml.ops, name) - - par = np.array(par) - - @qml.qnode(dev) - def circuit(): - op(par, wires=wires) - return qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliZ(1)), qml.expval(qml.PauliZ(2)) - - assert np.allclose(circuit(), expected_output, atol=tol, rtol=0) - # This test is ran on the state |0> with one Z expvals @pytest.mark.skipif( device_name == "lightning.tensor", @@ -1078,7 +1012,7 @@ def circuit(): ("ControlledPhaseShift", [math.pi], [-1 / 2, -1 / 2]), ], ) - @pytest.mark.parametrize("stateprep", [qml.QubitStateVector, qml.StatePrep]) + @pytest.mark.parametrize("stateprep", qml.StatePrep) def test_supported_gate_two_wires_with_parameters( self, qubit_device, tol, stateprep, name, par, expected_output ): @@ -1118,7 +1052,7 @@ def circuit(): ("Hadamard", [1 / math.sqrt(2), 1 / math.sqrt(2)], 1 / math.sqrt(2)), ], ) - @pytest.mark.parametrize("stateprep", [qml.QubitStateVector, qml.StatePrep]) + @pytest.mark.parametrize("stateprep", qml.StatePrep) def test_supported_observable_single_wire_no_parameters( self, qubit_device, tol, stateprep, name, state, expected_output ): @@ -1148,7 +1082,7 @@ def circuit(): ("Identity", [1 / math.sqrt(2), -1 / math.sqrt(2)], 1, []), ], ) - @pytest.mark.parametrize("stateprep", [qml.QubitStateVector, qml.StatePrep]) + @pytest.mark.parametrize("stateprep", qml.StatePrep) def test_supported_observable_single_wire_with_parameters( self, qubit_device, tol, stateprep, name, state, expected_output, par ): @@ -1395,7 +1329,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..4cc6ae81b 100644 --- a/tests/test_comparison.py +++ b/tests/test_comparison.py @@ -279,7 +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]) + @pytest.mark.parametrize("stateprep", qml.StatePrep) def test_n_qubit_circuit( self, monkeypatch, stateprep, wires, lightning_dev_version, num_threads ): diff --git a/tests/test_gates.py b/tests/test_gates.py index c3d361836..08f59a017 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 6860c1941..921595c4e 100644 --- a/tests/test_serialize.py +++ b/tests/test_serialize.py @@ -816,7 +816,7 @@ 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]) + @pytest.mark.parametrize("stateprep", qml.StatePrep) def test_skips_prep_circuit(self, stateprep, wires_map): """Test expected serialization for a simple circuit with state preparation, such that the state preparation is skipped""" From 601ca93ced3b86c7d493c3dd848998529308923a Mon Sep 17 00:00:00 2001 From: andrijapau Date: Thu, 7 Nov 2024 10:27:34 -0500 Subject: [PATCH 02/10] address Lillian's comments --- mpitests/test_adjoint_jacobian.py | 11 ++---- .../test_adjoint_jacobian_class.py | 1 + .../test_measurements_class.py | 26 +++++++++++++ tests/test_adjoint_jacobian.py | 10 ++--- tests/test_apply.py | 39 ++++++++----------- tests/test_comparison.py | 5 +-- tests/test_serialize.py | 5 +-- 7 files changed, 55 insertions(+), 42 deletions(-) diff --git a/mpitests/test_adjoint_jacobian.py b/mpitests/test_adjoint_jacobian.py index ece8f4bf8..3f6c3a08e 100644 --- a/mpitests/test_adjoint_jacobian.py +++ b/mpitests/test_adjoint_jacobian.py @@ -28,7 +28,6 @@ from pennylane import qnode from pennylane.devices import ExecutionConfig from pennylane.tape import QuantumScript -from scipy.stats import unitary_group from pennylane_lightning.lightning_gpu_ops import LightningException @@ -165,10 +164,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.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 +174,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 +190,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.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))], diff --git a/tests/lightning_qubit/test_adjoint_jacobian_class.py b/tests/lightning_qubit/test_adjoint_jacobian_class.py index 746c846e2..c98c73075 100644 --- a/tests/lightning_qubit/test_adjoint_jacobian_class.py +++ b/tests/lightning_qubit/test_adjoint_jacobian_class.py @@ -325,6 +325,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.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 761bc53fd..37f0c91a7 100644 --- a/tests/lightning_qubit/test_measurements_class.py +++ b/tests/lightning_qubit/test_measurements_class.py @@ -952,3 +952,29 @@ def test_sum(self, phi, theta, lightning_sv, tol): expected = np.cos(phi) + np.sin(theta) assert np.allclose(result, expected, tol) + + +@pytest.mark.parametrize( + "op,par,wires,expected", + [ + (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): + """Tests qubit state vector preparation and measure on subsets of 2 qubits""" + + tape = qml.tape.QuantumScript( + [op(par, wires=wires)], [qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliZ(1))] + ) + + statevector = lightning_sv(2) + statevector = get_final_state(statevector, tape) + + m = LightningMeasurements(statevector) + result = measure_final_state(m, tape) + + assert np.allclose(result, expected, tol) \ No newline at end of file diff --git a/tests/test_adjoint_jacobian.py b/tests/test_adjoint_jacobian.py index 1ef68f98a..2c5eb51f8 100644 --- a/tests/test_adjoint_jacobian.py +++ b/tests/test_adjoint_jacobian.py @@ -220,15 +220,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.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} @@ -244,14 +243,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.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)) diff --git a/tests/test_apply.py b/tests/test_apply.py index 836e2016d..65b3abea9 100644 --- a/tests/test_apply.py +++ b/tests/test_apply.py @@ -477,19 +477,18 @@ def test_apply_operation_preserve_pointer_two_wires_with_parameters( assert pointer_before == pointer_after - @pytest.mark.parametrize("stateprep", 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) @@ -547,14 +546,13 @@ class TestExpval: (qml.Identity, [1 / math.sqrt(2), -1 / math.sqrt(2)], 1), ], ) - @pytest.mark.parametrize("stateprep", 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) @@ -606,14 +604,13 @@ class TestVar: (qml.Identity, [1 / math.sqrt(2), -1 / math.sqrt(2)], 0), ], ) - @pytest.mark.parametrize("stateprep", 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) @@ -840,9 +837,8 @@ def circuit(): ("CZ", [-1 / 2, -1 / 2]), ], ) - @pytest.mark.parametrize("stateprep", qml.StatePrep) def test_supported_gate_two_wires_no_parameters( - self, qubit_device, tol, stateprep, name, expected_output + self, qubit_device, tol, name, expected_output ): """Tests supported gates that act on two wires that are not parameterized""" dev = qubit_device(wires=2) @@ -853,7 +849,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)) @@ -1012,9 +1008,8 @@ def circuit(): ("ControlledPhaseShift", [math.pi], [-1 / 2, -1 / 2]), ], ) - @pytest.mark.parametrize("stateprep", 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) @@ -1025,7 +1020,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)) @@ -1052,9 +1047,8 @@ def circuit(): ("Hadamard", [1 / math.sqrt(2), 1 / math.sqrt(2)], 1 / math.sqrt(2)), ], ) - @pytest.mark.parametrize("stateprep", 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) @@ -1065,7 +1059,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) @@ -1082,9 +1076,8 @@ def circuit(): ("Identity", [1 / math.sqrt(2), -1 / math.sqrt(2)], 1, []), ], ) - @pytest.mark.parametrize("stateprep", 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) @@ -1095,7 +1088,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) diff --git a/tests/test_comparison.py b/tests/test_comparison.py index 4cc6ae81b..bd1a30155 100644 --- a/tests/test_comparison.py +++ b/tests/test_comparison.py @@ -279,9 +279,8 @@ def circuit(measurement): ) @pytest.mark.parametrize("wires", range(1, 17)) @pytest.mark.parametrize("num_threads", [1, 2]) - @pytest.mark.parametrize("stateprep", qml.StatePrep) def test_n_qubit_circuit( - self, monkeypatch, stateprep, wires, lightning_dev_version, num_threads + self, monkeypatch, wires, lightning_dev_version, num_threads ): """Test an n-qubit circuit""" @@ -294,7 +293,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_serialize.py b/tests/test_serialize.py index 921595c4e..88a0bc9f9 100644 --- a/tests/test_serialize.py +++ b/tests/test_serialize.py @@ -816,12 +816,11 @@ def test_multicontrolledx(self, wires_map): assert s == s_expected @pytest.mark.parametrize("wires_map", [wires_dict, None]) - @pytest.mark.parametrize("stateprep", 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) From 5008f455578cae3bf64d14ade9b1ed89582b794a Mon Sep 17 00:00:00 2001 From: ringo-but-quantum Date: Thu, 7 Nov 2024 15:27:59 +0000 Subject: [PATCH 03/10] Auto update version from '0.40.0-dev1' to '0.40.0-dev4' --- pennylane_lightning/core/_version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pennylane_lightning/core/_version.py b/pennylane_lightning/core/_version.py index 43d577857..8432c4f82 100644 --- a/pennylane_lightning/core/_version.py +++ b/pennylane_lightning/core/_version.py @@ -16,4 +16,4 @@ Version number (major.minor.patch[-label]) """ -__version__ = "0.40.0-dev1" +__version__ = "0.40.0-dev4" From b97f8b09694b2a140e4748f7b9e7c78bfad2b46f Mon Sep 17 00:00:00 2001 From: andrijapau Date: Thu, 7 Nov 2024 10:30:51 -0500 Subject: [PATCH 04/10] add back stateprep --- mpitests/test_adjoint_jacobian.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mpitests/test_adjoint_jacobian.py b/mpitests/test_adjoint_jacobian.py index 3f6c3a08e..b1c768aba 100644 --- a/mpitests/test_adjoint_jacobian.py +++ b/mpitests/test_adjoint_jacobian.py @@ -28,6 +28,7 @@ from pennylane import qnode from pennylane.devices import ExecutionConfig from pennylane.tape import QuantumScript +from scipy.stats import unitary_group from pennylane_lightning.lightning_gpu_ops import LightningException @@ -758,6 +759,7 @@ def f(params1, params2): def circuit_ansatz(params, wires): """Circuit ansatz containing all the parametrized gates""" + 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])) From 06098289b69d96d1db61a0371fd54b7ac300f35c Mon Sep 17 00:00:00 2001 From: andrijapau Date: Thu, 7 Nov 2024 10:40:13 -0500 Subject: [PATCH 05/10] add back stateprep --- tests/test_apply.py | 64 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/tests/test_apply.py b/tests/test_apply.py index 65b3abea9..43f4fae20 100644 --- a/tests/test_apply.py +++ b/tests/test_apply.py @@ -886,6 +886,9 @@ def circuit(): ("BasisState", [0, 0], [1, 1]), ("BasisState", [1, 0], [-1, 1]), ("BasisState", [0, 1], [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): @@ -942,6 +945,67 @@ def circuit(): assert np.allclose(circuit(), expected_output, atol=tol, rtol=0) + # This test is run with two expvals + @pytest.mark.parametrize( + "name,par,wires,expected_output", + [ + ("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( + self, qubit_device, tol, name, par, wires, expected_output + ): + """Tests qubit state vector preparation on subsets of 2 qubits""" + dev = qubit_device(wires=2) + op = getattr(qml.ops, name) + + par = np.array(par) + + @qml.qnode(dev) + def circuit(): + op(par, wires=wires) + return qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliZ(1)) + + assert np.allclose(circuit(), expected_output, atol=tol, rtol=0) + + # This test is run with three expvals + @pytest.mark.parametrize( + "name,par,wires,expected_output", + [ + ( + "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], + ), + ("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( + self, qubit_device, tol, name, par, wires, expected_output + ): + """Tests qubit state vector preparation on subsets of 3 qubits""" + dev = qubit_device(wires=3) + op = getattr(qml.ops, name) + + par = np.array(par) + + @qml.qnode(dev) + def circuit(): + op(par, wires=wires) + return qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliZ(1)), qml.expval(qml.PauliZ(2)) + + assert np.allclose(circuit(), expected_output, atol=tol, rtol=0) + # This test is ran on the state |0> with one Z expvals @pytest.mark.skipif( device_name == "lightning.tensor", From e0976f0f78218b4777c482524b7747fd3f4ad97b Mon Sep 17 00:00:00 2001 From: andrijapau Date: Thu, 7 Nov 2024 10:41:34 -0500 Subject: [PATCH 06/10] add back newline --- tests/lightning_qubit/test_measurements_class.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/lightning_qubit/test_measurements_class.py b/tests/lightning_qubit/test_measurements_class.py index 37f0c91a7..dca5645e3 100644 --- a/tests/lightning_qubit/test_measurements_class.py +++ b/tests/lightning_qubit/test_measurements_class.py @@ -977,4 +977,4 @@ def test_state_vector_2_qubit_subset(tol, op, par, wires, expected, lightning_sv m = LightningMeasurements(statevector) result = measure_final_state(m, tape) - assert np.allclose(result, expected, tol) \ No newline at end of file + assert np.allclose(result, expected, tol) From d1828451b6878950de0af578127ddbcf5e481f5e Mon Sep 17 00:00:00 2001 From: andrijapau Date: Thu, 7 Nov 2024 10:42:32 -0500 Subject: [PATCH 07/10] add back stateprep --- tests/test_adjoint_jacobian.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_adjoint_jacobian.py b/tests/test_adjoint_jacobian.py index 2c5eb51f8..a7dc1e7cc 100644 --- a/tests/test_adjoint_jacobian.py +++ b/tests/test_adjoint_jacobian.py @@ -1079,6 +1079,7 @@ def f(params1, params2): def circuit_ansatz(params, wires): """Circuit ansatz containing all the parametrized gates""" + 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])) From 5a4d5475fdeff0c9e4edbdf1721bb749ad882043 Mon Sep 17 00:00:00 2001 From: andrijapau Date: Thu, 7 Nov 2024 15:40:00 -0500 Subject: [PATCH 08/10] ran make format --- .../algorithms/tests/Test_AdjointJacobianGPU.cpp | 3 +-- .../algorithms/tests/mpi/Test_AdjointJacobianGPUMPI.cpp | 3 +-- tests/test_apply.py | 4 +--- tests/test_comparison.py | 4 +--- 4 files changed, 4 insertions(+), 10 deletions(-) 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 09bab6672..e1170d81c 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 @@ -94,8 +94,7 @@ TEST_CASE("AdjointJacobianGPU::adjointJacobian Op=RY, Obs=X", TEST_CASE("AdjointJacobianGPU::adjointJacobian Op=[" "StatePrep, BasisState], Obs=[Z,Z]", "[AdjointJacobianGPU]") { - const std::string test_ops = - GENERATE("StatePrep", "BasisState"); + const std::string test_ops = GENERATE("StatePrep", "BasisState"); using StateVectorT = StateVectorCudaManaged; using ComplexT = StateVectorT::ComplexT; AdjointJacobian adj; 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 b79b44ee3..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 @@ -110,8 +110,7 @@ TEST_CASE("AdjointJacobianGPUMPI::adjointJacobianMPI Op=RX, Obs=[Z,Z]", TEST_CASE("AdjointJacobianGPUMPI::adjointJacobianMPI Op=[" "StatePrep, BasisState], Obs=[Z,Z]", "[AdjointJacobianGPUMPI]") { - const std::string test_ops = - GENERATE("StatePrep", "BasisState"); + const std::string test_ops = GENERATE("StatePrep", "BasisState"); using StateVectorT = StateVectorCudaMPI; MPIManager mpi_manager(MPI_COMM_WORLD); diff --git a/tests/test_apply.py b/tests/test_apply.py index 43f4fae20..927058d01 100644 --- a/tests/test_apply.py +++ b/tests/test_apply.py @@ -837,9 +837,7 @@ def circuit(): ("CZ", [-1 / 2, -1 / 2]), ], ) - def test_supported_gate_two_wires_no_parameters( - self, qubit_device, tol, 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) diff --git a/tests/test_comparison.py b/tests/test_comparison.py index bd1a30155..47575e5f1 100644 --- a/tests/test_comparison.py +++ b/tests/test_comparison.py @@ -279,9 +279,7 @@ def circuit(measurement): ) @pytest.mark.parametrize("wires", range(1, 17)) @pytest.mark.parametrize("num_threads", [1, 2]) - def test_n_qubit_circuit( - self, monkeypatch, 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)) From 4305888c593c14882723116c707c15c5ba6c5d93 Mon Sep 17 00:00:00 2001 From: andrijapau Date: Thu, 7 Nov 2024 15:42:56 -0500 Subject: [PATCH 09/10] add changelog --- .github/CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md index 423c10879..10fae8cbe 100644 --- a/.github/CHANGELOG.md +++ b/.github/CHANGELOG.md @@ -7,6 +7,9 @@ ### Breaking changes +* Removed all instances of `qml.QubitStateVector`. + [(#985)](https://github.com/PennyLaneAI/pennylane-lightning/pull/985) + ### Improvements * Update the `lightning.kokkos` CUDA backend for compatibility with Catalyst. @@ -26,7 +29,7 @@ This release contains contributions from (in alphabetical order): -Ali Asadi, Luis Alfredo Nuñez Meneses, Shuli Shu +Ali Asadi, Luis Alfredo Nuñez Meneses, Andrija Paurevic, Shuli Shu --- From 093413b6b15b3baedba7ce09a7dff3af9b15c3cb Mon Sep 17 00:00:00 2001 From: ringo-but-quantum Date: Thu, 14 Nov 2024 17:03:45 +0000 Subject: [PATCH 10/10] Auto update version from '0.40.0-dev8' to '0.40.0-dev9' --- pennylane_lightning/core/_version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pennylane_lightning/core/_version.py b/pennylane_lightning/core/_version.py index 726b770e3..c181bc10d 100644 --- a/pennylane_lightning/core/_version.py +++ b/pennylane_lightning/core/_version.py @@ -17,4 +17,4 @@ """ -__version__ = "0.40.0-dev8" +__version__ = "0.40.0-dev9"