-
Notifications
You must be signed in to change notification settings - Fork 605
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 pauli_rep
properties for common static gates
#6243
Comments
Hi, I’d like to work on this issue as my first contribution. |
Hi @ldi18, |
Hey @ldi18 |
Hi @dwierichs , @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 I defined the test def test(gate):
dim = gate.matrix().shape[0]
if dim == 2:
id_str = 'I(0)'
if dim == 4:
id_str = 'I(0)@I(1)'
gate_in_pauli_decomp = eval(str(gate.pauli_rep).replace('\n', ' ').replace('I', id_str)).matrix()
return np.allclose(gate.matrix(), gate_in_pauli_decomp) All ops pass the test, but only if I comment out the assignment in the operator constructor as shown below: class Operator(abc.ABC, meta class=ABCCaptureMeta):
...
...
def __init__(
self,
*params: TensorLike,
wires: Optional[WiresLike] = None,
id: Optional[str] = None,
):
# pylint: disable=too-many-branches
self._name: str = self.__class__.__name__ #: str: name of the operator
self._id: str = id
# Can this be commented out?
# self._pauli_rep: Optional[qml.pauli.PauliSentence] = (
# None # Union[PauliSentence, None]: Representation of the operator as a pauli sentence, if applicable
# )
...
...) Would it cause any issues elsewhere when I just remove the part I commented out above? To check this I tried running the tests as described in https://docs.pennylane.ai/en/stable/development/guide/tests.html . Actually this returns 11 errors, even for the current version in the GitHub - without any changes from me. Is this a problem with my testing environment or just normal? I'd just create a pull request now so that you can have a look. |
Hi @ldi18 |
…thmetic + changelog
Hi @dwierichs, Thanks for the hint! Using pauli_rep resolved the issue in the operator class. During testing, however, My changes pass all pytest checks. Pylint still shows a few errors/warnings related to the number of parameters, but many of these also appear in the developer version without any changes from me. |
Hi @ldi18 , |
Feature details
The
pauli_rep
property allows to represent an operator as aPauliSentence
instance.Currently, it is mostly implemented for
qml.X, qml.Y, qml.Z
and for operator math classes.It would be great to have
pauli_rep
attributes for, e.g., Clifford gates (Hadamard
,S
,CNOT
), but also for other ops.This would simplify manipulation of
qml.pauli
objects.This came up while writing this demo, where we currently have to do
i.e., we have to go via the dense matrix representation to obtain the new operator. With
qml.CNOT.pauli_rep
implemented, we could stay in the more efficient Pauli representation.Implementation
Basically trivial, as far as I can tell.
How important would you say this feature is?
1: Not important. Would be nice to have.
Additional information
No response
The text was updated successfully, but these errors were encountered: