Skip to content

Commit

Permalink
Creating the LightningTensor device class based on the new API (#671)
Browse files Browse the repository at this point in the history
* empty commit (triggering CI)

* Auto update version

* Definition of the two front-end classes

* adding the `lightning_tensor` string to the supported backends in `setup.py`

* adding `__init__.py` file to directly import the `lightning_tensor` device class

* re-naming file

* Auto update version

* Creating the first prototype of initial MPS state tensor using `quimb`

* providing the `backend`, `method` parameters and making `wires` optional

* Changing names and structure

* Auto update version

* adding method required by the new device API design

* Auto update version

* using the `kwargs` parameter in `LightningTensor` and `CircuitMPS` in `quimb`

* taking some further inputs from the new device API

* Perhaps decided the overall structure of `LIghtningTensor`

* Auto update version

* adding docs to methods

* temporary changes so that `pylint` does not complain at this stage

* running `isort`

* re-running formatter after `isort`

* re-running formatter after `isort`

* Applying suggested formatting change from CI

* adding tmp unit tests

* Adding `quimb` in `requirements.txt`

* runing `isort` on mps test

* removing `quimb` from requirement and deleting unit tests for `lightning.tensor`

* Auto update version

* re-inserting unit tests with an additional `yml` file

* running isort on quimb test

* changing name of yml file

* preventing error in import

* updating yml file

* inserting `quimb` package in requirements-dev

* strange error with `quimb`

* strange error with `quimb`

* specifying scipy version

* removing installation of scipy from yml file

* removing the new `yml` file

* testing if tests are tested

* Covering all lines in tests

* forgot final line for formatter

* Python formatter on CI complaints

* covering missing lines

* formatter on CI complaints

* Trying not to skip test if Cpp is enabled

* skipping tests if Cpp is enabled

* removing the only line not covered by tests so far

* Auto update version

* Applying suggestions from code review and making the `state` attribute private (new API design)

* Python formatter

* removing params from `QuimbMPS`

* Auto update version

* removing `**kwargs` from `QuimbMPS`

* removing unnecessary param at this stage

* covering test line

* formatter...

* removing param description

* Making `pylint` happy

* forgot new arg in test

* Updating base class and `preprocess` function

* Updating `LightningTensor` class with new names from more advanced PR

* Auto update version

* Auto update version

* Triggering CI

* Auto update version

* Trying to remove pin from `quimb` in `requirements.dev`

* Auto update version

* Auto update version

* Removing infos on derivatives and using config options to pass parameters to interface

* Usual `pylint` failures

* Trying to solve formatting errors

* typo in docstring

* Sunday update: improved docstrings and structure

* Removing method that was supposed to be in next PR

* removing old TODO comment

* Removing changes from the `setup.py` file

* restoring previous format to `setup.py`

* Auto update version from '0.36.0-dev34' to '0.36.0-dev41'

* Auto update version from '0.36.0-dev40' to '0.36.0-dev41'

* Removing kwargs as suggested from code review

* Addressing comments from CR

* Skipping tests if CPP binary is available

* Auto update version from '0.36.0-dev42' to '0.36.0-dev43'

* Auto update version from '0.36.0-dev43' to '0.36.0-dev44'

* Restoring name in changelog (?)

* Increasing time limit for Python tests

* Applying suggestions from code review

---------

Co-authored-by: Dev version update bot <github-actions[bot]@users.noreply.github.com>
Co-authored-by: ringo-but-quantum <[email protected]>
  • Loading branch information
3 people authored Apr 29, 2024
1 parent 534a222 commit dcfab73
Show file tree
Hide file tree
Showing 10 changed files with 642 additions and 6 deletions.
5 changes: 4 additions & 1 deletion .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 Python class for the `lightning.tensor` device which uses the new device API and the interface for `quimb` based on the MPS method.
[(#671)](https://github.com/PennyLaneAI/pennylane-lightning/pull/671)

* Add compile-time support for AVX2/512 streaming operations in `lightning.qubit`.
[(#664)](https://github.com/PennyLaneAI/pennylane-lightning/pull/664)

Expand Down Expand Up @@ -133,7 +136,7 @@

This release contains contributions from (in alphabetical order):

Ali Asadi, Amintor Dusko, Christina Lee, Vincent Michaud-Rioux, Lee James O'Riordan, Mudit Pandey, Shuli Shu
Ali Asadi, Amintor Dusko, Pietropaolo Frisoni, Christina Lee, Vincent Michaud-Rioux, Lee James O'Riordan, Mudit Pandey, Shuli Shu

---

Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/tests_linux_python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
matrix:
os: [ubuntu-22.04]
pl_backend: ["lightning_qubit"]
timeout-minutes: 60
timeout-minutes: 75
name: Python tests
runs-on: ${{ matrix.os }}

Expand Down Expand Up @@ -144,7 +144,7 @@ jobs:
matrix:
os: [ubuntu-22.04]
pl_backend: ["lightning_qubit"]
timeout-minutes: 60
timeout-minutes: 75
name: Python tests with OpenBLAS
runs-on: ${{ matrix.os }}

Expand Down Expand Up @@ -257,7 +257,7 @@ jobs:
exclude:
- pl_backend: ["all"]
exec_model: OPENMP
timeout-minutes: 60
timeout-minutes: 75
name: Python tests with Kokkos
runs-on: ${{ matrix.os }}

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-dev43"
__version__ = "0.36.0-dev44"
18 changes: 18 additions & 0 deletions pennylane_lightning/lightning_tensor/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2018-2024 Xanadu Quantum Technologies Inc.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""PennyLane lightning_tensor package."""

from pennylane_lightning.core import __version__

from .lightning_tensor import LightningTensor
100 changes: 100 additions & 0 deletions pennylane_lightning/lightning_tensor/backends/quimb/_mps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Copyright 2018-2024 Xanadu Quantum Technologies Inc.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Class implementation for the Quimb MPS interface for simulating quantum circuits while keeping the state always in MPS form.
"""

import numpy as np
import pennylane as qml
import quimb.tensor as qtn
from pennylane.wires import Wires

_operations = frozenset({}) # pragma: no cover
# The set of supported operations.

_observables = frozenset({}) # pragma: no cover
# The set of supported observables.


def stopping_condition(op: qml.operation.Operator) -> bool:
"""A function that determines if an operation is supported by ``lightning.tensor`` for this interface."""
return op.name in _operations # pragma: no cover


def accepted_observables(obs: qml.operation.Operator) -> bool:
"""A function that determines if an observable is supported by ``lightning.tensor`` for this interface."""
return obs.name in _observables # pragma: no cover


class QuimbMPS:
"""Quimb MPS class.
Used internally by the `LightningTensor` device.
Interfaces with `quimb` for MPS manipulation, and provides methods to execute quantum circuits.
Args:
num_wires (int): the number of wires in the circuit.
interf_opts (dict): dictionary containing the interface options.
dtype (np.dtype): the complex type used for the MPS.
"""

def __init__(self, num_wires, interf_opts, dtype=np.complex128):

if dtype not in [np.complex64, np.complex128]: # pragma: no cover
raise TypeError(f"Unsupported complex type: {dtype}")

self._wires = Wires(range(num_wires))
self._dtype = dtype

self._init_state_ops = {
"binary": "0" * max(1, len(self._wires)),
"dtype": self._dtype.__name__,
"tags": [str(l) for l in self._wires.labels],
}

self._gate_opts = {
"contract": "swap+split",
"parametrize": None,
"cutoff": interf_opts["cutoff"],
"max_bond": interf_opts["max_bond_dim"],
}

self._expval_opts = {
"dtype": self._dtype.__name__,
"simplify_sequence": "ADCRS",
"simplify_atol": 0.0,
}

self._circuitMPS = qtn.CircuitMPS(psi0=self._initial_mps())

@property
def state(self):
"""Current MPS handled by the interface."""
return self._circuitMPS.psi

def state_to_array(self) -> np.ndarray:
"""Contract the MPS into a dense array."""
return self._circuitMPS.to_dense()

def _initial_mps(self) -> qtn.MatrixProductState:
r"""
Returns an initial state to :math:`\ket{0}`.
Internally, it uses `quimb`'s `MPS_computational_state` method.
Returns:
MatrixProductState: The initial MPS of a circuit.
"""

return qtn.MPS_computational_state(**self._init_state_ops)
Loading

0 comments on commit dcfab73

Please sign in to comment.