Skip to content

Commit

Permalink
Change casting rules for StateMP to avoid copy in Lightning (#5995)
Browse files Browse the repository at this point in the history
### Before submitting

Please complete the following checklist when submitting a PR:

- [x] All new features must include a unit test.
If you've fixed a bug or added code that should be tested, add a test to
the
      test directory!

- [x] All new functions and code must be clearly commented and
documented.
If you do make documentation changes, make sure that the docs build and
      render correctly by running `make docs`.

- [x] Ensure that the test suite passes, by running `make test`.

- [x] Add a new entry to the `doc/releases/changelog-dev.md` file,
summarizing the
      change, and including a link back to the PR.

- [x] The PennyLane source code conforms to
      [PEP8 standards](https://www.python.org/dev/peps/pep-0008/).
We check all of our code against [Pylint](https://www.pylint.org/).
      To lint modified files, simply `pip install pylint`, and then
      run `pylint pennylane/path/to/file.py`.

When all the above are checked, delete everything above the dashed
line and fill in the pull request template.


------------------------------------------------------------------------------------------------------------

**Context:**
The changes made in #5547 led to a performance regression in Lightning
because a superfluous state vector copy is made when adding `0.j`, which
was introduced to cast the output to complex.

**Description of the Change:**
Use `qml.math.cast` instead.

**Benefits:**
Avoid unnecessary sum of `0.j`

**Possible Drawbacks:**

**Related GitHub Issues:**
[sc-65127]
  • Loading branch information
vincentmr committed Jul 17, 2024
1 parent 94b5533 commit ddeb50c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
3 changes: 3 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

<h3>Improvements 🛠</h3>

* `StateMP.process_state` defines rules in `cast_to_complex` for complex casting, avoiding a superfluous state vector copy in Lightning simulations
[(#5995)](https://github.com/PennyLaneAI/pennylane/pull/5995)

* Port the fast `apply_operation` implementation of `PauliZ` to `PhaseShift`, `S` and `T`.
[(#5876)](https://github.com/PennyLaneAI/pennylane/pull/5876)

Expand Down
14 changes: 11 additions & 3 deletions pennylane/measurements/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,18 @@ def shape(self, device, shots):

def process_state(self, state: Sequence[complex], wire_order: Wires):
# pylint:disable=redefined-outer-name
is_tf_interface = qml.math.get_deep_interface(state) == "tensorflow"
def cast_to_complex(state):
dtype = str(state.dtype)
if "complex" in dtype:
return state
if qml.math.get_interface(state) == "tensorflow":
return qml.math.cast(state, "complex128")
floating_single = "float32" in dtype or "complex64" in dtype
return qml.math.cast(state, "complex64" if floating_single else "complex128")

wires = self.wires
if not wires or wire_order == wires:
return qml.math.cast(state, "complex128") if is_tf_interface else state + 0.0j
return cast_to_complex(state)

if set(wires) != set(wire_order):
raise WireError(
Expand All @@ -190,7 +198,7 @@ def process_state(self, state: Sequence[complex], wire_order: Wires):
state = qml.math.reshape(state, shape)
state = qml.math.transpose(state, desired_axes)
state = qml.math.reshape(state, flat_shape)
return qml.math.cast(state, "complex128") if is_tf_interface else state + 0.0j
return cast_to_complex(state)

def process_density_matrix(self, density_matrix: Sequence[complex], wire_order: Wires):
# pylint:disable=redefined-outer-name
Expand Down

0 comments on commit ddeb50c

Please sign in to comment.