-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
273 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# This code is part of Qiskit. | ||
# | ||
# (C) Copyright IBM 2023. | ||
# | ||
# This code is licensed under the Apache License, Version 2.0. You may | ||
# obtain a copy of this license in the LICENSE.txt file in the root directory | ||
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
# | ||
# Any modifications or derivative works of this code must retain this | ||
# copyright notice, and modified files need to carry a notice indicating | ||
# that they have been altered from the originals. | ||
|
||
""" | ||
Quantum information utility functions for operators. | ||
""" | ||
|
||
from .anti_commutator import anti_commutator | ||
from .commutator import commutator | ||
from .double_commutator import double_commutator |
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,35 @@ | ||
# This code is part of Qiskit. | ||
# | ||
# (C) Copyright IBM 2023. | ||
# | ||
# This code is licensed under the Apache License, Version 2.0. You may | ||
# obtain a copy of this license in the LICENSE.txt file in the root directory | ||
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
# | ||
# Any modifications or derivative works of this code must retain this | ||
# copyright notice, and modified files need to carry a notice indicating | ||
# that they have been altered from the originals. | ||
|
||
"""Anti commutator function.""" | ||
|
||
from typing import TypeVar | ||
|
||
from qiskit.quantum_info.operators.linear_op import LinearOp | ||
|
||
OperatorTypeT = TypeVar("OperatorTypeT", bound=LinearOp) | ||
|
||
|
||
def anti_commutator(a: OperatorTypeT, b: OperatorTypeT) -> OperatorTypeT: | ||
r"""Compute anti-commutator of a and b. | ||
.. math:: | ||
ab + ba. | ||
Args: | ||
a: Operator a. | ||
b: Operator b. | ||
Returns: | ||
The anti-commutator | ||
""" | ||
return a @ b + b @ a |
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,35 @@ | ||
# This code is part of Qiskit. | ||
# | ||
# (C) Copyright IBM 2023. | ||
# | ||
# This code is licensed under the Apache License, Version 2.0. You may | ||
# obtain a copy of this license in the LICENSE.txt file in the root directory | ||
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
# | ||
# Any modifications or derivative works of this code must retain this | ||
# copyright notice, and modified files need to carry a notice indicating | ||
# that they have been altered from the originals. | ||
|
||
"""Commutator function.""" | ||
|
||
from typing import TypeVar | ||
|
||
from qiskit.quantum_info.operators.linear_op import LinearOp | ||
|
||
OperatorTypeT = TypeVar("OperatorTypeT", bound=LinearOp) | ||
|
||
|
||
def commutator(a: OperatorTypeT, b: OperatorTypeT) -> OperatorTypeT: | ||
r"""Compute commutator of a and b. | ||
.. math:: | ||
ab - ba. | ||
Args: | ||
a: Operator a. | ||
b: Operator b. | ||
Returns: | ||
The commutator | ||
""" | ||
return a @ b - b @ a |
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,75 @@ | ||
# This code is part of Qiskit. | ||
# | ||
# (C) Copyright IBM 2023. | ||
# | ||
# This code is licensed under the Apache License, Version 2.0. You may | ||
# obtain a copy of this license in the LICENSE.txt file in the root directory | ||
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
# | ||
# Any modifications or derivative works of this code must retain this | ||
# copyright notice, and modified files need to carry a notice indicating | ||
# that they have been altered from the originals. | ||
|
||
"""Double commutator function.""" | ||
|
||
from typing import TypeVar | ||
|
||
from qiskit.quantum_info.operators.linear_op import LinearOp | ||
|
||
OperatorTypeT = TypeVar("OperatorTypeT", bound=LinearOp) | ||
|
||
|
||
def double_commutator( | ||
a: OperatorTypeT, b: OperatorTypeT, c: OperatorTypeT, *, commutator: bool = True | ||
) -> OperatorTypeT: | ||
r"""Compute symmetric double commutator of a, b and c. | ||
See also Equation (13.6.18) in [1]. | ||
If `commutator` is `True`, it returns | ||
.. math:: | ||
[[A, B], C]/2 + [A, [B, C]]/2 | ||
= (2ABC + 2CBA - BAC - CAB - ACB - BCA)/2. | ||
If `commutator` is `False`, it returns | ||
.. math:: | ||
\lbrace[A, B], C\rbrace/2 + \lbrace A, [B, C]\rbrace/2 | ||
= (2ABC - 2CBA - BAC + CAB - ACB + BCA)/2. | ||
Args: | ||
a: Operator a. | ||
b: Operator b. | ||
c: Operator c. | ||
commutator: If ``True`` compute the double commutator, | ||
if ``False`` the double anti-commutator. | ||
Returns: | ||
The double commutator | ||
References: | ||
[1]: R. McWeeny. | ||
Methods of Molecular Quantum Mechanics. | ||
2nd Edition, Academic Press, 1992. | ||
ISBN 0-12-486552-6. | ||
""" | ||
sign_num = -1 if commutator else 1 | ||
|
||
ab = a @ b | ||
ba = b @ a | ||
ac = a @ c | ||
ca = c @ a | ||
|
||
abc = ab @ c | ||
cba = c @ ba | ||
bac = ba @ c | ||
cab = c @ ab | ||
acb = ac @ b | ||
bca = b @ ca | ||
|
||
res = abc - sign_num * cba + 0.5 * (-bac + sign_num * cab - acb + sign_num * bca) | ||
|
||
return res |
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,7 @@ | ||
--- | ||
features: | ||
- | | ||
Add utility functions :func:`~qiskit.quantum_info.commutator`, | ||
:func:`~qiskit.quantum_info.anti_commutator`, and | ||
:func:`~qiskit.quantum_info.double_commutator` to compute commutators | ||
for any objects implementing a :class:`.LinearOp`. |
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,58 @@ | ||
# This code is part of Qiskit. | ||
# | ||
# (C) Copyright IBM 2023. | ||
# | ||
# This code is licensed under the Apache License, Version 2.0. You may | ||
# obtain a copy of this license in the LICENSE.txt file in the root directory | ||
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
# | ||
# Any modifications or derivative works of this code must retain this | ||
# copyright notice, and modified files need to carry a notice indicating | ||
# that they have been altered from the originals. | ||
|
||
"""Tests utility functions for operator classes.""" | ||
|
||
import unittest | ||
|
||
from ddt import data, ddt, unpack | ||
|
||
from qiskit.quantum_info import SparsePauliOp, anti_commutator, commutator, double_commutator | ||
from qiskit.test import QiskitTestCase | ||
|
||
I = SparsePauliOp("I") | ||
X = SparsePauliOp("X") | ||
Y = SparsePauliOp("Y") | ||
Z = SparsePauliOp("Z") | ||
zero = SparsePauliOp("I", 0) | ||
|
||
|
||
@ddt | ||
class TestOperatorUtils(QiskitTestCase): | ||
"""Test utility functions for operator classes.""" | ||
|
||
@unpack | ||
@data((Z, I, zero), (Z, X, 2j * Y)) | ||
def test_commutator(self, a, b, com): | ||
"""Test commutator function on SparsePauliOp.""" | ||
self.assertTrue(commutator(a, b).equiv(com)) | ||
|
||
@unpack | ||
@data((Z, X, zero), (Z, I, 2 * Z)) | ||
def test_anti_commutator(self, a, b, com): | ||
"""Test anti_commutator function on SparsePauliOp.""" | ||
self.assertTrue(anti_commutator(a, b).equiv(com)) | ||
|
||
@unpack | ||
@data( | ||
(X, Y, Z, True, zero), | ||
(X, Y, X, True, -4 * Y), | ||
(X, Y, Z, False, 4j * I), | ||
(X, Y, X, False, zero), | ||
) | ||
def test_double_commutator(self, a, b, c, com, expected): | ||
"""Test double_commutator function on SparsePauliOp.""" | ||
self.assertTrue(double_commutator(a, b, c, commutator=com).equiv(expected)) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |