Skip to content

Commit

Permalink
Fix sparse_hamiltonianmpi_c and add getWires test.
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentmr committed Oct 23, 2023
1 parent dccfe3e commit a014177
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 33 deletions.
53 changes: 20 additions & 33 deletions pennylane_lightning/core/_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,26 +95,17 @@ def __init__(self, device_name, use_csingle: bool = False, use_mpi: bool = False

if use_mpi:
self.use_mpi = use_mpi
self.statevectormpi_c128 = lightning_ops.StateVectorMPIC128
self.named_obsmpi_c64 = lightning_ops.observablesMPI.NamedObsMPIC64
self.named_obsmpi_c128 = lightning_ops.observablesMPI.NamedObsMPIC128
self.hermitian_obsmpi_c64 = lightning_ops.observablesMPI.HermitianObsMPIC64
self.hermitian_obsmpi_c128 = lightning_ops.observablesMPI.HermitianObsMPIC128
self.tensor_prod_obsmpi_c64 = lightning_ops.observablesMPI.TensorProdObsMPIC64
self.tensor_prod_obsmpi_c128 = lightning_ops.observablesMPI.TensorProdObsMPIC128
self.hamiltonianmpi_c64 = lightning_ops.observablesMPI.HamiltonianMPIC64
self.hamiltonianmpi_c128 = lightning_ops.observablesMPI.HamiltonianMPIC128

if self.device_name == "lightning.gpu":
self.sparse_hamiltonianmpi_c64 = (
lightning_ops.observablesMPI.SparseHamiltonianMPIC64
)
self.sparse_hamiltonianmpi_c128 = (
lightning_ops.observablesMPI.SparseHamiltoniaMPInC128
)
else:
ValueError(f"{self.device_name} does not support MPI.")
self.mpi_manager = lightning_ops.MPIManager
self.statevector_mpi_c128 = lightning_ops.StateVectorMPIC128
self.named_obs_mpi_c64 = lightning_ops.observablesMPI.NamedObsMPIC64
self.named_obs_mpi_c128 = lightning_ops.observablesMPI.NamedObsMPIC128
self.hermitian_obs_mpi_c64 = lightning_ops.observablesMPI.HermitianObsMPIC64
self.hermitian_obs_mpi_c128 = lightning_ops.observablesMPI.HermitianObsMPIC128
self.tensor_prod_obs_mpi_c64 = lightning_ops.observablesMPI.TensorProdObsMPIC64
self.tensor_prod_obs_mpi_c128 = lightning_ops.observablesMPI.TensorProdObsMPIC128
self.hamiltonian_mpi_c64 = lightning_ops.observablesMPI.HamiltonianMPIC64
self.hamiltonian_mpi_c128 = lightning_ops.observablesMPI.HamiltonianMPIC128

Check warning on line 106 in pennylane_lightning/core/_serialize.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_serialize.py#L98-L106

Added lines #L98 - L106 were not covered by tests

self._mpi_manager = lightning_ops.MPIManager

Check warning on line 108 in pennylane_lightning/core/_serialize.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_serialize.py#L108

Added line #L108 was not covered by tests

@property
def ctype(self):
Expand All @@ -129,46 +120,42 @@ def rtype(self):
@property
def sv_type(self):
if self.use_mpi:
return self.statevectormpi_c128
return self.statevector_mpi_c128

Check warning on line 123 in pennylane_lightning/core/_serialize.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_serialize.py#L123

Added line #L123 was not covered by tests
return self.statevector_c128

@property
def named_obs(self):
"""Named observable matching ``use_csingle`` precision."""
if self.use_mpi:
return self.named_obsmpi_c64 if self.use_csingle else self.named_obsmpi_c128
return self.named_obs_mpi_c64 if self.use_csingle else self.named_obs_mpi_c128

Check warning on line 130 in pennylane_lightning/core/_serialize.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_serialize.py#L130

Added line #L130 was not covered by tests
return self.named_obs_c64 if self.use_csingle else self.named_obs_c128

@property
def hermitian_obs(self):
"""Hermitian observable matching ``use_csingle`` precision."""
if self.use_mpi:
return self.hermitian_obsmpi_c64 if self.use_csingle else self.hermitian_obsmpi_c128
return self.hermitian_obs_mpi_c64 if self.use_csingle else self.hermitian_obs_mpi_c128

Check warning on line 137 in pennylane_lightning/core/_serialize.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_serialize.py#L137

Added line #L137 was not covered by tests
return self.hermitian_obs_c64 if self.use_csingle else self.hermitian_obs_c128

@property
def tensor_obs(self):
"""Tensor product observable matching ``use_csingle`` precision."""
if self.use_mpi:
return self.tensor_prod_obsmpi_c64 if self.use_csingle else self.tensor_prod_obsmpi_c128
return (

Check warning on line 144 in pennylane_lightning/core/_serialize.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_serialize.py#L144

Added line #L144 was not covered by tests
self.tensor_prod_obs_mpi_c64 if self.use_csingle else self.tensor_prod_obs_mpi_c128
)
return self.tensor_prod_obs_c64 if self.use_csingle else self.tensor_prod_obs_c128

@property
def hamiltonian_obs(self):
"""Hamiltonian observable matching ``use_csingle`` precision."""
if self.use_mpi:
return self.hamiltonianmpi_c64 if self.use_csingle else self.hamiltonianmpi_c128
return self.hamiltonian_mpi_c64 if self.use_csingle else self.hamiltonian_mpi_c128

Check warning on line 153 in pennylane_lightning/core/_serialize.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_serialize.py#L153

Added line #L153 was not covered by tests
return self.hamiltonian_c64 if self.use_csingle else self.hamiltonian_c128

@property
def sparse_hamiltonian_obs(self):
"""SparseHamiltonian observable matching ``use_csingle`` precision."""
if self.use_mpi:
return (
self.sparse_hamiltonianmpi_c64
if self.use_csingle
else self.sparse_hamiltonianmpi_c128
)
return self.sparse_hamiltonian_c64 if self.use_csingle else self.sparse_hamiltonian_c128

def _named_obs(self, observable, wires_map: dict):
Expand Down Expand Up @@ -211,9 +198,9 @@ def _sparse_hamiltonian(self, observable, wires_map: dict):
H_sparse = SparseHamiltonian(Hmat, wires=range(1))

Check warning on line 198 in pennylane_lightning/core/_serialize.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_serialize.py#L197-L198

Added lines #L197 - L198 were not covered by tests
spm = H_sparse.sparse_matrix()
# Only root 0 needs the overall sparsematrix data
if self.mpi_manager().getRank() == 0:
if self._mpi_manager().getRank() == 0:

Check warning on line 201 in pennylane_lightning/core/_serialize.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_serialize.py#L201

Added line #L201 was not covered by tests
spm = observable.sparse_matrix()
self.mpi_manager().Barrier()
self._mpi_manager().Barrier()

Check warning on line 203 in pennylane_lightning/core/_serialize.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/core/_serialize.py#L203

Added line #L203 was not covered by tests
else:
spm = observable.sparse_matrix()
spm = observable.sparse_matrix()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,11 @@ template <typename TypeList> void testSparseHamiltonianBase() {
ComplexT{1.0, 0.0}, ComplexT{1.0, 0.0}},
{7, 6, 5, 4, 3, 2, 1, 0}, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {0, 1, 2});

DYNAMIC_SECTION("getWires - "
<< StateVectorToName<StateVectorT>::name) {
REQUIRE(sparseH->getWires() == std::vector<size_t>{0, 1, 2});
}

DYNAMIC_SECTION("getObsName - "
<< StateVectorToName<StateVectorT>::name) {
REQUIRE(sparseH->getObsName() ==
Expand Down

0 comments on commit a014177

Please sign in to comment.