`reduce_t_depth` moves and merges π/8 rotations across Clifford rotations and measurements they do not commute with

Open
#3,487 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
43/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
compilers

Research direction

Start with mlir/lib/PBC/Transforms/TLayerReduction.cpp, especially checkCommutationAndFindMerge, and trace how candidates reach the previous layer through getReachingValueAt in mlir/lib/PBC/Utils/PBCOpUtils.cpp. Then inspect mlir/test/PBC/TLayerReduction.mlir, whose current inputs omit Clifford rotations and measurements. Done when the pass preserves circuit output probabilities for both reproductions and regression tests cover rotations crossing Clifford operations and measurements.

Written by the indexing model from the issue text.

Description

Issue description

reduce_t_depth changes the result of some circuits. It moves a non-Clifford PPR into an earlier layer, or merges it with an equal PPR there, after checking commutation only against the non-Clifford PPRs of that layer. Clifford PPRs and PPMs that lie in between on the same qubits are not checked.

  • Expected behavior: The circuit has the same output probabilities with and without reduce_t_depth.

  • Actual behavior: With Catalyst 0.15.0 the probabilities change from [0.8536 0. 0.1464 0.] to [0.7286 0.125 0.125 0.0214]. The compiled IR shows the T gate of qubit 1 moved in front of the Hadamard that precedes it.

  • Reproduces how often: Always. There is no randomness in the first example.

  • System information:

Name: pennylane
Version: 0.45.1
Summary: PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
Home-page: 
Author: 
License: 
Location: <venv>/lib/python3.12/site-packages
Platform info:           macOS-26.5.1-arm64-arm-64bit
Python version:          3.12.13
Numpy version:           2.5.5
Scipy version:           1.18.1
JAX version:             0.7.1
Catalyst version:        0.15.0
Installed devices:
- nvidia.custatevec (pennylane_catalyst-0.15.0)
- nvidia.cutensornet (pennylane_catalyst-0.15.0)
- oqc.cloud (pennylane_catalyst-0.15.0)
- softwareq.qpp (pennylane_catalyst-0.15.0)
- default.clifford (pennylane-0.45.1)
- default.gaussian (pennylane-0.45.1)
- default.mixed (pennylane-0.45.1)
- default.qubit (pennylane-0.45.1)
- default.qutrit (pennylane-0.45.1)
- default.qutrit.mixed (pennylane-0.45.1)
- default.tensor (pennylane-0.45.1)
- null.qubit (pennylane-0.45.1)
- reference.qubit (pennylane-0.45.1)
- lightning.qubit (pennylane_lightning-0.45.0)

The two MLIR examples below are rewritten in the same way by catalyst --tool=opt from 0.15.0 and from the development wheel pennylane-catalyst 0.16.0.dev48. The Python script does not show the difference with that wheel: through the Python frontend the pass leaves the circuit unchanged there, and it also leaves the example of the reduce_t_depth docstring unchanged. On main, mlir/lib/PBC/Transforms/TLayerReduction.cpp and mlir/lib/PBC/Utils/PBCOpUtils.cpp differ from v0.15.0 only in formatting and in how the layer is iterated (checked on 2026-11-17).

Source code and tracebacks
import numpy as np
import pennylane as qp

dev = qp.device("lightning.qubit", wires=2)


def circuit():
    qp.H(1)
    qp.T(0)
    qp.H(1)
    qp.PauliRot(np.pi / 4, "Y", wires=0)
    qp.T(1)
    return qp.probs()


without = qp.qjit(capture=True)(qp.transforms.to_ppr(qp.qnode(dev)(circuit)))
reduced = qp.qjit(capture=True)(qp.transforms.reduce_t_depth(qp.transforms.to_ppr(qp.qnode(dev)(circuit))))
print("to_ppr                 ", np.round(without(), 4))
print("to_ppr + reduce_t_depth", np.round(reduced(), 4))

Output with Catalyst 0.15.0:

