Skip to content

Commit

Permalink
Merge branch 'master' into fix_adjops_jacobian
Browse files Browse the repository at this point in the history
  • Loading branch information
josephleekl authored Nov 14, 2024
2 parents 90ba867 + d473c61 commit 3f89c2e
Show file tree
Hide file tree
Showing 25 changed files with 69 additions and 105 deletions.
5 changes: 4 additions & 1 deletion .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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

---

Expand Down
1 change: 0 additions & 1 deletion doc/lightning_gpu/device.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ Supported operations and observables
~pennylane.PSWAP
~pennylane.QFT
~pennylane.QubitCarry
~pennylane.QubitStateVector
~pennylane.QubitSum
~pennylane.QubitUnitary
~pennylane.Rot
Expand Down
1 change: 0 additions & 1 deletion doc/lightning_kokkos/device.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ Supported operations and observables
~pennylane.PSWAP
~pennylane.QFT
~pennylane.QubitCarry
~pennylane.QubitStateVector
~pennylane.QubitSum
~pennylane.QubitUnitary
~pennylane.Rot
Expand Down
1 change: 0 additions & 1 deletion doc/lightning_qubit/device.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ Supported operations and observables
~pennylane.PSWAP
~pennylane.QFT
~pennylane.QubitCarry
~pennylane.QubitStateVector
~pennylane.QubitSum
~pennylane.QubitUnitary
~pennylane.Rot
Expand Down
1 change: 0 additions & 1 deletion doc/lightning_tensor/device.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 5 additions & 7 deletions mpitests/test_adjoint_jacobian.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,18 +165,17 @@ 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(
[0.43593284 - 0.02945156j, 0.40812291 + 0.80158023j], requires_grad=False
)

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],
)
Expand All @@ -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))],
Expand Down Expand Up @@ -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]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>;
using ComplexT = StateVectorT::ComplexT;
AdjointJacobian<StateVectorT> adj;
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>;

MPIManager mpi_manager(MPI_COMM_WORLD);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 0 additions & 1 deletion pennylane_lightning/lightning_gpu/lightning_gpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
_operations = frozenset(
{
"Identity",
"QubitStateVector",
"QubitUnitary",
"ControlledQubitUnitary",
"MultiControlledX",
Expand Down
1 change: 0 additions & 1 deletion pennylane_lightning/lightning_gpu/lightning_gpu.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ GlobalPhase = { properties = [ "invertible", "controllable", "differe
[operators.gates.decomp]

BasisState = {}
QubitStateVector = {}
StatePrep = {}
MultiControlledX = {}

Expand Down
1 change: 0 additions & 1 deletion pennylane_lightning/lightning_kokkos/lightning_kokkos.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
_operations = frozenset(
{
"Identity",
"QubitStateVector",
"QubitUnitary",
"ControlledQubitUnitary",
"MultiControlledX",
Expand Down
1 change: 0 additions & 1 deletion pennylane_lightning/lightning_kokkos/lightning_kokkos.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ T = { properties = [ "invertible", "differe

BasisState = {}
MultiControlledX = {}
QubitStateVector = {}
StatePrep = {}
ControlledQubitUnitary = {}

Expand Down
2 changes: 1 addition & 1 deletion tests/lightning_qubit/test_adjoint_jacobian_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
Expand Down
10 changes: 5 additions & 5 deletions tests/lightning_qubit/test_measurements_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 0 additions & 3 deletions tests/lightning_qubit/test_state_vector_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
(
Expand Down Expand Up @@ -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)],
Expand Down
12 changes: 5 additions & 7 deletions tests/test_adjoint_jacobian.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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))

Expand Down Expand Up @@ -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]))
Expand Down
Loading

0 comments on commit 3f89c2e

Please sign in to comment.