Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow non-hermitian observables (#2960)
* refactor: ♻️ Raise warning instead of error when using non-hermitian observables. * feat: ✨ Allow sampling with symbolic operators. * feat: ✨ Add qml.is_hermitian and qml.is_unitary checks. * refactor (is_hermitian): ♻️ Avoid slow computation. * fix (Adjoint): 🐛 Use getattr. * fix: 🐛 Fix small bug. * test: 🧪 Add tests. * revert: ⏪ Use eigh always. * revert (Prod): ⏪ Revert changes. * fix: 🐛 Fix small bug. * fix tests * fix tests * Update tests/ops/functions/test_is_hermitian.py Co-authored-by: Christina Lee <christina@xanadu.ai> * refactor: ♻️ Change warning message. * Update pennylane/ops/functions/is_unitary.py Co-authored-by: Christina Lee <christina@xanadu.ai> * docs: 📝 Improve docstrings. * docs: 📝 Improve docstrings. * test: 🧪 Fix tests. * style: 🎨 Fix codefactor errors. * Update pennylane/ops/functions/is_unitary.py Co-authored-by: Christina Lee <christina@xanadu.ai> * chore (changelog): ✏️ Add feature to changelog. * feat (MultiControlledX): ✨ Set is_hermitian to True. * black * docs: 📝 Add docs. * chore (changelog): ✏️ Address comments. * Update pennylane/ops/functions/is_hermitian.py Co-authored-by: Jay Soni <jbsoni@uwaterloo.ca> * Update pennylane/ops/functions/is_hermitian.py Co-authored-by: Jay Soni <jbsoni@uwaterloo.ca> * Update pennylane/ops/functions/is_unitary.py Co-authored-by: Jay Soni <jbsoni@uwaterloo.ca> * Update tests/ops/op_math/test_prod.py Co-authored-by: Jay Soni <jbsoni@uwaterloo.ca> * Update tests/ops/op_math/test_sum.py Co-authored-by: Jay Soni <jbsoni@uwaterloo.ca> * Update tests/ops/op_math/test_sum.py Co-authored-by: Jay Soni <jbsoni@uwaterloo.ca> * Update tests/ops/op_math/test_sprod.py Co-authored-by: Jay Soni <jbsoni@uwaterloo.ca> * Update tests/ops/op_math/test_sprod.py Co-authored-by: Jay Soni <jbsoni@uwaterloo.ca> * Address comments. * remove unused import. * address comments * small change * small change * add tests * use hadamard in tests. * fix test * Update tests/ops/op_math/test_prod.py Co-authored-by: Jay Soni <jbsoni@uwaterloo.ca> * small changes. * small changes. * add tests * change xfail tests Co-authored-by: Christina Lee <christina@xanadu.ai> Co-authored-by: Jay Soni <jbsoni@uwaterloo.ca>
- Loading branch information
1 parent
c5213dc
commit 754f2c9
Showing
15 changed files
with
462 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Copyright 2018-2021 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. | ||
""" | ||
This module contains the qml.is_hermitian function. | ||
""" | ||
import pennylane as qml | ||
from pennylane.operation import Operator | ||
|
||
|
||
def is_hermitian(op: Operator): | ||
"""Check if the operation is hermitian. | ||
A hermitian matrix is a complex square matrix that is equal to its own adjoint | ||
.. math:: O† = O | ||
Args: | ||
op (~.operation.Operator): the operator to check against | ||
Returns: | ||
bool: True if the operation is hermitian, False otherwise | ||
.. note:: | ||
This check might be expensive for large operators. | ||
**Example** | ||
>>> op = qml.PauliX(wires=0) | ||
>>> qml.is_hermitian(op) | ||
True | ||
>>> op2 = qml.RX(0.54, wires=0) | ||
>>> qml.is_hermitian(op2) | ||
False | ||
""" | ||
if op.is_hermitian is True: | ||
return True | ||
return qml.math.allclose(qml.matrix(op), qml.matrix(qml.adjoint(op))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Copyright 2018-2021 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. | ||
""" | ||
This module contains the qml.is_unitary function. | ||
""" | ||
import pennylane as qml | ||
from pennylane.operation import Operator | ||
|
||
|
||
def is_unitary(op: Operator): | ||
"""Check if the operation is unitary. | ||
A matrix is unitary if its adjoint is also its inverse, that is, if | ||
.. math:: O†O = OO† = I | ||
Args: | ||
op (~.operation.Operator): the operator to check against | ||
Returns: | ||
bool: True if the operation is unitary, False otherwise | ||
.. note:: | ||
This check might be expensive for large operators. | ||
**Example** | ||
>>> op = qml.RX(0.54, wires=0) | ||
>>> qml.is_unitary(op) | ||
True | ||
>>> op2 = op + op | ||
>>> qml.is_unitary(op2) | ||
False | ||
""" | ||
identity_mat = qml.math.eye(2 ** len(op.wires)) | ||
adj_op = qml.adjoint(op) | ||
op_prod_adjoint_matrix = qml.matrix(qml.prod(op, adj_op)) | ||
return qml.math.allclose(op_prod_adjoint_matrix, identity_mat) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.