Skip to content
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

Library of Unparameterizable Standard Gates Commutations #11192

Merged
merged 18 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,264 changes: 3,264 additions & 0 deletions qiskit/circuit/_standard_gates_commutations.py

Large diffs are not rendered by default.

439 changes: 333 additions & 106 deletions qiskit/circuit/commutation_checker.py

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions qiskit/circuit/commutation_library.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2017, 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.

"""Provides a commutation checker that caches the determined commutation results during this session """

from qiskit.circuit import CommutationChecker

from qiskit.circuit._standard_gates_commutations import standard_gates_commutations

StandardGateCommutations = standard_gates_commutations
SessionCommutationChecker = CommutationChecker(StandardGateCommutations)
4 changes: 2 additions & 2 deletions qiskit/dagcircuit/dagdependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@

import rustworkx as rx

from qiskit.circuit.commutation_library import SessionCommutationChecker as scc
from qiskit.circuit.controlflow import condition_resources
from qiskit.circuit.quantumregister import QuantumRegister, Qubit
from qiskit.circuit.classicalregister import ClassicalRegister, Clbit
from qiskit.dagcircuit.exceptions import DAGDependencyError
from qiskit.dagcircuit.dagdepnode import DAGDepNode
from qiskit.circuit.commutation_checker import CommutationChecker


# ToDo: DagDependency needs to be refactored:
Expand Down Expand Up @@ -112,7 +112,7 @@ def __init__(self):
self.duration = None
self.unit = "dt"

self.comm_checker = CommutationChecker()
self.comm_checker = scc

@property
def global_phase(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

from collections import defaultdict

from qiskit.circuit.commutation_library import SessionCommutationChecker as scc
from qiskit.dagcircuit import DAGOpNode
from qiskit.transpiler.basepasses import AnalysisPass
from qiskit.circuit.commutation_checker import CommutationChecker


class CommutationAnalysis(AnalysisPass):
Expand All @@ -29,7 +29,7 @@ class CommutationAnalysis(AnalysisPass):

def __init__(self):
super().__init__()
self.comm_checker = CommutationChecker()
self.comm_checker = scc

def run(self, dag):
"""Run the CommutationAnalysis pass on `dag`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@
# that they have been altered from the originals.

"""Cancel pairs of inverse gates exploiting commutation relations."""


from qiskit.circuit.commutation_library import SessionCommutationChecker as scc
from qiskit.dagcircuit import DAGCircuit, DAGOpNode
from qiskit.quantum_info import Operator
from qiskit.quantum_info.operators.predicates import matrix_equal
from qiskit.transpiler.basepasses import TransformationPass
from qiskit.circuit.commutation_checker import CommutationChecker


class CommutativeInverseCancellation(TransformationPass):
Expand Down Expand Up @@ -94,8 +92,8 @@ def run(self, dag: DAGCircuit):

removed = [False for _ in range(circ_size)]

cc = scc
phase_update = 0
cc = CommutationChecker()

for idx1 in range(0, circ_size):
if self._skip_node(topo_sorted_nodes[idx1]):
Expand Down
13 changes: 13 additions & 0 deletions releasenotes/notes/add-commutation-library-88b7ff65b3d35f9a.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
features:
- |
Adds a commutation library to the :class:`.CommutationChecker`. The commutation library stores all
commutation relations of unparameterizable standard gates into a dictionary that allows for efficient
lookup at runtime. Furthermore, the :class:`.CommutationChecker` was refactored and an upper limit was
set to the number of cached commutation relations that are not in the commutation library. A session
commutation checker was added, that can be used to cache commutations computed during one qiskit
execution. Addresses `#8020 <https://github.com/Qiskit/qiskit-terra/issues/8020>_` and
`#7101 <https://github.com/Qiskit/qiskit-terra/issues/7101>_`
- |
Adds a `SessionCommutationChecker`, i.e. a commutation checker with commutations that are cached
consistently during the runtime of a python execution.
Loading
Loading