PennyLaneAI / pennylane

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.
https://pennylane.ai
Apache License 2.0
2.22k stars 577 forks source link

Update circuit optimization transforms to work on QNodes #2398

Closed glassnotes closed 8 months ago

glassnotes commented 2 years ago

Feature details

Currently, @qml.compile and all related optimization transforms work only on tapes and qfuncs, i.e.,

@qml.qnode(dev)
@qml.compile()
def my_qfunc(params):
    qml.RX(params[0], wires=0)
    return qml.expval(qml.PauliZ(0))

However there are a number of situations where it would be valuable to have them work also on QNodes. For example, suppose we want to compile a QNode after some expansion / decompositions at the device level have happened (e.g., expanding gates from a template).

@qml.compile()
@qml.qnode(dev, expansion_strategy="device")
def my_qfunc(params):
    qml.AngleEmbedding(params, wires=0)
    return qml.expval(qml.PauliZ(0))

Or, for computing the specs before/after compilation:

@qml.qnode(dev)
def my_qfunc(params):
    qml.AngleEmbedding(params, wires=0)
    return qml.expval(qml.PauliZ(0))

original_specs = qml.specs(my_qfunc)(params)
compiled_specs = qml.specs(qml.compile(my_qfunc))(params)

Implementation

Should be able to do something similar to how, e.g., qml.transforms.commutation_dag works, by checking the input type and constructing the tape appropriately.

How important would you say this feature is?

1: Not important. Would be nice to have.

Additional information

I will get to implementing this when I can, but it might also be a nice first issue for someone looking to learn more about how transforms work.

(Implementing this would also be a good opportunity to update the optimization transforms to use the new for op in tape syntax in lieu of for op in tape.operations + tape.measurements).

josh146 commented 2 years ago

@glassnotes this reminds me, I have an idea for merging all the transform decorators, which long term should solve this issue! Plus make it easier for developers, as they no longer need to work out which decorator to use

glassnotes commented 2 years ago

@josh146 fantastic! that sounds like an even better solution :100:

rmoyard commented 8 months ago

That was solved with the transform rework :+1: