Skip to content

Commit

Permalink
pauli string reps added (#6243) and bug fixed in pauli arithmetic
Browse files Browse the repository at this point in the history
  • Loading branch information
Lasse Dierich committed Nov 9, 2024
1 parent e710ca6 commit 4e0257c
Show file tree
Hide file tree
Showing 5 changed files with 373 additions and 6 deletions.
158 changes: 155 additions & 3 deletions pennylane/ops/qubit/non_parametric_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ def __repr__(self) -> str:
def name(self) -> str:
return "Hadamard"

@property
def pauli_rep(self):
if self._pauli_rep_cached is None:
self._pauli_rep_cached = qml.pauli.PauliSentence(
{
qml.pauli.PauliWord({self.wires[0]: "X"}): INV_SQRT2,
qml.pauli.PauliWord({self.wires[0]: "Z"}): INV_SQRT2,
}
)
return self._pauli_rep_cached

def __init__(self, wires: WiresLike, id: Optional[str] = None):
super().__init__(wires=wires, id=id)
self._pauli_rep_cached = None

@staticmethod
@lru_cache()
def compute_matrix() -> np.ndarray: # pylint: disable=arguments-differ
Expand Down Expand Up @@ -242,9 +257,17 @@ class PauliX(Observable, Operation):

_queue_category = "_ops"

@property
def pauli_rep(self):
if self._pauli_rep_cached is None:
self._pauli_rep_cached = qml.pauli.PauliSentence(
{qml.pauli.PauliWord({self.wires[0]: "X"}): 1.0}
)
return self._pauli_rep_cached

def __init__(self, wires: Optional[WiresLike] = None, id: Optional[str] = None):
super().__init__(wires=wires, id=id)
self._pauli_rep = qml.pauli.PauliSentence({qml.pauli.PauliWord({self.wires[0]: "X"}): 1.0})
self._pauli_rep_cached = None

def label(
self,
Expand Down Expand Up @@ -434,9 +457,17 @@ class PauliY(Observable, Operation):

_queue_category = "_ops"

@property
def pauli_rep(self):
if self._pauli_rep_cached is None:
self._pauli_rep_cached = qml.pauli.PauliSentence(
{qml.pauli.PauliWord({self.wires[0]: "Y"}): 1.0}
)
return self._pauli_rep_cached

def __init__(self, wires: WiresLike, id: Optional[str] = None):
super().__init__(wires=wires, id=id)
self._pauli_rep = qml.pauli.PauliSentence({qml.pauli.PauliWord({self.wires[0]: "Y"}): 1.0})
self._pauli_rep_cached = None

def __repr__(self) -> str:
"""String representation."""
Expand Down Expand Up @@ -623,9 +654,17 @@ class PauliZ(Observable, Operation):

_queue_category = "_ops"

@property
def pauli_rep(self):
if self._pauli_rep_cached is None:
self._pauli_rep_cached = qml.pauli.PauliSentence(
{qml.pauli.PauliWord({self.wires[0]: "Z"}): 1.0}
)
return self._pauli_rep_cached

def __init__(self, wires: WiresLike, id: Optional[str] = None):
super().__init__(wires=wires, id=id)
self._pauli_rep = qml.pauli.PauliSentence({qml.pauli.PauliWord({self.wires[0]: "Z"}): 1.0})
self._pauli_rep_cached = None

def __repr__(self) -> str:
"""String representation."""
Expand Down Expand Up @@ -815,6 +854,21 @@ class S(Operation):

batch_size = None

@property
def pauli_rep(self):
if self._pauli_rep_cached is None:
self._pauli_rep_cached = qml.pauli.PauliSentence(
{
qml.pauli.PauliWord({self.wires[0]: "I"}): 0.5 + 0.5j,
qml.pauli.PauliWord({self.wires[0]: "Z"}): 0.5 - 0.5j,
}
)
return self._pauli_rep_cached

def __init__(self, wires: WiresLike, id: Optional[str] = None):
super().__init__(wires=wires, id=id)
self._pauli_rep_cached = None

@staticmethod
@lru_cache()
def compute_matrix() -> np.ndarray: # pylint: disable=arguments-differ
Expand Down Expand Up @@ -927,6 +981,21 @@ class T(Operation):

batch_size = None

@property
def pauli_rep(self):
if self._pauli_rep_cached is None:
self._pauli_rep_cached = qml.pauli.PauliSentence(
{
qml.pauli.PauliWord({self.wires[0]: "I"}): (0.5 + 0.5 * INV_SQRT2 * (1 + 1.0j)),
qml.pauli.PauliWord({self.wires[0]: "Z"}): (0.5 - 0.5 * INV_SQRT2 * (1 + 1.0j)),
}
)
return self._pauli_rep_cached

def __init__(self, wires: WiresLike, id: Optional[str] = None):
super().__init__(wires=wires, id=id)
self._pauli_rep_cached = None

@staticmethod
@lru_cache()
def compute_matrix() -> np.ndarray: # pylint: disable=arguments-differ
Expand Down Expand Up @@ -1037,6 +1106,21 @@ class SX(Operation):

basis = "X"

@property
def pauli_rep(self):
if self._pauli_rep_cached is None:
self._pauli_rep_cached = qml.pauli.PauliSentence(
{
qml.pauli.PauliWord({self.wires[0]: "I"}): (0.5 + 0.5j),
qml.pauli.PauliWord({self.wires[0]: "X"}): (0.5 - 0.5j),
}
)
return self._pauli_rep_cached

def __init__(self, wires: WiresLike, id: Optional[str] = None):
super().__init__(wires=wires, id=id)
self._pauli_rep_cached = None

@staticmethod
@lru_cache()
def compute_matrix() -> np.ndarray: # pylint: disable=arguments-differ
Expand Down Expand Up @@ -1152,6 +1236,23 @@ class SWAP(Operation):

batch_size = None

@property
def pauli_rep(self):
if self._pauli_rep_cached is None:
self._pauli_rep_cached = qml.pauli.PauliSentence(
{
qml.pauli.PauliWord({self.wires[0]: "I", self.wires[1]: "I"}): 0.5,
qml.pauli.PauliWord({self.wires[0]: "X", self.wires[1]: "X"}): 0.5,
qml.pauli.PauliWord({self.wires[0]: "Y", self.wires[1]: "Y"}): 0.5,
qml.pauli.PauliWord({self.wires[0]: "Z", self.wires[1]: "Z"}): 0.5,
}
)
return self._pauli_rep_cached

def __init__(self, wires: WiresLike, id: Optional[str] = None):
super().__init__(wires=wires, id=id)
self._pauli_rep_cached = None

@staticmethod
@lru_cache()
def compute_matrix() -> np.ndarray: # pylint: disable=arguments-differ
Expand Down Expand Up @@ -1243,6 +1344,21 @@ class ECR(Operation):

batch_size = None

@property
def pauli_rep(self):
if self._pauli_rep_cached is None:
self._pauli_rep_cached = qml.pauli.PauliSentence(
{
qml.pauli.PauliWord({self.wires[0]: "X", self.wires[1]: "I"}): INV_SQRT2,
qml.pauli.PauliWord({self.wires[0]: "Y", self.wires[1]: "X"}): -INV_SQRT2,
}
)
return self._pauli_rep_cached

def __init__(self, wires: WiresLike, id: Optional[str] = None):
super().__init__(wires=wires, id=id)
self._pauli_rep_cached = None

@staticmethod
def compute_matrix() -> np.ndarray: # pylint: disable=arguments-differ
r"""Representation of the operator as a canonical matrix in the computational basis (static method).
Expand Down Expand Up @@ -1371,6 +1487,23 @@ class ISWAP(Operation):

batch_size = None

@property
def pauli_rep(self):
if self._pauli_rep_cached is None:
self._pauli_rep_cached = qml.pauli.PauliSentence(
{
qml.pauli.PauliWord({self.wires[0]: "I", self.wires[1]: "I"}): 0.5,
qml.pauli.PauliWord({self.wires[0]: "X", self.wires[1]: "X"}): 0.5j,
qml.pauli.PauliWord({self.wires[0]: "Y", self.wires[1]: "Y"}): 0.5j,
qml.pauli.PauliWord({self.wires[0]: "Z", self.wires[1]: "Z"}): 0.5,
}
)
return self._pauli_rep_cached

def __init__(self, wires: WiresLike, id: Optional[str] = None):
super().__init__(wires=wires, id=id)
self._pauli_rep_cached = None

@staticmethod
@lru_cache()
def compute_matrix() -> np.ndarray: # pylint: disable=arguments-differ
Expand Down Expand Up @@ -1488,6 +1621,25 @@ class SISWAP(Operation):

batch_size = None

@property
def pauli_rep(self):
if self._pauli_rep_cached is None:
self._pauli_rep_cached = qml.pauli.PauliSentence(
{
qml.pauli.PauliWord({self.wires[0]: "I", self.wires[1]: "I"}): 0.5
+ 0.5 * INV_SQRT2,
qml.pauli.PauliWord({self.wires[0]: "X", self.wires[1]: "X"}): 0.5j * INV_SQRT2,
qml.pauli.PauliWord({self.wires[0]: "Y", self.wires[1]: "Y"}): 0.5j * INV_SQRT2,
qml.pauli.PauliWord({self.wires[0]: "Z", self.wires[1]: "Z"}): 0.5
- 0.5 * INV_SQRT2,
}
)
return self._pauli_rep_cached

def __init__(self, wires: WiresLike, id: Optional[str] = None):
super().__init__(wires=wires, id=id)
self._pauli_rep_cached = None

@staticmethod
@lru_cache()
def compute_matrix() -> np.ndarray: # pylint: disable=arguments-differ
Expand Down
Loading

0 comments on commit 4e0257c

Please sign in to comment.