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

Add native PauliRot implementation in LightningKokkos [sc-71642] #855

Merged
merged 73 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
9cda69a
Implement native PauliRot in LightningQubit.
vincentmr Jul 30, 2024
37418d9
Fix issues wih I in pauli word.
vincentmr Jul 31, 2024
e0d5377
Directly get indices and data.
vincentmr Jul 31, 2024
0266860
Change core_function signature for applyNCN
vincentmr Jul 31, 2024
5d49dab
Include sin in data.
vincentmr Jul 31, 2024
0a7a97e
*= c while copying arr.
vincentmr Jul 31, 2024
06675c7
Use numpy request to pass pointers.
vincentmr Jul 31, 2024
9e6863f
generateBitPatterns
vincentmr Jul 31, 2024
c915b96
Permute early.
vincentmr Aug 1, 2024
3a29b66
Do not precompute offsets.
vincentmr Aug 1, 2024
e74a2d4
Modify stopping_condition and clean up.
vincentmr Aug 1, 2024
1e6d324
Clean up
vincentmr Aug 1, 2024
8f3c99f
Auto update version from '0.38.0-dev20' to '0.38.0-dev22'
ringo-but-quantum Aug 1, 2024
1645a5a
Fix device test.
vincentmr Aug 2, 2024
8cca37e
Replace indices, data arguments by word for PauliRot.
vincentmr Aug 2, 2024
4a51fc6
Auto update version from '0.38.0-dev22' to '0.38.0-dev25'
ringo-but-quantum Aug 2, 2024
3d10b73
Merge branch 'master' into lq_pauli_rot
vincentmr Aug 2, 2024
aa24f4d
Fix few lint warnings.
vincentmr Aug 2, 2024
6927caf
Update changelog
vincentmr Aug 2, 2024
ee34eeb
Fix docstring.
vincentmr Aug 2, 2024
c9dc41e
Fix docstrings.
vincentmr Aug 5, 2024
c702b64
Merge remote-tracking branch 'origin/master' into lq_pauli_rot
vincentmr Aug 5, 2024
9bb5a8a
Auto update version from '0.38.0-dev25' to '0.38.0-dev26'
ringo-but-quantum Aug 5, 2024
6cb0f8e
Update GateIndices.hpp
vincentmr Aug 5, 2024
b8488c2
WIP loops
vincentmr Aug 6, 2024
670570f
Avoid copying arr into coeffs.
vincentmr Aug 7, 2024
bc58a7d
Comment code a bit.
vincentmr Aug 7, 2024
f43a77f
Auto update version from '0.38.0-dev26' to '0.38.0-dev27'
ringo-but-quantum Aug 7, 2024
6a79685
Apply suggestions from code review
vincentmr Aug 7, 2024
5bc8551
Revert to old implementation.
vincentmr Aug 8, 2024
32b9b34
Merge remote-tracking branch 'origin/lq_pauli_rot' into lq_pauli_rot
vincentmr Aug 8, 2024
b99e108
Add doc [skip ci].
vincentmr Aug 8, 2024
4b99691
Add PauliRot C++ tests.
vincentmr Aug 8, 2024
9072665
Merge branch 'master' into lq_pauli_rot
vincentmr Aug 8, 2024
2741f29
Reimplement applyPauliRot
vincentmr Aug 14, 2024
6326894
popcount
vincentmr Aug 14, 2024
73cfaed
Clean up.
vincentmr Aug 15, 2024
7b06ec1
Auto update version from '0.38.0-dev27' to '0.38.0-dev34'
ringo-but-quantum Aug 15, 2024
8762c39
Merge remote-tracking branch 'origin/master' into lq_pauli_rot
vincentmr Aug 15, 2024
9398b37
Merge remote-tracking branch 'origin/lq_pauli_rot' into lq_pauli_rot
vincentmr Aug 15, 2024
63ea43f
Fix setStateVecotr
vincentmr Aug 15, 2024
1b0aba9
Fix adjoint
vincentmr Aug 15, 2024
9059862
Fix tidy
vincentmr Aug 15, 2024
15b533e
Fix capture
vincentmr Aug 15, 2024
fe390cd
Merge branch 'master' into lq_pauli_rot
vincentmr Aug 15, 2024
f9bff5a
trigger ci
vincentmr Aug 15, 2024
e90ee3f
Clean up runner
vincentmr Aug 15, 2024
f04c69f
Auto update version from '0.38.0-dev34' to '0.38.0-dev35'
ringo-but-quantum Aug 15, 2024
0d1ad50
Fix tidy
vincentmr Aug 15, 2024
72d219f
sudo du -sh
vincentmr Aug 15, 2024
d1307f1
sudo du -sh
vincentmr Aug 15, 2024
2d37fdd
sudo du -sh
vincentmr Aug 15, 2024
b0f505e
sudo du -sh
vincentmr Aug 15, 2024
d8aea05
sudo du -sh
vincentmr Aug 15, 2024
509de91
sudo du -sh
vincentmr Aug 15, 2024
e46551b
sudo du -sh
vincentmr Aug 15, 2024
694f856
PauliRot in LK
vincentmr Aug 15, 2024
509c035
Update changelog [skip ci].
vincentmr Aug 15, 2024
f0d222e
Merge remote-tracking branch 'origin/master' into lk_pauli_rot
vincentmr Aug 20, 2024
131583f
Auto update version from '0.38.0-dev38' to '0.38.0-dev39'
ringo-but-quantum Aug 20, 2024
b36c48f
PauliRot => multirz
vincentmr Aug 20, 2024
e29c93c
Merge remote-tracking branch 'origin/master' into lk_pauli_rot
vincentmr Sep 5, 2024
3bdab93
Auto update version from '0.39.0-dev2' to '0.39.0-dev3'
ringo-but-quantum Sep 5, 2024
7d81d32
Merge branch 'master' into lk_pauli_rot
vincentmr Sep 5, 2024
be95a83
Auto update version from '0.39.0-dev4' to '0.39.0-dev5'
ringo-but-quantum Sep 5, 2024
4c3a365
Format
vincentmr Sep 5, 2024
e06fdab
Merge remote-tracking branch 'origin/master' into lk_pauli_rot
vincentmr Sep 9, 2024
210eb61
Add C++ tests
vincentmr Sep 9, 2024
0c37793
Merge remote-tracking branch 'origin/master' into lk_pauli_rot
vincentmr Sep 9, 2024
33ffd58
Merge branch 'master' into lk_pauli_rot
vincentmr Sep 10, 2024
3131c1f
Auto update version from '0.39.0-dev13' to '0.39.0-dev14'
ringo-but-quantum Sep 10, 2024
e4036ef
Merge branch 'master' into lk_pauli_rot
vincentmr Sep 10, 2024
391d0da
Auto update version from '0.39.0-dev14' to '0.39.0-dev15'
ringo-but-quantum Sep 10, 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
Prev Previous commit
Next Next commit
Merge remote-tracking branch 'origin/master' into lk_pauli_rot
  • Loading branch information