to_ppr                  [0.8536 0.     0.1464 0.    ]
to_ppr + reduce_t_depth [0.7286 0.125  0.125  0.0214]

The same on the pass alone. First example, a rotation moved across a Clifford rotation, catalyst --tool=opt --reduce-t-depth example1.mlir:

func.func @repro(%q0: !quantum.bit, %q1: !quantum.bit) -> (!quantum.bit, !quantum.bit) {
    %0 = pbc.ppr ["Z"](8) %q0 : !quantum.bit
    %1 = pbc.ppr ["X"](4) %q1 : !quantum.bit
    %2 = pbc.ppr ["Y"](8) %0 : !quantum.bit
    %3 = pbc.ppr ["Z"](8) %1 : !quantum.bit
    return %2, %3 : !quantum.bit, !quantum.bit
}

Output. Z(π/8) on the second qubit is now in front of X(π/4) on the same qubit:

func.func @repro(%arg0: !quantum.bit, %arg1: !quantum.bit) -> (!quantum.bit, !quantum.bit) {
  %0 = pbc.ppr ["Z"](8) %arg0 : !quantum.bit
  %1 = pbc.ppr ["Z"](8) %arg1 : !quantum.bit
  %2 = pbc.ppr ["X"](4) %1 : !quantum.bit
  %3 = pbc.ppr ["Y"](8) %0 : !quantum.bit
  return %3, %2 : !quantum.bit, !quantum.bit
}

Second example, a rotation merged across a measurement:

func.func @repro(%q0: !quantum.bit, %q1: !quantum.bit) -> (i1, !quantum.bit, !quantum.bit) {
    %0 = pbc.ppr ["Z"](8) %q0 : !quantum.bit
    %1 = pbc.ppr ["X"](8) %q1 : !quantum.bit
    %m, %2 = pbc.ppm ["X"] %0 : i1, !quantum.bit
    %3 = pbc.ppr ["Z"](8) %1 : !quantum.bit
    %4 = pbc.ppr ["Z"](8) %2 : !quantum.bit
    return %m, %4, %3 : i1, !quantum.bit, !quantum.bit
}

Output. The Z(π/8) after the X measurement of the first qubit is merged into the one before the measurement, which becomes Z(π/4):

func.func @repro(%arg0: !quantum.bit, %arg1: !quantum.bit) -> (i1, !quantum.bit, !quantum.bit) {
  %0 = pbc.ppr ["Z"](4) %arg0 : !quantum.bit
  %1 = pbc.ppr ["X"](8) %arg1 : !quantum.bit
  %mres, %out_qubits = pbc.ppm ["X"] %0 : i1, !quantum.bit
  %2 = pbc.ppr ["Z"](8) %1 : !quantum.bit
  return %mres, %out_qubits, %2 : i1, !quantum.bit, !quantum.bit
}
Additional information

Cause, as far as I can tell. In TLayerReduction.cpp the layers contain only non-Clifford PPRs, because Clifford PPRs are skipped in the walk. checkCommutationAndFindMerge compares the candidate only with the ops of the previous layer. To do so it follows the qubits of the candidate back with getReachingValueAt (PBCOpUtils.cpp), which steps through every op that implements PBCOpInterface. A Clifford PPR or a PPM between the two layers is therefore passed over without a commutation check, and the candidate is then moved or merged in front of it.

The second pattern also occurred in a circuit compiled with to_ppr, commute_ppr, merge_ppr_ppm and then reduce_t_depth, in which a mid-circuit pauli_measure is followed by a further π/8 rotation.

The inputs in mlir/test/PBC/TLayerReduction.mlir contain only π/8 rotations, no Clifford rotation and no measurement, so these cases are not covered by the tests of the pass.

Dominant language
Python
Stars
234
Forks
84
Avg merge
2d 12h
Merged PRs (30d)
84

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from PennyLaneAI/catalyst

All issues in PennyLaneAI/catalyst

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.