Coder Social home page Coder Social logo

Comments (6)

rmoyard avatar rmoyard commented on May 20, 2024 1

Hello @abhishekabhishek, thank you for double checking!

Your understanding of the problem is correct, you should remove the limitation concerning those measurements. And yes the main issue is that wires needs to be translated to operators and it seems that you are on a good track!

Let me know if anything is not clear

from pennylane.

rmoyard avatar rmoyard commented on May 20, 2024 1

Hi @abhishekabhishek, with the old device it is indeed part of the pipeline and called under the hood. With the new device it is not used and split non commuting is not necessary because it can handle non commuting measurements.

I would suggest to mainly use the transform directly on a tape (qml.tape.QuantumTape) and for integration testing you can apply it directly on the QNode. Even if it is not necessary, the transform will be applied as part of the transform program.

     @qml.qnode(dev) 
     def circuit(params): 
         qml.RX(params[0], wires=0) 
         qml.RY(params[1], wires=1) 
         return qml.expval(qml.PauliZ(0) @ qml.PauliZ(1)), qml.expval(qml.PauliY(0)))

split_non_commuting(circuit)([0.1, 0.2])

In the test that you showed, I would suggest to explicitly apply the transform on the QNodes.

Let me know it that helps!

from pennylane.

abhishekabhishek avatar abhishekabhishek commented on May 20, 2024

Hi @Alex-Preciado,

I am currently working on this issue and just wanted to double check my understanding to make sure I'm on the right path. As I understand currently, the issue is the following:

Consider the following qnode:

dev = qml.device("default.qubit", wires=2)

@qml.qnode(dev)
def circuit(x):
    qml.RX(x,wires=0)
    return qml.probs(wires=[0, 1]), qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliX(0))
    
>>> print(qml.draw(circuit)(0.5))

0: ──RX(0.50)─┤ ╭Probs  <Z>  <X>
1: ───────────┤ ╰Probs

Applying circuit = qml.transforms.split_non_commuting(circuit) should return a circuit s.t.

>>> print(qml.draw(circuit)(0.5))

0: ──RX(0.50)─┤ ╭Probs  <Z>
1: ───────────┤ ╰Probs 
\
0: ──RX(0.50)─┤ <X>
1: ───────────┤

But the transform currently doesn't support SampleMP, ProbabilityMP or CountsMP which can take either wires or obs. I think I have a solution which I'm currently implementing and testing i.e. using qml.PauliZ(wires[0])@qml.PauliZ(wires[1])@[email protected](wires[len(wires)-1]) as observables when either of these are used only with wires argument and I'm looking into the reorder_fn to determine if I need to make any changes to it.

Please let me know if you think I missed something 🙂

Cheers,
Abhi

from pennylane.

Alex-Preciado avatar Alex-Preciado commented on May 20, 2024

Hi @abhishekabhishek , thank you so much for looking into this 🚀 . @rmoyard will be providing some guidance here shortly.

from pennylane.

abhishekabhishek avatar abhishekabhishek commented on May 20, 2024

Thanks @rmoyard, I am currently writing tests for the modified transform but got a little confused by the existing integration and autodiff tests for e.g. the one below:

@pytest.mark.autograd
def test_split_with_autograd(self):
"""Test that results after splitting are still differentiable with autograd"""
dev = qml.device("default.qubit", wires=3)
@qml.qnode(dev)
def circuit(params):
qml.RX(params[0], wires=0)
qml.RY(params[1], wires=1)
return (
qml.expval(qml.PauliZ(0) @ qml.PauliZ(1)),
qml.expval(qml.PauliY(0)),
qml.expval(qml.PauliZ(0)),
)
def cost(params):
res = circuit(params)
return qml.math.stack(res)
params = pnp.array([0.5, 0.5])
res = cost(params)
grad = qml.jacobian(cost)(params)
assert all(np.isclose(res, exp_res))
assert all(np.isclose(grad, exp_grad).flatten())

It seems that in this test, the split_non_commuting transform does not get called on the Qnode. Am I missing something here? Does the transform automatically gets applied to the Qnode when more than one expval are returned?

from pennylane.

albi3ro avatar albi3ro commented on May 20, 2024

Closed by #4972

from pennylane.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.