---
title: CommutationChecker (v2.4)
description: API reference for qiskit.circuit.CommutationChecker in qiskit v2.4
source: https://eu-de.quantum.cloud.ibm.com/docs/en/api/qiskit/2.4/qiskit.circuit.CommutationChecker
---

# CommutationChecker

*class* `qiskit.circuit.CommutationChecker(standard_gate_commutations=None, cache_max_entries=1000000, *, gates=None)`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.4/qiskit/circuit/commutation_checker.py#L24-L149)

Bases: [`object`](https://docs.python.org/3/library/functions.html#object)

Check commutations of two operations.

Two unitaries $A$ and $B$ on $n$ qubits commute if

$$
\frac{2^n F_{\text{process}}(AB, BA) + 1}{2^n + 1} > 1 - \varepsilon,
$$

where

$$
F_{\text{process}}(U_1, U_2) = \left|\frac{\mathrm{Tr}(U_1 U_2^\dagger)}{2^n} \right|^2,
$$

and we set $\varepsilon$ to $10^{-12}$ to account for round-off errors on few-qubit systems. This metric is chosen for consistency with other closeness checks in Qiskit.

When possible, commutation relations are queried from a lookup table. This is the case for standard gates without parameters (such as [`XGate`](/docs/api/qiskit/2.4/qiskit.circuit.library.XGate "qiskit.circuit.library.XGate") or [`HGate`](/docs/api/qiskit/2.4/qiskit.circuit.library.HGate "qiskit.circuit.library.HGate")) or gates with free parameters (such as [`RXGate`](/docs/api/qiskit/2.4/qiskit.circuit.library.RXGate "qiskit.circuit.library.RXGate") with a [`ParameterExpression`](/docs/api/qiskit/2.4/qiskit.circuit.ParameterExpression "qiskit.circuit.ParameterExpression") as angle). Otherwise, a matrix-based check is performed, where two operations are said to commute, if the average gate fidelity of performing the commutation is above a certain threshold (see `approximation_degree`). The result of this commutation is then added to the cached lookup table.

## Methods

**Parameters**

- **standard\_gate\_commutations** ([*dict*](https://docs.python.org/3/library/stdtypes.html#dict) *| None*)
- **cache\_max\_entries** ([*int*](https://docs.python.org/3/library/functions.html#int))
- **gates** ([*set*](https://docs.python.org/3/library/stdtypes.html#set)*\[*[*str*](https://docs.python.org/3/library/stdtypes.html#str)*] | None*)

### check\_commutation\_entries

`check_commutation_entries(first_op, first_qargs, second_op, second_qargs)`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.4/qiskit/circuit/commutation_checker.py#L129-L149)

Returns stored commutation relation if any

**Parameters**

- **first\_op** ([*Operation*](/docs/api/qiskit/2.4/qiskit.circuit.Operation "qiskit.circuit.operation.Operation")) – first operation.
- **first\_qargs** ([*list*](https://docs.python.org/3/library/stdtypes.html#list)) – first operation’s qubits.
- **second\_op** ([*Operation*](/docs/api/qiskit/2.4/qiskit.circuit.Operation "qiskit.circuit.operation.Operation")) – second operation.
- **second\_qargs** ([*list*](https://docs.python.org/3/library/stdtypes.html#list)) – second operation’s qubits.

**Returns**

True if the gates commute and false if it is not the case.

**Return type**

[bool](https://docs.python.org/3/library/functions.html#bool)

### clear\_cached\_commutations

`clear_cached_commutations()`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.4/qiskit/circuit/commutation_checker.py#L125-L127)

Clears the dictionary holding cached commutations

### commute

`commute(op1, qargs1, cargs1, op2, qargs2, cargs2, max_num_qubits=None, approximation_degree=1.0, matrix_max_num_qubits=3)`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.4/qiskit/circuit/commutation_checker.py#L71-L119)

Checks if two Operations commute. The return value of `True` means that the operations truly commute, and the return value of `False` means that either the operations do not commute or that the commutation check was skipped (for example, when the operations have conditions or have too many qubits).

**Parameters**

- **op1** ([*Operation*](/docs/api/qiskit/2.4/qiskit.circuit.Operation "qiskit.circuit.operation.Operation")) – first operation.
- **qargs1** ([*Sequence*](https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence)*\[*[*Qubit*](/docs/api/qiskit/2.4/circuit#qiskit.circuit.Qubit "qiskit.circuit.Qubit")  *|*[*int*](https://docs.python.org/3/library/functions.html#int)*]*) – first operation’s qubits.
- **cargs1** ([*Sequence*](https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence)*\[*[*Qubit*](/docs/api/qiskit/2.4/circuit#qiskit.circuit.Qubit "qiskit.circuit.Qubit")  *|*[*int*](https://docs.python.org/3/library/functions.html#int)*]*) – first operation’s clbits.
- **op2** ([*Operation*](/docs/api/qiskit/2.4/qiskit.circuit.Operation "qiskit.circuit.operation.Operation")) – second operation.
- **qargs2** ([*Sequence*](https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence)*\[*[*Qubit*](/docs/api/qiskit/2.4/circuit#qiskit.circuit.Qubit "qiskit.circuit.Qubit")  *|*[*int*](https://docs.python.org/3/library/functions.html#int)*]*) – second operation’s qubits.
- **cargs2** ([*Sequence*](https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence)*\[*[*Qubit*](/docs/api/qiskit/2.4/circuit#qiskit.circuit.Qubit "qiskit.circuit.Qubit")  *|*[*int*](https://docs.python.org/3/library/functions.html#int)*]*) – second operation’s clbits.
- **max\_num\_qubits** ([*int*](https://docs.python.org/3/library/functions.html#int) *| None*) – the maximum number of qubits to consider, the check may be skipped if the number of qubits for either operation exceeds this amount. Defaults to `None`, which means no limit. See also `matrix_max_num_qubits` to limit the dimension of matrices computed.
- **approximation\_degree** ([*float*](https://docs.python.org/3/library/functions.html#float)) – If the average gate fidelity in between the two operations is above this number (up to `1e-12`) they are assumed to commute.
- **matrix\_max\_num\_qubits** ([*int*](https://docs.python.org/3/library/functions.html#int)) – the maximum number of qubits for which it is allowed to compute the matrix representation. This is needed if there is no efficient check readily available, e.g. for custom gates.

**Returns**

Whether two operations commute.

**Return type**

[bool](https://docs.python.org/3/library/functions.html#bool)

### commute\_nodes

`commute_nodes(op1, op2, max_num_qubits=3, approximation_degree=1.0)`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.4/qiskit/circuit/commutation_checker.py#L61-L69)

Checks if two DAGOpNodes commute.

**Parameters**

- **max\_num\_qubits** ([*int*](https://docs.python.org/3/library/functions.html#int))
- **approximation\_degree** ([*float*](https://docs.python.org/3/library/functions.html#float))

**Return type**

[bool](https://docs.python.org/3/library/functions.html#bool)

### num\_cached\_entries

`num_cached_entries()`

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.4/qiskit/circuit/commutation_checker.py#L121-L123)

Returns number of cached entries