vincentmr committed Sep 5, 2024
commit e29c93c0259692ecd9cab07346784e6321bcdb44
17 changes: 13 additions & 4 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
* Optimize gate cache recording for `lightning.tensor` C++ layer.
[(#879)](https://github.com/PennyLaneAI/pennylane-lightning/pull/879)

* LightningKokkos gains native support for the `PauliRot` gate.
[(#855)](https://github.com/PennyLaneAI/pennylane-lightning/pull/855)

### Documentation

### Bug fixes
Expand Down Expand Up @@ -68,11 +71,17 @@ Amintor Dusko, Luis Alfredo Nuñez Meneses, Shuli Shu

### Improvements

* LightningKokkos gains native support for the `PauliRot` gate.
[(#855)](https://github.com/PennyLaneAI/pennylane-lightning/pull/855)
* Updated calls of ``size_t`` to ``std::size_t`` everywhere.
[(#816)](https://github.com/PennyLaneAI/pennylane-lightning/pull/816)

* LightningQubit gains native support for the `PauliRot` gate.
[(#834)](https://github.com/PennyLaneAI/pennylane-lightning/pull/834)
* Update Lightning tests to support the generalization of basis state preparation.
[(#864)](https://github.com/PennyLaneAI/pennylane-lightning/pull/864)

* Add `SetState` and `SetBasisState` to `LightningKokkosSimulator`.
[(#861)](https://github.com/PennyLaneAI/pennylane-lightning/pull/861)

* Remove use of the deprecated `Operator.expand` in favour of `Operator.decomposition`.
[(#846)](https://github.com/PennyLaneAI/pennylane-lightning/pull/846)

* The `setBasisState` and `setStateVector` methods of `StateVectorLQubit` and `StateVectorKokkos` are overloaded to support PennyLane-like parameters.
[(#843)](https://github.com/PennyLaneAI/pennylane-lightning/pull/843)
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.38.0-dev39"
__version__ = "0.39.0-dev2"
6 changes: 6 additions & 0 deletions pennylane_lightning/lightning_kokkos/_state_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,12 @@ def _apply_lightning(
self._apply_lightning_midmeasure(
operation, mid_measurements, postselect_mode=postselect_mode
)
elif isinstance(operation, qml.PauliRot):
method = getattr(state, "applyPauliRot")
paulis = operation._hyperparameters["pauli_word"]
wires = [i for i, w in zip(wires, paulis) if w != "I"]
word = "".join(p for p in paulis if p != "I") # pylint: disable=protected-access
method(wires, invert_param, operation.parameters, word)
vincentmr marked this conversation as resolved.
Show resolved Hide resolved
elif method is not None: # apply specialized gate
param = operation.parameters
method(wires, invert_param, param)
Expand Down
160 changes: 12 additions & 148 deletions pennylane_lightning/lightning_kokkos/lightning_kokkos.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import os
import sys
from dataclasses import replace
from functools import reduce
from numbers import Number
from pathlib import Path
from typing import Callable, Optional, Sequence, Tuple, Union
Expand Down Expand Up @@ -316,7 +317,10 @@ def stopping_condition(op: Operator) -> bool:
return len(op.wires) < 10
if isinstance(op, qml.GroverOperator):
return len(op.wires) < 13

if isinstance(op, qml.PauliRot):
word = op._hyperparameters["pauli_word"] # pylint: disable=protected-access
# decomposes to IsingXX, etc. for n <= 2
return reduce(lambda x, y: x + (y != "I"), word, 0) > 2
return op.name in _operations


Expand Down Expand Up @@ -372,7 +376,7 @@ def _supports_adjoint(circuit):

def _adjoint_ops(op: qml.operation.Operator) -> bool:
"""Specify whether or not an Operator is supported by adjoint differentiation."""
return adjoint_ops(op)
return not isinstance(op, qml.PauliRot) and adjoint_ops(op)


def _add_adjoint_transforms(program: TransformProgram) -> None:
Expand Down Expand Up @@ -408,89 +412,9 @@ def _kokkos_configuration():
return print_configuration()


allowed_operations = {
"Identity",
"BasisState",
"QubitStateVector",
"StatePrep",
"QubitUnitary",
"ControlledQubitUnitary",
"MultiControlledX",
"DiagonalQubitUnitary",
"PauliX",
"PauliY",
"PauliZ",
"MultiRZ",
"GlobalPhase",
"C(GlobalPhase)",
"Hadamard",
"S",
"Adjoint(S)",
"T",
"Adjoint(T)",
"SX",
"Adjoint(SX)",
"CNOT",
"SWAP",
"ISWAP",
"PSWAP",
"Adjoint(ISWAP)",
"SISWAP",
"Adjoint(SISWAP)",
"SQISW",
"CSWAP",
"Toffoli",
"CY",
"CZ",
"PhaseShift",
"ControlledPhaseShift",
"RX",
"RY",
"RZ",
"Rot",
"CRX",
"CRY",
"CRZ",
"CRot",
"IsingXX",
"IsingYY",
"IsingZZ",
"IsingXY",
"SingleExcitation",
"SingleExcitationPlus",
"SingleExcitationMinus",
"DoubleExcitation",
"DoubleExcitationPlus",
"DoubleExcitationMinus",
"QubitCarry",
"QubitSum",
"OrbitalRotation",
"QFT",
"ECR",
"BlockEncode",
"C(BlockEncode)",
"PauliRot",
}

allowed_observables = {
"PauliX",
"PauliY",
"PauliZ",
"Hadamard",
"Hermitian",
"Identity",
"Projector",
"SparseHamiltonian",
"Hamiltonian",
"LinearCombination",
"Sum",
"SProd",
"Prod",
"Exp",
}


class LightningKokkos(LightningBase):
@simulator_tracking
@single_tape_support
class LightningKokkos(Device):
"""PennyLane Lightning Kokkos device.

A device that interfaces with C++ to perform fast linear algebra calculations.
Expand Down Expand Up @@ -643,69 +567,9 @@ def preprocess(self, execution_config: ExecutionConfig = DefaultExecutionConfig)
)
program.add_transform(qml.transforms.broadcast_expand)

Args:
operations (list[~pennylane.operation.Operation]): operations to apply
dtype (type): Type of numpy ``complex`` to be used. Can be important
to specify for large systems for memory allocation purposes.

Returns:
array[complex]: the output state tensor
"""
# Skip over identity operations instead of performing
# matrix multiplication with the identity.
state = self.state_vector

for ops in operations:
if isinstance(ops, Adjoint):
name = ops.base.name
invert_param = True
else:
name = ops.name
invert_param = False
if isinstance(ops, qml.Identity):
continue
method = getattr(state, name, None)
wires = self.wires.indices(ops.wires)

if isinstance(ops, Conditional):
if ops.meas_val.concretize(mid_measurements):
self.apply_lightning([ops.base])
elif isinstance(ops, MidMeasureMP):
self._apply_lightning_midmeasure(ops, mid_measurements, postselect_mode)
elif isinstance(ops, qml.PauliRot):
method = getattr(state, "applyPauliRot")
paulis = ops._hyperparameters["pauli_word"]
wires = [i for i, w in zip(wires, paulis) if w != "I"]
word = "".join(p for p in paulis if p != "I") # pylint: disable=protected-access
method(wires, invert_param, ops.parameters, word)
elif isinstance(ops, qml.ops.op_math.Controlled) and isinstance(
ops.base, qml.GlobalPhase
):
controls = ops.control_wires
control_values = ops.control_values
param = ops.base.parameters[0]
matrix = global_phase_diagonal(param, self.wires, controls, control_values)
state.apply(name, wires, False, [[param]], matrix)
elif method is None:
# Inverse can be set to False since qml.matrix(ops) is already in inverted form
try:
mat = qml.matrix(ops)
except AttributeError: # pragma: no cover
# To support older versions of PL
mat = ops.matrix

if len(mat) == 0:
raise ValueError("Unsupported operation")
state.apply(
name,
wires,
False,
[],
mat.ravel(order="C"), # inv = False: Matrix already in correct form;
) # Parameters can be ignored for explicit matrices; F-order for cuQuantum
else:
param = ops.parameters
method(wires, invert_param, param)
if exec_config.gradient_method == "adjoint":
_add_adjoint_transforms(program)
return program, exec_config

# pylint: disable=unused-argument
def execute(
Expand Down
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.