Can qml.simplify be used for Quantum Scripts?

Hi I want to use the preexisting simplify function to test if it’s usable in my project but it looks like it doesn’t work for qml.tape.QuantumScript even thought is accepting the input. Is there an obvious mistake or is the internal structure just not possible to optimize in this way?
Here is my code that I used to test the function just to give you an idea

from evaluator import adapted_fitness_function
from tools import random_qscript, random_qnode, getFidelity
import pennylane as qml

n_bits = 4
dev = qml.device('default.qubit', wires=n_bits)
target = random_qnode(dev, n_bits,5,10)
target_dm = qml.math.dm_from_state_vector(target())
qscript = random_qscript(n_bits,10,10)

print("Before simp")
print(f"Fitness : {adapted_fitness_function(qscript,target_dm,dev,n_bits,1,0.3,0.2)}")
print(f"Fidelity: {getFidelity(qscript,target_dm,dev,n_bits)}")
print(qscript.draw(wire_order=list(range(len(qscript.op_wires)))))
simpscript = qml.simplify(qscript)
print("After simp")
print(f"Fitness : {adapted_fitness_function(simpscript,target_dm,dev,n_bits,1,0.3,0.2)}")
print(f"Fidelity: {getFidelity(simpscript,target_dm,dev,n_bits)}")
print(qscript.draw(wire_order=list(range(len(qscript.op_wires)))))

That’s the result:

Before simp
Fitness : [0.79084708]
Fidelity: [0.07165292]
0: ─╭●──H──S──S────╭●──T───────╭●────╭X──I──I──T────┤  State
1: ─│───T──S──I──T─╰X──I──I────│───H─│───H──T──H────┤  State
2: ─│───S──I──S─╭X──T─╭X──S──I─╰X──H─│──╭●──T──I────┤  State
3: ─╰X──H──H────╰●────╰●──S──T───────╰●─╰X──S──S──T─┤  State
After simp
Fitness : [0.79084708]
Fidelity: [0.07165292]
0: ─╭●──H──S──S────╭●──T───────╭●────╭X──I──I──T────┤  State
1: ─│───T──S──I──T─╰X──I──I────│───H─│───H──T──H────┤  State
2: ─│───S──I──S─╭X──T─╭X──S──I─╰X──H─│──╭●──T──I────┤  State
3: ─╰X──H──H────╰●────╰●──S──T───────╰●─╰X──S──S──T─┤  State

Process finished with exit code 0

Name: PennyLane
Version: 0.33.0
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: GitHub - PennyLaneAI/pennylane: PennyLane is a cross-platform Python library for differentiable programming of quantum computers. Train a quantum computer the same way as a neural network.
Author:
Author-email:
License: Apache License 2.0
Location: c:\users\tombi\pycharmprojects\venv\lib\site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions
Required-by: PennyLane-Lightning

Platform info: Windows-10-10.0.22631-SP0
Python version: 3.9.13
Numpy version: 1.26.1
Scipy version: 1.11.3
Installed devices:

  • default.gaussian (PennyLane-0.33.0)
  • default.mixed (PennyLane-0.33.0)
  • default.qubit (PennyLane-0.33.0)
  • default.qubit.autograd (PennyLane-0.33.0)
  • default.qubit.jax (PennyLane-0.33.0)
  • default.qubit.legacy (PennyLane-0.33.0)
  • default.qubit.tf (PennyLane-0.33.0)
  • default.qubit.torch (PennyLane-0.33.0)
  • default.qutrit (PennyLane-0.33.0)
  • null.qubit (PennyLane-0.33.0)
  • lightning.qubit (PennyLane-Lightning-0.33.1)

Hello @Cap_Cap!

Most likely, qml.simplify is not the best for your purposes. You might want to look at the following transforms instead: cancel_inverses, commute_controlled, merge_rotations, and single_qubit_fusion.

Hope this helps!

Alvaro