|
| 1 | +# Copyright 2025 Xanadu Quantum Technologies Inc. |
| 2 | + |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | + |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +""" |
| 15 | +Implements the pauli measurement. |
| 16 | +""" |
| 17 | + |
| 18 | +import uuid |
| 19 | +from functools import lru_cache |
| 20 | + |
| 21 | +from pennylane import math |
| 22 | +from pennylane.capture import enabled as capture_enabled |
| 23 | +from pennylane.operation import Operator |
| 24 | +from pennylane.wires import Wires, WiresLike |
| 25 | + |
| 26 | +from .measurement_value import MeasurementValue |
| 27 | + |
| 28 | +_VALID_PAULI_CHARS = "XYZ" |
| 29 | + |
| 30 | + |
| 31 | +class PauliMeasure(Operator): |
| 32 | + """A Pauli product measurement.""" |
| 33 | + |
| 34 | + resource_keys = {"pauli_word"} |
| 35 | + |
| 36 | + def __init__( |
| 37 | + self, |
| 38 | + pauli_word: str, |
| 39 | + wires: WiresLike, |
| 40 | + postselect: int | None = None, |
| 41 | + id: str | None = None, |
| 42 | + ): |
| 43 | + if not all(c in _VALID_PAULI_CHARS for c in pauli_word): |
| 44 | + raise ValueError( |
| 45 | + f'The given Pauli word "{pauli_word}" contains characters that ' |
| 46 | + "are not allowed. Allowed characters are X, Y and Z." |
| 47 | + ) |
| 48 | + |
| 49 | + wires = Wires(wires) |
| 50 | + if len(pauli_word) != len(wires): |
| 51 | + raise ValueError( |
| 52 | + "The number of wires must be equal to the length of the Pauli " |
| 53 | + f"word. The Pauli word {pauli_word} has length {len(pauli_word)} " |
| 54 | + f"and {len(wires)} wires were given: {wires}." |
| 55 | + ) |
| 56 | + super().__init__(wires=wires, id=id) |
| 57 | + self.hyperparameters["pauli_word"] = pauli_word |
| 58 | + self.hyperparameters["postselect"] = postselect |
| 59 | + |
| 60 | + @property |
| 61 | + def pauli_word(self) -> str: |
| 62 | + """The Pauli word for the measurement.""" |
| 63 | + return self.hyperparameters["pauli_word"] |
| 64 | + |
| 65 | + @property |
| 66 | + def postselect(self) -> int | None: |
| 67 | + """Which outcome to postselect after the measurement.""" |
| 68 | + return self.hyperparameters["postselect"] |
| 69 | + |
| 70 | + @classmethod |
| 71 | + def _primitive_bind_call(cls, *args, **kwargs): |
| 72 | + return type.__call__(cls, *args, **kwargs) |
| 73 | + |
| 74 | + def __repr__(self) -> str: |
| 75 | + return f"PauliMeasure('{self.pauli_word}', wires={self.wires.tolist()})" |
| 76 | + |
| 77 | + @property |
| 78 | + def resource_params(self) -> dict: |
| 79 | + return {"pauli_word": self.hyperparameters["pauli_word"]} |
| 80 | + |
| 81 | + @property |
| 82 | + def hash(self) -> int: |
| 83 | + """int: An integer hash uniquely representing the measurement.""" |
| 84 | + return hash((self.__class__.__name__, self.pauli_word, tuple(self.wires.tolist()), self.id)) |
| 85 | + |
| 86 | + |
| 87 | +def _pauli_measure_impl(wires: WiresLike, pauli_word: str, postselect: int | None = None): |
| 88 | + """Concrete implementation of the pauli_measure primitive.""" |
| 89 | + measurement_id = str(uuid.uuid4()) |
| 90 | + measurement = PauliMeasure(pauli_word, wires, postselect, measurement_id) |
| 91 | + return MeasurementValue([measurement]) |
| 92 | + |
| 93 | + |
| 94 | +@lru_cache |
| 95 | +def _create_pauli_measure_primitive(): |
| 96 | + """Create a primitive corresponding to a Pauli product measurement.""" |
| 97 | + |
| 98 | + # pylint: disable=import-outside-toplevel |
| 99 | + import jax |
| 100 | + |
| 101 | + from pennylane.capture.custom_primitives import QmlPrimitive |
| 102 | + |
| 103 | + pauli_measure_p = QmlPrimitive("pauli_measure") |
| 104 | + |
| 105 | + @pauli_measure_p.def_impl |
| 106 | + def _pauli_measure_primitive_impl(*wires, pauli_word="", postselect=None): |
| 107 | + return _pauli_measure_impl(wires, pauli_word=pauli_word, postselect=postselect) |
| 108 | + |
| 109 | + @pauli_measure_p.def_abstract_eval |
| 110 | + def _pauli_measure_primitive_abstract_eval(*_, **__): |
| 111 | + dtype = jax.numpy.int64 if jax.config.jax_enable_x64 else jax.numpy.int32 |
| 112 | + return jax.core.ShapedArray((), dtype) |
| 113 | + |
| 114 | + return pauli_measure_p |
| 115 | + |
| 116 | + |
| 117 | +def pauli_measure(pauli_word: str, wires: WiresLike, postselect: int | None = None): |
| 118 | + """Perform a Pauli product measurement.""" |
| 119 | + |
| 120 | + if capture_enabled(): |
| 121 | + primitive = _create_pauli_measure_primitive() |
| 122 | + wires = (wires,) if math.shape(wires) == () else tuple(wires) |
| 123 | + return primitive.bind(*wires, pauli_word=pauli_word, postselect=postselect) |
| 124 | + |
| 125 | + return _pauli_measure_impl(wires, pauli_word, postselect) |
0 commit comments