Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lightning qubit2 add measurements #627

Merged
merged 121 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
121 commits
Select commit Hold shift + click to select a range
9fdd4c4
lightning interface for new device api
albi3ro Feb 2, 2024
50c3bae
Auto update version
github-actions[bot] Feb 2, 2024
6ac4b89
Merge Master
AmintorDusko Feb 13, 2024
af41a63
update version
AmintorDusko Feb 13, 2024
17a4fb0
update version
AmintorDusko Feb 13, 2024
7d9c048
update lightning_qubit_2
AmintorDusko Feb 13, 2024
67b11c5
add LightningQubit2 to init
AmintorDusko Feb 13, 2024
6e82a1a
add LightningStateVector class
AmintorDusko Feb 13, 2024
0df4601
add LightningMeasurements class
AmintorDusko Feb 13, 2024
79e4d64
add new QuantumScriptSerializer class
AmintorDusko Feb 13, 2024
194696a
allow lightning.qubit2 to be tested within our suite
AmintorDusko Feb 13, 2024
ecf1bc6
add tests and CI workflow for lightning_qubit_2
AmintorDusko Feb 13, 2024
0460ae2
update CI
AmintorDusko Feb 13, 2024
1358706
update CI
AmintorDusko Feb 13, 2024
ba94149
Merge branch 'master' into lightning-qubit2-interface
albi3ro Feb 13, 2024
b975d03
Merge branch 'lightning-qubit2-interface' into lightning-qubit2-add-s…
albi3ro Feb 13, 2024
8c414db
add wire mapping, black
albi3ro Feb 13, 2024
24e4fa1
Merge branch 'lightning-qubit2-interface' into lightning-qubit2-add-s…
albi3ro Feb 13, 2024
695f4b6
add tests for custom wires
AmintorDusko Feb 13, 2024
91777b3
add tests for custom wires
AmintorDusko Feb 13, 2024
6d98910
add review suggestions
AmintorDusko Feb 13, 2024
a4147e9
format
AmintorDusko Feb 13, 2024
d784b5f
remove python class to reverse order of PRs
albi3ro Feb 16, 2024
c6f83ec
Merge branch 'master' into lightning-qubit2-add-simulate
AmintorDusko Feb 16, 2024
87779ea
update simulate to get a LightningStateVector
AmintorDusko Feb 16, 2024
9bbff30
add reset state
AmintorDusko Feb 16, 2024
b4ed92b
update simulate
AmintorDusko Feb 16, 2024
16cd1f8
update docs
AmintorDusko Feb 16, 2024
cd2828a
add Result import
AmintorDusko Feb 16, 2024
a4350b5
Update pennylane_lightning/lightning_qubit/_measurements.py
AmintorDusko Feb 21, 2024
80de995
Update pennylane_lightning/lightning_qubit/_measurements.py
AmintorDusko Feb 21, 2024
dc3149f
fix reset state
AmintorDusko Feb 21, 2024
378e376
Update pennylane_lightning/lightning_qubit/_state_vector.py
AmintorDusko Feb 21, 2024
7a5c010
Update pennylane_lightning/lightning_qubit/_state_vector.py
AmintorDusko Feb 21, 2024
b23eabf
Update pennylane_lightning/lightning_qubit/_state_vector.py
AmintorDusko Feb 21, 2024
112d2ff
remove LightningQubit2 references
AmintorDusko Feb 21, 2024
1a8fe19
remove unnecessary modules
AmintorDusko Feb 21, 2024
9ded798
merging Serializer classes
AmintorDusko Feb 21, 2024
2e43fd6
update serialize tests
AmintorDusko Feb 21, 2024
f80c363
update measurements with new serialize class
AmintorDusko Feb 21, 2024
4f4b29e
remove outdated test
AmintorDusko Feb 21, 2024
1f78bef
remove obsolete tests
AmintorDusko Feb 22, 2024
b7ccb84
remove unused dtype input from simulate
AmintorDusko Feb 22, 2024
58032ef
update measurements
AmintorDusko Feb 22, 2024
a3b696e
update state_vector
AmintorDusko Feb 22, 2024
cd7c67c
update lightning_qubit2
AmintorDusko Feb 22, 2024
910246b
format
AmintorDusko Feb 22, 2024
af6cfef
pylint
AmintorDusko Feb 22, 2024
dd31965
Update pennylane_lightning/lightning_qubit/_state_vector.py
AmintorDusko Feb 22, 2024
44bc987
remove old comment
AmintorDusko Feb 22, 2024
5a5b20a
some review suggestions
AmintorDusko Feb 22, 2024
dcdd9ff
Merge branch 'master' into lightning-qubit2-add-simulate
AmintorDusko Feb 22, 2024
a64cf78
Auto update version
github-actions[bot] Feb 22, 2024
ad1e3a4
remove print
AmintorDusko Feb 22, 2024
aec5c07
update state vector class
AmintorDusko Feb 22, 2024
286fd4c
add state vector class tests
AmintorDusko Feb 22, 2024
4ee6609
adding measurement tests
albi3ro Feb 22, 2024
c262650
Merge branch 'lightning-qubit2-add-simulate' of https://github.com/Pe…
albi3ro Feb 22, 2024
e8b606e
update state vector and tests
AmintorDusko Feb 23, 2024
7ab3ade
move and rename test files, and format
AmintorDusko Feb 23, 2024
36c6b79
Merge branch 'master' into lightning-qubit2-add-simulate
AmintorDusko Feb 23, 2024
9aa3e35
Auto update version
github-actions[bot] Feb 23, 2024
b9fa21d
skip measurements class for other devices and in the absence of binaries
AmintorDusko Feb 23, 2024
48c6724
format
AmintorDusko Feb 23, 2024
a716611
update measurements class
AmintorDusko Feb 23, 2024
261cb6d
expand measurement class testing
AmintorDusko Feb 23, 2024
aca8468
garbage collection
AmintorDusko Feb 23, 2024
80e3494
typo
AmintorDusko Feb 23, 2024
3aa71fb
Merge branch 'master' into lightning-qubit2-add-simulate
AmintorDusko Feb 26, 2024
a284d78
update coverage and StateVector class
AmintorDusko Feb 26, 2024
43f72f6
expand measurements class coverage
AmintorDusko Feb 26, 2024
2a973d9
Auto update version
github-actions[bot] Feb 26, 2024
f42f298
add coverage for n-controlled operations
AmintorDusko Feb 27, 2024
9cd5471
add map to standard wires to get_final_state for safety
AmintorDusko Feb 27, 2024
cd3096c
update jax config import
AmintorDusko Feb 27, 2024
099e180
Merge branch 'master' into lightning-qubit2-add-simulate
AmintorDusko Feb 27, 2024
98db9db
Auto update version
github-actions[bot] Feb 27, 2024
4f746d0
trigger CI
AmintorDusko Feb 27, 2024
0395945
update state vector class and tests for improved coverage
AmintorDusko Feb 28, 2024
4a863ba
update measurement class tests
AmintorDusko Feb 28, 2024
4374680
Merge branch 'master' into lightning-qubit2-add-simulate
AmintorDusko Feb 28, 2024
4b44e4f
update dev version
AmintorDusko Feb 28, 2024
62c43f0
remove device definition
AmintorDusko Feb 28, 2024
7956c16
Merge branch 'master' into lightning-qubit2-add-simulate
AmintorDusko Feb 28, 2024
2d26288
update dev version
AmintorDusko Feb 28, 2024
8a9b9a5
clean test_measurements_class.py
AmintorDusko Feb 29, 2024
36d810e
isort+black
AmintorDusko Feb 29, 2024
816955f
review suggestion
AmintorDusko Feb 29, 2024
940644c
fix docs
AmintorDusko Feb 29, 2024
3922c32
Add qml.var support.
vincentmr Mar 4, 2024
140654a
Merge branch 'master' into lightning-qubit2-add-simulate
AmintorDusko Mar 4, 2024
5b31b07
Add probs support.
vincentmr Mar 5, 2024
b54d3ea
increase tolerance
AmintorDusko Mar 5, 2024
e1e9c05
Merge branch 'master' into lightning-qubit2-add-simulate
AmintorDusko Mar 5, 2024
3703b94
Auto update version
github-actions[bot] Mar 5, 2024
8519c1c
isort
AmintorDusko Mar 5, 2024
60aef32
Add double-obs tests.
vincentmr Mar 5, 2024
6308ba2
Pin pytest version (#624)
AmintorDusko Mar 4, 2024
3a08c62
Version Bump (#626)
github-actions[bot] Mar 4, 2024
daab5ad
increase tolerance
AmintorDusko Mar 5, 2024
078c8d8
Introduce isort. (#623)
vincentmr Mar 5, 2024
4ecf2fb
Auto update version
github-actions[bot] Mar 5, 2024
3bab48b
isort
AmintorDusko Mar 5, 2024
11f6a1b
Add qml.var support.
vincentmr Mar 4, 2024
186e53b
Add probs support.
vincentmr Mar 5, 2024
0222336
Add measurement tests with wires.
vincentmr Mar 5, 2024
6ea2ee8
Merge remote-tracking branch 'origin/lightning-qubit2-add-simulate' i…
vincentmr Mar 5, 2024
bdc28f8
review suggestions
AmintorDusko Mar 5, 2024
45b5448
remove unused imports
AmintorDusko Mar 5, 2024
5e80afb
remove diagonalization gate application from state vector
AmintorDusko Mar 5, 2024
96618ca
pytest.skip tests
vincentmr Mar 5, 2024
437e300
Merge branch 'lightning-qubit2-add-simulate' into lightning-qubit2-ad…
vincentmr Mar 5, 2024
edd65a8
Merge branch 'master' into lightning-qubit2-add-measurements
AmintorDusko Mar 6, 2024
24976a9
Auto update version
github-actions[bot] Mar 6, 2024
fda2b3e
Fix format
vincentmr Mar 6, 2024
4d15806
Update tests/lightning_qubit/test_measurements_class.py
vincentmr Mar 6, 2024
d993959
Update pennylane_lightning/lightning_qubit/_measurements.py
vincentmr Mar 6, 2024
9d7b03a
Remove pylint: disable=protected-access
vincentmr Mar 6, 2024
092977b
trigger ci
vincentmr Mar 6, 2024
a73b3c3
Add var tests
vincentmr Mar 7, 2024
5d305a0
update changelog
vincentmr Mar 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

### New features since last release

* Add analytic-mode `qml.probs` and `qml.var` support in `lightning.qubit2`.
[(#627)](https://github.com/PennyLaneAI/pennylane-lightning/pull/627)

### Breaking changes

### Improvements
Expand Down
2 changes: 1 addition & 1 deletion pennylane_lightning/core/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
Version number (major.minor.patch[-label])
"""

__version__ = "0.36.0-dev2"
__version__ = "0.36.0-dev3"
12 changes: 7 additions & 5 deletions pennylane_lightning/lightning_kokkos/lightning_kokkos.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,20 @@
from pennylane.wires import Wires

# pylint: disable=import-error, no-name-in-module, ungrouped-imports
from pennylane_lightning.core._serialize import (
QuantumScriptSerializer,
global_phase_diagonal,
)
from pennylane_lightning.core._version import __version__
from pennylane_lightning.lightning_kokkos_ops.algorithms import (
AdjointJacobianC64,
AdjointJacobianC128,
create_ops_listC64,
create_ops_listC128,
)

# pylint: disable=import-error, no-name-in-module, ungrouped-imports
from pennylane_lightning.core._serialize import (
QuantumScriptSerializer,
global_phase_diagonal,
)
from pennylane_lightning.core._version import __version__

def _kokkos_dtype(dtype):
if dtype not in [np.complex128, np.complex64]: # pragma: no cover
raise ValueError(f"Data type is not supported for state-vector computation: {dtype}")
Expand Down
76 changes: 71 additions & 5 deletions pennylane_lightning/lightning_qubit/_measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@

import numpy as np
import pennylane as qml
from pennylane.measurements import ExpectationMP, MeasurementProcess, StateMeasurement
from pennylane.measurements import (
ExpectationMP,
MeasurementProcess,
ProbabilityMP,
StateMeasurement,
VarianceMP,
)
from pennylane.tape import QuantumScript
from pennylane.typing import Result, TensorLike
from pennylane.wires import Wires
Expand Down Expand Up @@ -87,7 +93,6 @@ def state_diagonalizing_gates(self, measurementprocess: StateMeasurement) -> Ten
"""
diagonalizing_gates = measurementprocess.diagonalizing_gates()
self._qubit_state.apply_operations(measurementprocess.diagonalizing_gates())

state_array = self._qubit_state.state
wires = Wires(range(self._qubit_state.num_wires))

Expand Down Expand Up @@ -133,6 +138,60 @@ def expval(self, measurementprocess: MeasurementProcess):
measurementprocess.obs.name, measurementprocess.obs.wires
)

def probs(self, measurementprocess: MeasurementProcess):
"""Probabilities of the supplied observable or wires contained in the MeasurementProcess.

Args:
measurementprocess (StateMeasurement): measurement to apply to the state

Returns:
Probabilities of the supplied observable or wires
"""
diagonalizing_gates = measurementprocess.diagonalizing_gates()
if diagonalizing_gates:
self._qubit_state.apply_operations(diagonalizing_gates)
results = self._measurement_lightning.probs(measurementprocess.wires.tolist())
if diagonalizing_gates:
self._qubit_state.apply_operations(
[qml.adjoint(g, lazy=False) for g in reversed(diagonalizing_gates)]
)
return results

def var(self, measurementprocess: MeasurementProcess):
"""Variance of the supplied observable contained in the MeasurementProcess.

Args:
measurementprocess (StateMeasurement): measurement to apply to the state

Returns:
Variance of the observable
"""

if measurementprocess.obs.name == "SparseHamiltonian":
# ensuring CSR sparse representation.
CSR_SparseHamiltonian = measurementprocess.obs.sparse_matrix(
wire_order=list(range(self._qubit_state.num_wires))
).tocsr(copy=False)
return self._measurement_lightning.var(
CSR_SparseHamiltonian.indptr,
CSR_SparseHamiltonian.indices,
CSR_SparseHamiltonian.data,
)

if (
measurementprocess.obs.name in ["Hamiltonian", "Hermitian"]
or (measurementprocess.obs.arithmetic_depth > 0)
or isinstance(measurementprocess.obs.name, List)
):
ob_serialized = QuantumScriptSerializer(
self._qubit_state.device_name, self.dtype == np.complex64
)._ob(measurementprocess.obs)
return self._measurement_lightning.var(ob_serialized)

return self._measurement_lightning.var(
measurementprocess.obs.name, measurementprocess.obs.wires
)

def get_measurement_function(
self, measurementprocess: MeasurementProcess
) -> Callable[[MeasurementProcess, TensorLike], TensorLike]:
Expand All @@ -153,6 +212,16 @@ def get_measurement_function(
return self.state_diagonalizing_gates
return self.expval

if isinstance(measurementprocess, ProbabilityMP):
return self.probs

if isinstance(measurementprocess, VarianceMP):
if measurementprocess.obs.name in [
"Identity",
"Projector",
]:
return self.state_diagonalizing_gates
return self.var
if measurementprocess.obs is None or measurementprocess.obs.has_diagonalizing_gates:
return self.state_diagonalizing_gates

Expand Down Expand Up @@ -182,9 +251,6 @@ def measure_final_state(self, circuit: QuantumScript) -> Result:
Tuple[TensorLike]: The measurement results
"""

if circuit.shots:
raise NotImplementedError
# analytic case
if len(circuit.measurements) == 1:
return self.measurement(circuit.measurements[0])

Expand Down
Loading
Loading