---
title: CommutationChecker (v2.1)
description: API reference for qiskit.circuit.CommutationChecker in qiskit v2.1
source: https://eu-de.quantum.cloud.ibm.com/docs/en/api/qiskit/2.1/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.1/qiskit/circuit/commutation_checker.py#L22-L133)

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.1/qiskit.circuit.library.XGate "qiskit.circuit.library.XGate") or [`HGate`](/docs/api/qiskit/2.1/qiskit.circuit.library.HGate "qiskit.circuit.library.HGate")) or gates with free parameters (such as [`RXGate`](/docs/api/qiskit/2.1/qiskit.circuit.library.RXGate "qiskit.circuit.library.RXGate") with a [`ParameterExpression`](/docs/api/qiskit/2.1/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)) –
- **cache\_max\_entries** ([*int*](https://docs.python.org/3/library/functions.html#int)) –
- **gates** (*Optional\[Set\[*[*str*](https://docs.python.org/3/library/stdtypes.html#str)*]]*) –

### check\_commutation\_entries

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

[GitHub](https://github.com/Qiskit/qiskit/tree/stable/2.1/qiskit/circuit/commutation_checker.py#L113-L133)

Returns stored commutation relation if any

**Parameters**

- **first\_op** ([*Operation*](/docs/api/qiskit/2.1/qiskit.circuit.Operation "qiskit.circuit.operation.Operation")) – first operation.
- **first\_qargs** ([*List*](https://docs.python.org/3/library/typing.html#typing.List)) – first operation’s qubits.
- **second\_op** ([*Operation*](/docs/api/qiskit/2.1/qiskit.circuit.Operation "qiskit.circuit.operation.Operation")) – second operation.
- **second\_qargs** ([*List*](https://docs.python.org/3/library/typing.html#typing.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.1/qiskit/circuit/commutation_checker.py#L109-L111)

Clears the dictionary holding cached commutations

### commute

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

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

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.1/qiskit.circuit.Operation "qiskit.circuit.operation.Operation")) – first operation.
- **qargs1** ([*List*](https://docs.python.org/3/library/typing.html#typing.List)) – first operation’s qubits.
- **cargs1** ([*List*](https://docs.python.org/3/library/typing.html#typing.List)) – first operation’s clbits.
- **op2** ([*Operation*](/docs/api/qiskit/2.1/qiskit.circuit.Operation "qiskit.circuit.operation.Operation")) – second operation.
- **qargs2** ([*List*](https://docs.python.org/3/library/typing.html#typing.List)) – second operation’s qubits.
- **cargs2** ([*List*](https://docs.python.org/3/library/typing.html#typing.List)) – second operation’s clbits.
- **max\_num\_qubits** ([*int*](https://docs.python.org/3/library/functions.html#int)) – the maximum number of qubits to consider, the check may be skipped if the number of qubits for either operation exceeds this amount.
- **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.

**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.1/qiskit/circuit/commutation_checker.py#L59-L67)

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.1/qiskit/circuit/commutation_checker.py#L105-L107)

Returns number of cached entries
